Skip to content

Commit ee54c35

Browse files
committed
feat: add core no-redeclare rule
1 parent b7d19ac commit ee54c35

16 files changed

Lines changed: 2563 additions & 751 deletions

File tree

internal/plugins/typescript/rules/no_redeclare/no_redeclare.go

Lines changed: 2 additions & 633 deletions
Large diffs are not rendered by default.

internal/plugins/typescript/rules/no_redeclare/no_redeclare_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func TestNoRedeclareRule(t *testing.T) {
6969
// declarations are module-scoped and do not merge with lib globals.
7070
{Code: "export {};\nvar Object = 0;", Options: map[string]interface{}{"builtinGlobals": true}},
7171
{Code: "import {} from './foo';\nvar Array = 0;", Options: map[string]interface{}{"builtinGlobals": true}},
72+
{Code: "export {};\n/*globals Array */", Options: map[string]interface{}{"builtinGlobals": true}},
7273

7374
// ====================================================================
7475
// TypeScript declaration merging (default ignoreDeclarationMerge: true).
@@ -246,8 +247,8 @@ func TestNoRedeclareRule(t *testing.T) {
246247
// interfaces in different declaration spaces. ESLint, which does
247248
// name-level matching, flags these. We intentionally don't.
248249
// ====================================================================
249-
{Code: "type NodeListOf = 1;"}, // lib.dom
250-
{Code: "type HTMLElement = 1;"}, // lib.dom
250+
{Code: "type NodeListOf = 1;"}, // lib.dom
251+
{Code: "type HTMLElement = 1;"}, // lib.dom
251252
{Code: "type Array = 1;", Options: map[string]interface{}{"builtinGlobals": true}}, // lib.es5 core interface
252253
// Extending a lib interface with a same-named user interface is
253254
// the idiomatic TS pattern (ImportMeta augmentation, etc.) — it
@@ -516,6 +517,13 @@ func TestNoRedeclareRule(t *testing.T) {
516517
{MessageId: "redeclaredAsBuiltin", Line: 1, Column: 5},
517518
},
518519
},
520+
{
521+
Code: "/*globals Array */",
522+
Errors: []rule_tester.InvalidTestCaseError{
523+
{MessageId: "redeclaredAsBuiltin", Line: 1, Column: 11},
524+
},
525+
Options: map[string]interface{}{"builtinGlobals": true},
526+
},
519527
// ====================================================================
520528
// Documented divergence #1 (see no_redeclare.md):
521529
// `builtinGlobals` resolves against the project's `lib` configuration

internal/rules/all.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import (
8686
"github.com/web-infra-dev/rslint/internal/rules/no_param_reassign"
8787
"github.com/web-infra-dev/rslint/internal/rules/no_proto"
8888
"github.com/web-infra-dev/rslint/internal/rules/no_prototype_builtins"
89+
"github.com/web-infra-dev/rslint/internal/rules/no_redeclare"
8990
"github.com/web-infra-dev/rslint/internal/rules/no_regex_spaces"
9091
"github.com/web-infra-dev/rslint/internal/rules/no_restricted_imports"
9192
"github.com/web-infra-dev/rslint/internal/rules/no_restricted_syntax"
@@ -222,6 +223,7 @@ func GetAllRules() []rule.Rule {
222223
no_octal_escape.NoOctalEscapeRule,
223224
no_param_reassign.NoParamReassignRule,
224225
no_proto.NoProtoRule,
226+
no_redeclare.NoRedeclareRule,
225227
radix.RadixRule,
226228
no_regex_spaces.NoRegexSpacesRule,
227229
no_return_assign.NoReturnAssignRule,

internal/rules/no_global_assign/no_global_assign.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,6 @@ import (
88
"github.com/web-infra-dev/rslint/internal/utils"
99
)
1010

11-
// builtinGlobals contains names of known read-only built-in globals.
12-
var builtinGlobals = map[string]bool{
13-
"AggregateError": true, "Array": true, "ArrayBuffer": true, "AsyncDisposableStack": true,
14-
"AsyncIterator": true, "Atomics": true,
15-
"BigInt": true, "BigInt64Array": true, "BigUint64Array": true,
16-
"Boolean": true, "DataView": true, "Date": true,
17-
"decodeURI": true, "decodeURIComponent": true, "DisposableStack": true,
18-
"encodeURI": true, "encodeURIComponent": true,
19-
"Error": true, "escape": true, "EvalError": true,
20-
"FinalizationRegistry": true, "Float32Array": true, "Float64Array": true, "Function": true,
21-
"globalThis": true, "Infinity": true, "Int8Array": true,
22-
"Int16Array": true, "Int32Array": true, "Intl": true, "isFinite": true,
23-
"isNaN": true, "Iterator": true, "JSON": true, "Map": true, "Math": true,
24-
"NaN": true, "Number": true, "Object": true, "parseFloat": true,
25-
"parseInt": true, "Promise": true, "Proxy": true, "RangeError": true,
26-
"ReferenceError": true, "Reflect": true, "RegExp": true,
27-
"Set": true, "SharedArrayBuffer": true, "String": true, "SuppressedError": true,
28-
"Symbol": true, "SyntaxError": true, "TypeError": true,
29-
"Uint8Array": true, "Uint8ClampedArray": true, "Uint16Array": true,
30-
"Uint32Array": true, "unescape": true, "URIError": true, "undefined": true,
31-
"WeakMap": true, "WeakRef": true, "WeakSet": true,
32-
}
33-
3411
type options struct {
3512
exceptions map[string]bool
3613
}
@@ -78,7 +55,7 @@ var NoGlobalAssignRule = rule.Rule{
7855
return rule.RuleListeners{
7956
ast.KindIdentifier: func(node *ast.Node) {
8057
name := node.Text()
81-
if !builtinGlobals[name] || opts.exceptions[name] {
58+
if !utils.IsECMAScriptGlobal(name) || opts.exceptions[name] {
8259
return
8360
}
8461

0 commit comments

Comments
 (0)