Skip to content

Commit f302b8c

Browse files
authored
feat: port rule one-var (#833)
1 parent 86a151a commit f302b8c

14 files changed

Lines changed: 4141 additions & 47 deletions

File tree

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ import (
230230
"github.com/web-infra-dev/rslint/internal/rules/no_var"
231231
"github.com/web-infra-dev/rslint/internal/rules/no_with"
232232
"github.com/web-infra-dev/rslint/internal/rules/object_shorthand"
233+
"github.com/web-infra-dev/rslint/internal/rules/one_var"
233234
"github.com/web-infra-dev/rslint/internal/rules/prefer_const"
234235
core_prefer_promise_reject_errors "github.com/web-infra-dev/rslint/internal/rules/prefer_promise_reject_errors"
235236
"github.com/web-infra-dev/rslint/internal/rules/prefer_rest_params"
@@ -729,6 +730,7 @@ func registerAllCoreEslintRules() {
729730
GlobalRuleRegistry.Register("no-unreachable", no_unreachable.NoUnreachableRule)
730731
GlobalRuleRegistry.Register("require-atomic-updates", require_atomic_updates.RequireAtomicUpdatesRule)
731732
GlobalRuleRegistry.Register("object-shorthand", object_shorthand.ObjectShorthandRule)
733+
GlobalRuleRegistry.Register("one-var", one_var.OneVarRule)
732734
GlobalRuleRegistry.Register("no-dupe-else-if", no_dupe_else_if.NoDupeElseIfRule)
733735
GlobalRuleRegistry.Register("no-throw-literal", no_throw_literal.NoThrowLiteralRule)
734736
GlobalRuleRegistry.Register("no-useless-call", no_useless_call.NoUselessCallRule)

internal/rule/rule.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ func CreateRule(r Rule) Rule {
9999
type RuleMessage struct {
100100
Id string
101101
Description string
102+
// Data exposes the placeholder values that were substituted into
103+
// Description (e.g. ESLint's `report({ data: { type } })`). Downstream
104+
// reporters / IDE clients can use this for structured access. Optional —
105+
// rules that don't carry placeholders may leave it nil.
106+
Data map[string]string
102107
}
103108

104109
type RuleFix struct {

internal/rules/no_loop_func/no_loop_func.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -316,34 +316,12 @@ func isSymbolDeclaredInside(sym *ast.Symbol, funcNode *ast.Node) bool {
316316
return false
317317
}
318318

319-
// GetVarDeclListKind returns the kind of a VariableDeclarationList: one of
320-
// "const", "let", "var", "using", "await using", or "" for anything else.
321-
//
322-
// IMPORTANT: tsgo encodes `await using` as `NodeFlagsConst | NodeFlagsUsing`
323-
// (see the node-flags definitions in typescript-go's ast package), so the aggregate flag
324-
// `NodeFlagsAwaitUsing` is NOT a single bit. We must inspect the individual
325-
// `NodeFlagsUsing` and `NodeFlagsConst` bits and only report `"await using"`
326-
// when BOTH are set, otherwise `flags & NodeFlagsAwaitUsing != 0` would
327-
// collapse plain `const` and plain `using` into `"await using"`.
319+
// GetVarDeclListKind is a thin wrapper around utils.GetVarDeclListKind kept
320+
// for backwards compatibility with the typescript-eslint counterpart in
321+
// internal/plugins/typescript/rules/no_loop_func, which imports this symbol.
322+
// New callers should use utils.GetVarDeclListKind directly.
328323
func GetVarDeclListKind(declList *ast.Node) string {
329-
if declList == nil || declList.Kind != ast.KindVariableDeclarationList {
330-
return ""
331-
}
332-
flags := declList.Flags
333-
hasUsing := flags&ast.NodeFlagsUsing != 0
334-
hasConst := flags&ast.NodeFlagsConst != 0
335-
switch {
336-
case hasUsing && hasConst:
337-
return "await using"
338-
case hasUsing:
339-
return "using"
340-
case hasConst:
341-
return "const"
342-
case flags&ast.NodeFlagsLet != 0:
343-
return "let"
344-
default:
345-
return "var"
346-
}
324+
return utils.GetVarDeclListKind(declList)
347325
}
348326

349327
// enclosingVarDeclOfBindingElement walks up through nested BindingElement /

internal/rules/no_var/no_var.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package no_var
22

33
import (
44
"github.com/microsoft/typescript-go/shim/ast"
5-
"github.com/microsoft/typescript-go/shim/core"
6-
"github.com/microsoft/typescript-go/shim/scanner"
75

86
"github.com/web-infra-dev/rslint/internal/rule"
97
"github.com/web-infra-dev/rslint/internal/utils"
@@ -41,7 +39,7 @@ var NoVarRule = rule.Rule{
4139
}
4240

4341
if ctx.TypeChecker != nil && canFix(node, &ctx) {
44-
varRange := getVarKeywordRange(node, ctx.SourceFile)
42+
varRange := utils.GetVarKeywordRange(node, ctx.SourceFile)
4543
ctx.ReportNodeWithFixes(reportNode, msg,
4644
rule.RuleFixReplaceRange(varRange, "let"))
4745
} else {
@@ -59,12 +57,6 @@ func isInDeclareGlobal(node *ast.Node) bool {
5957
}) != nil
6058
}
6159

62-
// getVarKeywordRange returns the text range of the `var` keyword in a VariableDeclarationList.
63-
func getVarKeywordRange(node *ast.Node, sourceFile *ast.SourceFile) core.TextRange {
64-
s := scanner.GetScannerForSourceFile(sourceFile, node.Pos())
65-
return core.NewTextRange(s.TokenStart(), s.TokenEnd())
66-
}
67-
6860
// ---------- canFix: determines if var→let is safe ----------
6961

7062
// canFix checks all 10 ESLint conditions to determine if var→let is safe.

0 commit comments

Comments
 (0)