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

bug no-unused-vars - is assigned a value but never used #13172

Closed
jonathantribouharet opened this issue Apr 11, 2020 · 13 comments
Closed

bug no-unused-vars - is assigned a value but never used #13172

jonathantribouharet opened this issue Apr 11, 2020 · 13 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 bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules

Comments

@jonathantribouharet
Copy link

Node version: v13.11.0
npm version: v6.13.7
Local ESLint version: v6.8.0 (Currently used)
Global ESLint version: Not found
Typescript Version: 3.8.3
VueJS Version: 2.6.11

What parser (default, Babel-ESLint, etc.) are you using?
Babel-ESLint

Please show your full configuration:

Configuration
module.exports = {
  root: true,
  env: {
    node: true,
  },
  extends: [
    'plugin:vue/recommended',
    '@vue/typescript/recommended',
  ],
  parserOptions: {
    ecmaVersion: 2020,
  },
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
    '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
    "indent": ["error", 2],
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/camelcase': 'off',
    '@typescript-eslint/no-non-null-assertion': 'off',
  },
  settings: {
    polyfills: [
      "promises",
    ]
  }
};

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

    let myArray = [1,2,3,4].filter((x) => x == 0);
    myArray = myArray.filter((x) => x == 1);

What did you expect to happen?
It should be an error.

What actually happened? Please include the actual, raw output from ESLint.
error 'myArray' is assigned a value but never used no-unused-vars
error 'myArray' is assigned a value but never used @typescript-eslint/no-unused-vars

Are you willing to submit a pull request to fix this bug?
No

