Skip to content

Commit a2245da

Browse files
committed
Add more expectations from minifying
1 parent 6f86e15 commit a2245da

File tree

3 files changed

+86
-22
lines changed

3 files changed

+86
-22
lines changed

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

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,67 @@ Source code:
1111
declare const placeholder2: any;
1212

1313
// splits a line by potential comment starts and joins until one is an actual comment
14+
// \`abc def\`
1415
styled.div\`abc def//ghi//jkl\`
1516

1617
// ignores comment markers that are inside strings
18+
// \`abc def"//"ghi\\'//\\'jkl\`
1719
styled.div\`abc def"//"ghi\\'//\\'jkl//the end\`
20+
// \`abc def"//"\`
1821
styled.div\`abc def"//"\`
1922

2023
// ignores comment markers that are inside parantheses
24+
// \`bla (//) bla\`
2125
styled.div\`bla (//) bla//the end\`
2226

2327
// ignores even unescaped URLs
28+
// \`https://test.com\`
2429
styled.div\`https://test.com// comment//\`
2530

2631
// removes multi-line comments
32+
// \`this is a test\`
2733
styled.div\`this is a/* ignore me please */test\`
2834

2935
// joins all lines of code
36+
// \`this is a test\`
3037
styled.div\`this\\nis\\na/* ignore me \\n please */\\ntest\`
3138

3239
// removes line comments filling an entire line
40+
// \`line one line two\`
3341
styled.div\`line one
3442
// remove this comment
3543
line two\`
3644

3745
// removes line comments at the end of lines of code
46+
// \`valid line with out comments\`
3847
styled.div\`valid line with // a comment
3948
out comments\`
4049

4150
// preserves multi-line comments starting with /*!
51+
// \`this is a /*! dont ignore me please */ test\`
4252
styled.div\`this is a /*! dont ignore me please */ test/* but you can ignore me */\`
4353

4454
// returns the indices of removed placeholders (expressions)
55+
// \`this is some input with \${placeholder1} and //\${placeholder2}\`
4556
styled.div\`this is some\\ninput with \${placeholder1} and // ignored \${placeholder2}\`
4657

4758
// works with raw escape codes
59+
// \`this\\\\nis\\\\na \\\\ntest\`
4860
styled.div\`this\\\\nis\\\\na/* ignore me \\\\n please */\\\\ntest\`
61+
// \`this\\nis\\na \\ntest\`
4962
styled.div\`this\\nis\\na/* ignore me \\n please */\\ntest\`
63+
// \`this is a test\`
5064
styled.div\`this
5165
is
52-
a/* ignore me \\n please *
53-
ntest\`
66+
a/* ignore me \\n please */
67+
test\`
5468

5569
// removes spaces around symbols
70+
// \`;:{},;\`
5671
styled.div\`; : { } , ; \`
5772

5873
// ignores symbols inside strings
74+
// \`;" : "\\' : \\';\`
5975
styled.div\`; " : " \\' : \\' ;\`
6076

6177

@@ -65,33 +81,49 @@ TypeScript before transform:
6581
declare const placeholder1: any;
6682
declare const placeholder2: any;
6783
// splits a line by potential comment starts and joins until one is an actual comment
84+
// \`abc def\`
6885
styled.div \`abc def//ghi//jkl\`;
6986
// ignores comment markers that are inside strings
87+
// \`abc def"//"ghi\\'//\\'jkl\`
7088
styled.div \`abc def"//"ghi'//'jkl//the end\`;
89+
// \`abc def"//"\`
7190
styled.div \`abc def"//"\`;
7291
// ignores comment markers that are inside parantheses
92+
// \`bla (//) bla\`
7393
styled.div \`bla (//) bla//the end\`;
7494
// ignores even unescaped URLs
95+
// \`https://test.com\`
7596
styled.div \`https://test.com// comment//\`;
7697
// removes multi-line comments
98+
// \`this is a test\`
7799
styled.div \`this is a/* ignore me please */test\`;
78100
// joins all lines of code
101+
// \`this is a test\`
79102
styled.div \`this\\nis\\na/* ignore me \\n please */\\ntest\`;
80103
// removes line comments filling an entire line
104+
// \`line one line two\`
81105
styled.div \`line one\\n// remove this comment\\nline two\`;
82106
// removes line comments at the end of lines of code
107+
// \`valid line with out comments\`
83108
styled.div \`valid line with // a comment\\nout comments\`;
84109
// preserves multi-line comments starting with /*!
110+
// \`this is a /*! dont ignore me please */ test\`
85111
styled.div \`this is a /*! dont ignore me please */ test/* but you can ignore me */\`;
86112
// returns the indices of removed placeholders (expressions)
113+
// \`this is some input with \${placeholder1} and //\${placeholder2}\`
87114
styled.div \`this is some\\ninput with \${placeholder1} and // ignored \${placeholder2}\`;
88115
// works with raw escape codes
116+
// \`this\\\\nis\\\\na \\\\ntest\`
89117
styled.div \`this\\\\nis\\\\na/* ignore me \\\\n please */\\\\ntest\`;
118+
// \`this\\nis\\na \\ntest\`
119+
styled.div \`this\\nis\\na/* ignore me \\n please */\\ntest\`;
120+
// \`this is a test\`
90121
styled.div \`this\\nis\\na/* ignore me \\n please */\\ntest\`;
91-
styled.div \`this\\nis\\na/* ignore me \\n please *\\nntest\`;
92122
// removes spaces around symbols
123+
// \`;:{},;\`
93124
styled.div \`; : { } , ; \`;
94125
// ignores symbols inside strings
126+
// \`;" : "\\' : \\';\`
95127
styled.div \`; " : " ' : ' ;\`;
96128

97129

@@ -101,33 +133,49 @@ TypeScript after transform:
101133
declare const placeholder1: any;
102134
declare const placeholder2: any;
103135
// splits a line by potential comment starts and joins until one is an actual comment
104-
styled.div \`abc def//\`;
136+
// \`abc def\`
137+
styled.div \`abc def\`;
105138
// ignores comment markers that are inside strings
106-
styled.div \`abc def"//"ghi'//'jkl//\`;
139+
// \`abc def"//"ghi\\'//\\'jkl\`
140+
styled.div \`abc def"//"ghi'//'jkl\`;
141+
// \`abc def"//"\`
107142
styled.div \`abc def"//"\`;
108143
// ignores comment markers that are inside parantheses
109-
styled.div \`bla (//\`;
144+
// \`bla (//) bla\`
145+
styled.div \`bla (\`;
110146
// ignores even unescaped URLs
111-
styled.div \`https://\`;
147+
// \`https://test.com\`
148+
styled.div \`https:\`;
112149
// removes multi-line comments
150+
// \`this is a test\`
113151
styled.div \`this is atest\`;
114152
// joins all lines of code
153+
// \`this is a test\`
115154
styled.div \`this is atest\`;
116155
// removes line comments filling an entire line
156+
// \`line one line two\`
117157
styled.div \`line oneline two\`;
118158
// removes line comments at the end of lines of code
159+
// \`valid line with out comments\`
119160
styled.div \`valid line without comments\`;
120161
// preserves multi-line comments starting with /*!
162+
// \`this is a /*! dont ignore me please */ test\`
121163
styled.div \`this is atest\`;
122164
// returns the indices of removed placeholders (expressions)
165+
// \`this is some input with \${placeholder1} and //\${placeholder2}\`
123166
styled.div \`this is some input with\${placeholder1} and//\${placeholder2}\`;
124167
// works with raw escape codes
168+
// \`this\\\\nis\\\\na \\\\ntest\`
125169
styled.div \`this\\\\nis\\\\na\\\\ntest\`;
170+
// \`this\\nis\\na \\ntest\`
171+
styled.div \`this is atest\`;
172+
// \`this is a test\`
126173
styled.div \`this is atest\`;
127-
styled.div \`this is a/*\`;
128174
// removes spaces around symbols
175+
// \`;:{},;\`
129176
styled.div \`;:{},;\`;
130177
// ignores symbols inside strings
178+
// \`;" : "\\' : \\';\`
131179
styled.div \`;" : "' : ';\`;
132180

133181

src/__tests__/fixtures/minification/simple.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,65 @@ declare const placeholder1: any;
44
declare const placeholder2: any;
55

66
// splits a line by potential comment starts and joins until one is an actual comment
7+
// `abc def`
78
styled.div`abc def//ghi//jkl`
89

