|
| 1 | +package no_explicit_any |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/microsoft/typescript-go/shim/ast" |
| 5 | + "github.com/web-infra-dev/rslint/internal/rule" |
| 6 | +) |
| 7 | + |
| 8 | +type NoExplicitAnyOptions struct { |
| 9 | + FixToUnknown bool `json:"fixToUnknown"` |
| 10 | + IgnoreRestArgs bool `json:"ignoreRestArgs"` |
| 11 | +} |
| 12 | + |
| 13 | +func buildUnexpectedAnyMessage() rule.RuleMessage { |
| 14 | + return rule.RuleMessage{ |
| 15 | + Id: "unexpectedAny", |
| 16 | + Description: "Unexpected any. Specify a different type.", |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func buildSuggestUnknownMessage() rule.RuleMessage { |
| 21 | + return rule.RuleMessage{ |
| 22 | + Id: "suggestUnknown", |
| 23 | + Description: "Use `unknown` instead, this will force you to explicitly, and safely assert the type is correct.", |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func buildSuggestNeverMessage() rule.RuleMessage { |
| 28 | + return rule.RuleMessage{ |
| 29 | + Id: "suggestNever", |
| 30 | + Description: "Use `never` instead, this is useful when instantiating generic type parameters that you don't need to know the type of.", |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func buildSuggestPropertyKeyMessage() rule.RuleMessage { |
| 35 | + return rule.RuleMessage{ |
| 36 | + Id: "suggestPropertyKey", |
| 37 | + Description: "Use `PropertyKey` instead, this is more explicit than `keyof any`.", |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func parseOptions(options any) NoExplicitAnyOptions { |
| 42 | + opts := NoExplicitAnyOptions{} |
| 43 | + if options == nil { |
| 44 | + return opts |
| 45 | + } |
| 46 | + // Handle array format: [{ option: value }] |
| 47 | + if arr, ok := options.([]interface{}); ok { |
| 48 | + if len(arr) > 0 { |
| 49 | + if m, ok := arr[0].(map[string]interface{}); ok { |
| 50 | + if v, ok := m["fixToUnknown"].(bool); ok { |
| 51 | + opts.FixToUnknown = v |
| 52 | + } |
| 53 | + if v, ok := m["ignoreRestArgs"].(bool); ok { |
| 54 | + opts.IgnoreRestArgs = v |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return opts |
| 59 | + } |
| 60 | + // Handle direct object format |
| 61 | + if m, ok := options.(map[string]interface{}); ok { |
| 62 | + if v, ok := m["fixToUnknown"].(bool); ok { |
| 63 | + opts.FixToUnknown = v |
| 64 | + } |
| 65 | + if v, ok := m["ignoreRestArgs"].(bool); ok { |
| 66 | + opts.IgnoreRestArgs = v |
| 67 | + } |
| 68 | + } |
| 69 | + return opts |
| 70 | +} |
| 71 | + |
| 72 | +func isAnyInRestParameter(node *ast.Node) bool { |
| 73 | + // Check if the any keyword is inside a rest parameter with array type |
| 74 | + // We need to check if the any is part of an array type in a rest parameter |
| 75 | + // Valid patterns to ignore: ...args: any[], ...args: readonly any[], ...args: Array<any>, ...args: ReadonlyArray<any> |
| 76 | + |
| 77 | + // First check if we're inside an ArrayType |
| 78 | + inArrayType := false |
| 79 | + for p := node.Parent; p != nil; p = p.Parent { |
| 80 | + if p.Kind == ast.KindArrayType { |
| 81 | + inArrayType = true |
| 82 | + break |
| 83 | + } |
| 84 | + if p.Kind == ast.KindTypeReference { |
| 85 | + typeRef := p.AsTypeReference() |
| 86 | + if typeRef != nil && ast.IsIdentifier(typeRef.TypeName) { |
| 87 | + identifier := typeRef.TypeName.AsIdentifier() |
| 88 | + if identifier != nil && (identifier.Text == "Array" || identifier.Text == "ReadonlyArray") { |
| 89 | + inArrayType = true |
| 90 | + break |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if !inArrayType { |
| 97 | + return false |
| 98 | + } |
| 99 | + |
| 100 | + // Then check if we're in a rest parameter |
| 101 | + for p := node.Parent; p != nil; p = p.Parent { |
| 102 | + if p.Kind == ast.KindParameter { |
| 103 | + param := p.AsParameterDeclaration() |
| 104 | + return param.DotDotDotToken != nil |
| 105 | + } |
| 106 | + } |
| 107 | + return false |
| 108 | +} |
| 109 | + |
| 110 | +func isWithinKeyofAny(node *ast.Node) bool { |
| 111 | + if node.Parent == nil || node.Parent.Kind != ast.KindTypeOperator { |
| 112 | + return false |
| 113 | + } |
| 114 | + typeOp := node.Parent.AsTypeOperatorNode() |
| 115 | + return typeOp != nil && typeOp.Operator == ast.KindKeyOfKeyword |
| 116 | +} |
| 117 | + |
| 118 | +var NoExplicitAnyRule = rule.CreateRule(rule.Rule{ |
| 119 | + Name: "no-explicit-any", |
| 120 | + Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { |
| 121 | + opts := parseOptions(options) |
| 122 | + |
| 123 | + return rule.RuleListeners{ |
| 124 | + ast.KindAnyKeyword: func(node *ast.Node) { |
| 125 | + if opts.IgnoreRestArgs && isAnyInRestParameter(node) { |
| 126 | + return |
| 127 | + } |
| 128 | + if isWithinKeyofAny(node) { |
| 129 | + if opts.FixToUnknown { |
| 130 | + ctx.ReportNodeWithFixes(node, buildUnexpectedAnyMessage(), rule.RuleFixReplace(ctx.SourceFile, node.Parent, "PropertyKey")) |
| 131 | + } else { |
| 132 | + ctx.ReportNodeWithSuggestions(node, buildUnexpectedAnyMessage(), rule.RuleSuggestion{ |
| 133 | + Message: buildSuggestPropertyKeyMessage(), |
| 134 | + FixesArr: []rule.RuleFix{rule.RuleFixReplace(ctx.SourceFile, node.Parent, "PropertyKey")}, |
| 135 | + }) |
| 136 | + } |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + if opts.FixToUnknown { |
| 141 | + ctx.ReportNodeWithFixes(node, buildUnexpectedAnyMessage(), rule.RuleFixReplace(ctx.SourceFile, node, "unknown")) |
| 142 | + } else { |
| 143 | + ctx.ReportNodeWithSuggestions(node, buildUnexpectedAnyMessage(), |
| 144 | + rule.RuleSuggestion{ |
| 145 | + Message: buildSuggestUnknownMessage(), |
| 146 | + FixesArr: []rule.RuleFix{rule.RuleFixReplace(ctx.SourceFile, node, "unknown")}, |
| 147 | + }, |
| 148 | + rule.RuleSuggestion{ |
| 149 | + Message: buildSuggestNeverMessage(), |
| 150 | + FixesArr: []rule.RuleFix{rule.RuleFixReplace(ctx.SourceFile, node, "never")}, |
| 151 | + }, |
| 152 | + ) |
| 153 | + } |
| 154 | + }, |
| 155 | + } |
| 156 | + }, |
| 157 | +}) |
0 commit comments