|
| 1 | +package linthost |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + |
| 7 | + shimast "github.com/microsoft/typescript-go/shim/ast" |
| 8 | + shimcore "github.com/microsoft/typescript-go/shim/core" |
| 9 | +) |
| 10 | + |
| 11 | +// noInnerDeclarations enforces the root-declaration positions and option |
| 12 | +// defaults of ESLint's core no-inner-declarations rule. TypeScript-Go parses |
| 13 | +// JavaScript and TypeScript with the current ECMAScript grammar, so strict |
| 14 | +// functions in ordinary parsed source have ES2015 block-function semantics. |
| 15 | +// https://eslint.org/docs/latest/rules/no-inner-declarations |
| 16 | +type noInnerDeclarations struct{} |
| 17 | + |
| 18 | +type noInnerDeclarationsOptions struct { |
| 19 | + both bool |
| 20 | + allowBlockScopedFunc bool |
| 21 | +} |
| 22 | + |
| 23 | +type noInnerDeclarationsBlockOptions struct { |
| 24 | + BlockScopedFunctions string `json:"blockScopedFunctions"` |
| 25 | +} |
| 26 | + |
| 27 | +func (noInnerDeclarations) Name() string { return "no-inner-declarations" } |
| 28 | + |
| 29 | +func (noInnerDeclarations) Visits() []shimast.Kind { |
| 30 | + return []shimast.Kind{ |
| 31 | + shimast.KindFunctionDeclaration, |
| 32 | + shimast.KindVariableDeclarationList, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (noInnerDeclarations) Check(ctx *Context, node *shimast.Node) { |
| 37 | + options := resolveNoInnerDeclarationsOptions(ctx) |
| 38 | + declaration := node |
| 39 | + declarationType := "function" |
| 40 | + |
| 41 | + if node.Kind == shimast.KindVariableDeclarationList { |
| 42 | + if !options.both || !shimast.IsVar(node) { |
| 43 | + return |
| 44 | + } |
| 45 | + declarationType = "variable" |
| 46 | + // A statement-level list is wrapped by VariableStatement in the tsgo AST. |
| 47 | + // Use that wrapper for root classification and for the diagnostic range; |
| 48 | + // loop-header lists have no wrapper and remain nested declarations. |
| 49 | + if node.Parent != nil && node.Parent.Kind == shimast.KindVariableStatement { |
| 50 | + declaration = node.Parent |
| 51 | + } |
| 52 | + } else if options.allowBlockScopedFunc && |
| 53 | + noInnerDeclarationsSupportsBlockFunctions(ctx.File) && |
| 54 | + noInnerDeclarationsIsStrict(ctx.File, node) { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if noInnerDeclarationsIsRoot(declaration) { |
| 59 | + return |
| 60 | + } |
| 61 | + ctx.Report( |
| 62 | + declaration, |
| 63 | + "Move "+declarationType+" declaration to "+noInnerDeclarationsAllowedBody(declaration)+" root.", |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +// resolveNoInnerDeclarationsOptions reads the canonical ESLint positional |
| 68 | +// options. The config transport preserves one positional value directly and |
| 69 | +// two or more values as an array, so both [severity, mode] and |
| 70 | +// [severity, mode, object] arrive without rule-specific parser behavior. |
| 71 | +func resolveNoInnerDeclarationsOptions(ctx *Context) noInnerDeclarationsOptions { |
| 72 | + resolved := noInnerDeclarationsOptions{allowBlockScopedFunc: true} |
| 73 | + if ctx == nil || len(ctx.Options) == 0 { |
| 74 | + return resolved |
| 75 | + } |
| 76 | + |
| 77 | + raw := bytes.TrimSpace(ctx.Options) |
| 78 | + if len(raw) == 0 { |
| 79 | + return resolved |
| 80 | + } |
| 81 | + slots := []json.RawMessage{raw} |
| 82 | + if raw[0] == '[' { |
| 83 | + if err := json.Unmarshal(raw, &slots); err != nil { |
| 84 | + return resolved |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if len(slots) > 0 { |
| 89 | + var mode string |
| 90 | + if json.Unmarshal(slots[0], &mode) == nil && mode == "both" { |
| 91 | + resolved.both = true |
| 92 | + } |
| 93 | + } |
| 94 | + if len(slots) > 1 { |
| 95 | + var options noInnerDeclarationsBlockOptions |
| 96 | + if json.Unmarshal(slots[1], &options) == nil && options.BlockScopedFunctions == "disallow" { |
| 97 | + resolved.allowBlockScopedFunc = false |
| 98 | + } |
| 99 | + } |
| 100 | + return resolved |
| 101 | +} |
| 102 | + |
| 103 | +func noInnerDeclarationsSupportsBlockFunctions(file *shimast.SourceFile) bool { |
| 104 | + return file != nil && file.ScriptKind != shimcore.ScriptKindJSON |
| 105 | +} |
| 106 | + |
| 107 | +// noInnerDeclarationsIsStrict derives strictness from parser-owned AST facts: |
| 108 | +// external-module identity, class ancestry, and real directive prologues. It |
| 109 | +// deliberately does not infer semantics from filenames or substring searches. |
| 110 | +func noInnerDeclarationsIsStrict(file *shimast.SourceFile, node *shimast.Node) bool { |
| 111 | + if file != nil && file.ExternalModuleIndicator != nil { |
| 112 | + return true |
| 113 | + } |
| 114 | + for ancestor := node.Parent; ancestor != nil; ancestor = ancestor.Parent { |
| 115 | + switch ancestor.Kind { |
| 116 | + case shimast.KindClassDeclaration, shimast.KindClassExpression: |
| 117 | + return true |
| 118 | + case shimast.KindSourceFile, shimast.KindModuleBlock: |
| 119 | + if noInnerDeclarationsHasUseStrictDirective(file, ancestor) { |
| 120 | + return true |
| 121 | + } |
| 122 | + case shimast.KindBlock: |
| 123 | + if isFunctionLikeKind(ancestor.Parent) && noInnerDeclarationsHasUseStrictDirective(file, ancestor) { |
| 124 | + return true |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return false |
| 129 | +} |
| 130 | + |
| 131 | +func noInnerDeclarationsHasUseStrictDirective(file *shimast.SourceFile, container *shimast.Node) bool { |
| 132 | + for _, statement := range parentStatements(container) { |
| 133 | + if statement == nil || statement.Kind != shimast.KindExpressionStatement { |
| 134 | + return false |
| 135 | + } |
| 136 | + expressionStatement := statement.AsExpressionStatement() |
| 137 | + if expressionStatement == nil || expressionStatement.Expression == nil || |
| 138 | + expressionStatement.Expression.Kind != shimast.KindStringLiteral { |
| 139 | + return false |
| 140 | + } |
| 141 | + // ES5 forbids escapes in a Use Strict Directive. Match the parser/binder |
| 142 | + // contract against the parser-classified literal token, not its cooked |
| 143 | + // value (`"use\x20strict"` must stay sloppy). |
| 144 | + raw := nodeText(file, expressionStatement.Expression) |
| 145 | + if raw == `"use strict"` || raw == `'use strict'` { |
| 146 | + return true |
| 147 | + } |
| 148 | + } |
| 149 | + return false |
| 150 | +} |
| 151 | + |
| 152 | +func noInnerDeclarationsIsRoot(declaration *shimast.Node) bool { |
| 153 | + if declaration == nil || declaration.Parent == nil { |
| 154 | + return true |
| 155 | + } |
| 156 | + parent := declaration.Parent |
| 157 | + switch parent.Kind { |
| 158 | + case shimast.KindSourceFile, shimast.KindModuleBlock, shimast.KindClassStaticBlockDeclaration: |
| 159 | + return true |
| 160 | + case shimast.KindBlock: |
| 161 | + return isFunctionLikeKind(parent.Parent) || |
| 162 | + (parent.Parent != nil && parent.Parent.Kind == shimast.KindClassStaticBlockDeclaration) |
| 163 | + } |
| 164 | + return false |
| 165 | +} |
| 166 | + |
| 167 | +func noInnerDeclarationsAllowedBody(declaration *shimast.Node) string { |
| 168 | + for ancestor := declaration.Parent; ancestor != nil; ancestor = ancestor.Parent { |
| 169 | + if ancestor.Kind == shimast.KindClassStaticBlockDeclaration { |
| 170 | + return "class static block body" |
| 171 | + } |
| 172 | + if isFunctionLikeKind(ancestor) { |
| 173 | + return "function body" |
| 174 | + } |
| 175 | + } |
| 176 | + return "program" |
| 177 | +} |
| 178 | + |
| 179 | +func init() { |
| 180 | + Register(noInnerDeclarations{}) |
| 181 | +} |
0 commit comments