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

[no-unused-vars] Incorrect code location when self referencing #13546

Closed
Zodiase opened this issue Aug 3, 2020 · 7 comments
Closed

[no-unused-vars] Incorrect code location when self referencing #13546

Zodiase opened this issue Aug 3, 2020 · 7 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion auto closed The bot closed this issue evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules works as intended The behavior described in this issue is working correctly

Comments

@Zodiase
Copy link

Zodiase commented Aug 3, 2020

BTW, typescript-eslint/typescript-eslint#2353 (comment) pointed me here.

Tell us about your environment

Node version: v10.20.0
npm version: v6.14.4
Local ESLint version: v7.5.0 (Currently used)
Global ESLint version: Not found

What parser (default, Babel-ESLint, etc.) are you using?
@typescript-eslint/parser.

Please show your full configuration:

Configuration
module.exports = {
    env: {
        browser: true,
        // Parcel can shim node apis.
        node: true,
        es2020: true
    },
    extends: [
        'plugin:@typescript-eslint/recommended',
        'plugin:react/recommended',
        'plugin:react-hooks/recommended',
        // Prettier needs to be the last.
        'prettier',
        'prettier/@typescript-eslint',
        'prettier/babel',
        'prettier/react'
    ],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        ecmaFeatures: {
            jsx: true
        },
        ecmaVersion: 11,
        sourceType: 'module'
    },
    plugins: ['react', 'react-hooks', '@typescript-eslint'],
    rules: {
        '@typescript-eslint/no-unused-vars': [
            'warn',
            { ignoreRestSiblings: true }
        ],
        '@typescript-eslint/no-use-before-define': [
            'error',
            { functions: false }
        ],
        '@typescript-eslint/no-inferrable-types': 'off',
        'newline-after-var': ['error', 'always'],
        'no-console': 'warn',
        'no-undef': 'error',
        'react/display-name': 'off',
        'react/jsx-fragments': ['error', 'element'],
        'react/prop-types': ['error', { skipUndeclared: true }]
    },
    overrides: [
        {
            files: ['*.js'],
            rules: {
                // Don't check function types for `.js` files.
                '@typescript-eslint/explicit-module-boundary-types': 'off'
            }
        },
        {
            files: ['*.test.js', '*.test.ts', '*.test.tsx'],
            env: {
                jest: true
            },
            rules: {
                '@typescript-eslint/no-namespace': 'off',
                // Don't care about using type `any` in tests.
                '@typescript-eslint/no-explicit-any': 'off',
                '@typescript-eslint/no-var-requires': 'off',
                '@typescript-eslint/explicit-function-return-type': 'off',
                // Don't care about using `console` in tests.
                'no-console': 'off'
            }
        },
        {
            files: ['*.stories.js', '*.stories.ts', '*.stories.tsx'],
            env: {
                node: true
            },
            rules: {
                // Don't care about using type `any` in Storybook.
                '@typescript-eslint/no-explicit-any': 'off',
                // Don't care about using `console` in Storybook.
                'no-console': 'off'
            }
        },
        {
            files: ['maintenance/**/*.ts'],
            rules: {
                '@typescript-eslint/no-var-requires': 'off',
                'no-console': 'off'
            }
        }
    ],
    settings: {
        react: {
            version: 'detect'
        }
    }
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

const foobar = () => ({
    getFoobar: () => foobar() //<-- 'foobar' is assigned a value but never used.
});
npx eslint src/test.ts

What did you expect to happen?
The foobar on line 1 should be labeled as not used.

What actually happened? Please include the actual, raw output from ESLint.

src/test.ts
  2:22  warning  'foobar' is assigned a value but never used  @typescript-eslint/no-unused-vars

Are you willing to submit a pull request to fix this bug?
Sorry but my boss is already going to kill me for wasting time on things like this (reporting bugs).

@Zodiase Zodiase added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Aug 3, 2020
@anikethsaha
Copy link
Member

anikethsaha commented Aug 3, 2020

Thanks for the report.

Actually this is intended behavior.
It should report the last read/write reference as it will more helpful in locating the variable.

More info #13181

@anikethsaha anikethsaha added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion works as intended The behavior described in this issue is working correctly rule Relates to ESLint's core rules and removed bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Aug 3, 2020
@Zodiase
Copy link
Author

Zodiase commented Aug 4, 2020

OK but it doesn't make sense to me...

In #13181,

image

I think the error should be reported at the a highlighted in the screenshot, instead of the second a on the right hand of the assignment, since the a on the left hand side is the one not being used, where the right hand side one is a good reference with no problem.

In my case,

image

The foobar on line 1 is the variable not being used, regardless of it being the declaration or something else.

I can make it like this:

let foobar = null;

foobar = () => ({
    getFoobar: () => foobar()
});

So that line 3 is no longer a declaration. foobar on line 3 should be reported since that is the entity where no other code is using.

@kaicataldo
Copy link
Member

@Zodiase I agree that reporting the last assignment would make sense. We've gone through a few iterations of the reported location at this point, so we'd want to make sure we have really ironed out what it is we want if we want to consider changing it again.

@anikethsaha
Copy link
Member

If these issues keep coming back with the other location being proposed,

I think how about making the reporting location as an option?

@mdjermanovic
Copy link
Member

I agree that it would make more sense to report foobar on line 1, and the left a in a = a + 3.

@kaicataldo
Copy link
Member

@anikethsaha I'd prefer not to go down that route, as that opens up a whole new can of words around configuration. I would also argue that this is a stylistic choice. I do think it makes the most sense to report the last assignment, however, and not the last occurrence.

@eslint-deprecated eslint-deprecated bot added the auto closed The bot closed this issue label Sep 7, 2020
@eslint-deprecated
Copy link

Unfortunately, it looks like there wasn't enough interest from the team
or community to implement this change. While we wish we'd be able to
accommodate everyone's requests, we do need to prioritize. We've found
that issues failing to reach accepted status after 21 days tend to
never be accepted, and as such, we close those issues.
This doesn't mean the idea isn't interesting or useful, just that it's
not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Mar 18, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Mar 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion auto closed The bot closed this issue evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules works as intended The behavior described in this issue is working correctly
Projects
None yet
Development

No branches or pull requests

4 participants