@ttsc/lint 0.7.1 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -24
- package/lib/index.js.map +1 -1
- package/package.json +3 -3
- package/plugin/ast_helpers.go +158 -158
- package/plugin/compile.go +303 -303
- package/plugin/config.go +322 -322
- package/plugin/engine.go +152 -153
- package/plugin/host.go +128 -128
- package/plugin/main.go +21 -21
- package/plugin/rules_arrays.go +51 -51
- package/plugin/rules_console.go +22 -23
- package/plugin/rules_debugger.go +4 -4
- package/plugin/rules_dupes.go +134 -134
- package/plugin/rules_empty.go +68 -68
- package/plugin/rules_eval.go +28 -28
- package/plugin/rules_finally.go +91 -91
- package/plugin/rules_gap.go +188 -188
- package/plugin/rules_logic.go +268 -268
- package/plugin/rules_loops.go +89 -89
- package/plugin/rules_misc.go +43 -43
- package/plugin/rules_problems.go +396 -396
- package/plugin/rules_protos.go +17 -17
- package/plugin/rules_self.go +56 -56
- package/plugin/rules_strings.go +65 -65
- package/plugin/rules_suggestions.go +863 -863
- package/plugin/rules_throw.go +16 -16
- package/plugin/rules_ts.go +161 -161
- package/plugin/rules_ts_extra.go +467 -467
- package/plugin/rules_var.go +86 -86
- package/src/index.ts +1 -4
package/plugin/rules_var.go
CHANGED
|
@@ -9,13 +9,13 @@ type noVar struct{}
|
|
|
9
9
|
func (noVar) Name() string { return "no-var" }
|
|
10
10
|
func (noVar) Visits() []shimast.Kind { return []shimast.Kind{shimast.KindVariableStatement} }
|
|
11
11
|
func (noVar) Check(ctx *Context, node *shimast.Node) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
stmt := node.AsVariableStatement()
|
|
13
|
+
if stmt == nil || stmt.DeclarationList == nil {
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
if shimast.IsVar(stmt.DeclarationList) {
|
|
17
|
+
ctx.Report(node, "Unexpected var, use let or const instead.")
|
|
18
|
+
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
// prefer-const: flag `let` declarations whose binding is never reassigned.
|
|
@@ -28,80 +28,80 @@ type preferConst struct{}
|
|
|
28
28
|
func (preferConst) Name() string { return "prefer-const" }
|
|
29
29
|
func (preferConst) Visits() []shimast.Kind { return []shimast.Kind{shimast.KindSourceFile} }
|
|
30
30
|
func (preferConst) Check(ctx *Context, node *shimast.Node) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
type candidate struct {
|
|
32
|
+
name string
|
|
33
|
+
node *shimast.Node
|
|
34
|
+
}
|
|
35
|
+
var candidates []candidate
|
|
36
|
+
assigned := map[string]bool{}
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
38
|
+
walkDescendants(node, func(child *shimast.Node) {
|
|
39
|
+
switch child.Kind {
|
|
40
|
+
case shimast.KindVariableDeclaration:
|
|
41
|
+
decl := child.AsVariableDeclaration()
|
|
42
|
+
if decl == nil || child.Parent == nil || child.Parent.Kind != shimast.KindVariableDeclarationList {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
listNode := child.Parent
|
|
46
|
+
if !shimast.IsLet(listNode) {
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
name := identifierText(decl.Name())
|
|
50
|
+
if name == "" {
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
if !isConstEligibleLetDeclaration(child, decl) {
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
candidates = append(candidates, candidate{name: name, node: child})
|
|
57
|
+
case shimast.KindBinaryExpression:
|
|
58
|
+
expr := child.AsBinaryExpression()
|
|
59
|
+
if expr == nil || expr.OperatorToken == nil || !isAssignmentOperator(expr.OperatorToken.Kind) {
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
for _, name := range bindingIdentifierNames(expr.Left) {
|
|
63
|
+
assigned[name] = true
|
|
64
|
+
}
|
|
65
|
+
case shimast.KindPrefixUnaryExpression:
|
|
66
|
+
expr := child.AsPrefixUnaryExpression()
|
|
67
|
+
if expr == nil {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
if expr.Operator == shimast.KindPlusPlusToken || expr.Operator == shimast.KindMinusMinusToken {
|
|
71
|
+
if name := identifierText(expr.Operand); name != "" {
|
|
72
|
+
assigned[name] = true
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
case shimast.KindPostfixUnaryExpression:
|
|
76
|
+
expr := child.AsPostfixUnaryExpression()
|
|
77
|
+
if expr == nil {
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
if expr.Operator == shimast.KindPlusPlusToken || expr.Operator == shimast.KindMinusMinusToken {
|
|
81
|
+
if name := identifierText(expr.Operand); name != "" {
|
|
82
|
+
assigned[name] = true
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
87
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
for _, c := range candidates {
|
|
89
|
+
if !assigned[c.name] {
|
|
90
|
+
ctx.Report(c.node, "Use const instead of let.")
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
func isConstEligibleLetDeclaration(node *shimast.Node, decl *shimast.VariableDeclaration) bool {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
96
|
+
if decl.Initializer != nil {
|
|
97
|
+
if node.Parent != nil && node.Parent.Parent != nil && node.Parent.Parent.Kind == shimast.KindForStatement {
|
|
98
|
+
list := node.Parent.AsVariableDeclarationList()
|
|
99
|
+
return list == nil || list.Declarations == nil || len(list.Declarations.Nodes) == 1
|
|
100
|
+
}
|
|
101
|
+
return true
|
|
102
|
+
}
|
|
103
|
+
return node.Parent != nil && node.Parent.Parent != nil &&
|
|
104
|
+
(node.Parent.Parent.Kind == shimast.KindForInStatement || node.Parent.Parent.Kind == shimast.KindForOfStatement)
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
// no-undef-init: forbid `let x = undefined` and `var x = undefined`.
|
|
@@ -111,17 +111,17 @@ type noUndefInit struct{}
|
|
|
111
111
|
func (noUndefInit) Name() string { return "no-undef-init" }
|
|
112
112
|
func (noUndefInit) Visits() []shimast.Kind { return []shimast.Kind{shimast.KindVariableDeclaration} }
|
|
113
113
|
func (noUndefInit) Check(ctx *Context, node *shimast.Node) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
114
|
+
decl := node.AsVariableDeclaration()
|
|
115
|
+
if decl == nil || decl.Initializer == nil {
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
if identifierText(decl.Initializer) == "undefined" {
|
|
119
|
+
ctx.Report(decl.Initializer, "It's not necessary to initialize \"undefined\".")
|
|
120
|
+
}
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
func init() {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
Register(noVar{})
|
|
125
|
+
Register(preferConst{})
|
|
126
|
+
Register(noUndefInit{})
|
|
127
127
|
}
|
package/src/index.ts
CHANGED