Skip to content

Commit ab6d73a

Browse files
authored
Merge pull request #41 from Igorbek/bugfix/issue40
Fix another minification issue #40
2 parents ed406c2 + 8eafb0b commit ab6d73a

File tree

7 files changed

+82
-6
lines changed

7 files changed

+82
-6
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`issue40.ts 1`] = `
4+
5+
File: issue40.ts
6+
Source code:
7+
8+
declare const styled: any;
9+
10+
styled.div\`padding: 0 0 13px \${'50px'};\`
11+
12+
13+
TypeScript before transform:
14+
15+
declare const styled: any;
16+
styled.div \`padding: 0 0 13px \${"50px"};\`;
17+
18+
19+
TypeScript after transform:
20+
21+
declare const styled: any;
22+
styled.div \`padding:0 0 13px \${'50px'};\`;
23+
24+
25+
26+
`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ TypeScript after transform:
164164
styled.div \`this is a test\`;
165165
// returns the indices of removed placeholders (expressions)
166166
// \`this is some input with \${placeholder1} and //\${placeholder2}\`
167-
styled.div \`this is some input with\${placeholder1} and//\${placeholder2}\`;
167+
styled.div \`this is some input with \${placeholder1} and//\${placeholder2}\`;
168168
// works with raw escape codes
169169
// \`this\\\\nis\\\\na \\\\ntest\`
170170
styled.div \`this\\\\nis\\\\na \\\\ntest\`;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`issue40.ts 1`] = `
4+
5+
File: issue40.ts
6+
Source code:
7+
8+
declare const styled: any;
9+
10+
styled.div\`padding: 0 0 13px \${'50px'};\`
11+
12+
13+
TypeScript before transform:
14+
15+
declare const styled: any;
16+
styled.div \`padding: 0 0 13px \${"50px"};\`;
17+
18+
19+
TypeScript after transform:
20+
21+
declare const styled: any;
22+
styled.div \`padding:0 0 13px \${'50px'};\`;
23+
24+
25+
26+
`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ TypeScript after transform:
164164
styled.div \`this is a test\`;
165165
// returns the indices of removed placeholders (expressions)
166166
// \`this is some input with \${placeholder1} and //\${placeholder2}\`
167-
styled.div \`this is some input with\${placeholder1} and//\${placeholder2}\`;
167+
styled.div \`this is some input with \${placeholder1} and//\${placeholder2}\`;
168168
// works with raw escape codes
169169
// \`this\\\\nis\\\\na \\\\ntest\`
170170
styled.div \`this\\\\nis\\\\na \\\\ntest\`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const styled: any;
2+
3+
styled.div`padding: 0 0 13px ${'50px'};`

src/__tests__/minify.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createMinifier } from '../minify';
2+
3+
it.skip('debug', () => {
4+
const minifer = createMinifier();
5+
6+
move(' w: ');
7+
move(';\n ');
8+
9+
function move(next: string, last?: boolean) {
10+
console.log({ move: { next, last }, result: minifer(next, last) });
11+
}
12+
});

src/minify.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const stateMachine: StateMachine = {
3232
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
3333
if (ch == ' ' || ch == '\n' || ch == '\r') return { skipEmit: true, state: ' ' } // we may need a space
3434
if (ch == '/') return { state: '/', skipEmit: true }
35-
if (isSymbol(ch)) return;
35+
if (isSymbol(ch)) return { state: ';' };
3636
return { state: 'x' }
3737
}
3838
},
@@ -51,6 +51,9 @@ const stateMachine: StateMachine = {
5151
if (ch == '/') return { state: '/', skipEmit: true }
5252
if (isSymbol(ch)) return { state: ';' };
5353
return { state: 'x', emit: ' ' + ch };
54+
},
55+
flush(last) {
56+
if (!last) return { emit: ' ' };
5457
}
5558
},
5659
'\n': { // may need new line
@@ -170,7 +173,7 @@ const stateMachine: StateMachine = {
170173
}
171174
};
172175

173-
function createMinifier(): (next: string, last?: boolean) => string {
176+
export function createMinifier(): (next: string, last?: boolean) => string {
174177
let state: State = ';';
175178

176179
return (next, last = false) => {
@@ -195,11 +198,17 @@ function createMinifier(): (next: string, last?: boolean) => string {
195198
while (pos < len) {
196199
const ch = next[pos++];
197200
const reducer = stateMachine[state];
198-
apply(reducer.next && reducer.next(ch), ch)
201+
const prevState = state;
202+
const reducerResult = reducer.next && reducer.next(ch);
203+
apply(reducerResult, ch)
204+
//console.log('next(', { ch, state: prevState }, '): ', reducerResult, ' -> ', { state, minified });
199205
}
200206

201207
const reducer = stateMachine[state];
202-
apply(reducer.flush && reducer.flush(last));
208+
const prevState = state;
209+
const reducerResult = reducer.flush && reducer.flush(last);
210+
apply(reducerResult);
211+
//console.log('flush', { state: prevState }, '): ', reducerResult, ' -> ', { state, minified });
203212

204213
return minified;
205214
}

0 commit comments

Comments
 (0)