New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix plugin-transform-block-scoping const violations #13248
Merged
JLHwung
merged 5 commits into
babel:main
from
overlookmotel:block-scope-const-violations
May 3, 2021
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9fb8e9a
Fix plugin-transform-block-scoping const violations
overlookmotel 370b9a5
Replace `a++` with `+a` where const violation
overlookmotel 2d553db
Remove assignment where const violation
overlookmotel e1daf4f
Remove assignment for `&&=`, `||=`, `??=` where const violation
overlookmotel 808a0e6
Shorten test
overlookmotel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
41 changes: 41 additions & 0 deletions
41
...ugin-transform-block-scoping/test/fixtures/const-violations/assignment-expression/exec.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,41 @@ | ||
const state1 = {}; | ||
expect(function() { | ||
const a = 3; | ||
let b = 1; | ||
state1.getA = () => a; | ||
state1.getB = () => b; | ||
|
||
a += b++; | ||
}).toThrow('"a" is read-only'); | ||
expect(state1.getA()).toBe(3); // Assignment did not succeed | ||
expect(state1.getB()).toBe(2); // `b++` was evaluated before error thrown | ||
|
||
const state2 = {}; | ||
expect(function() { | ||
const a = { | ||
valueOf() { | ||
state2.valueOfIsCalled = true; | ||
} | ||
}; | ||
state2.a = a; | ||
state2.getA = () => a; | ||
state2.getB = () => b; | ||
|
||
let b = 1; | ||
a += b++; | ||
}).toThrow('"a" is read-only'); | ||
expect(state2.getA()).toBe(state2.a); // Assignment did not succeed | ||
expect(state2.getB()).toBe(2); // `b++` was evaluated before error thrown | ||
expect(state2.valueOfIsCalled).toBe(true); // `a` was read before error thrown | ||
|
||
const state3 = {}; | ||
expect(function() { | ||
const a = 32; | ||
let b = 1; | ||
state3.getA = () => a; | ||
state3.getB = () => b; | ||
|
||
a >>>= ++b; | ||
}).toThrow('"a" is read-only'); | ||
expect(state3.getA()).toBe(32); // Assignment did not succeed | ||
expect(state3.getB()).toBe(2); // `++b` was evaluated before error thrown |
11 changes: 11 additions & 0 deletions
11
...gin-transform-block-scoping/test/fixtures/const-violations/assignment-expression/input.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,11 @@ | ||
const a = 5; | ||
let b = 0; | ||
a += b++; | ||
|
||
const c = 32; | ||
let d = 1; | ||
c >>>= ++d; | ||
|
||
const e = 0; | ||
let f = 1; | ||
e ||= ++f; |
9 changes: 9 additions & 0 deletions
9
...in-transform-block-scoping/test/fixtures/const-violations/assignment-expression/output.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,9 @@ | ||
var a = 5; | ||
var b = 0; | ||
a + b++, babelHelpers.readOnlyError("a"); | ||
var c = 32; | ||
var d = 1; | ||
c >>> ++d, babelHelpers.readOnlyError("c"); | ||
var e = 0; | ||
var f = 1; | ||
e ||= (++f, babelHelpers.readOnlyError("e")); |
2 changes: 1 addition & 1 deletion
2
...lugin-transform-block-scoping/test/fixtures/const-violations/deadcode-violation/output.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
(function () { | ||
var a = "foo"; | ||
if (false) a = (babelHelpers.readOnlyError("a"), "false"); | ||
if (false) a = ("false", babelHelpers.readOnlyError("a")); | ||
return a; | ||
})(); |
2 changes: 1 addition & 1 deletion
2
...transform-block-scoping/test/fixtures/const-violations/destructuring-assignment/output.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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
var a = 1, | ||
b = 2; | ||
a = (babelHelpers.readOnlyError("a"), 3); | ||
a = (3, babelHelpers.readOnlyError("a")); |
4 changes: 4 additions & 0 deletions
4
packages/babel-plugin-transform-block-scoping/test/fixtures/const-violations/loop/input.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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
for (const i = 0; i < 3; i = i + 1) { | ||
console.log(i); | ||
} | ||
|
||
for (const j = 0; j < 3; j++) { | ||
console.log(j); | ||
} |
6 changes: 5 additions & 1 deletion
6
packages/babel-plugin-transform-block-scoping/test/fixtures/const-violations/loop/output.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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
for (var i = 0; i < 3; i = (babelHelpers.readOnlyError("i"), i + 1)) { | ||
for (var i = 0; i < 3; i = (i + 1, babelHelpers.readOnlyError("i"))) { | ||
console.log(i); | ||
} | ||
|
||
for (var j = 0; j < 3; j + 1, babelHelpers.readOnlyError("j")) { | ||
console.log(j); | ||
} |
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
16 changes: 16 additions & 0 deletions
16
...babel-plugin-transform-block-scoping/test/fixtures/const-violations/no-assignment/exec.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 |
---|---|---|
@@ -1,4 +1,20 @@ | ||
const state1 = {}; | ||
expect(function() { | ||
const a = 3; | ||
state1.getA = () => a; | ||
|
||
a = 7; | ||
}).toThrow('"a" is read-only'); | ||
expect(state1.getA()).toBe(3); // Assignment did not succeed | ||
|
||
const state2 = {}; | ||
expect(function() { | ||
const a = 3; | ||
let b = 0; | ||
state2.getA = () => a; | ||
state2.getB = () => b; | ||
|
||
a = b++; | ||
}).toThrow('"a" is read-only'); | ||
expect(state2.getA()).toBe(3); // Assignment did not succeed | ||
expect(state2.getB()).toBe(1); // `b++` was evaluated before error thrown |
5 changes: 4 additions & 1 deletion
5
...abel-plugin-transform-block-scoping/test/fixtures/const-violations/no-assignment/input.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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
const MULTIPLIER = 5; | ||
|
||
MULTIPLIER = "overwrite"; | ||
|
||
const a = 5; | ||
let b = 0; | ||
a = b++; |
5 changes: 4 additions & 1 deletion
5
...bel-plugin-transform-block-scoping/test/fixtures/const-violations/no-assignment/output.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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
var MULTIPLIER = 5; | ||
MULTIPLIER = (babelHelpers.readOnlyError("MULTIPLIER"), "overwrite"); | ||
MULTIPLIER = ("overwrite", babelHelpers.readOnlyError("MULTIPLIER")); | ||
var a = 5; | ||
var b = 0; | ||
a = (b++, babelHelpers.readOnlyError("a")); |
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
2 changes: 1 addition & 1 deletion
2
...s/babel-plugin-transform-block-scoping/test/fixtures/const-violations/no-for-in/output.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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
var MULTIPLIER = 5; | ||
|
||
for (MULTIPLIER in arr) { | ||
for (var _MULTIPLIER in arr) { | ||
babelHelpers.readOnlyError("MULTIPLIER"); | ||
; | ||
} |
19 changes: 19 additions & 0 deletions
19
...n-transform-block-scoping/test/fixtures/const-violations/update-expression-prefix/exec.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 |
---|---|---|
@@ -1,4 +1,23 @@ | ||
const state1 = {}; | ||
expect(function() { | ||
const a = "str"; | ||
state1.getA = () => a; | ||
|
||
--a; | ||
}).toThrow('"a" is read-only'); | ||
expect(state1.getA()).toBe("str"); // Assignment did not succeed | ||
|
||
const state2 = {}; | ||
expect(function() { | ||
const b = { | ||
valueOf() { | ||
state2.valueOfIsCalled = true; | ||
} | ||
}; | ||
state2.b = b; | ||
state2.getB = () => b; | ||
|
||
--b; | ||
}).toThrow('"b" is read-only'); | ||
expect(state2.getB()).toBe(state2.b); // Assignment did not succeed | ||
expect(state2.valueOfIsCalled).toBe(true); // `bar` was read before error thrown |
2 changes: 1 addition & 1 deletion
2
...transform-block-scoping/test/fixtures/const-violations/update-expression-prefix/output.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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
var a = "str"; | ||
babelHelpers.readOnlyError("a"), --a; | ||
a - 1, babelHelpers.readOnlyError("a"); |
19 changes: 19 additions & 0 deletions
19
...l-plugin-transform-block-scoping/test/fixtures/const-violations/update-expression/exec.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 |
---|---|---|
@@ -1,4 +1,23 @@ | ||
const state1 = {}; | ||
expect(function() { | ||
const foo = 1; | ||
state1.getFoo = () => foo; | ||
|
||
foo++; | ||
}).toThrow('"foo" is read-only'); | ||
expect(state1.getFoo()).toBe(1); // Assignment did not succeed | ||
|
||
const state2 = {}; | ||
expect(function() { | ||
const bar = { | ||
valueOf() { | ||
state2.valueOfIsCalled = true; | ||
} | ||
}; | ||
state2.bar = bar; | ||
state2.getBar = () => bar; | ||
|
||
bar++; | ||
}).toThrow('"bar" is read-only'); | ||
expect(state2.getBar()).toBe(state2.bar); // Assignment did not succeed | ||
expect(state2.valueOfIsCalled).toBe(true); // `bar` was read before error thrown |
2 changes: 1 addition & 1 deletion
2
...plugin-transform-block-scoping/test/fixtures/const-violations/update-expression/output.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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
var foo = 1; | ||
babelHelpers.readOnlyError("foo"), foo++; | ||
foo + 1, babelHelpers.readOnlyError("foo"); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify this to
t.unaryExpression("+", violation.get("argument").node)
(even if it saves literally a single byte).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
+c
has same effect of triggering.valueOf()
asc + 1
. Have added a commit to make this change.I hadn't really bothered with optimization since in most cases code like this is a bug anyway.
Have also added another commit for the other most obvious optimization - shorten
c = f()
to justf()
since the assignment will never be executed anyway. There are other optimizations which could be made (a += 'long_string'
toa + ''
for example), but in my view going further would add complexity for little gain.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added one more commit to shorten
c &&= x++
->c && x++
.