Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(es/lints): Disallow reassignment to a class (#6362)
- Loading branch information
Showing
11 changed files
with
124 additions
and
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
class X { static y = X = 0 } |
7 changes: 7 additions & 0 deletions
7
crates/swc/tests/errors/lints/const-assign/3/output.swc-stderr
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 @@ | ||
|
||
x cannot reassign to a class | ||
,---- | ||
1 | class X { static y = X = 0 } | ||
: | ^ | ||
: `-- class name | ||
`---- |
99 changes: 12 additions & 87 deletions
99
crates/swc/tests/tsc-references/assignmentToParenthesizedIdentifiers.1.normal.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,88 +1,13 @@ | ||
//// [assignmentToParenthesizedIdentifiers.ts] | ||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; | ||
var x; | ||
x = 3; // OK | ||
x = 3; // OK | ||
x = ""; // Error | ||
x = ""; // Error | ||
var M; | ||
(function(M) { | ||
var y; | ||
M.y = y; | ||
})(M || (M = {})); | ||
M.y = 3; // OK | ||
M.y = 3; // OK | ||
M.y = 3; // OK | ||
M.y = ""; // Error | ||
M.y = ""; // Error | ||
M.y = ""; // Error | ||
M = { | ||
y: 3 | ||
}; // Error | ||
M = { | ||
y: 3 | ||
}; // Error | ||
var M2; | ||
(function(M2) { | ||
var M3; | ||
(function(M3) { | ||
var x; | ||
M3.x = x; | ||
})(M3 = M2.M3 || (M2.M3 = {})); | ||
M3 = { | ||
x: 3 | ||
}; // Error | ||
})(M2 || (M2 = {})); | ||
M2.M3 = { | ||
x: 3 | ||
}; // OK | ||
M2.M3 = { | ||
x: 3 | ||
}; // OK | ||
M2.M3 = { | ||
x: 3 | ||
}; // OK | ||
M2.M3 = { | ||
x: "" | ||
}; // Error | ||
M2.M3 = { | ||
x: "" | ||
}; // Error | ||
M2.M3 = { | ||
x: "" | ||
}; // Error | ||
function fn() {} | ||
fn = function() { | ||
return 3; | ||
}; // Bug 823548: Should be error (fn is not a reference) | ||
fn = function() { | ||
return 3; | ||
}; // Should be error | ||
function fn2(x, y) { | ||
x = 3; | ||
x = 3; // OK | ||
x = ""; // Error | ||
x = ""; // Error | ||
y.t = 3; // OK | ||
y.t = 3; // OK | ||
y.t = ""; // Error | ||
y.t = ""; // Error | ||
y["t"] = 3; // OK | ||
y["t"] = 3; // OK | ||
y["t"] = 3; // OK | ||
y["t"] = ""; // Error | ||
y["t"] = ""; // Error | ||
y["t"] = ""; // Error | ||
} | ||
var E; | ||
(function(E) { | ||
E[E["A"] = 0] = "A"; | ||
})(E || (E = {})); | ||
E = undefined; // Error | ||
E = undefined; // Error | ||
var C = function C() { | ||
"use strict"; | ||
_class_call_check(this, C); | ||
}; | ||
C = undefined; // Error | ||
C = undefined; // Error | ||
//! | ||
//! x cannot reassign to a class | ||
//! ,-[65:1] | ||
//! 65 | class C { | ||
//! : | | ||
//! : `-- class name | ||
//! 66 | | ||
//! 67 | } | ||
//! 68 | | ||
//! 69 | C = undefined; // Error | ||
//! : ^ | ||
//! `---- |
38 changes: 12 additions & 26 deletions
38
crates/swc/tests/tsc-references/assignmentToParenthesizedIdentifiers.2.minified.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,27 +1,13 @@ | ||
//// [assignmentToParenthesizedIdentifiers.ts] | ||
var M, M2, E; | ||
!function(M) { | ||
var y; | ||
M.y = y; | ||
}(M || (M = {})), M.y = 3, M.y = 3, M.y = 3, M.y = "", M.y = "", M.y = "", M = { | ||
y: 3 | ||
}, M = { | ||
y: 3 | ||
}, function(M2) { | ||
var x; | ||
(M2.M3 || (M2.M3 = {})).x = x; | ||
}(M2 || (M2 = {})), M2.M3 = { | ||
x: 3 | ||
}, M2.M3 = { | ||
x: 3 | ||
}, M2.M3 = { | ||
x: 3 | ||
}, M2.M3 = { | ||
x: "" | ||
}, M2.M3 = { | ||
x: "" | ||
}, M2.M3 = { | ||
x: "" | ||
}, function(E) { | ||
E[E.A = 0] = "A"; | ||
}(E || (E = {})), E = void 0, E = void 0; | ||
//! | ||
//! x cannot reassign to a class | ||
//! ,-[65:1] | ||
//! 65 | class C { | ||
//! : | | ||
//! : `-- class name | ||
//! 66 | | ||
//! 67 | } | ||
//! 68 | | ||
//! 69 | C = undefined; // Error | ||
//! : ^ | ||
//! `---- |
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,30 +1,10 @@ | ||
//// [assignments.ts] | ||
// In this file: | ||
// Assign to a module | ||
// Assign to a class | ||
// Assign to an enum | ||
// Assign to a function | ||
// Assign to a variable | ||
// Assign to a parameter | ||
// Assign to an interface | ||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; | ||
M = null; // Error | ||
var C = function C() { | ||
"use strict"; | ||
_class_call_check(this, C); | ||
}; | ||
C = null; // Error | ||
var E; | ||
(function(E) { | ||
E[E["A"] = 0] = "A"; | ||
})(E || (E = {})); | ||
E = null; // Error | ||
E.A = null; // OK per spec, Error per implementation (509581) | ||
function fn() {} | ||
fn = null; // Should be error | ||
var v; | ||
v = null; // OK | ||
function fn2(p) { | ||
p = null; // OK | ||
} | ||
I = null; // Error | ||
//! | ||
//! x cannot reassign to a class | ||
//! ,-[13:1] | ||
//! 13 | class C { } | ||
//! : | | ||
//! : `-- class name | ||
//! 14 | C = null; // Error | ||
//! : ^ | ||
//! `---- |
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,10 @@ | ||
//// [assignments.ts] | ||
var E; | ||
M = null, function(E) { | ||
E[E.A = 0] = "A"; | ||
}(E || (E = {})), (E = null).A = null, I = null; | ||
//! | ||
//! x cannot reassign to a class | ||
//! ,-[13:1] | ||
//! 13 | class C { } | ||
//! : | | ||
//! : `-- class name | ||
//! 14 | C = null; // Error | ||
//! : ^ | ||
//! `---- |
35 changes: 10 additions & 25 deletions
35
crates/swc/tests/tsc-references/invalidUndefinedAssignments.1.normal.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,26 +1,11 @@ | ||
//// [invalidUndefinedAssignments.ts] | ||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; | ||
var x; | ||
var E; | ||
(function(E) { | ||
E[E["A"] = 0] = "A"; | ||
})(E || (E = {})); | ||
E = x; | ||
E.A = x; | ||
var C = function C() { | ||
"use strict"; | ||
_class_call_check(this, C); | ||
}; | ||
var f; | ||
C = x; | ||
var g; | ||
g = x; | ||
I = x; | ||
var M; | ||
(function(M) { | ||
var x = M.x = 1; | ||
})(M || (M = {})); | ||
M = x; | ||
function i(a) {} | ||
// BUG 767030 | ||
i = x; | ||
//! | ||
//! x cannot reassign to a class | ||
//! ,-[7:1] | ||
//! 7 | class C { foo: string } | ||
//! : | | ||
//! : `-- class name | ||
//! 8 | var f: C; | ||
//! 9 | C = x; | ||
//! : ^ | ||
//! `---- |
14 changes: 10 additions & 4 deletions
14
crates/swc/tests/tsc-references/invalidUndefinedAssignments.2.minified.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,11 @@ | ||
//// [invalidUndefinedAssignments.ts] | ||
var x, E, M; | ||
!function(E) { | ||
E[E.A = 0] = "A"; | ||
}(E || (E = {})), (E = x).A = x, I = x, (M || (M = {})).x = 1, M = x; | ||
//! | ||
//! x cannot reassign to a class | ||
//! ,-[7:1] | ||
//! 7 | class C { foo: string } | ||
//! : | | ||
//! : `-- class name | ||
//! 8 | var f: C; | ||
//! 9 | C = x; | ||
//! : ^ | ||
//! `---- |
43 changes: 11 additions & 32 deletions
43
crates/swc/tests/tsc-references/validNullAssignments.1.normal.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,33 +1,12 @@ | ||
//// [validNullAssignments.ts] | ||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; | ||
var a = null; | ||
var b = null; | ||
var c = null; | ||
var d = null; | ||
var e = null; | ||
e = null; // ok | ||
var E; | ||
(function(E) { | ||
E[E["A"] = 0] = "A"; | ||
})(E || (E = {})); | ||
E.A = null; // error | ||
var C = function C() { | ||
"use strict"; | ||
_class_call_check(this, C); | ||
}; | ||
var f; | ||
f = null; // ok | ||
C = null; // error | ||
var g; | ||
g = null; // ok | ||
I = null; // error | ||
var M; | ||
(function(M) { | ||
var x = M.x = 1; | ||
})(M || (M = {})); | ||
M = null; // error | ||
var h = null; | ||
function i(a) { | ||
a = null; | ||
} | ||
i = null; // error | ||
//! | ||
//! x cannot reassign to a class | ||
//! ,-[12:1] | ||
//! 12 | class C { foo: string } | ||
//! : | | ||
//! : `-- class name | ||
//! 13 | var f: C; | ||
//! 14 | f = null; // ok | ||
//! 15 | C = null; // error | ||
//! : ^ | ||
//! `---- |
15 changes: 11 additions & 4 deletions
15
crates/swc/tests/tsc-references/validNullAssignments.2.minified.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,12 @@ | ||
//// [validNullAssignments.ts] | ||
var E, M; | ||
!function(E) { | ||
E[E.A = 0] = "A"; | ||
}(E || (E = {})), E.A = null, I = null, (M || (M = {})).x = 1, M = null; | ||
//! | ||
//! x cannot reassign to a class | ||
//! ,-[12:1] | ||
//! 12 | class C { foo: string } | ||
//! : | | ||
//! : `-- class name | ||
//! 13 | var f: C; | ||
//! 14 | f = null; // ok | ||
//! 15 | C = null; // error | ||
//! : ^ | ||
//! `---- |
Oops, something went wrong.
3793e83
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.
Benchmark
es/full/bugs-1
350860
ns/iter (± 20700
)348228
ns/iter (± 19534
)1.01
es/full/minify/libraries/antd
1852739007
ns/iter (± 19952990
)1901637821
ns/iter (± 46749020
)0.97
es/full/minify/libraries/d3
401327937
ns/iter (± 21371991
)418685431
ns/iter (± 13781535
)0.96
es/full/minify/libraries/echarts
1568398001
ns/iter (± 16519383
)1613872450
ns/iter (± 36929596
)0.97
es/full/minify/libraries/jquery
100016478
ns/iter (± 3864453
)119365341
ns/iter (± 8773959
)0.84
es/full/minify/libraries/lodash
117890973
ns/iter (± 3211335
)136487309
ns/iter (± 7400297
)0.86
es/full/minify/libraries/moment
58828093
ns/iter (± 752187
)59527896
ns/iter (± 1715040
)0.99
es/full/minify/libraries/react
19868229
ns/iter (± 227336
)20747002
ns/iter (± 898055
)0.96
es/full/minify/libraries/terser
307131999
ns/iter (± 4781613
)315728962
ns/iter (± 7801742
)0.97
es/full/minify/libraries/three
559706199
ns/iter (± 12780862
)568646840
ns/iter (± 30625141
)0.98
es/full/minify/libraries/typescript
3382097933
ns/iter (± 22458590
)3491109046
ns/iter (± 47846620
)0.97
es/full/minify/libraries/victory
825497824
ns/iter (± 10507536
)858557132
ns/iter (± 26752846
)0.96
es/full/minify/libraries/vue
151816948
ns/iter (± 2602558
)152055768
ns/iter (± 5563797
)1.00
es/full/codegen/es3
32841
ns/iter (± 1424
)35192
ns/iter (± 5421
)0.93
es/full/codegen/es5
32816
ns/iter (± 1277
)33955
ns/iter (± 4179
)0.97
es/full/codegen/es2015
32839
ns/iter (± 1205
)34034
ns/iter (± 4401
)0.96
es/full/codegen/es2016
32790
ns/iter (± 401
)33590
ns/iter (± 4594
)0.98
es/full/codegen/es2017
32759
ns/iter (± 517
)34333
ns/iter (± 4566
)0.95
es/full/codegen/es2018
32786
ns/iter (± 773
)35200
ns/iter (± 5417
)0.93
es/full/codegen/es2019
31841
ns/iter (± 1373
)32986
ns/iter (± 1728
)0.97
es/full/codegen/es2020
31929
ns/iter (± 1281
)32357
ns/iter (± 1322
)0.99
es/full/all/es3
183529938
ns/iter (± 4318748
)188282853
ns/iter (± 4177376
)0.97
es/full/all/es5
174199073
ns/iter (± 3240433
)179275476
ns/iter (± 5596067
)0.97
es/full/all/es2015
139604611
ns/iter (± 3841830
)143278757
ns/iter (± 2890295
)0.97
es/full/all/es2016
138244689
ns/iter (± 3059201
)142571986
ns/iter (± 4470917
)0.97
es/full/all/es2017
138515787
ns/iter (± 3238457
)141666120
ns/iter (± 4779840
)0.98
es/full/all/es2018
136422522
ns/iter (± 3634465
)141373552
ns/iter (± 4179702
)0.96
es/full/all/es2019
136062001
ns/iter (± 3742305
)148868582
ns/iter (± 12036422
)0.91
es/full/all/es2020
130927232
ns/iter (± 2932843
)144637675
ns/iter (± 7212519
)0.91
es/full/parser
674794
ns/iter (± 23769
)799122
ns/iter (± 101172
)0.84
es/full/base/fixer
25162
ns/iter (± 1191
)26794
ns/iter (± 2548
)0.94
es/full/base/resolver_and_hygiene
87900
ns/iter (± 2547
)93121
ns/iter (± 9954
)0.94
serialization of ast node
208
ns/iter (± 5
)237
ns/iter (± 45
)0.88
serialization of serde
210
ns/iter (± 5
)220
ns/iter (± 21
)0.95
This comment was automatically generated by workflow using github-action-benchmark.