Skip to content

Commit

Permalink
Fix missing space after placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
Igorbek committed Mar 27, 2019
1 parent aff4796 commit 0b7dfc4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TypeScript before transform:
TypeScript after transform:

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



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TypeScript after transform:
declare const keyframes: any;
declare const styled: any;
const rotate360 = keyframes \`from{transform:rotate(0deg);}to{transform:rotate(360deg);}\`;
export const StyledDiv = styled.div \`width:100px;height:100px;background-color:greenyellow;animation:\${rotate360}2s linear infinite;\`;
export const StyledDiv = styled.div \`width:100px;height:100px;background-color:greenyellow;animation:\${rotate360} 2s linear infinite;\`;



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TypeScript before transform:
TypeScript after transform:

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



Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/baselines/minification/issue36.tsx.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TypeScript after transform:
declare const keyframes: any;
declare const styled: any;
const rotate360 = keyframes \`from{transform:rotate(0deg);}to{transform:rotate(360deg);}\`;
export const StyledDiv = styled.div.withConfig({ displayName: "StyledDiv" }) \`width:100px;height:100px;background-color:greenyellow;animation:\${rotate360}2s linear infinite;\`;
export const StyledDiv = styled.div.withConfig({ displayName: "StyledDiv" }) \`width:100px;height:100px;background-color:greenyellow;animation:\${rotate360} 2s linear infinite;\`;



Expand Down
22 changes: 19 additions & 3 deletions src/minify.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as ts from 'typescript';
import { isNoSubstitutionTemplateLiteral, isTemplateExpression } from './ts-is-kind';

type State = ';' | 'x' | ' ' | '\n' | '"' | '(' | '\'' | '/' | '//' | '/$' | '//$' | '/*' | '/**' | '/*$' | '/*$*';
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 @@ -22,6 +23,18 @@ const stateMachine: StateMachine = {
if (ch == '/') return { state: '/', skipEmit: true }
if (isSymbol(ch)) return;
return { state: 'x' }
},
flush() {
return { state: ';$' }
}
},
';$': { // after placeholder
next(ch) {
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
if (ch == ' ' || ch == '\n' || ch == '\r') return { skipEmit: true, state: ' ' } // we may need a space
if (ch == '/') return { state: '/', skipEmit: true }
if (isSymbol(ch)) return;
return { state: 'x' }
}
},
'x': {
Expand Down Expand Up @@ -141,16 +154,19 @@ function createMinifier(): (next: string, last?: boolean) => string {
minified += ch;
}
}

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

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

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

return minified;
Expand Down

0 comments on commit 0b7dfc4

Please sign in to comment.