diff --git a/src/ast/nodes/SwitchCase.ts b/src/ast/nodes/SwitchCase.ts index fd78688e03a..7f3f50dadd8 100644 --- a/src/ast/nodes/SwitchCase.ts +++ b/src/ast/nodes/SwitchCase.ts @@ -39,17 +39,18 @@ export default class SwitchCase extends NodeBase { } render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void { - if (this.consequent.length > 0) { - if (this.test) { - this.test.render(code, options); + if (this.test) { + this.test.render(code, options); + if (this.test.start === this.start + 4) { + code.prependLeft(this.test.start, ' '); } + } + if (this.consequent.length > 0) { const testEnd = this.test ? this.test.end : findFirstOccurrenceOutsideComment(code.original, 'default', this.start) + 7; const consequentStart = findFirstOccurrenceOutsideComment(code.original, ':', testEnd) + 1; renderStatementList(this.consequent, code, consequentStart, nodeRenderOptions!.end!, options); - } else { - super.render(code, options); } } } diff --git a/src/ast/nodes/UnaryExpression.ts b/src/ast/nodes/UnaryExpression.ts index a683c8741b9..3f0c9d69852 100644 --- a/src/ast/nodes/UnaryExpression.ts +++ b/src/ast/nodes/UnaryExpression.ts @@ -90,7 +90,7 @@ export default class UnaryExpression extends NodeBase { if (this.renderedLiteralValue !== UNASSIGNED) return this.renderedLiteralValue; return (this.renderedLiteralValue = includeChildrenRecursively ? UnknownValue - : getSimplifiedLiterals( + : getRenderedLiteralValue( this.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this) )); } @@ -115,12 +115,18 @@ export default class UnaryExpression extends NodeBase { if (typeof this.renderedLiteralValue === 'symbol') { super.render(code, options); } else { - code.overwrite(this.start, this.end, this.renderedLiteralValue); + let value = this.renderedLiteralValue; + if (!CHARACTERS_THAT_DO_NOT_REQUIRE_SPACE.test(code.original[this.start - 1])) { + value = ` ${value}`; + } + code.overwrite(this.start, this.end, value); } } } -function getSimplifiedLiterals(value: unknown) { +const CHARACTERS_THAT_DO_NOT_REQUIRE_SPACE = /[\s([=%&*+-/<>^|,?:;]/; + +function getRenderedLiteralValue(value: unknown) { if (value === undefined || typeof value === 'boolean') { return String(value); } diff --git a/test/form/samples/switch-cases/missing-space/_config.js b/test/form/samples/switch-cases/missing-space/_config.js new file mode 100644 index 00000000000..f9d55d696d7 --- /dev/null +++ b/test/form/samples/switch-cases/missing-space/_config.js @@ -0,0 +1,3 @@ +module.exports = defineTest({ + description: 'Inserts space when simplifying switch cases without space' +}); diff --git a/test/form/samples/switch-cases/missing-space/_expected.js b/test/form/samples/switch-cases/missing-space/_expected.js new file mode 100644 index 00000000000..ae4276cfd60 --- /dev/null +++ b/test/form/samples/switch-cases/missing-space/_expected.js @@ -0,0 +1,5 @@ +switch (bar) { + case 1: + console.log('1'); + break; +} diff --git a/test/form/samples/switch-cases/missing-space/main.js b/test/form/samples/switch-cases/missing-space/main.js new file mode 100644 index 00000000000..9abe40f7902 --- /dev/null +++ b/test/form/samples/switch-cases/missing-space/main.js @@ -0,0 +1,5 @@ +switch (bar) { + case!0?1:2: + console.log('1'); + break; +} \ No newline at end of file diff --git a/test/form/samples/treeshake-unary/_expected.js b/test/form/samples/treeshake-unary/_expected.js index 316ba8db940..83cb25ba06a 100644 --- a/test/form/samples/treeshake-unary/_expected.js +++ b/test/form/samples/treeshake-unary/_expected.js @@ -27,3 +27,27 @@ function foo3() { } console.log(delete foo3.a); + +switch (bar) { + case false: + console.log('false'); + break; + case true: + console.log('true'); + break; + case 1: + console.log('1'); + break; + case 1: + console.log('1'); + break; + case 1: + console.log('1'); + break; +} + +async function baz(){ + await true; +} + +export { baz }; diff --git a/test/form/samples/treeshake-unary/main.js b/test/form/samples/treeshake-unary/main.js index b33e0067b8f..86fdd722978 100644 --- a/test/form/samples/treeshake-unary/main.js +++ b/test/form/samples/treeshake-unary/main.js @@ -32,3 +32,25 @@ function foo3() { } console.log(delete foo3.a); + +switch (bar) { + case!1: + console.log('false'); + break; + case!0: + console.log('true'); + break; + case+1: + console.log('1'); + break; + case~-2: + console.log('1'); + break; + case ~-2: + console.log('1'); + break; +} + +export async function baz(){ + await!0; +} \ No newline at end of file