@ttsc/lint 0.7.2 → 0.8.0-dev.20260505

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.
@@ -9,99 +9,99 @@ type forDirection struct{}
9
9
  func (forDirection) Name() string { return "for-direction" }
10
10
  func (forDirection) Visits() []shimast.Kind { return []shimast.Kind{shimast.KindForStatement} }
11
11
  func (forDirection) Check(ctx *Context, node *shimast.Node) {
12
- loop := node.AsForStatement()
13
- if loop == nil || loop.Condition == nil || loop.Incrementor == nil {
14
- return
15
- }
16
- cond := loop.Condition
17
- if cond.Kind != shimast.KindBinaryExpression {
18
- return
19
- }
20
- bin := cond.AsBinaryExpression()
21
- if bin == nil || bin.OperatorToken == nil {
22
- return
23
- }
24
- leftName := identifierText(bin.Left)
25
- rightName := identifierText(bin.Right)
26
- if leftName == "" && rightName == "" {
27
- return
28
- }
29
- directionFromCondition := 0
30
- switch bin.OperatorToken.Kind {
31
- case shimast.KindLessThanToken, shimast.KindLessThanEqualsToken:
32
- // `i < limit` requires increment
33
- if leftName != "" {
34
- directionFromCondition = +1
35
- } else {
36
- directionFromCondition = -1
37
- }
38
- case shimast.KindGreaterThanToken, shimast.KindGreaterThanEqualsToken:
39
- if leftName != "" {
40
- directionFromCondition = -1
41
- } else {
42
- directionFromCondition = +1
43
- }
44
- default:
45
- return
46
- }
47
- counterName := leftName
48
- if counterName == "" {
49
- counterName = rightName
50
- }
51
- directionFromIncr := updateDirection(loop.Incrementor, counterName)
52
- if directionFromIncr == 0 {
53
- return
54
- }
55
- if directionFromIncr*directionFromCondition < 0 {
56
- ctx.Report(loop.Incrementor, "The update clause in this loop moves the variable in the wrong direction.")
57
- }
12
+ loop := node.AsForStatement()
13
+ if loop == nil || loop.Condition == nil || loop.Incrementor == nil {
14
+ return
15
+ }
16
+ cond := loop.Condition
17
+ if cond.Kind != shimast.KindBinaryExpression {
18
+ return
19
+ }
20
+ bin := cond.AsBinaryExpression()
21
+ if bin == nil || bin.OperatorToken == nil {
22
+ return
23
+ }
24
+ leftName := identifierText(bin.Left)
25
+ rightName := identifierText(bin.Right)
26
+ if leftName == "" && rightName == "" {
27
+ return
28
+ }
29
+ directionFromCondition := 0
30
+ switch bin.OperatorToken.Kind {
31
+ case shimast.KindLessThanToken, shimast.KindLessThanEqualsToken:
32
+ // `i < limit` requires increment
33
+ if leftName != "" {
34
+ directionFromCondition = +1
35
+ } else {
36
+ directionFromCondition = -1
37
+ }
38
+ case shimast.KindGreaterThanToken, shimast.KindGreaterThanEqualsToken:
39
+ if leftName != "" {
40
+ directionFromCondition = -1
41
+ } else {
42
+ directionFromCondition = +1
43
+ }
44
+ default:
45
+ return
46
+ }
47
+ counterName := leftName
48
+ if counterName == "" {
49
+ counterName = rightName
50
+ }
51
+ directionFromIncr := updateDirection(loop.Incrementor, counterName)
52
+ if directionFromIncr == 0 {
53
+ return
54
+ }
55
+ if directionFromIncr*directionFromCondition < 0 {
56
+ ctx.Report(loop.Incrementor, "The update clause in this loop moves the variable in the wrong direction.")
57
+ }
58
58
  }
59
59
 
60
60
  func updateDirection(node *shimast.Node, counter string) int {
61
- if node == nil || counter == "" {
62
- return 0
63
- }
64
- switch node.Kind {
65
- case shimast.KindPostfixUnaryExpression:
66
- post := node.AsPostfixUnaryExpression()
67
- if post == nil || identifierText(post.Operand) != counter {
68
- return 0
69
- }
70
- switch post.Operator {
71
- case shimast.KindPlusPlusToken:
72
- return +1
73
- case shimast.KindMinusMinusToken:
74
- return -1
75
- }
76
- case shimast.KindPrefixUnaryExpression:
77
- pre := node.AsPrefixUnaryExpression()
78
- if pre == nil || identifierText(pre.Operand) != counter {
79
- return 0
80
- }
81
- switch pre.Operator {
82
- case shimast.KindPlusPlusToken:
83
- return +1
84
- case shimast.KindMinusMinusToken:
85
- return -1
86
- }
87
- case shimast.KindBinaryExpression:
88
- bin := node.AsBinaryExpression()
89
- if bin == nil || bin.OperatorToken == nil {
90
- return 0
91
- }
92
- if identifierText(bin.Left) != counter {
93
- return 0
94
- }
95
- switch bin.OperatorToken.Kind {
96
- case shimast.KindPlusEqualsToken:
97
- return +1
98
- case shimast.KindMinusEqualsToken:
99
- return -1
100
- }
101
- }
102
- return 0
61
+ if node == nil || counter == "" {
62
+ return 0
63
+ }
64
+ switch node.Kind {
65
+ case shimast.KindPostfixUnaryExpression:
66
+ post := node.AsPostfixUnaryExpression()
67
+ if post == nil || identifierText(post.Operand) != counter {
68
+ return 0
69
+ }
70
+ switch post.Operator {
71
+ case shimast.KindPlusPlusToken:
72
+ return +1
73
+ case shimast.KindMinusMinusToken:
74
+ return -1
75
+ }
76
+ case shimast.KindPrefixUnaryExpression:
77
+ pre := node.AsPrefixUnaryExpression()
78
+ if pre == nil || identifierText(pre.Operand) != counter {
79
+ return 0
80
+ }
81
+ switch pre.Operator {
82
+ case shimast.KindPlusPlusToken:
83
+ return +1
84
+ case shimast.KindMinusMinusToken:
85
+ return -1
86
+ }
87
+ case shimast.KindBinaryExpression:
88
+ bin := node.AsBinaryExpression()
89
+ if bin == nil || bin.OperatorToken == nil {
90
+ return 0
91
+ }
92
+ if identifierText(bin.Left) != counter {
93
+ return 0
94
+ }
95
+ switch bin.OperatorToken.Kind {
96
+ case shimast.KindPlusEqualsToken:
97
+ return +1
98
+ case shimast.KindMinusEqualsToken:
99
+ return -1
100
+ }
101
+ }
102
+ return 0
103
103
  }
