Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement --treeshake.propertyReadSideEffects=always (#3985)
- Loading branch information
Showing
10 changed files
with
80 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
description: 'verify property accesses are retained for getters with side effects', | ||
command: `rollup main.js --validate --treeshake.propertyReadSideEffects=always` | ||
}; |
17 changes: 17 additions & 0 deletions
17
test/cli/samples/propertyReadSideEffects-always/_expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class C { | ||
get x() { | ||
console.log(`side effect ${++count}`); | ||
return 42 | ||
} | ||
} | ||
let count = 0; | ||
const obj = new C; | ||
console.log(obj.x); | ||
|
||
// these statements should be retained | ||
if (obj.x) { | ||
obj["x"]; | ||
const {x} = obj; | ||
} | ||
let x; | ||
({x} = obj); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class C { | ||
get x() { | ||
console.log(`side effect ${++count}`) | ||
return 42 | ||
} | ||
} | ||
let count = 0 | ||
const obj = new C | ||
console.log(obj.x) | ||
|
||
// these statements should be retained | ||
if (obj.x) { | ||
obj["x"] | ||
const {x} = obj | ||
} | ||
let x | ||
({x} = obj) | ||
|
||
// demonstrate that tree shaking still works | ||
const unused = x => x | ||
unused(123) |
4 changes: 4 additions & 0 deletions
4
test/function/samples/propertyReadSideEffects-always/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
description: 'verify property accesses are retained for getters with side effects', | ||
options: { treeshake: { propertyReadSideEffects: 'always' } } | ||
}; |
14 changes: 14 additions & 0 deletions
14
test/function/samples/propertyReadSideEffects-always/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
let effects = 0 | ||
var obj = {} | ||
Object.defineProperty(obj, 'x', { | ||
get() { | ||
++effects | ||
} | ||
}) | ||
let value | ||
({x: value} = obj) | ||
obj.x | ||
obj["x"] | ||
const {x} = obj | ||
|
||
assert.strictEqual(effects, 4) |