-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
regexp/prefer-result-array-groups
rule (#351)
- Loading branch information
Showing
12 changed files
with
845 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "regexp/prefer-result-array-groups" | ||
description: "enforce using result array `groups`" | ||
--- | ||
# regexp/prefer-result-array-groups | ||
|
||
> enforce using result array `groups` | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports and fixes regexp result arrays where named capturing groups are accessed by index instead of using [`groups`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges#using_named_groups). | ||
|
||
<eslint-code-block fix> | ||
|
||
```js | ||
/* eslint regexp/prefer-result-array-groups: "error" */ | ||
|
||
const regex = /(?<foo>a)(b)c/g | ||
let match | ||
while (match = regex.exec(str)) { | ||
/* ✓ GOOD */ | ||
var p1 = match.groups.foo | ||
var p2 = match[2] | ||
|
||
/* ✗ BAD */ | ||
var p1 = match[1] | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"regexp/prefer-result-array-groups": ["error", { | ||
"strictTypes": true | ||
}] | ||
} | ||
``` | ||
|
||
- `strictTypes` ... If `true`, strictly check the type of object to determine if the string instance was used in `match()` and `matchAll()`. Default is `true`. | ||
This option is always on when using TypeScript. | ||
|
||
## :couple: Related rules | ||
|
||
- [regexp/prefer-named-backreference] | ||
- [regexp/prefer-named-capture-group] | ||
- [regexp/prefer-named-replacement] | ||
|
||
[regexp/prefer-named-backreference]: ./prefer-named-backreference.md | ||
[regexp/prefer-named-capture-group]: ./prefer-named-capture-group.md | ||
[regexp/prefer-named-replacement]: ./prefer-named-replacement.md | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/prefer-result-array-groups.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/prefer-result-array-groups.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import type { RegExpVisitor } from "regexpp/visitor" | ||
import { isOpeningBracketToken } from "eslint-utils" | ||
import type { RegExpContext } from "../utils" | ||
import { createRule, defineRegexpVisitor } from "../utils" | ||
import { | ||
getTypeScriptTools, | ||
isAny, | ||
isClassOrInterface, | ||
} from "../utils/ts-utils" | ||
import type { Expression, Super } from "estree" | ||
|
||
export default createRule("prefer-result-array-groups", { | ||
meta: { | ||
docs: { | ||
description: "enforce using result array `groups`", | ||
category: "Stylistic Issues", | ||
recommended: false, | ||
}, | ||
fixable: "code", | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
strictTypes: { type: "boolean" }, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
unexpected: | ||
"Unexpected indexed access for the named capturing group '{{ name }}' from regexp result array.", | ||
}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
const strictTypes = context.options[0]?.strictTypes ?? true | ||
const sourceCode = context.getSourceCode() | ||
|
||
/** | ||
* Create visitor | ||
*/ | ||
function createVisitor( | ||
regexpContext: RegExpContext, | ||
): RegExpVisitor.Handlers { | ||
const { | ||
getAllCapturingGroups, | ||
getCapturingGroupReferences, | ||
} = regexpContext | ||
|
||
const capturingGroups = getAllCapturingGroups() | ||
if (!capturingGroups.length) { | ||
return {} | ||
} | ||
|
||
for (const ref of getCapturingGroupReferences({ strictTypes })) { | ||
if ( | ||
ref.type === "ArrayRef" && | ||
ref.kind === "index" && | ||
ref.ref != null | ||
) { | ||
const cgNode = capturingGroups[ref.ref - 1] | ||
if (cgNode && cgNode.name) { | ||
const memberNode = | ||
ref.prop.type === "member" ? ref.prop.node : null | ||
context.report({ | ||
node: ref.prop.node, | ||
messageId: "unexpected", | ||
data: { | ||
name: cgNode.name, | ||
}, | ||
fix: | ||
memberNode && memberNode.computed | ||
? (fixer) => { | ||
const tokens = sourceCode.getTokensBetween( | ||
memberNode.object, | ||
memberNode.property, | ||
) | ||
let openingBracket = tokens.pop() | ||
while ( | ||
openingBracket && | ||
!isOpeningBracketToken( | ||
openingBracket, | ||
) | ||
) { | ||
openingBracket = tokens.pop() | ||
} | ||
if (!openingBracket) { | ||
// unknown ast | ||
return null | ||
} | ||
|
||
const kind = getRegExpArrayTypeKind( | ||
memberNode.object, | ||
) | ||
if (kind === "unknown") { | ||
// Using TypeScript but I can't identify the type or it's not a RegExpXArray type. | ||
return null | ||
} | ||
const needNonNull = | ||
kind === "RegExpXArray" | ||
|
||
return fixer.replaceTextRange( | ||
[ | ||
openingBracket.range![0], | ||
memberNode.range![1], | ||
], | ||
`${ | ||
memberNode.optional ? "" : "." | ||
}groups${ | ||
needNonNull ? "!" : "" | ||
}.${cgNode.name}`, | ||
) | ||
} | ||
: null, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return {} | ||
} | ||
|
||
return defineRegexpVisitor(context, { | ||
createVisitor, | ||
}) | ||
|
||
type RegExpArrayTypeKind = | ||
| "RegExpXArray" // RegExpMatchArray or RegExpExecArray | ||
| "any" | ||
| "unknown" // It's cannot autofix | ||
|
||
/** Gets the type kind of the given node. */ | ||
function getRegExpArrayTypeKind( | ||
node: Expression | Super, | ||
): RegExpArrayTypeKind | null { | ||
const { | ||
tsNodeMap, | ||
checker, | ||
usedTS, | ||
hasFullTypeInformation, | ||
} = getTypeScriptTools(context) | ||
if (!usedTS) { | ||
// Not using TypeScript. | ||
return null | ||
} | ||
if (!hasFullTypeInformation) { | ||
// The user has not given the type information to ESLint. So we don't know if this can be autofix. | ||
return "unknown" | ||
} | ||
const tsNode = tsNodeMap.get(node) | ||
const tsType = (tsNode && checker.getTypeAtLocation(tsNode)) || null | ||
if (!tsType) { | ||
// The node type cannot be determined. | ||
return "unknown" | ||
} | ||
|
||
if (isClassOrInterface(tsType)) { | ||
const name = tsType.symbol.escapedName | ||
return name === "RegExpMatchArray" || name === "RegExpExecArray" | ||
? "RegExpXArray" | ||
: "unknown" | ||
} | ||
if (isAny(tsType)) { | ||
return "any" | ||
} | ||
return "unknown" | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.