910
// ignores comment markers that are inside strings
11+
// `abc def"//"ghi\'//\'jkl`
1012
styled.div`abc def"//"ghi\'//\'jkl//the end`
13+
// `abc def"//"`
1114
styled.div`abc def"//"`
1215

1316
// ignores comment markers that are inside parantheses
17+
// `bla (//) bla`
1418
styled.div`bla (//) bla//the end`
1519

1620
// ignores even unescaped URLs
21+
// `https://test.com`
1722
styled.div`https://test.com// comment//`
1823

1924
// removes multi-line comments
25+
// `this is a test`
2026
styled.div`this is a/* ignore me please */test`
2127

2228
// joins all lines of code
29+
// `this is a test`
2330
styled.div`this\nis\na/* ignore me \n please */\ntest`
2431

2532
// removes line comments filling an entire line
33+
// `line one line two`
2634
styled.div`line one
2735
// remove this comment
2836
line two`
2937

3038
// removes line comments at the end of lines of code
39+
// `valid line with out comments`
3140
styled.div`valid line with // a comment
3241
out comments`
3342

3443
// preserves multi-line comments starting with /*!
44+
// `this is a /*! dont ignore me please */ test`
3545
styled.div`this is a /*! dont ignore me please */ test/* but you can ignore me */`
3646

