|
| 1 | +import type { RegExpVisitor } from "regexpp/visitor" |
| 2 | +import { isOpeningBracketToken } from "eslint-utils" |
| 3 | +import type { RegExpContext } from "../utils" |
| 4 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 5 | +import { |
| 6 | + getTypeScriptTools, |
| 7 | + isAny, |
| 8 | + isClassOrInterface, |
| 9 | +} from "../utils/ts-utils" |
| 10 | +import type { Expression, Super } from "estree" |
| 11 | + |
| 12 | +export default createRule("prefer-result-array-groups", { |
| 13 | + meta: { |
| 14 | + docs: { |
| 15 | + description: "enforce using result array `groups`", |
| 16 | + category: "Stylistic Issues", |
| 17 | + recommended: false, |
| 18 | + }, |
| 19 | + fixable: "code", |
| 20 | + schema: [ |
| 21 | + { |
| 22 | + type: "object", |
| 23 | + properties: { |
| 24 | + strictTypes: { type: "boolean" }, |
| 25 | + }, |
| 26 | + additionalProperties: false, |
| 27 | + }, |
| 28 | + ], |
| 29 | + messages: { |
| 30 | + unexpected: |
| 31 | + "Unexpected indexed access for the named capturing group '{{ name }}' from regexp result array.", |
| 32 | + }, |
| 33 | + type: "suggestion", |
| 34 | + }, |
| 35 | + create(context) { |
| 36 | + const strictTypes = context.options[0]?.strictTypes ?? true |
| 37 | + const sourceCode = context.getSourceCode() |
| 38 | + |
| 39 | + /** |
| 40 | + * Create visitor |
| 41 | + */ |
| 42 | + function createVisitor( |
| 43 | + regexpContext: RegExpContext, |
| 44 | + ): RegExpVisitor.Handlers { |
| 45 | + const { |
| 46 | + getAllCapturingGroups, |
| 47 | + getCapturingGroupReferences, |
| 48 | + } = regexpContext |
| 49 | + |
| 50 | + const capturingGroups = getAllCapturingGroups() |
| 51 | + if (!capturingGroups.length) { |
| 52 | + return {} |
| 53 | + } |
| 54 | + |
| 55 | + for (const ref of getCapturingGroupReferences({ strictTypes })) { |
| 56 | + if ( |
| 57 | + ref.type === "ArrayRef" && |
| 58 | + ref.kind === "index" && |
| 59 | + ref.ref != null |
| 60 | + ) { |
| 61 | + const cgNode = capturingGroups[ref.ref - 1] |
| 62 | + if (cgNode && cgNode.name) { |
| 63 | + const memberNode = |
| 64 | + ref.prop.type === "member" ? ref.prop.node : null |
| 65 | + context.report({ |
| 66 | + node: ref.prop.node, |
| 67 | + messageId: "unexpected", |
| 68 | + data: { |
| 69 | + name: cgNode.name, |
| 70 | + }, |
| 71 | + fix: |
| 72 | + memberNode && memberNode.computed |
| 73 | + ? (fixer) => { |
| 74 | + const tokens = sourceCode.getTokensBetween( |
| 75 | + memberNode.object, |
| 76 | + memberNode.property, |
| 77 | + ) |
| 78 | + let openingBracket = tokens.pop() |
| 79 | + while ( |
| 80 | + openingBracket && |
| 81 | + !isOpeningBracketToken( |
| 82 | + openingBracket, |
| 83 | + ) |
| 84 | + ) { |
| 85 | + openingBracket = tokens.pop() |
| 86 | + } |
| 87 | + if (!openingBracket) { |
| 88 | + // unknown ast |
| 89 | + return null |
| 90 | + } |
| 91 | + |
| 92 | + const kind = getRegExpArrayTypeKind( |
| 93 | + memberNode.object, |
| 94 | + ) |
| 95 | + if (kind === "unknown") { |
| 96 | + // Using TypeScript but I can't identify the type or it's not a RegExpXArray type. |
| 97 | + return null |
| 98 | + } |
| 99 | + const needNonNull = |
| 100 | + kind === "RegExpXArray" |
| 101 | + |
| 102 | + return fixer.replaceTextRange( |
| 103 | + [ |
| 104 | + openingBracket.range![0], |
| 105 | + memberNode.range![1], |
| 106 | + ], |
| 107 | + `${ |
| 108 | + memberNode.optional ? "" : "." |
| 109 | + }groups${ |
| 110 | + needNonNull ? "!" : "" |
| 111 | + }.${cgNode.name}`, |
| 112 | + ) |
| 113 | + } |
| 114 | + : null, |
| 115 | + }) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return {} |
| 121 | + } |
| 122 | + |
| 123 | + return defineRegexpVisitor(context, { |
| 124 | + createVisitor, |
| 125 | + }) |
| 126 | + |
| 127 | + type RegExpArrayTypeKind = |
| 128 | + | "RegExpXArray" // RegExpMatchArray or RegExpExecArray |
| 129 | + | "any" |
| 130 | + | "unknown" // It's cannot autofix |
| 131 | + |
| 132 | + /** Gets the type kind of the given node. */ |
| 133 | + function getRegExpArrayTypeKind( |
| 134 | + node: Expression | Super, |
| 135 | + ): RegExpArrayTypeKind | null { |
| 136 | + const { |
| 137 | + tsNodeMap, |
| 138 | + checker, |
| 139 | + usedTS, |
| 140 | + hasFullTypeInformation, |
| 141 | + } = getTypeScriptTools(context) |
| 142 | + if (!usedTS) { |
| 143 | + // Not using TypeScript. |
| 144 | + return null |
| 145 | + } |
| 146 | + if (!hasFullTypeInformation) { |
| 147 | + // The user has not given the type information to ESLint. So we don't know if this can be autofix. |
| 148 | + return "unknown" |
| 149 | + } |
| 150 | + const tsNode = tsNodeMap.get(node) |
| 151 | + const tsType = (tsNode && checker.getTypeAtLocation(tsNode)) || null |
| 152 | + if (!tsType) { |
| 153 | + // The node type cannot be determined. |
| 154 | + return "unknown" |
| 155 | + } |
| 156 | + |
| 157 | + if (isClassOrInterface(tsType)) { |
| 158 | + const name = tsType.symbol.escapedName |
| 159 | + return name === "RegExpMatchArray" || name === "RegExpExecArray" |
| 160 | + ? "RegExpXArray" |
| 161 | + : "unknown" |
| 162 | + } |
| 163 | + if (isAny(tsType)) { |
| 164 | + return "any" |
| 165 | + } |
| 166 | + return "unknown" |
| 167 | + } |
| 168 | + }, |
| 169 | +}) |
0 commit comments