8000 [no-restricted-globals] False positive with optional chaining · Issue #1090 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

[no-restricted-globals] False positive with optional chaining #1090

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

Closed
m-rutter opened this issue Oct 15, 2019 · 6 comments · Fixed by #1169
Closed

[no-restricted-globals] False positive with optional chaining #1090

m-rutter opened this issue Oct 15, 2019 · 6 comments · Fixed by #1169
Labels
bug Something isn't working has pr there is a PR raised to close this package: parser Issues related to @typescript-eslint/parser scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser

Comments

@m-rutter
Copy link
m-rutter commented Oct 15, 2019

I wasn't quite sure which should sub-project was appropriate for this issue. I am aware that strictly ts 3.7.0-beta isn't supported while TS 3.7 syntax is, but out of caution I thought to raise the issue as I couldn't see any other reports.

What code were you trying to parse?

// foobar.ts
const a = foo?.bar?.name ?? "foobar"

What did you expect to happen?

That eslint would not emit any warnings nor errors

What actually happened?

Eslint emitted the following error:
Unexpected use of 'name' no-restricted-globals

This does not happen for all properties, but so far I've only been able to reproduce with properties called name.

Versions

package version
@typescript-eslint/parser 2.4.0
TypeScript 3.7.0-beta (15 Oct 2019)
ESLint 6.5.1
node 12.11.1
npm 6.11.3
yarn 1.19.1
@m-rutter m-rutter added package: parser Issues related to @typescript-eslint/parser triage Waiting for team members to take a look labels Oct 15, 2019
@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for team members to take a look labels Oct 15, 2019
@bradzacher
8000
Copy link
Member

Thinking about it - this is happening for the same reason that #1104 is happening.

They both rely upon scope analysis to determine variables in the program.
With the AST change required to support optional chaining, this caused the scope analysis no longer consider the name Identifier to be a member, so it instead considers it a variable.

@bradzacher bradzacher added the scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser label Oct 21, 2019
@bradzacher bradzacher changed the title Unexpected "no-restricted-globals" error when using optional chaining [no-restricted-globals] False positive with optional chaining Oct 21, 2019
@bradzacher bradzacher added the has pr there is a PR raised to close this label Nov 8, 2019
@aecorredor
Copy link

Still happens to me on 2.19.0. Only when referencing a property called name.

@bradzacher
Copy link
Member
bradzacher commented Feb 4, 2020

That would be because name is a global in the browser environment (window.name is a thing, IIRC it has the window name if it was named via window.open), hence it is marked as restricted by the rule.
https://eslint.org/docs/user-guide/configuring#specifying-environments

@aecorredor
Copy link
aecorredor commented Feb 4, 2020

@bradzacher ok I get that. The weird thins is that it's happening to me when I reference it from a JSON object that has some data I imported.

Example:

import banks from './data/banks.json'

const getBankNameByCode = code => {
   const selectedBank = banks.find(bank => bank.code === code);
   return selectedBank?.name ?? ''; // error happens here.
}

@bradzacher
Copy link
Member

I am unable to repro this against master.

image

{
code: `
const a = foo?.bar?.name
`,
},
{
code: `
const a = foo?.bar?.name ?? "foobar"
`,
},

@aecorredor
Copy link

@bradzacher thanks for checking. I'll dig in more on my side and report back here if anything. Maybe it's related to my specific project config for some reason.

@bradzacher bradzacher added this to the scope analysis rewrite milestone Apr 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working has pr there is a PR raised to close this package: parser Issues related to @typescript-eslint/parser scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0