Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/rollup/rollup into sync-9…
Browse files Browse the repository at this point in the history
…4917087
  • Loading branch information
docschina-bot committed Jan 7, 2025
2 parents e544146 + 9491708 commit df7d58c
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 12 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# rollup changelog

## 4.30.1

_2025-01-07_

### Bug Fixes

- Prevent invalid code when simplifying unary expressions in switch cases (#5786)

### Pull Requests

- [#5786](https://github.com/rollup/rollup/pull/5786): fix: consider that literals cannot following switch case. (@TrickyPi)

## 4.30.0

_2025-01-06_
Expand Down
2 changes: 1 addition & 1 deletion browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rollup/browser",
"version": "4.30.0",
"version": "4.30.1",
"description": "Next-generation ES module bundler browser build",
"main": "dist/rollup.browser.js",
"module": "dist/es/rollup.browser.js",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup",
"version": "4.30.0",
"version": "4.30.1",
"description": "Next-generation ES module bundler",
"main": "dist/rollup.js",
"module": "dist/es/rollup.js",
Expand Down
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;
}

0 comments on commit df7d58c

Please sign in to comment.