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

Incorrect no-return-assign report #13135

Closed
brianwhu opened this issue Apr 2, 2020 · 6 comments · Fixed by #13138
Closed

Incorrect no-return-assign report #13135

brianwhu opened this issue Apr 2, 2020 · 6 comments · Fixed by #13138
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion documentation Relates to ESLint's documentation rule Relates to ESLint's core rules

Comments

@brianwhu
Copy link

brianwhu commented Apr 2, 2020

Tell us about your environment

Windows 10

Node version: v10.16.0
npm version: v6.9.0
Local ESLint version: v6.8.0
Global ESLint version: Not found

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

babel-eslint

Please show your full configuration:

Configuration
module.exports = {
  root: true,

  parserOptions: {
    parser: 'babel-eslint',
    sourceType: 'module'
  },

  env: {
    browser: true
  },

  extends: [
    'standard',
    // Uncomment any of the lines below to choose desired strictness,
    // but leave only one uncommented!
    // See https://eslint.vuejs.org/rules/#available-rules
    'plugin:vue/essential' // Priority A: Essential (Error Prevention)
    // 'plugin:vue/strongly-recommended' // Priority B: Strongly Recommended (Improving Readability)
    // 'plugin:vue/recommended' // Priority C: Recommended (Minimizing Arbitrary Choices and Cognitive Overhead)
  ],

  // required to lint *.vue files
  plugins: [
    'vue'
  ],

  globals: {
    'ga': true, // Google Analytics
    'cordova': true,
    '__statics': true,
    'process': true,
    'Capacitor': true,
    'chrome': true
  },

  // add your custom rules here
  rules: {
    // allow async-await
    'generator-star-spacing': 'off',
    // allow paren-less arrow functions
    'arrow-parens': 'off',
    'one-var': 'off',

    'import/first': 'off',
    'import/named': 'error',
    'import/namespace': 'error',
    'import/default': 'error',
    'import/export': 'error',
    'import/extensions': 'off',
    'import/no-unresolved': 'off',
    'import/no-extraneous-dependencies': 'off',
    'prefer-promise-reject-errors': 'off',

    // allow debugger during development only
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',

    'no-sequences': 'off',
  }
}

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

I have an arrow function that returns a comma expression (i.e. sequence). After I changed the rule to allow comma operator, eslint reported "no-return-assign" error while in fact it is the comma expression that is being returned.

row.reduce((object, value, index) => (object[rs.columns[index]] = value, object), {})

Not sure. ESlint was invoked by quasar/cli

quasar dev

What did you expect to happen?

The code is NOT returning an assignment, but a comma expression where the second operand is a single object. This should be accepted by ESlint

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

ESlint reported no-return-assign error:

error Arrow function should not return assignment no-return-assign

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

No

@brianwhu brianwhu added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Apr 2, 2020
@ljharb
Copy link
Sponsor Contributor

ljharb commented Apr 2, 2020

Oof. why isn't this => { object[rs.columns[index]] = value; return object; }?

@mdjermanovic
Copy link
Member

Hi @brianwhu, thanks for the bug report!

I can confirm this is the intended behavior of this rule (introduced in #6041, issue #5913).

On the other hand, we really don't have any incorrect examples with a nested assignment, and there are also no examples with arrow functions, so I'm marking this as an accepted issue to improve the documentation for this rule.

@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion documentation Relates to ESLint's documentation 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 Apr 2, 2020
@mdjermanovic
Copy link
Member

As for the provided example, you can wrap object[rs.columns[index]] = value in parens:

/*eslint no-return-assign: error*/

row.reduce((object, value, index) => ((object[rs.columns[index]] = value), object), {})

online demo

@brianwhu
Copy link
Author

brianwhu commented Apr 3, 2020

Hi @mdjermanovic, thanks for the suggestion. But this make the case even more puzzling. Now ESlint would accept

() => (a = b)

But not

() => (a = b, c)

?

@anikethsaha
Copy link
Member

Hi @mdjermanovic, thanks for the suggestion. But this make the case even more puzzling. Now ESlint would accept

() => (a = b)

But not

() => (a = b, c)

?

this is because, as per the rule , except-parens (which is enabled by default) allows return assignment if parenthesis around assignment expression only.. so

  • () => (a = b)

in this, the parenthesis is around assignment expression. so it's ok

  • () => (a = b, c)

in this, parenthesis is around returned expression not around assignment expression, so its an error

@mdjermanovic
Copy link
Member

Just to add, correct code without linting errors would be:

/*eslint no-return-assign: "error"*/

() => ((a = b), c)

This rule by default allows parenthesized assignments (parentheses should be directly around the assignment expression).

kaicataldo pushed a commit that referenced this issue Apr 4, 2020
#13138)

* Chore: docs example for arrow function and tests (fixes #13135)

* Chore: removed wrong test case

* Chore: refactore snipper and added tests
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Oct 2, 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 Oct 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion documentation Relates to ESLint's documentation rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants