Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix plugin-transform-block-scoping const violations (#13248)
* Fix plugin-transform-block-scoping const violations Fixes #13245 * Replace `a++` with `+a` where const violation * Remove assignment where const violation * Remove assignment for `&&=`, `||=`, `??=` where const violation * Shorten test
- Loading branch information
1 parent
fa01fbe
commit f166b7a
Showing
18 changed files
with
194 additions
and
14 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
62 changes: 62 additions & 0 deletions
62
...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,62 @@ | ||
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 | ||
|
||
const state4 = {}; | ||
expect(function() { | ||
const a = 1; | ||
let b = 1; | ||
state4.getA = () => a; | ||
state4.getB = () => b; | ||
|
||
a &&= ++b; | ||
}).toThrow('"a" is read-only'); | ||
expect(state4.getA()).toBe(1); // Assignment did not succeed | ||
expect(state4.getB()).toBe(2); // `++b` was evaluated before error thrown | ||
|
||
{ | ||
const a = 1; | ||
let b = 1; | ||
a ||= ++b; | ||
|
||
expect(a).toBe(1); // Assignment not made | ||
expect(b).toBe(1); // `++b` was not evaluated | ||
} |
7 changes: 7 additions & 0 deletions
7
...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,7 @@ | ||
const a = 5; | ||
let b = 0; | ||
a += b++; | ||
a >>>= b++; | ||
a ||= b++; | ||
a &&= b++; | ||
a ??= b++; |
7 changes: 7 additions & 0 deletions
7
...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,7 @@ | ||
var a = 5; | ||
var b = 0; | ||
a + b++, babelHelpers.readOnlyError("a"); | ||
a >>> b++, babelHelpers.readOnlyError("a"); | ||
a || (b++, babelHelpers.readOnlyError("a")); | ||
a && (b++, babelHelpers.readOnlyError("a")); | ||
a ?? (b++, babelHelpers.readOnlyError("a")); |
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) "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); | ||
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 + 1, babelHelpers.readOnlyError("i")) { | ||
console.log(i); | ||
} | ||
|
||
for (var j = 0; j < 3; +j, 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"); | ||
"overwrite", babelHelpers.readOnlyError("MULTIPLIER"); | ||
var a = 5; | ||
var b = 0; | ||
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, 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, babelHelpers.readOnlyError("foo"); |