3747
// returns the indices of removed placeholders (expressions)
48+
// `this is some input with ${placeholder1} and //${placeholder2}`
3849
styled.div`this is some\ninput with ${placeholder1} and // ignored ${placeholder2}`
3950

4051
// works with raw escape codes
52+
// `this\\nis\\na \\ntest`
4153
styled.div`this\\nis\\na/* ignore me \\n please */\\ntest`
54+
// `this\nis\na \ntest`
4255
styled.div`this\nis\na/* ignore me \n please */\ntest`
56+
// `this is a test`
4357
styled.div`this
4458
is
45-
a/* ignore me \n please *
46-
ntest`
59+
a/* ignore me \n please */
60+
test`
4761

4862
// removes spaces around symbols
63+
// `;:{},;`
4964
styled.div`; : { } , ; `
5065

5166
// ignores symbols inside strings
67+
// `;" : "\' : \';`
5268
styled.div`; " : " \' : \' ;`

src/minify.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State } | void
66
type StateMachine = {
77
[K in State]: {
88
next?(ch: string): ReducerResult;
9-
flush?(): ReducerResult;
9+
flush?(last: boolean): ReducerResult;
1010
}
1111
};
1212

@@ -66,7 +66,8 @@ const stateMachine: StateMachine = {
6666
if (ch == '\n') return { state: ';', skipEmit: true }
6767
return { skipEmit: true };
6868
},
69-
flush() {
69+
flush(last) {
70+
if (last) return { skipEmit: true }
7071
return { state: '//$', emit: '//' }
7172
}
7273
},
@@ -82,7 +83,8 @@ const stateMachine: StateMachine = {
8283
if (ch == '*') return { state: '/**', skipEmit: true }
8384
return { skipEmit: true };
8485
},
85-
flush() {
86+
flush(last) {
87+
if (last) return { skipEmit: true }
8688
return { state: '/*$', emit: '/*' }
8789
}
8890
},
@@ -106,10 +108,10 @@ const stateMachine: StateMachine = {
106108
}
107109
};
108110

109-
function createMinifier(): (next: string, middle?: boolean) => string {
111+
function createMinifier(): (next: string, last?: boolean) => string {
110112
let state: State = ';';
111113

112-
return (next, middle = false) => {
114+
return (next, last = false) => {
113115
let minified = '';
114116

115117
function apply(result: ReducerResult, ch?: string) {
@@ -135,7 +137,7 @@ function createMinifier(): (next: string, middle?: boolean) => string {
135137
}
136138

137139
const reducer = stateMachine[state];
138-
apply(reducer.flush && reducer.flush());
140+
apply(reducer.flush && reducer.flush(last));
139141

140142
return minified;
141143
}
@@ -153,19 +155,17 @@ export function minifyTemplate(templateLiteral: ts.TemplateLiteral) {
153155
const minifier = createMinifier();
154156

155157
if (isNoSubstitutionTemplateLiteral(templateLiteral)) {
156-
// const sourceMapRange = ts.getSourceMapRange(templateLiteral);
157-
const node = ts.createNoSubstitutionTemplateLiteral(minifier(templateLiteral.text));
158-
// ts.setSourceMapRange(node, sourceMapRange);
158+
const node = ts.createNoSubstitutionTemplateLiteral(minifier(templateLiteral.text, true));
159159
return node;
160160
} else if (isTemplateExpression(templateLiteral)) {
161161
const head = ts.createTemplateHead(minifier(templateLiteral.head.text));
162162
const templateSpans = templateLiteral.templateSpans.map(span => ts.createTemplateSpan(span.expression,
163163
span.literal.kind === ts.SyntaxKind.TemplateMiddle
164-
? ts.createTemplateMiddle(minifier(span.literal.text, true))
165-
: ts.createTemplateTail(minifier(span.literal.text))));
164+
? ts.createTemplateMiddle(minifier(span.literal.text))
165+
: ts.createTemplateTail(minifier(span.literal.text, true))));
166166
const node = ts.createTemplateExpression(head, templateSpans);
167167
return node;
168168
}
169169

170170
return templateLiteral;
171-
}
171+
}

0 commit comments

Comments
 (0)