Skip to content

Commit a8bee3f

Browse files
committed
Fix broken cases
1 parent eebc404 commit a8bee3f

File tree

7 files changed

+47
-15
lines changed

7 files changed

+47
-15
lines changed

src/__tests__/baselines/minification-only/issue36-extended.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ TypeScript after transform:
3838

3939
declare const styled: any;
4040
export const A = styled.div \`border:\${'solid'} 10px;\`;
41-
styled.div \`border:\${'solid'}10px;border:solid10px;\`;
41+
styled.div \`border:\${'solid'} 10px;border:solid 10px;\`;
4242
styled.div \`border:\${'solid'} 10px;border:\${'solid'} 10px;\`;
4343

4444

src/__tests__/baselines/minification-only/minify-css-to-use-without-transpilation.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TypeScript after transform:
6565
const SpecialCharacters = styled.div \`content:" \${props => props.text} ";color:red;\`;
6666
const Comment = styled.div \`width:100%;color:red;\`;
6767
const Parens = styled.div \`&:hover{color:blue;}color:red;\`;
68-
const UrlComments = styled.div \`color:red; background:red;border:1px solid green;\`;
68+
const UrlComments = styled.div \`color:red;background:red;border:1px solid green;\`;
6969
export {};
7070

7171

src/__tests__/baselines/minification-only/simple.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ TypeScript after transform:
158158
styled.div \`line one{line:two;}\`;
159159
// removes line comments at the end of lines of code
160160
// \`valid line with out comments\`
161-
styled.div \`valid line without comments\`;
161+
styled.div \`valid line with out comments\`;
162162
// preserves multi-line comments starting with /*!
163163
// \`this is a /*! dont ignore me please */ test\`
164164
styled.div \`this is a test\`;

src/__tests__/baselines/minification/issue36-extended.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ TypeScript after transform:
3838

3939
declare const styled: any;
4040
export const A = styled.div.withConfig({ displayName: "A" }) \`border:\${'solid'} 10px;\`;
41-
styled.div \`border:\${'solid'}10px;border:solid10px;\`;
41+
styled.div \`border:\${'solid'} 10px;border:solid 10px;\`;
4242
styled.div \`border:\${'solid'} 10px;border:\${'solid'} 10px;\`;
4343

4444

src/__tests__/baselines/minification/minify-css-to-use-without-transpilation.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TypeScript after transform:
6565
const SpecialCharacters = styled.div.withConfig({ displayName: "SpecialCharacters" }) \`content:" \${props => props.text} ";color:red;\`;
6666
const Comment = styled.div.withConfig({ displayName: "Comment" }) \`width:100%;color:red;\`;
6767
const Parens = styled.div.withConfig({ displayName: "Parens" }) \`&:hover{color:blue;}color:red;\`;
68-
const UrlComments = styled.div.withConfig({ displayName: "UrlComments" }) \`color:red; background:red;border:1px solid green;\`;
68+
const UrlComments = styled.div.withConfig({ displayName: "UrlComments" }) \`color:red;background:red;border:1px solid green;\`;
6969
export {};
7070

7171

src/__tests__/baselines/minification/simple.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ TypeScript after transform:
158158
styled.div \`line one{line:two;}\`;
159159
// removes line comments at the end of lines of code
160160
// \`valid line with out comments\`
161-
styled.div \`valid line without comments\`;
161+
styled.div \`valid line with out comments\`;
162162
// preserves multi-line comments starting with /*!
163163
// \`this is a /*! dont ignore me please */ test\`
164164
styled.div \`this is a test\`;

src/minify.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import * as ts from 'typescript';
22
import { isNoSubstitutionTemplateLiteral, isTemplateExpression } from './ts-is-kind';
33

4-
type State = ';' | ';$' | 'x' | ' ' | '\n' | '"' | '(' | '\'' | '/' | '//' | '/$' | '//$' | '/*' | '/**' | '/*$' | '/*$*';
5-
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State } | void;
4+
type State = ';' | ';$' | 'x' | ' ' | '\n' | '"' | '(' | '\'' | '/' | '//' | ';/' | ';//' | '/$' | '//$' | '/*' | '/**' | ';/*' | ';/**' | '/*$' | '/*$*';
5+
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State; } | void;
66
type StateMachine = {
77
[K in State]: {
88
next?(ch: string): ReducerResult;
99
flush?(last: boolean): ReducerResult;
10-
placeholder?(): ReducerResult;
1110
}
1211
};
1312

@@ -20,7 +19,7 @@ const stateMachine: StateMachine = {
2019
next(ch) {
2120
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
2221
if (ch == ' ' || ch == '\n' || ch == '\r') return { skipEmit: true }
23-
if (ch == '/') return { state: '/', skipEmit: true }
22+
if (ch == '/') return { state: ';/', skipEmit: true }
2423
if (isSymbol(ch)) return;
2524
return { state: 'x' }
2625
},
@@ -89,6 +88,26 @@ const stateMachine: StateMachine = {
8988
}
9089
},
9190
'//': {
91+
next(ch) {
92+
if (ch == '\n') return { state: ' ', skipEmit: true }
93+
return { skipEmit: true };
94+
},
95+
flush(last) {
96+
if (last) return { skipEmit: true }
97+
return { state: '//$', emit: '//' }
98+
}
99+
},
100+
';/': {
101+
next(ch) {
102+
if (ch == '/') return { state: ';//', skipEmit: true }
103+
if (ch == '*') return { state: ';/*', skipEmit: true }
104+
return { state: ';', emit: '/' + ch }
105+
},
106+
flush() {
107+
return { state: '/$', emit: '/' }
108+
}
109+
},
110+
';//': {
92111
next(ch) {
93112
if (ch == '\n') return { state: ';', skipEmit: true }
94113
return { skipEmit: true };
@@ -121,6 +140,22 @@ const stateMachine: StateMachine = {
121140
return { state: '/*', skipEmit: true }
122141
}
123142
},
143+
';/*': {
144+
next(ch) {
145+
if (ch == '*') return { state: ';/**', skipEmit: true }
146+
return { skipEmit: true };
147+
},
148+
flush(last) {
149+
if (last) return { skipEmit: true }
150+
return { state: '/*$', emit: '/*' }
151+
}
152+
},
153+
';/**': {
154+
next(ch) {
155+
if (ch == '/') return { state: ';', skipEmit: true }
156+
return { state: ';/*', skipEmit: true }
157+
}
158+
},
124159
'/*$': {
125160
next(ch) {
126161
if (ch == '*') return { state: '/*$*', skipEmit: true };
@@ -155,18 +190,15 @@ function createMinifier(): (next: string, last?: boolean) => string {
155190
}
156191
}
157192

158-
let reducer = stateMachine[state];
159-
apply(reducer.placeholder && reducer.placeholder());
160-
161193
let pos = 0;
162194
let len = next.length;
163195
while (pos < len) {
164196
const ch = next[pos++];
165-
reducer = stateMachine[state];
197+
const reducer = stateMachine[state];
166198
apply(reducer.next && reducer.next(ch), ch)
167199
}
168200

169-
reducer = stateMachine[state];
201+
const reducer = stateMachine[state];
170202
apply(reducer.flush && reducer.flush(last));
171203

172204
return minified;

0 commit comments

Comments
 (0)