Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
11 changes: 6 additions & 5 deletions src/ast/nodes/SwitchCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/ast/nodes/UnaryExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
}
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/switch-cases/missing-space/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = defineTest({
description: 'Inserts space when simplifying switch cases without space'
});
5 changes: 5 additions & 0 deletions test/form/samples/switch-cases/missing-space/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
switch (bar) {
case 1:
console.log('1');
break;
}
5 changes: 5 additions & 0 deletions test/form/samples/switch-cases/missing-space/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
switch (bar) {
case!0?1:2:
console.log('1');
break;
}
24 changes: 24 additions & 0 deletions test/form/samples/treeshake-unary/_expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
22 changes: 22 additions & 0 deletions test/form/samples/treeshake-unary/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading