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

[naming-convention] Allow multiple underscore #1712

Closed
jhnns opened this issue Mar 9, 2020 · 7 comments · Fixed by #2812
Closed

[naming-convention] Allow multiple underscore #1712

jhnns opened this issue Mar 9, 2020 · 7 comments · Fixed by #2812
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@jhnns
Copy link
Contributor

jhnns commented Mar 9, 2020

Repro

Hey ho 👋, this is a feature request. What do you think of allowing multiple leading or trailing underscores? There are some libraries that use double underscore (__) to avoid name clashes. Probably the most popular one is GraphQL where you can specify a __typename (see here).

Personally, I want to use camelCase or PascalCase and I'm ok with people pre- or postfixing names with underscores. We could introduce a 4th value for the leadingUnderscore and trailingUnderscore option that is called allowMultiple, like this:

{
  "rules": {
    "@typescript-eslint/naming-convention": ["warn", {
        "format": ["camelCase", "PascalCase", "UPPER_CASE"],
        "leadingUnderscore": "allowMultiple",
        "selector": "default",
        "trailingUnderscore": "allowMultiple"
    }]
  }
}
@jhnns jhnns added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Mar 9, 2020
@bradzacher
Copy link
Member

bradzacher commented Mar 9, 2020

It's a pattern people use for protected members as well.
Happy to accept a PR to add the option!

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed triage Waiting for maintainers to take a look labels Mar 9, 2020
@felixfbecker
Copy link

Also React's dangerouslySetInnerHTML={{ __html: "" }}

@ScottAwesome
Copy link

ScottAwesome commented Jul 13, 2020

I want to comment here as well, as I would love to see this. I tried doing a custom regex filter, so if anything started with double leading underscores, it wouldn't error:

const propertySetting = {
                selector: 'property',
                format: ['camelCase', 'PascalCase', 'UPPER_CASE'],
                filter: {
                    regex: '^(__|_)([0-9A-Za-z]+)$',
                    match: false,
                },
            };

No such luck, giving me errors.

If anyone has a secret sauce here that does work, please share!

Also, another option to think about is allowing optionalPrefixes instead? if I do prefix with a blank string, eslint throws.

@joh-klein
Copy link

@ScottAwesome I got your filter to work, when the ONLY rule was the one you posted (propertySetting). When there are other rules that fit (in my case I had another one with selector: 'default'), it will apply those rules again – and possibly fail.

@BorisZubchenko
Copy link

For those looking for a __typename solution:

{
    "selector": "property",
    "filter": "__typename",
    "format": null
 }

Good:

console.log({
  __typename: 'Type'
});

bradzacher added a commit that referenced this issue Nov 24, 2020
…ouble`, `allowSingleOrDouble` options for underscores

Fixes #1712

I chose not to add an unbounded `allowMultiple` for now. I don't think that it's a commonly used pattern.
Single and double I've seen, but I don't believe I've ever seen more than that.
bradzacher added a commit that referenced this issue Nov 24, 2020
…ouble`, `allowSingleOrDouble` options for underscores

Fixes #1712

I chose not to add an unbounded `allowMultiple` for now. I don't think that it's a commonly used pattern.
Single and double I've seen, but I don't believe I've ever seen more than that.
@ScottAwesome
Copy link

For those looking for a __typename solution:

{
    "selector": "property",
    "filter": "__typename",
    "format": null
 }

Good:

console.log({
  __typename: 'Type'
});

This doesn’t solve for the fact I only want certain formats otherwise though, does it? See my previous example where it won’t work if other rules are being applied

@ab-pm
Copy link

ab-pm commented Nov 30, 2020

What I found to be working is a prefix:

{
	selector: 'property',
	prefix: ['__'],
	format: ['camelCase']
},

However, for some reason (not sure if it's a bug) I had to restrict the normal property rule to not match the underscored properties, as it would complain otherwise:

{
	selector: 'property',
	filter: '^(?!__)',
	format: []
},

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
7 participants