Skip to content

Commit d6e1217

Browse files
committed
Skip minifing inside () and add more tests
1 parent a2245da commit d6e1217

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ TypeScript after transform:
142142
styled.div \`abc def"//"\`;
143143
// ignores comment markers that are inside parantheses
144144
// \`bla (//) bla\`
145-
styled.div \`bla (\`;
145+
styled.div \`bla (//)bla\`;
146146
// ignores even unescaped URLs
147147
// \`https://test.com\`
148148
styled.div \`https:\`;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`simple2.ts 1`] = `
4+
5+
File: simple2.ts
6+
Source code:
7+
8+
export {}
9+
declare const styled: any;
10+
11+
// spaces before and after ' " ( )
12+
styled.div\`a b" c " d\` // \`a b" c "d\`
13+
styled.div\`a b' c ' d\` // \`a b' c 'd\`
14+
styled.div\`a b( c ) d\` // \`a b( c )d\`
15+
styled.div\`a b " c "d\` // \`a b " c "d\`
16+
styled.div\`a b ' c 'd\` // \`a b ' c 'd\`
17+
styled.div\`a b ( c )d\` // \`a b ( c )d\`
18+
19+
20+
TypeScript before transform:
21+
22+
export {};
23+
declare const styled: any;
24+
// spaces before and after ' " ( )
25+
styled.div \`a b" c " d\`; // \`a b" c "d\`
26+
styled.div \`a b' c ' d\`; // \`a b' c 'd\`
27+
styled.div \`a b( c ) d\`; // \`a b( c )d\`
28+
styled.div \`a b " c "d\`; // \`a b " c "d\`
29+
styled.div \`a b ' c 'd\`; // \`a b ' c 'd\`
30+
styled.div \`a b ( c )d\`; // \`a b ( c )d\`
31+
32+
33+
TypeScript after transform:
34+
35+
export {};
36+
declare const styled: any;
37+
// spaces before and after ' " ( )
38+
styled.div \`a b" c "d\`; // \`a b" c "d\`
39+
styled.div \`a b' c 'd\`; // \`a b' c 'd\`
40+
styled.div \`a b( c )d\`; // \`a b( c )d\`
41+
styled.div \`a b " c "d\`; // \`a b " c "d\`
42+
styled.div \`a b ' c 'd\`; // \`a b ' c 'd\`
43+
styled.div \`a b ( c )d\`; // \`a b ( c )d\`
44+
45+
46+
47+
`;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export {}
2+
declare const styled: any;
3+
4+
// spaces before and after ' " ( )
5+
styled.div`a b" c " d` // `a b" c "d`
6+
styled.div`a b' c ' d` // `a b' c 'd`
7+
styled.div`a b( c ) d` // `a b( c )d`
8+
styled.div`a b " c "d` // `a b " c "d`
9+
styled.div`a b ' c 'd` // `a b ' c 'd`
10+
styled.div`a b ( c )d` // `a b ( c )d`

src/minify.ts

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

4-
type State = ';' | 'x' | ' ' | '"' | '\'' | '/' | '//' | '/$' | '//$' | '/*' | '/**' | '/*$' | '/*$*';
4+
type State = ';' | 'x' | ' ' | '"' | '(' | '\'' | '/' | '//' | '/$' | '//$' | '/*' | '/**' | '/*$' | '/*$*';
55
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State } | void;
66
type StateMachine = {
77
[K in State]: {
@@ -11,13 +11,13 @@ type StateMachine = {
1111
};
1212

1313
function isSymbol(ch: string) {
14-
return ch == ';' || ch == ':' || ch == '{' || ch == '}';
14+
return ch == ';' || ch == ':' || ch == '{' || ch == '}' || ch == ',';
1515
}
1616

1717
const stateMachine: StateMachine = {
1818
';': {
1919
next(ch) {
20-
if (ch == '\'' || ch == '"') return { state: ch }
20+
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
2121
if (ch == ' ' || ch == '\n' || ch == '\r') return { skipEmit: true }
2222
if (ch == '/') return { state: '/', skipEmit: true }
2323
if (isSymbol(ch)) return;
@@ -26,15 +26,15 @@ const stateMachine: StateMachine = {
2626
},
2727
'x': {
2828
next(ch) {
29-
if (ch == '\'' || ch == '"') return { state: ch }
29+
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
3030
if (ch == ' ' || ch == '\n' || ch == '\r') return { state: ' ', skipEmit: true }
3131
if (ch == '/') return { state: '/', skipEmit: true }
3232
if (isSymbol(ch)) return { state: ';' };
3333
}
3434
},
3535
' ': { // may need space
3636
next(ch) {
37-
if (ch == '\'' || ch == '"') return { state: ch }
37+
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch, emit: ' ' + ch }
3838
if (ch == ' ' || ch == '\n' || ch == '\r') return { state: ' ', skipEmit: true }
3939
if (ch == '/') return { state: '/', skipEmit: true }
4040
if (isSymbol(ch)) return { state: ';' };
@@ -51,6 +51,11 @@ const stateMachine: StateMachine = {
5151
if (ch == '"') return { state: ';' };
5252
}
5353
},
54+
'(': {
55+
next(ch) {
56+
if (ch == ')') return { state: ';' }; // maybe return ' '? then it'd always add space after
57+
}
58+
},
5459
'/': {
5560
next(ch) {
5661
if (ch == '/') return { state: '//', skipEmit: true }

0 commit comments

Comments
 (0)