Skip to content

Commit

Permalink
Fix broken cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Igorbek committed Mar 27, 2019
1 parent eebc404 commit a8bee3f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TypeScript after transform:

declare const styled: any;
export const A = styled.div \`border:\${'solid'} 10px;\`;
styled.div \`border:\${'solid'}10px;border:solid10px;\`;
styled.div \`border:\${'solid'} 10px;border:solid 10px;\`;
styled.div \`border:\${'solid'} 10px;border:\${'solid'} 10px;\`;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TypeScript after transform:
const SpecialCharacters = styled.div \`content:" \${props => props.text} ";color:red;\`;
const Comment = styled.div \`width:100%;color:red;\`;
const Parens = styled.div \`&:hover{color:blue;}color:red;\`;
const UrlComments = styled.div \`color:red; background:red;border:1px solid green;\`;
const UrlComments = styled.div \`color:red;background:red;border:1px solid green;\`;
export {};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ TypeScript after transform:
styled.div \`line one{line:two;}\`;
// removes line comments at the end of lines of code
// \`valid line with out comments\`
styled.div \`valid line without comments\`;
styled.div \`valid line with out comments\`;
// preserves multi-line comments starting with /*!
// \`this is a /*! dont ignore me please */ test\`
styled.div \`this is a test\`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TypeScript after transform:

declare const styled: any;
export const A = styled.div.withConfig({ displayName: "A" }) \`border:\${'solid'} 10px;\`;
styled.div \`border:\${'solid'}10px;border:solid10px;\`;
styled.div \`border:\${'solid'} 10px;border:solid 10px;\`;
styled.div \`border:\${'solid'} 10px;border:\${'solid'} 10px;\`;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TypeScript after transform:
const SpecialCharacters = styled.div.withConfig({ displayName: "SpecialCharacters" }) \`content:" \${props => props.text} ";color:red;\`;
const Comment = styled.div.withConfig({ displayName: "Comment" }) \`width:100%;color:red;\`;
const Parens = styled.div.withConfig({ displayName: "Parens" }) \`&:hover{color:blue;}color:red;\`;
const UrlComments = styled.div.withConfig({ displayName: "UrlComments" }) \`color:red; background:red;border:1px solid green;\`;
const UrlComments = styled.div.withConfig({ displayName: "UrlComments" }) \`color:red;background:red;border:1px solid green;\`;
export {};


Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/baselines/minification/simple.ts.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ TypeScript after transform:
styled.div \`line one{line:two;}\`;
// removes line comments at the end of lines of code
// \`valid line with out comments\`
styled.div \`valid line without comments\`;
styled.div \`valid line with out comments\`;
// preserves multi-line comments starting with /*!
// \`this is a /*! dont ignore me please */ test\`
styled.div \`this is a test\`;
Expand Down
50 changes: 41 additions & 9 deletions src/minify.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as ts from 'typescript';
import { isNoSubstitutionTemplateLiteral, isTemplateExpression } from './ts-is-kind';

type State = ';' | ';$' | 'x' | ' ' | '\n' | '"' | '(' | '\'' | '/' | '//' | '/$' | '//$' | '/*' | '/**' | '/*$' | '/*$*';
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State } | void;
type State = ';' | ';$' | 'x' | ' ' | '\n' | '"' | '(' | '\'' | '/' | '//' | ';/' | ';//' | '/$' | '//$' | '/*' | '/**' | ';/*' | ';/**' | '/*$' | '/*$*';
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State; } | void;
type StateMachine = {
[K in State]: {
next?(ch: string): ReducerResult;
flush?(last: boolean): ReducerResult;
placeholder?(): ReducerResult;
}
};

Expand All @@ -20,7 +19,7 @@ const stateMachine: StateMachine = {
next(ch) {
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
if (ch == ' ' || ch == '\n' || ch == '\r') return { skipEmit: true }
if (ch == '/') return { state: '/', skipEmit: true }
if (ch == '/') return { state: ';/', skipEmit: true }
if (isSymbol(ch)) return;
return { state: 'x' }
},
Expand Down Expand Up @@ -89,6 +88,26 @@ const stateMachine: StateMachine = {
}
},
'//': {
next(ch) {
if (ch == '\n') return { state: ' ', skipEmit: true }
return { skipEmit: true };
},
flush(last) {
if (last) return { skipEmit: true }
return { state: '//$', emit: '//' }
}
},
';/': {
next(ch) {
if (ch == '/') return { state: ';//', skipEmit: true }
if (ch == '*') return { state: ';/*', skipEmit: true }
return { state: ';', emit: '/' + ch }
},
flush() {
return { state: '/$', emit: '/' }
}
},
';//': {
next(ch) {
if (ch == '\n') return { state: ';', skipEmit: true }
return { skipEmit: true };
Expand Down Expand Up @@ -121,6 +140,22 @@ const stateMachine: StateMachine = {
return { state: '/*', skipEmit: true }
}
},
';/*': {
next(ch) {
if (ch == '*') return { state: ';/**', skipEmit: true }
return { skipEmit: true };
},
flush(last) {
if (last) return { skipEmit: true }
return { state: '/*$', emit: '/*' }
}
},
';/**': {
next(ch) {
if (ch == '/') return { state: ';', skipEmit: true }
return { state: ';/*', skipEmit: true }
}
},
'/*$': {
next(ch) {
if (ch == '*') return { state: '/*$*', skipEmit: true };
Expand Down Expand Up @@ -155,18 +190,15 @@ function createMinifier(): (next: string, last?: boolean) => string {
}
}

let reducer = stateMachine[state];
apply(reducer.placeholder && reducer.placeholder());

let pos = 0;
let len = next.length;
while (pos < len) {
const ch = next[pos++];
reducer = stateMachine[state];
const reducer = stateMachine[state];
apply(reducer.next && reducer.next(ch), ch)
}

reducer = stateMachine[state];
const reducer = stateMachine[state];
apply(reducer.flush && reducer.flush(last));

return minified;
Expand Down

0 comments on commit a8bee3f

Please sign in to comment.