|
| 1 | +package no_obj_calls |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/microsoft/typescript-go/shim/ast" |
| 7 | + "github.com/web-infra-dev/rslint/internal/rule" |
| 8 | +) |
| 9 | + |
| 10 | +var nonCallableGlobals = map[string]bool{ |
| 11 | + "Math": true, "JSON": true, "Reflect": true, "Atomics": true, "Intl": true, |
| 12 | +} |
| 13 | + |
| 14 | +// https://eslint.org/docs/latest/rules/no-obj-calls |
| 15 | +var NoObjCallsRule = rule.Rule{ |
| 16 | + Name: "no-obj-calls", |
| 17 | + Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { |
| 18 | + checkCallee := func(node *ast.Node, calleeNode *ast.Node) { |
| 19 | + if calleeNode.Kind == ast.KindIdentifier { |
| 20 | + name := calleeNode.AsIdentifier().Text |
| 21 | + if nonCallableGlobals[name] && !isShadowed(calleeNode, name) { |
| 22 | + ctx.ReportNode(node, rule.RuleMessage{ |
| 23 | + Id: "unexpectedCall", |
| 24 | + Description: fmt.Sprintf("'%s' is not a function.", name), |
| 25 | + }) |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return rule.RuleListeners{ |
| 31 | + ast.KindCallExpression: func(node *ast.Node) { |
| 32 | + callExpr := node.AsCallExpression() |
| 33 | + checkCallee(node, callExpr.Expression) |
| 34 | + }, |
| 35 | + ast.KindNewExpression: func(node *ast.Node) { |
| 36 | + newExpr := node.AsNewExpression() |
| 37 | + checkCallee(node, newExpr.Expression) |
| 38 | + }, |
| 39 | + } |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +// isShadowed checks if an identifier is shadowed by a local variable, parameter, |
| 44 | +// or function declaration in an enclosing scope (not global). |
| 45 | +func isShadowed(node *ast.Node, name string) bool { |
| 46 | + current := node.Parent |
| 47 | + for current != nil { |
| 48 | + switch current.Kind { |
| 49 | + case ast.KindBlock, ast.KindCaseClause, ast.KindDefaultClause: |
| 50 | + if blockDeclaresName(current, name) { |
| 51 | + return true |
| 52 | + } |
| 53 | + case ast.KindFunctionDeclaration, ast.KindFunctionExpression, |
| 54 | + ast.KindArrowFunction, ast.KindMethodDeclaration: |
| 55 | + if functionParamDeclaresName(current, name) { |
| 56 | + return true |
| 57 | + } |
| 58 | + case ast.KindVariableStatement: |
| 59 | + if varStatementDeclaresName(current, name) { |
| 60 | + return true |
| 61 | + } |
| 62 | + case ast.KindSourceFile: |
| 63 | + // Reached the top-level scope — not shadowed |
| 64 | + return false |
| 65 | + } |
| 66 | + current = current.Parent |
| 67 | + } |
| 68 | + return false |
| 69 | +} |
| 70 | + |
| 71 | +// blockDeclaresName checks if a block contains a variable/function declaration with the given name. |
| 72 | +func blockDeclaresName(block *ast.Node, name string) bool { |
| 73 | + var statements *ast.NodeList |
| 74 | + switch block.Kind { |
| 75 | + case ast.KindBlock: |
| 76 | + b := block.AsBlock() |
| 77 | + if b != nil { |
| 78 | + statements = b.Statements |
| 79 | + } |
| 80 | + case ast.KindCaseClause, ast.KindDefaultClause: |
| 81 | + c := block.AsCaseOrDefaultClause() |
| 82 | + if c != nil { |
| 83 | + statements = c.Statements |
| 84 | + } |
| 85 | + } |
| 86 | + if statements == nil { |
| 87 | + return false |
| 88 | + } |
| 89 | + for _, stmt := range statements.Nodes { |
| 90 | + switch stmt.Kind { |
| 91 | + case ast.KindVariableStatement: |
| 92 | + if varStatementDeclaresName(stmt, name) { |
| 93 | + return true |
| 94 | + } |
| 95 | + case ast.KindFunctionDeclaration: |
| 96 | + fd := stmt.AsFunctionDeclaration() |
| 97 | + if fd != nil && fd.Name() != nil && fd.Name().Text() == name { |
| 98 | + return true |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return false |
| 103 | +} |
| 104 | + |
| 105 | +// varStatementDeclaresName checks if a variable statement declares the given name. |
| 106 | +func varStatementDeclaresName(node *ast.Node, name string) bool { |
| 107 | + vs := node.AsVariableStatement() |
| 108 | + if vs == nil || vs.DeclarationList == nil { |
| 109 | + return false |
| 110 | + } |
| 111 | + dl := vs.DeclarationList.AsVariableDeclarationList() |
| 112 | + if dl == nil || dl.Declarations == nil { |
| 113 | + return false |
| 114 | + } |
| 115 | + for _, decl := range dl.Declarations.Nodes { |
| 116 | + vd := decl.AsVariableDeclaration() |
| 117 | + if vd != nil && vd.Name() != nil && vd.Name().Kind == ast.KindIdentifier && vd.Name().Text() == name { |
| 118 | + return true |
| 119 | + } |
| 120 | + } |
| 121 | + return false |
| 122 | +} |
| 123 | + |
| 124 | +// functionParamDeclaresName checks if a function's parameters include the given name. |
| 125 | +func functionParamDeclaresName(node *ast.Node, name string) bool { |
| 126 | + var params *ast.NodeList |
| 127 | + switch node.Kind { |
| 128 | + case ast.KindFunctionDeclaration: |
| 129 | + fd := node.AsFunctionDeclaration() |
| 130 | + if fd != nil { |
| 131 | + params = fd.Parameters |
| 132 | + // Also check function name itself |
| 133 | + if fd.Name() != nil && fd.Name().Text() == name { |
| 134 | + return true |
| 135 | + } |
| 136 | + } |
| 137 | + case ast.KindFunctionExpression: |
| 138 | + fe := node.AsFunctionExpression() |
| 139 | + if fe != nil { |
| 140 | + params = fe.Parameters |
| 141 | + } |
| 142 | + case ast.KindArrowFunction: |
| 143 | + af := node.AsArrowFunction() |
| 144 | + if af != nil { |
| 145 | + params = af.Parameters |
| 146 | + } |
| 147 | + case ast.KindMethodDeclaration: |
| 148 | + md := node.AsMethodDeclaration() |
| 149 | + if md != nil { |
| 150 | + params = md.Parameters |
| 151 | + } |
| 152 | + } |
| 153 | + if params == nil { |
| 154 | + return false |
| 155 | + } |
| 156 | + for _, p := range params.Nodes { |
| 157 | + pd := p.AsParameterDeclaration() |
| 158 | + if pd != nil && pd.Name() != nil && pd.Name().Kind == ast.KindIdentifier && pd.Name().Text() == name { |
| 159 | + return true |
| 160 | + } |
| 161 | + } |
| 162 | + return false |
| 163 | +} |
0 commit comments