104
104
 
105
105
  func init() {
106
- Register(forDirection{})
106
+ Register(forDirection{})
107
107
  }
@@ -10,39 +10,39 @@ type radix struct{}
10
10
  func (radix) Name() string { return "radix" }
11
11
  func (radix) Visits() []shimast.Kind { return []shimast.Kind{shimast.KindCallExpression} }
12
12
  func (radix) Check(ctx *Context, node *shimast.Node) {
13
- call := node.AsCallExpression()
14
- if call == nil {
15
- return
16
- }
17
- name := callCalleeName(call)
18
- if name != "parseInt" && name != "Number.parseInt" && !isMatchingPropertyAccess(call.Expression, "Number", "parseInt") {
19
- return
20
- }
21
- args := 0
22
- if call.Arguments != nil {
23
- args = len(call.Arguments.Nodes)
24
- }
25
- if args == 0 {
26
- return
27
- }
28
- if args == 1 {
29
- ctx.Report(node, "Missing radix parameter.")
30
- return
31
- }
32
- radixArg := call.Arguments.Nodes[1]
33
- radixArg = stripParens(radixArg)
34
- if radixArg == nil {
35
- ctx.Report(node, "Missing radix parameter.")
36
- return
37
- }
38
- if radixArg.Kind == shimast.KindNumericLiteral {
39
- text := numericLiteralText(radixArg)
40
- if text == "10" || text == "16" || text == "8" || text == "2" {
41
- return
42
- }
43
- ctx.Report(radixArg, "Invalid radix parameter.")
44
- return
45
- }
13
+ call := node.AsCallExpression()
14
+ if call == nil {
15
+ return
16
+ }
17
+ name := callCalleeName(call)
18
+ if name != "parseInt" && name != "Number.parseInt" && !isMatchingPropertyAccess(call.Expression, "Number", "parseInt") {
19
+ return
20
+ }
21
+ args := 0
22
+ if call.Arguments != nil {
23
+ args = len(call.Arguments.Nodes)
24
+ }
25
+ if args == 0 {
26
+ return
27
+ }
28
+ if args == 1 {
29
+ ctx.Report(node, "Missing radix parameter.")
30
+ return
31
+ }
32
+ radixArg := call.Arguments.Nodes[1]
33
+ radixArg = stripParens(radixArg)
34
+ if radixArg == nil {
35
+ ctx.Report(node, "Missing radix parameter.")
36
+ return
37
+ }
38
+ if radixArg.Kind == shimast.KindNumericLiteral {
39
+ text := numericLiteralText(radixArg)
40
+ if text == "10" || text == "16" || text == "8" || text == "2" {
41
+ return
42
+ }
43
+ ctx.Report(radixArg, "Invalid radix parameter.")
44
+ return
45
+ }
46
46
  }
47
47
 
48
48
  // no-new-wrappers: `new String("")`, `new Number(0)`, `new Boolean(false)`
@@ -53,17 +53,17 @@ type noNewWrappers struct{}
53
53
  func (noNewWrappers) Name() string { return "no-new-wrappers" }
54
54
  func (noNewWrappers) Visits() []shimast.Kind { return []shimast.Kind{shimast.KindNewExpression} }
55
55
  func (noNewWrappers) Check(ctx *Context, node *shimast.Node) {
56
- ne := node.AsNewExpression()
57
- if ne == nil {
58
- return
59
- }
60
- switch identifierText(ne.Expression) {
61
- case "String", "Number", "Boolean":
62
- ctx.Report(node, "Do not use "+identifierText(ne.Expression)+" as a constructor.")
63
- }
56
+ ne := node.AsNewExpression()
57
+ if ne == nil {
58
+ return
59
+ }
60
+ switch identifierText(ne.Expression) {
61
+ case "String", "Number", "Boolean":
62
+ ctx.Report(node, "Do not use "+identifierText(ne.Expression)+" as a constructor.")
63
+ }
64
64
  }
65
65
 
66
66
  func init() {
67
- Register(radix{})
68
- Register(noNewWrappers{})
67
+ Register(radix{})
68
+ Register(noNewWrappers{})
69
69
  }