-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
527 additions
and
9 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
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 index access in regexp result array that do not use the name of their referenced capturing group. | ||
|
||
<eslint-code-block fix> | ||
|
||
```js | ||
/* eslint regexp/prefer-result-array-groups: "error" */ | ||
|
||
const regex = /(?<foo>a)(b)c/ | ||
let re | ||
while (re = regex.exec(str)) { | ||
/* ✓ GOOD */ | ||
var p1 = re.groups.foo | ||
var p2 = re[2] | ||
|
||
/* ✗ BAD */ | ||
var p1 = re[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,104 @@ | ||
import type { RegExpVisitor } from "regexpp/visitor" | ||
import { isOpeningBracketToken } from "eslint-utils" | ||
import type { RegExpContext } from "../utils" | ||
import { createRule, defineRegexpVisitor } from "../utils" | ||
|
||
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 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", | ||
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 | ||
} | ||
return fixer.replaceTextRange( | ||
[ | ||
openingBracket.range![0], | ||
memberNode.range![1], | ||
], | ||
`${ | ||
memberNode.optional ? "" : "." | ||
}groups.${cgNode.name}`, | ||
) | ||
} | ||
: null, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return {} | ||
} | ||
|
||
return defineRegexpVisitor(context, { | ||
createVisitor, | ||
}) | ||
}, | ||
}) |
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
Oops, something went wrong.