Skip to content

Commit

Permalink
When tree-shaking logical expression, make sure to remove all trailin…
Browse files Browse the repository at this point in the history
…g white-space. (#5581)

This should hopefully also improve sourcemaps.
  • Loading branch information
lukastaegert authored Jul 20, 2024
1 parent a9a6108 commit 1d2c1e9
Show file tree
Hide file tree
Showing 23 changed files with 128 additions and 50 deletions.
12 changes: 6 additions & 6 deletions src/ast/nodes/LogicalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,20 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
}: NodeRenderOptions = BLANK
): void {
if (!this.left.included || !this.right.included) {
const operatorPos = findFirstOccurrenceOutsideComment(
code.original,
this.operator,
this.left.end
);
if (this.right.included) {
const operatorPos = findFirstOccurrenceOutsideComment(
code.original,
this.operator,
this.left.end
);
const removePos = findNonWhiteSpace(code.original, operatorPos + 2);
code.remove(this.start, removePos);
if (preventASI) {
removeLineBreaks(code, removePos, this.right.start);
}
this.left.removeAnnotations(code);
} else {
code.remove(operatorPos, this.end);
code.remove(this.left.end, this.end);
}
this.getUsedBranch()!.render(code, options, {
isCalleeOfRenderedParent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import assert from "node:assert";
import { foo, __proto__ } from "./cjs.js";
assert.strictEqual(foo, 1);
assert.strictEqual(__proto__, undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.foo = 1;
Object.defineProperty(exports, "__proto__", {
enumerable: true,
value: 2
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import assert from "node:assert";
import { foo, __proto__ } from "./cjs.js";
assert.strictEqual(foo, 1);
assert.strictEqual(__proto__, 2);
5 changes: 5 additions & 0 deletions test/form/samples/cjs-transpiler-re-exports/_expected/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.foo = 1;
Object.defineProperty(exports, "__proto__", {
enumerable: true,
value: 2
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let isFirstReassigned = false;

const result1 = (foo(), isFirstReassigned );
const result1 = (foo(), isFirstReassigned);
console.log(result1);

let isSecondReassigned = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
console.log('keep' );
console.log('keep');
console.log('keep');

const x = 'keep';
console.log(x );
console.log(x);
console.log(x);
6 changes: 3 additions & 3 deletions test/form/samples/nullish-coalescing/_expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ function getNullSideEffect() {

console.log('null');
console.log('undefined');
console.log(0 );
console.log('' );
console.log('Hello' );
console.log(0);
console.log('');
console.log('Hello');

console.log('null return');
console.log(getNullSideEffect() ?? 'null return with side-effect');
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ define((function () { 'use strict';
(null).foo = 1;

// effect
(true )();
(false )();
(true)();
(false)();
((() => console.log( 'effect' )))();
((() => console.log( 'effect' )))();

// effect
(true )()();
(false )()();
(true)()();
(false)()();
((() => () => console.log( 'effect' )))()();
((() => () => console.log( 'effect' )))()();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ const foo = {
(null).foo = 1;

// effect
(true )();
(false )();
(true)();
(false)();
((() => console.log( 'effect' )))();
((() => console.log( 'effect' )))();

// effect
(true )()();
(false )()();
(true)()();
(false)()();
((() => () => console.log( 'effect' )))()();
((() => () => console.log( 'effect' )))()();
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const foo = {
(null).foo = 1;

// effect
(true )();
(false )();
(true)();
(false)();
((() => console.log( 'effect' )))();
((() => console.log( 'effect' )))();

// effect
(true )()();
(false )()();
(true)()();
(false)()();
((() => () => console.log( 'effect' )))()();
((() => () => console.log( 'effect' )))()();
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
(null).foo = 1;

// effect
(true )();
(false )();
(true)();
(false)();
((() => console.log( 'effect' )))();
((() => console.log( 'effect' )))();

// effect
(true )()();
(false )()();
(true)()();
(false)()();
((() => () => console.log( 'effect' )))()();
((() => () => console.log( 'effect' )))()();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ System.register([], (function () {
(null).foo = 1;

// effect
(true )();
(false )();
(true)();
(false)();
((() => console.log( 'effect' )))();
((() => console.log( 'effect' )))();

// effect
(true )()();
(false )()();
(true)()();
(false)()();
((() => () => console.log( 'effect' )))()();
((() => () => console.log( 'effect' )))()();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
(null).foo = 1;

// effect
(true )();
(false )();
(true)();
(false)();
((() => console.log( 'effect' )))();
((() => console.log( 'effect' )))();

// effect
(true )()();
(false )()();
(true)()();
(false)()();
((() => () => console.log( 'effect' )))()();
((() => () => console.log( 'effect' )))()();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ function fun2(enable) {
}
}

console.log(fun2());
console.log(fun2());
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ function module2 (enable) {
}
}

console.log(module2());
console.log(module2());
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ define((function () { 'use strict';

console.log(getStringA());

console.log(false );
console.log(false);

console.log(true );
console.log(true);

function getStringD() {
return 'D';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ function getStringA() {

console.log(getStringA());

console.log(false );
console.log(false);

console.log(true );
console.log(true);

function getStringD() {
return 'D';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ function getStringA() {

console.log(getStringA());

console.log(false );
console.log(false);

console.log(true );
console.log(true);

function getStringD() {
return 'D';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

console.log(getStringA());

console.log(false );
console.log(false);

console.log(true );
console.log(true);

function getStringD() {
return 'D';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ System.register([], (function () {

console.log(getStringA());

console.log(false );
console.log(false);

console.log(true );
console.log(true);

function getStringD() {
return 'D';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

console.log(getStringA());

console.log(false );
console.log(false);

console.log(true );
console.log(true);

function getStringD() {
return 'D';
Expand Down

0 comments on commit 1d2c1e9

Please sign in to comment.