Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect minification of calc() #436

Merged
merged 3 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/__tests__/baselines/minification-only/issue233.ts.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`issue233.ts 1`] = `

File: issue233.ts
Source code:

declare const styled: any;

// repro from #233
const ValueCalc = styled.div\`
height: calc(var(--line-height) - 5px);
width: calc(100% - 5px);
\`;

// another case from #233
styled.div\`--padding-button: calc(var(--padding-button-vertical) - 2px) calc(var(--padding-button-horizontal) - 2px);\`

// a few more cases
styled.div\`
width: calc( 1%)
width: calc( 1% + var(--a) - calc(2%))
width: calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))
\`


TypeScript before transform:

declare const styled: any;
// repro from #233
const ValueCalc = styled.div \`
height: calc(var(--line-height) - 5px);
width: calc(100% - 5px);
\`;
// another case from #233
styled.div \`--padding-button: calc(var(--padding-button-vertical) - 2px) calc(var(--padding-button-horizontal) - 2px);\`;
// a few more cases
styled.div \`
width: calc( 1%)
width: calc( 1% + var(--a) - calc(2%))
width: calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))
\`;


TypeScript after transform:

declare const styled: any;
// repro from #233
const ValueCalc = styled.div \`height:calc(var(--line-height) - 5px);width:calc(100% - 5px);\`;
// another case from #233
styled.div \`--padding-button:calc(var(--padding-button-vertical) - 2px)calc(var(--padding-button-horizontal) - 2px);\`;
// a few more cases
styled.div \`width:calc( 1%)width:calc( 1% + var(--a) - calc(2%))width:calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))\`;


TypeScript after transpile module:

// repro from #233
const ValueCalc = styled.div \`height:calc(var(--line-height) - 5px);width:calc(100% - 5px);\`;
// another case from #233
styled.div \`--padding-button:calc(var(--padding-button-vertical) - 2px)calc(var(--padding-button-horizontal) - 2px);\`;
// a few more cases
styled.div \`width:calc( 1%)width:calc( 1% + var(--a) - calc(2%))width:calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))\`;



`;
67 changes: 67 additions & 0 deletions src/__tests__/baselines/minification/issue233.ts.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`issue233.ts 1`] = `

File: issue233.ts
Source code:

declare const styled: any;

// repro from #233
const ValueCalc = styled.div\`
height: calc(var(--line-height) - 5px);
width: calc(100% - 5px);
\`;

// another case from #233
styled.div\`--padding-button: calc(var(--padding-button-vertical) - 2px) calc(var(--padding-button-horizontal) - 2px);\`

// a few more cases
styled.div\`
width: calc( 1%)
width: calc( 1% + var(--a) - calc(2%))
width: calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))
\`


TypeScript before transform:

declare const styled: any;
// repro from #233
const ValueCalc = styled.div \`
height: calc(var(--line-height) - 5px);
width: calc(100% - 5px);
\`;
// another case from #233
styled.div \`--padding-button: calc(var(--padding-button-vertical) - 2px) calc(var(--padding-button-horizontal) - 2px);\`;
// a few more cases
styled.div \`
width: calc( 1%)
width: calc( 1% + var(--a) - calc(2%))
width: calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))
\`;


TypeScript after transform:

declare const styled: any;
// repro from #233
const ValueCalc = styled.div.withConfig({ displayName: "ValueCalc" }) \`height:calc(var(--line-height) - 5px);width:calc(100% - 5px);\`;
// another case from #233
styled.div \`--padding-button:calc(var(--padding-button-vertical) - 2px)calc(var(--padding-button-horizontal) - 2px);\`;
// a few more cases
styled.div \`width:calc( 1%)width:calc( 1% + var(--a) - calc(2%))width:calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))\`;


TypeScript after transpile module:

// repro from #233
const ValueCalc = styled.div.withConfig({ displayName: "ValueCalc" }) \`height:calc(var(--line-height) - 5px);width:calc(100% - 5px);\`;
// another case from #233
styled.div \`--padding-button:calc(var(--padding-button-vertical) - 2px)calc(var(--padding-button-horizontal) - 2px);\`;
// a few more cases
styled.div \`width:calc( 1%)width:calc( 1% + var(--a) - calc(2%))width:calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))\`;



`;
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/minification/issue233.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare const styled: any;

// repro from #233
const ValueCalc = styled.div`
height: calc(var(--line-height) - 5px);
width: calc(100% - 5px);
`;

// another case from #233
styled.div`--padding-button: calc(var(--padding-button-vertical) - 2px) calc(var(--padding-button-horizontal) - 2px);`

