@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,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
- 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
- }
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
- type candidate struct {
32
- name string
33
- node *shimast.Node
34
- }
35
- var candidates []candidate
36
- assigned := map[string]bool{}
31
+ type candidate struct {
32
+ name string
33
+ node *shimast.Node
34
+ }
35
+ var candidates []candidate
36
+ assigned := map[string]bool{}
37
37
 
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
- })
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
- for _, c := range candidates {
89
- if !assigned[c.name] {
90
- ctx.Report(c.node, "Use const instead of let.")
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
- 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)
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
- 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
- }
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
- Register(noVar{})
125
- Register(preferConst{})
126
- Register(noUndefInit{})
124
+ Register(noVar{})
125
+ Register(preferConst{})
126
+ Register(noUndefInit{})
127
127
  }
package/src/index.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import path from "node:path";
2
- import type {
3
- ITtscPlugin,
4
- ITtscPluginFactoryContext,
5
- } from "ttsc";
2
+ import type { ITtscPlugin, ITtscPluginFactoryContext } from "ttsc";
6
3
 
7
4
  import type { ITtscLintPluginConfig } from "./structures";
8
5