-
Notifications
You must be signed in to change notification settings - Fork 11
Add regexp/grapheme-string-literal
rule
#646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9f61288
Add `regexp/grapheme-string-literal` rule
ota-meshi c049eed
Create chatty-walls-juggle.md
ota-meshi 2132512
Merge branch 'master' into grapheme-string-literal
ota-meshi 10b3976
Apply suggestions from code review
ota-meshi 331159a
update
ota-meshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
"eslint-plugin-regexp": minor | ||
--- | ||
|
||
Add `regexp/grapheme-string-literal` rule |
This file contains hidden or 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 hidden or 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,55 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "regexp/grapheme-string-literal" | ||
description: "enforce single grapheme in string literal" | ||
--- | ||
# regexp/grapheme-string-literal | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
> enforce single grapheme in string literal | ||
|
||
## :book: Rule Details | ||
|
||
This rule is aimed to clarify the difference between using a string literal and normal disjunctions by not using string literals for purposes other than expressing a single grapheme. | ||
|
||
<eslint-code-block> | ||
|
||
```js | ||
/* eslint regexp/grapheme-string-literal: "error" */ | ||
|
||
/* ✓ GOOD */ | ||
var foo = /[\p{RGI_Emoji}--\q{🇦🇨|🇦🇩|🇦🇪|🇦🇫|🇦🇬|🇦🇮|🇦🇱|🇦🇲|🇦🇴|🇦🇶|🇦🇷|🇦🇸|🇦🇹|🇦🇺|🇦🇼|🇦🇽|🇦🇿|🇧🇦|🇧🇧|🇧🇩|🇧🇪|🇧🇫|🇧🇬|🇧🇭|🇧🇮|🇧🇯}]/v | ||
var foo = /[\q{a|b|c}]/v | ||
|
||
/* ✗ BAD */ | ||
var foo = /[\q{abc|def}]/v | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
This rule does not report empty string literals. Use [regexp/no-empty-string-literal] and [regexp/no-empty-alternative] if you want to check them. | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :couple: Related rules | ||
|
||
- [regexp/no-empty-string-literal] | ||
- [regexp/no-empty-alternative] | ||
- [regexp/no-useless-string-literal] | ||
|
||
[regexp/no-empty-string-literal]: ./no-empty-string-literal.md | ||
[regexp/no-empty-alternative]: ./no-empty-alternative.md | ||
[regexp/no-useless-string-literal]: ./no-useless-string-literal.md | ||
|
||
## :rocket: Version | ||
|
||
:exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/grapheme-string-literal.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/grapheme-string-literal.ts) |
This file contains hidden or 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 hidden or 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,49 @@ | ||
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor" | ||
import type { RegExpContext } from "../utils" | ||
import { createRule, defineRegexpVisitor } from "../utils" | ||
|
||
const segmenter = new Intl.Segmenter() | ||
|
||
export default createRule("grapheme-string-literal", { | ||
meta: { | ||
docs: { | ||
description: "enforce single grapheme in string literal", | ||
category: "Best Practices", | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
useSingleGrapheme: "Use single grapheme in string literal.", | ||
}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
function createVisitor( | ||
regexpContext: RegExpContext, | ||
): RegExpVisitor.Handlers { | ||
const { node, getRegexpLocation } = regexpContext | ||
|
||
return { | ||
onStringAlternativeEnter(saNode) { | ||
if (saNode.elements.length <= 1) return | ||
const string = String.fromCodePoint( | ||
...saNode.elements.map((element) => element.value), | ||
) | ||
|
||
const segments = [...segmenter.segment(string)] | ||
if (segments.length > 1) { | ||
context.report({ | ||
node, | ||
loc: getRegexpLocation(saNode), | ||
messageId: "useSingleGrapheme", | ||
}) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
return defineRegexpVisitor(context, { | ||
createVisitor, | ||
}) | ||
}, | ||
}) |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether this should be a best practice. The practice this rule enforces is good, because it brings regexes into a canonical form (e.g. don't use
[\q{foo|bar}]
, use(?:foo|bar)
), but that's more of a stylistic thing IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you 👍 I will categorize it under "Stylistic Issues".