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] add a modifier to identify a variable/parameter as having been destructured #2512

Closed
3 tasks done
Timmmm opened this issue Sep 7, 2020 · 6 comments · Fixed by #2808
Closed
3 tasks done
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

@Timmmm
Copy link

Timmmm commented Sep 7, 2020

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/naming-convention": "error"
  }
}
import electron from "electron";
const { BrowserWindow } = electron;

Expected Result

It shouldn't complain about the name of BrowserWindow, because I didn't have any choice in the matter.

Actual Result

It does complain.

Additional Info

I feel like there should be an option to don't check destructured names at all.

Versions

All latest.

@Timmmm Timmmm added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Sep 7, 2020
@bradzacher
Copy link
Member

because I didn't have any choice in the matter

You could just as easily write:

const browserWindow = electron.BrowserWindow;
// or
const { BrowserWindow: browserWindow } = electron;

You have a choice of the name, it's just that the simplest syntax reuses the existing name.


Destructuring creates variables. These variables should adhere to your naming convention.
Otherwise you will introduce names that do not match your conventions into your codebase.

Side note that there is currently a bug with destructured names: #2250.

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look labels Sep 7, 2020
@Timmmm
Copy link
Author

Timmmm commented Sep 9, 2020

Destructuring creates variables.

Right, but variables that I didn't name. If I did name them (because the come from my codebase) then I'll get a warning at the place where they are defined anyway. The Rust developers agree, if that persuades you!

Maybe this should be an option.

@Timmmm

This comment has been minimized.

@Timmmm

This comment has been minimized.

@bradzacher
Copy link
Member

The Rust developers agree, if that persuades you!

It doesn't - pattern matching is a bit different. I don't think there's a way to rename the variable there (I don't know anything about rust syntax but I couldn't see it in a quick scan of the docs).


Using PascalCase when your naming convention specified camelCase doesn't seem like to egregious of an issue.

But consider that ignoring destructuring naming would allow this:

const { mY_rId_ICULOUS_ly_$$$_named_variable } = {};

It's hard to draw that line and says "this is acceptable variance".
IMO it's better to just say "all variables must follow the naming convention, no matter their source".

Perhaps we could mark these variables with a modifier, and allow you to reconfigure it as strict as you like?

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed awaiting response Issues waiting for a reply from the OP or another party labels Sep 9, 2020
@bradzacher bradzacher changed the title [naming-convention] False positive when destructuring [naming-convention] add a modifier to identify a variable/parameter as having been destructured Sep 9, 2020
@Timmmm
Copy link
Author

Timmmm commented Sep 10, 2020

I don't think there's a way to rename the variable there

There is - pattern matching is very similar to destructuring in Rust.

You could instead write

    match pet {
       Animal::Spider { iHateSnakes: i_hate_snakes } => dbg!(i_hate_snakes),
   };

Perhaps we could mark these variables with a modifier, and allow you to reconfigure it as strict as you like?

That sounds like a perfect solution!

bradzacher added a commit that referenced this issue Nov 24, 2020
…lobal`, and `destructured` modifiers - add selectors `classProperty`, `objectLiteralProperty`, `typeProperty`, `classMethod`, `objectLiteralMethod`, `typeMethod`

Fixes #2239
Fixes #2512
Fixes #2318
Fixes #1477

Big update to add a bunch of stuff.
I couldn't be bothered splitting this out into separate PRs.
bradzacher added a commit that referenced this issue Nov 24, 2020
…lobal`, and `destructured` modifiers - add selectors `classProperty`, `objectLiteralProperty`, `typeProperty`, `classMethod`, `objectLiteralMethod`, `typeMethod`

Fixes #2239
Fixes #2512
Fixes #2318
Fixes #1477

Big update to add a bunch of stuff.
I couldn't be bothered splitting this out into separate PRs.

Adds the following modifiers:
- `exported` - matches anything that is exported from the module.
- `global` - matches a variable/function declared in the top-level scope.
- `destructured` - matches a variable declared via an object destructuring pattern (`const {x, ignored: y, z = 2}`).

Adds the following selectors (self explanatory - just breaking the selectors up):
- `classProperty`
- `objectLiteralProperty`
- `typeProperty`
- `classMethod`
- `objectLiteralMethod`
- `typeMethod`

Converts
- `property` to a meta selector for `classProperty`, `objectLiteralProperty`, `typeProperty`
- `method` to a meta selector for `classMethod`, `objectLiteralMethod`, `typeMethod`
bradzacher added a commit that referenced this issue Nov 24, 2020
…lobal`, and `destructured` modifiers - add selectors `classProperty`, `objectLiteralProperty`, `typeProperty`, `classMethod`, `objectLiteralMethod`, `typeMethod`

Fixes #2239
Fixes #2512
Fixes #2318
Fixes #1477

Big update to add a bunch of stuff.
I couldn't be bothered splitting this out into separate PRs.

Adds the following modifiers:
- `exported` - matches anything that is exported from the module.
- `global` - matches a variable/function declared in the top-level scope.
- `destructured` - matches a variable declared via an object destructuring pattern (`const {x, ignored: y, z = 2}`).

Adds the following selectors (self explanatory - just breaking the selectors up):
- `classProperty`
- `objectLiteralProperty`
- `typeProperty`
- `classMethod`
- `objectLiteralMethod`
- `typeMethod`

Converts
- `property` to a meta selector for `classProperty`, `objectLiteralProperty`, `typeProperty`
- `method` to a meta selector for `classMethod`, `objectLiteralMethod`, `typeMethod`
bradzacher added a commit that referenced this issue Nov 24, 2020
…lobal`, and `destructured`

Fixes #2239
Fixes #2512
Fixes #2318
Closes #2802

Adds the following modifiers:
- `exported` - matches anything that is exported from the module.
- `global` - matches a variable/function declared in the top-level scope.
- `destructured` - matches a variable declared via an object destructuring pattern (`const {x, ignored: y, z = 2}`).
bradzacher added a commit that referenced this issue Nov 24, 2020
…lobal`, and `destructured`

Fixes #2239
Fixes #2512
Fixes #2318
Closes #2802

Adds the following modifiers:
- `exported` - matches anything that is exported from the module.
- `global` - matches a variable/function declared in the top-level scope.
- `destructured` - matches a variable declared via an object destructuring pattern (`const {x, z = 2}`).
bradzacher added a commit that referenced this issue Nov 24, 2020
…lobal`, and `destructured`

Fixes #2239
Fixes #2512
Fixes #2318
Closes #2802

Adds the following modifiers:
- `exported` - matches anything that is exported from the module.
- `global` - matches a variable/function declared in the top-level scope.
- `destructured` - matches a variable declared via an object destructuring pattern (`const {x, z = 2}`).
bradzacher added a commit that referenced this issue Nov 24, 2020
…lobal`, and `destructured` (#2808)

Fixes #2239
Fixes #2512
Fixes #2318
Closes #2802

Adds the following modifiers:
- `exported` - matches anything that is exported from the module.
- `global` - matches a variable/function declared in the top-level scope.
- `destructured` - matches a variable declared via an object destructuring pattern (`const {x, z = 2}`).
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.