|
| 1 | +import type { RegExpContext, UnparsableRegExpContext } from "../utils" |
| 2 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 3 | +import type { ExpressionReference } from "../utils/ast-utils" |
| 4 | +import { |
| 5 | + extractExpressionReferences, |
| 6 | + isKnownMethodCall, |
| 7 | +} from "../utils/ast-utils" |
| 8 | +import { createTypeTracker } from "../utils/type-tracker" |
| 9 | + |
| 10 | +/** |
| 11 | + * Parse option |
| 12 | + */ |
| 13 | +function parseOption( |
| 14 | + userOption: |
| 15 | + | { |
| 16 | + strictTypes?: boolean |
| 17 | + } |
| 18 | + | undefined, |
| 19 | +) { |
| 20 | + let strictTypes = true |
| 21 | + if (userOption) { |
| 22 | + if (userOption.strictTypes != null) { |
| 23 | + strictTypes = userOption.strictTypes |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return { |
| 28 | + strictTypes, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export default createRule("no-missing-g-flag", { |
| 33 | + meta: { |
| 34 | + docs: { |
| 35 | + description: |
| 36 | + "disallow missing `g` flag in patterns used in `String#matchAll` and `String#replaceAll`", |
| 37 | + category: "Possible Errors", |
| 38 | + // TODO Switch to recommended in the major version. |
| 39 | + // recommended: true, |
| 40 | + recommended: false, |
| 41 | + }, |
| 42 | + fixable: "code", |
| 43 | + schema: [ |
| 44 | + { |
| 45 | + type: "object", |
| 46 | + properties: { |
| 47 | + strictTypes: { type: "boolean" }, |
| 48 | + }, |
| 49 | + additionalProperties: false, |
| 50 | + }, |
| 51 | + ], |
| 52 | + messages: { |
| 53 | + missingGlobalFlag: |
| 54 | + "The pattern given to the argument of `String#{{method}}()` requires the `g` flag, but is missing it.", |
| 55 | + }, |
| 56 | + type: "problem", |
| 57 | + }, |
| 58 | + create(context) { |
| 59 | + const { strictTypes } = parseOption(context.options[0]) |
| 60 | + const typeTracer = createTypeTracker(context) |
| 61 | + |
| 62 | + /** The logic of this rule */ |
| 63 | + function visit(regexpContext: RegExpContext | UnparsableRegExpContext) { |
| 64 | + const { regexpNode, flags, flagsString } = regexpContext |
| 65 | + |
| 66 | + if ( |
| 67 | + flags.global || |
| 68 | + // We were unable to determine which flags were used. |
| 69 | + flagsString == null |
| 70 | + ) { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + for (const ref of extractExpressionReferences( |
| 75 | + regexpNode, |
| 76 | + context, |
| 77 | + )) { |
| 78 | + verifyExpressionReference(ref, regexpContext) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Verify RegExp reference |
| 84 | + */ |
| 85 | + function verifyExpressionReference( |
| 86 | + ref: ExpressionReference, |
| 87 | + { |
| 88 | + regexpNode, |
| 89 | + fixReplaceFlags, |
| 90 | + flagsString, |
| 91 | + }: RegExpContext | UnparsableRegExpContext, |
| 92 | + ): void { |
| 93 | + if (ref.type !== "argument") { |
| 94 | + // It is not used for function call arguments. |
| 95 | + return |
| 96 | + } |
| 97 | + const node = ref.callExpression |
| 98 | + if ( |
| 99 | + // It is not specified in the first argument. |
| 100 | + node.arguments[0] !== ref.node || |
| 101 | + // It is not replaceAll() and matchAll(). |
| 102 | + !isKnownMethodCall(node, { |
| 103 | + matchAll: 1, |
| 104 | + replaceAll: 2, |
| 105 | + }) |
| 106 | + ) { |
| 107 | + return |
| 108 | + } |
| 109 | + if ( |
| 110 | + strictTypes |
| 111 | + ? !typeTracer.isString(node.callee.object) |
| 112 | + : !typeTracer.maybeString(node.callee.object) |
| 113 | + ) { |
| 114 | + // The callee object is not a string. |
| 115 | + return |
| 116 | + } |
| 117 | + context.report({ |
| 118 | + node: ref.node, |
| 119 | + messageId: "missingGlobalFlag", |
| 120 | + data: { |
| 121 | + method: node.callee.property.name, |
| 122 | + }, |
| 123 | + fix: buildFixer(), |
| 124 | + }) |
| 125 | + |
| 126 | + /** Build fixer */ |
| 127 | + function buildFixer() { |
| 128 | + if ( |
| 129 | + node.arguments[0] !== regexpNode || |
| 130 | + ((regexpNode.type === "NewExpression" || |
| 131 | + regexpNode.type === "CallExpression") && |
| 132 | + regexpNode.arguments[1] && |
| 133 | + regexpNode.arguments[1].type !== "Literal") |
| 134 | + ) { |
| 135 | + // It can only be safely auto-fixed if it is defined directly in the argument. |
| 136 | + return null |
| 137 | + } |
| 138 | + return fixReplaceFlags(`${flagsString!}g`, false) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return defineRegexpVisitor(context, { |
| 143 | + createVisitor(regexpContext) { |
| 144 | + visit(regexpContext) |
| 145 | + return {} |
| 146 | + }, |
| 147 | + visitInvalid: visit, |
| 148 | + visitUnknown: visit, |
| 149 | + }) |
| 150 | + }, |
| 151 | +}) |
0 commit comments