Skip to content
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

fix(eslint-plugin): [naming-convention] support unicode in regex #2241

Merged
merged 1 commit into from Jun 24, 2020

Conversation

octogonz
Copy link
Contributor

@octogonz octogonz commented Jun 24, 2020

Add support for unicode regular expressions.

The rule docs present the example below, saying "You can use the filter option to ignore names that require quoting":

{
  "@typescript-eslint/naming-convention": [
    "error",
    {
      "selector": "property",
      "format": ["strictCamelCase"],
      "filter": {
        // you can expand this regex as you find more cases that require quoting that you want to allow
        "regex": "[- ]",
        "match": false
      }
    }
  ]
}

(Aside: Ideally there should be a selector for detecting quoted names, since quotes can be a useful shorthand for bypassing lint rules. But let's ignore that for now. Instead we will follow the above example's premise, of substituting "must this name be quoted?" as an approximation of "was this name quoted?")

The example criteria of "does it have a minus or space?" is not an accurate test. Instead, it should be more like "Does it contain characters other than letters, numbers, and underscores?" Something like this:

        "regex": "[^a-zA-Z0-9_]",

But the Spanish identifier _animación will fail this test. To support languages besides English, the expression needs to be generalized to something like this:

        "regex": "[^\\p{L}\\p{N}_]",

Unfortunately we can't do this today with @typescript-eslint/naming-convention, because its RegExp doesn't accept Unicode character classes.

This PR fixes that.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @octogonz!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@codecov
Copy link

codecov bot commented Jun 24, 2020

Codecov Report

Merging #2241 into master will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2241   +/-   ##
=======================================
  Coverage   93.44%   93.44%           
=======================================
  Files         174      174           
  Lines        7954     7954           
  Branches     2282     2282           
=======================================
  Hits         7433     7433           
  Misses        246      246           
  Partials      275      275           
Flag Coverage Δ
#unittest 93.44% <100.00%> (ø)
Impacted Files Coverage Δ
...kages/eslint-plugin/src/rules/naming-convention.ts 88.82% <100.00%> (ø)

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://mathiasbynens.be/notes/es6-unicode-regex#recommendations

Use the u flag for every regular expression you write from now on.

Huh, TIL. I never do anything with unicode, so I haven't ever looked into the flag or what it does.

LGTM - thanks

@bradzacher bradzacher added the bug Something isn't working label Jun 24, 2020
@bradzacher bradzacher changed the title fix(eslint-plugin): [naming-convention] support unicode fix(eslint-plugin): [naming-convention] support unicode in regex Jun 24, 2020
@bradzacher bradzacher merged commit 5fdd21a into typescript-eslint:master Jun 24, 2020
@glen-84
Copy link
Contributor

glen-84 commented Jun 29, 2020

Just as an FYI, there's actually an ESLint rule for this.

@octogonz octogonz deleted the octogonz/unicode branch July 3, 2020 06:27
@octogonz
Copy link
Contributor Author

octogonz commented Jul 3, 2020

Just as an FYI, there's actually an ESLint rule for this.

I did not know that!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants