Skip to content

False positive in no-side-effects-in-computed-properties #230

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
lgnap opened this issue Nov 7, 2017 · 5 comments · Fixed by #231
Closed

False positive in no-side-effects-in-computed-properties #230

lgnap opened this issue Nov 7, 2017 · 5 comments · Fixed by #231

Comments

@lgnap
Copy link

lgnap commented Nov 7, 2017

Tell us about your environment
I try using 5f4f3ce (last master) from this repos

  • ESLint Version: ^3.19.0

  • eslint-plugin-eslint-plugin: ^0.8.0

  • eslint-plugin-vue-libs: ^1.2.0

  • mocha: ^3.2.0

  • eslint-plugin-vue Version: version: 3.13.1

  • Node Version: 8.8.1

Please show your full configuration:
Not relevant

What did you do? Please include the actual source code causing the issue.

I write a unit test case but I can't solve the issue myself:

'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-side-effects-in-computed-properties')
const RuleTester = require('eslint').RuleTester

const parserOptions = {
  ecmaVersion: 6,
  sourceType: 'module',
  ecmaFeatures: { experimentalObjectRestSpread: true }
}

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester()
ruleTester.run('no-side-effects-in-computed-properties', rule, {
  valid: [
    {
      code: `Vue.component('test', {
        computed: {
          test9 () {
            let computedResult = {}
            const index = ''
            computedResult[index] = this.images[index]
            return computedResult
          },    
        }
      })`,
      parserOptions
    }
  ],
  invalid: [
    {
      code: `Vue.component('test', {
        computed: {
          test5 () {
            let computedResult = {}
            const index = ''
            this.images[index] = computedResult[index]
            return computedResult
          },     
        }
      })`,
      parserOptions,
      errors: [{
        line: 6,
        message: 'Unexpected side effect in "test5" computed property.'
      }]
    }
  ]
})

What did you expect to happen?
The first test must succeed (I don't update this.images, just read it), the second must fail

What actually happened? Please include the actual, raw output from ESLint.
I got an Unexpected side effect in both case.

The section of parser lib/rules/no-side-effects-in-computed-properties.js relevant for my case is the following:

      // this.xxx <=|+=|-=>
      'AssignmentExpression > MemberExpression' (node) {
        if (utils.parseMemberExpression(node)[0] === 'this') {
          forbiddenNodes.push(node)
        }
      },

The grammar parser ( AssignmentExpression > MemberExpression) see independent nodes and a console log will show the two part separately, it shouldn't.
A console.log will show the following:

[ 'computedResult', 'index' ]
[ 'this', 'images', 'index' ]
@michalsnik
Copy link
Member

Hey @lgnap, thanks for posting this issue. Impressive breakdown! I can dig right into implementation :)

@michalsnik
Copy link
Member

Ok I got it, it's waiting for code review now @lgnap

@lgnap
Copy link
Author

lgnap commented Nov 14, 2017

Ho thanks, I see (but AST is not my domain -- this is why I wrote this bug instead of PR) that you could use the :first-child selector (cfr https://eslint.org/docs/developer-guide/selectors).
'AssignmentExpression:first-child'
What do you thing?

@michalsnik
Copy link
Member

It doesn't work unfortunately @lgnap. I'm going to merge and realese new version with current solution :)

@lgnap
Copy link
Author

lgnap commented Nov 16, 2017

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants