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

Skip to content

Commit da694cc

Browse files
committed
consider that literals cannot following switch case
1 parent 958d5eb commit da694cc

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

src/ast/nodes/SwitchCase.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ export default class SwitchCase extends NodeBase {
3939
}
4040

4141
render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void {
42-
if (this.consequent.length > 0) {
43-
if (this.test) {
44-
this.test.render(code, options);
42+
if (this.test) {
43+
this.test.render(code, options);
44+
if (this.test.start === this.start + 4) {
45+
code.prependLeft(this.test.start, ' ');
4546
}
47+
}
48+
if (this.consequent.length > 0) {
4649
const testEnd = this.test
4750
? this.test.end
4851
: findFirstOccurrenceOutsideComment(code.original, 'default', this.start) + 7;
4952
const consequentStart = findFirstOccurrenceOutsideComment(code.original, ':', testEnd) + 1;
5053
renderStatementList(this.consequent, code, consequentStart, nodeRenderOptions!.end!, options);
51-
} else {
52-
super.render(code, options);
5354
}
5455
}
5556
}

src/ast/nodes/UnaryExpression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export default class UnaryExpression extends NodeBase {
9090
if (this.renderedLiteralValue !== UNASSIGNED) return this.renderedLiteralValue;
9191
return (this.renderedLiteralValue = includeChildrenRecursively
9292
? UnknownValue
93-
: getSimplifiedLiterals(
93+
: getRenderedLiteralValue(
9494
this.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this)
9595
));
9696
}
@@ -120,7 +120,7 @@ export default class UnaryExpression extends NodeBase {
120120
}
121121
}
122122

123-
function getSimplifiedLiterals(value: unknown) {
123+
function getRenderedLiteralValue(value: unknown) {
124124
if (value === undefined || typeof value === 'boolean') {
125125
return String(value);
126126
}

test/form/samples/treeshake-unary/_expected.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ function foo3() {
2727
}
2828

2929
console.log(delete foo3.a);
30+
31+
switch (bar) {
32+
case false:
33+
console.log('false');
34+
break;
35+
case true:
36+
console.log('true');
37+
break;
38+
case 1:
39+
console.log('1');
40+
break;
41+
case 1:
42+
console.log('1');
43+
break;
44+
case 1:
45+
console.log('1');
46+
break;
47+
}

test/form/samples/treeshake-unary/main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,21 @@ function foo3() {
3232
}
3333

3434
console.log(delete foo3.a);
35+
36+
switch (bar) {
37+
case!1:
38+
console.log('false');
39+
break;
40+
case!0:
41+
console.log('true');
42+
break;
43+
case+1:
44+
console.log('1');
45+
break;
46+
case~-2:
47+
console.log('1');
48+
break;
49+
case ~-2:
50+
console.log('1');
51+
break;
52+
}

0 commit comments

Comments
 (0)