@jonathantribouharet jonathantribouharet added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Apr 11, 2020
@jonathantribouharet jonathantribouharet changed the title bug no-unused-vars - is assigned a value but never used` bug no-unused-vars - is assigned a value but never used Apr 11, 2020
@anikethsaha
Copy link
Member

I am not sure but I think this is intentional.
cause when using this

 let myArray = [1,2,3,4].filter((x) => x == 0);
    foo= myArray.filter((x) => x == 1);

it is lint-free.

Also just to note, that you are using both @typescript-eslint/no-unused-vars and no-unused-vars (from eslint). Its recommended to use @typescript-eslint/no-unused-vars and turn off the base rule (no-unused-vars) if its a TS project. this rule is extending the base rule (no-unused-vars)

@jonathantribouharet
Copy link
Author

It feels strange, because my example is very trivial but I don't understand the why.
In my real case, I use the array in a loop so I cannot create a new variable each time a reassign the variable myArray.

@anikethsaha
Copy link
Member

I would chain them if possible.

let myArray = [1,2,3,4].filter((x) => x == 0).filter((x) => x == 1)

@jonathantribouharet
Copy link
Author

In my real case the two operations are not exactly like this, the 2nd line is in a loop and the condition change.

@mdjermanovic
Copy link
Member

@jonathantribouharet thanks for the bug report!

    let myArray = [1,2,3,4].filter((x) => x == 0);
    myArray = myArray.filter((x) => x == 1);

What did you expect to happen?
It should be an error.
What actually happened? Please include the actual, raw output from ESLint.
error 'myArray' is assigned a value but never used no-unused-vars
error 'myArray' is assigned a value but never used @typescript-eslint/no-unused-vars

Can you please clarify this issue, it seems that the actual behavior matches the expected behavior?

@jonathantribouharet
Copy link
Author

jonathantribouharet commented Apr 12, 2020

@mdjermanovic the issue is I'm actually using the variable myArray so why having an error saying the opposite?

The real code look like this:

function (items: [OrderItem]) {
  let itemsToProcess = items.filter((x) => x.relationships.item.data != null);
  let results = items.filter((x) => x.relationships.item.data == null);
  results = results.sort((a, b) => b.amountWithVat - a.amountWithVat)

  itemsToProcess = itemsToProcess.filter((item) => {
    const parentId = item.relationships.item.data.id;

    const index = results.findIndex((x) => x.id == parentId);
    if (index >= 0) {
      results.splice(index + 1, 0, item);
      return false;
    }

    return true;
  })

  itemsToProcess.filter((item) => {
    const parentId = item.relationships.item.data.id;

    const index = results.findIndex((x) => x.id == parentId);
    if (index >= 0) {
      results.splice(index + 1, 0, item);
      return false;
    }

    return true;
  })

  return results;
}

The error is on itemsToProcess on line 2.

@mdjermanovic mdjermanovic added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Apr 13, 2020
@mdjermanovic
Copy link
Member

@mdjermanovic the issue is I'm actually using the variable myArray so why having an error saying the opposite?

no-unused-vars by design reports an error if the variable is used only to update itself.

I agree it would be nice to change the error message in this case.

The real code look like this:

function (items: [OrderItem]) {
  let itemsToProcess = items.filter((x) => x.relationships.item.data != null);
  let results = items.filter((x) => x.relationships.item.data == null);
  results = results.sort((a, b) => b.amountWithVat - a.amountWithVat)

  itemsToProcess = itemsToProcess.filter((item) => {
    const parentId = item.relationships.item.data.id;

    const index = results.findIndex((x) => x.id == parentId);
    if (index >= 0) {
      results.splice(index + 1, 0, item);
      return false;
    }

    return true;
  })

  itemsToProcess.filter((item) => {
    const parentId = item.relationships.item.data.id;

    const index = results.findIndex((x) => x.id == parentId);
    if (index >= 0) {
      results.splice(index + 1, 0, item);
      return false;
    }

    return true;
  })

  return results;
}

The error is on itemsToProcess on line 2.

This would be a bug, but I can't reproduce it. Please let me know if I'm missing something.

config:

module.exports = {
    parser: "@typescript-eslint/parser",
    plugins: ["@typescript-eslint"],
    rules : {
        'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
        '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }]
    },
};

code (had to add function's name to make it valid syntax):

function foo(items: [OrderItem]) {
    let itemsToProcess = items.filter((x) => x.relationships.item.data != null);
    let results = items.filter((x) => x.relationships.item.data == null);
    results = results.sort((a, b) => b.amountWithVat - a.amountWithVat)
  
    itemsToProcess = itemsToProcess.filter((item) => {
      const parentId = item.relationships.item.data.id;
  
      const index = results.findIndex((x) => x.id == parentId);
      if (index >= 0) {
        results.splice(index + 1, 0, item);
        return false;
      }
  
      return true;
    })
  
    itemsToProcess.filter((item) => {
      const parentId = item.relationships.item.data.id;
  
      const index = results.findIndex((x) => x.id == parentId);
      if (index >= 0) {
        results.splice(index + 1, 0, item);
        return false;
      }
  
      return true;
    })
  
    return results;
  }

output:

  1:10  error  'foo' is defined but never used  no-unused-vars
  1:10  error  'foo' is defined but never used  @typescript-eslint/no-unused-vars

Neither of the two no-unused-vars rules reports errors on itemsToProcess.

I also tried with @typescript-eslint/parser and the result is same.

Here's also online demo with the default parser.

@jonathantribouharet
Copy link
Author

Ok it makes sense for the rule
For the online demo, I see the problem, in fact the code I sent wasn't good, it is:

/* eslint 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }] */   

function foo(items) {
    let itemsToProcess = items.filter((x) => x.relationships.item.data != null);
    let results = items.filter((x) => x.relationships.item.data == null);
    results = results.sort((a, b) => b.amountWithVat - a.amountWithVat)
  
    itemsToProcess = itemsToProcess.filter((item) => {
      const parentId = item.relationships.item.data.id;
  
      const index = results.findIndex((x) => x.id == parentId);
      if (index >= 0) {
        results.splice(index + 1, 0, item);
        return false;
      }
  
      return true;
    })
  
    itemsToProcess = itemsToProcess.filter((item) => {
      const parentId = item.relationships.item.data.id;
  
      const index = results.findIndex((x) => x.id == parentId);
      if (index >= 0) {
        results.splice(index + 1, 0, item);
        return false;
      }
  
      return true;
    })
  
    return results;
  }

And the problem is line 20 with itemsToProcess = itemsToProcess where I don't use afterwards itemsToProcess but the error appear on line 4 at the declaration. So there is no problem with the rule, maybe just indicate the line 20 instead of line 4.

@mdjermanovic mdjermanovic added the enhancement This change enhances an existing feature of ESLint label Apr 13, 2020
@mdjermanovic
Copy link
Member

And the problem is line 20 with itemsToProcess = itemsToProcess where I don't use afterwards itemsToProcess but the error appear on line 4 at the declaration. So there is no problem with the rule, maybe just indicate the line 20 instead of line 4.

Makes sense 👍

I believe this is the same request as #12871, which unfortunately didn't reach consensus at the time.

I think we could at least change the error message.

@anikethsaha
Copy link
Member

anikethsaha commented Apr 14, 2020

Yes, it should report at the last references instead of the declaration if it has references.

@nzakas
Copy link
Member

nzakas commented Apr 15, 2020

Can someone make a concrete proposal so we can decide on it?

@anikethsaha
Copy link
Member

@nzakas I made a fresh issue with the proposal here #13181

@eslint-deprecated eslint-deprecated bot added the auto closed The bot closed this issue label May 17, 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-deprecated eslint-deprecated bot locked and limited conversation to collaborators Nov 14, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Nov 14, 2020
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 bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

4 participants