From deda24029f467a3c99d1382e8c6adfb705da850f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B7=BC?= Date: Mon, 8 Jun 2020 20:19:08 +0800 Subject: [PATCH] [fix] fix missing space before "-" --- .../minification-only/issue233.tsx.baseline | 32 +++++++++++++++++++ .../minification/issue233.tsx.baseline | 32 +++++++++++++++++++ .../fixtures/minification/issue233.tsx | 6 ++++ src/minify.ts | 8 ++++- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/baselines/minification-only/issue233.tsx.baseline create mode 100644 src/__tests__/baselines/minification/issue233.tsx.baseline create mode 100644 src/__tests__/fixtures/minification/issue233.tsx diff --git a/src/__tests__/baselines/minification-only/issue233.tsx.baseline b/src/__tests__/baselines/minification-only/issue233.tsx.baseline new file mode 100644 index 0000000..afdcdf7 --- /dev/null +++ b/src/__tests__/baselines/minification-only/issue233.tsx.baseline @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`issue233.tsx 1`] = ` + +File: issue233.tsx +Source code: + + declare const styled: any; + + const ValueCalc = styled.div\` + height: calc(var(--line-height) - 5px); + width: calc(100% - 5px); + \`; + + +TypeScript before transform: + + declare const styled: any; + const ValueCalc = styled.div \` + height: calc(var(--line-height) - 5px); + width: calc(100% - 5px); + \`; + + +TypeScript after transform: + + declare const styled: any; + const ValueCalc = styled.div \`height:calc(var(--line-height) - 5px);width:calc(100% - 5px);\`; + + + +`; diff --git a/src/__tests__/baselines/minification/issue233.tsx.baseline b/src/__tests__/baselines/minification/issue233.tsx.baseline new file mode 100644 index 0000000..5ae5bfa --- /dev/null +++ b/src/__tests__/baselines/minification/issue233.tsx.baseline @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`issue233.tsx 1`] = ` + +File: issue233.tsx +Source code: + + declare const styled: any; + + const ValueCalc = styled.div\` + height: calc(var(--line-height) - 5px); + width: calc(100% - 5px); + \`; + + +TypeScript before transform: + + declare const styled: any; + const ValueCalc = styled.div \` + height: calc(var(--line-height) - 5px); + width: calc(100% - 5px); + \`; + + +TypeScript after transform: + + declare const styled: any; + const ValueCalc = styled.div.withConfig({ displayName: "ValueCalc" }) \`height:calc(var(--line-height) - 5px);width:calc(100% - 5px);\`; + + + +`; diff --git a/src/__tests__/fixtures/minification/issue233.tsx b/src/__tests__/fixtures/minification/issue233.tsx new file mode 100644 index 0000000..a98bb00 --- /dev/null +++ b/src/__tests__/fixtures/minification/issue233.tsx @@ -0,0 +1,6 @@ +declare const styled: any; + +const ValueCalc = styled.div` + height: calc(var(--line-height) - 5px); + width: calc(100% - 5px); +`; diff --git a/src/minify.ts b/src/minify.ts index ca3112b..e6601d3 100644 --- a/src/minify.ts +++ b/src/minify.ts @@ -1,7 +1,7 @@ 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]: { @@ -81,9 +81,15 @@ const stateMachine: StateMachine = { }, '(': { next(ch) { + if (ch == "(") return { state: '((' }; if (ch == ')') return { state: ';' }; // maybe return ' '? then it'd always add space after } }, + '((': { + next(ch) { + if(ch == ')') return { state: "(" }; + } + }, '/': { next(ch) { if (ch == '/') return { state: '//', skipEmit: true }