Skip to content

(Fixes #420) Fix false positive in no-side-effect-in-cp #464

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/no-side-effects-in-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
},
// this.xxx.func()
'CallExpression' (node) {
const code = context.getSourceCode().getText(node)
const code = utils.parseMemberOrCallExpression(node)
const MUTATION_REGEX = /(this.)((?!(concat|slice|map|filter)\().)[^\)]*((push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)\()/g

if (MUTATION_REGEX.test(code)) {
Expand Down
37 changes: 37 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,5 +617,42 @@ module.exports = {
return
}
return body.errors.some(error => typeof error.code === 'string' && error.code.startsWith('eof-'))
},

/**
* Parse CallExpression or MemberExpression to get simplified version without arguments
*
* @param {Object} node The node to parse (MemberExpression | CallExpression)
* @return {String} eg. 'this.asd.qwe().map().filter().test.reduce()'
*/
parseMemberOrCallExpression (node) {
const parsedCallee = []
let n = node
let isFunc

while (n.type === 'MemberExpression' || n.type === 'CallExpression') {
if (n.type === 'CallExpression') {
n = n.callee
isFunc = true
} else {
if (n.computed) {
parsedCallee.push('[]')
} else if (n.property.type === 'Identifier') {
parsedCallee.push(n.property.name + (isFunc ? '()' : ''))
}
isFunc = false
n = n.object
}
}

if (n.type === 'Identifier') {
parsedCallee.push(n.name)
}

if (n.type === 'ThisExpression') {
parsedCallee.push('this')
}

return parsedCallee.reverse().join('.').replace(/\.\[/g, '[')
}
}
19 changes: 19 additions & 0 deletions tests/lib/rules/no-side-effects-in-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ ruleTester.run('no-side-effects-in-computed-properties', rule, {
get() {
return Object.keys(this.a).sort()
}
},
test11() {
const categories = {}

this.types.forEach(c => {
categories[c.category] = categories[c.category] || []
categories[c.category].push(c)
})

return categories
},
test12() {
return this.types.map(t => {
// [].push('xxx')
return t
})
},
test13 () {
this.someArray.forEach(arr => console.log(arr))
}
}
})`,
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,23 @@ describe('getStaticPropertyName', () => {
assert.ok(parsed === 'computed')
})
})

describe('parseMemberOrCallExpression', () => {
let node

const parse = function (code) {
return babelEslint.parse(code).body[0].declarations[0].init
}

it('should parse CallExpression', () => {
node = parse(`const test = this.lorem['ipsum'].map(d => d.id).filter((a, b) => a > b).reduce((acc, d) => acc + d, 0)`)
const parsed = utils.parseMemberOrCallExpression(node)
assert.equal(parsed, 'this.lorem[].map().filter().reduce()')
})

it('should parse MemberExpression', () => {
node = parse(`const test = this.lorem['ipsum'][0].map(d => d.id).dolor.reduce((acc, d) => acc + d, 0).sit`)
const parsed = utils.parseMemberOrCallExpression(node)
assert.equal(parsed, 'this.lorem[][].map().dolor.reduce().sit')
})
})