// a few more cases
styled.div`
width: calc( 1%)
width: calc( 1% + var(--a) - calc(2%))
width: calc( 1% + var(--a) - calc(2%) + calc( 1px + calc(1px + 2px) + var(--a)))
`
45 changes: 34 additions & 11 deletions src/minify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ 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 StateDataDef = {
['(']: { count: number }
}
type StateData<K extends State> = K extends keyof StateDataDef ? StateDataDef[K] : void
type StateResult = { [K in State]: K extends keyof StateDataDef ? [K, StateDataDef[K]] : K }[State]
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: StateResult; } | void;
type StateMachine = {
[K in State]: {
next?(ch: string): ReducerResult;
next?(ch: string, data: StateData<K>): ReducerResult;
flush?(last: boolean): ReducerResult;
}
};
Expand All @@ -21,7 +26,8 @@ function isSpace(ch: string) {
const stateMachine: StateMachine = {
';': {
next(ch) {
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
if (ch == '(') return { state: ['(', {count: 1}]}
if (ch == '\'' || ch == '"') return { state: ch }
if (isSpace(ch)) return { skipEmit: true }
if (ch == '/') return { state: ';/', skipEmit: true }
if (isSymbol(ch)) return;
Expand All @@ -33,7 +39,8 @@ const stateMachine: StateMachine = {
},
';$': { // after placeholder
next(ch) {
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
if (ch == '(') return { state: ['(', {count: 1}]}
if (ch == '\'' || ch == '"') return { state: ch }
if (isSpace(ch)) return { skipEmit: true, state: ' ' } // we may need a space
if (ch == '/') return { state: '/', skipEmit: true }
if (isSymbol(ch)) return { state: ';' };
Expand All @@ -42,15 +49,17 @@ const stateMachine: StateMachine = {
},
'x': {
next(ch) {
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
if (ch == '(') return { state: ['(', {count: 1}]}
if (ch == '\'' || ch == '"') return { state: ch }
if (isSpace(ch)) return { state: ' ', skipEmit: true }
if (ch == '/') return { state: '/', skipEmit: true }
if (isSymbol(ch)) return { state: ';' };
}
},
' ': { // may need space
next(ch) {
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch, emit: ' ' + ch }
if (ch == '(') return { state: ['(', {count: 1}], emit: ' ' + ch}
if (ch == '\'' || ch == '"') return { state: ch, emit: ' ' + ch }
if (isSpace(ch)) return { state: ' ', skipEmit: true }
if (ch == '/') return { state: '/', skipEmit: true }
if (isSymbol(ch)) return { state: ';' };
Expand All @@ -62,7 +71,8 @@ const stateMachine: StateMachine = {
},
'\n': { // may need new line
next(ch) {
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch, emit: '\n' + ch }
if (ch == '(') return { state: ['(', {count: 1}], emit: '\n' + ch}
if (ch == '\'' || ch == '"') return { state: ch, emit: '\n' + ch }
if (isSpace(ch)) return { state: '\n', skipEmit: true }
if (ch == '/') return { state: '/', emit: '\n' }
if (isSymbol(ch)) return { state: ';', emit: '\n' + ch };
Expand All @@ -80,8 +90,13 @@ const stateMachine: StateMachine = {
}
},
'(': {
next(ch) {
if (ch == ')') return { state: ';' }; // maybe return ' '? then it'd always add space after
next(ch, { count }) {
if (ch == '(') return { state: ['(', { count: count+1 }] }
if (ch == ')')
if (count > 1)
return { state: ['(', { count: count-1 }] }
else
return { state: ';' }; // maybe return ' '? then it'd always add space after
}
},
'/': {
Expand Down Expand Up @@ -179,6 +194,7 @@ const stateMachine: StateMachine = {

export function createMinifier(): (next: string, last?: boolean) => string {
let state: State = ';';
let stateData: StateData<State> = undefined as StateData<';'>

return (next, last = false) => {
let minified = '';
Expand All @@ -189,7 +205,14 @@ export function createMinifier(): (next: string, last?: boolean) => string {
minified += ch;
} else {
if (result.state !== undefined)
state = result.state;
{
if (typeof result.state === 'string')
state = result.state;
else {
state = result.state[0]
stateData = result.state[1]
}
}
if (result.emit !== undefined)
minified += result.emit;
else if (result.skipEmit !== true && ch !== undefined)
Expand All @@ -203,7 +226,7 @@ export function createMinifier(): (next: string, last?: boolean) => string {
const ch = next[pos++];
const reducer = stateMachine[state];
const prevState = state;
const reducerResult = reducer.next && reducer.next(ch);
const reducerResult = reducer.next && reducer.next(ch, stateData as any);
apply(reducerResult, ch)
// console.log('next(', { ch, state: prevState }, '): ', reducerResult, ' -> ', { state, minified });
}
Expand Down