forked from swc-project/swc
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 (swc-project#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.