Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
519 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/** | ||
* @fileoverview Check if component props are not mutated | ||
* @author 2018 Armano | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow mutation of props', | ||
category: undefined, | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/no-mutating-props.md' | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [ | ||
// fill in your schema | ||
] | ||
}, | ||
|
||
create (context) { | ||
let mutatedNodes = [] | ||
let props = [] | ||
|
||
function checkForMutations () { | ||
for (const prop of props) { | ||
const propName = utils.getStaticPropertyName(prop.key) | ||
|
||
for (const node of mutatedNodes) { | ||
if (propName === node.name) { | ||
context.report({ | ||
node: node.node, | ||
message: 'Unexpected mutation of "{{key}}" prop.', | ||
data: { key: node.name } | ||
}) | ||
} | ||
} | ||
} | ||
mutatedNodes = [] | ||
} | ||
|
||
function checkTemplateProperty (node) { | ||
if (node.type === 'MemberExpression') { | ||
const expression = utils.parseMemberExpression(node) | ||
mutatedNodes.push({ | ||
name: expression[0] === 'this' ? expression[1] : expression[0], | ||
node | ||
}) | ||
} else if (node.type === 'Identifier') { | ||
mutatedNodes.push({ | ||
name: node.name, | ||
node | ||
}) | ||
} | ||
} | ||
|
||
return Object.assign({}, | ||
{ | ||
// this.xxx <=|+=|-=> | ||
'AssignmentExpression' (node) { | ||
if (node.left.type !== 'MemberExpression') return | ||
const expression = utils.parseMemberExpression(node.left) | ||
if (expression[0] === 'this') { | ||
mutatedNodes.push({ | ||
name: expression[1], | ||
node | ||
}) | ||
} | ||
}, | ||
// this.xxx <++|--> | ||
'UpdateExpression > MemberExpression' (node) { | ||
const expression = utils.parseMemberExpression(node) | ||
if (expression[0] === 'this') { | ||
mutatedNodes.push({ | ||
name: expression[1], | ||
node | ||
}) | ||
} | ||
}, | ||
// this.xxx.func() | ||
'CallExpression' (node) { | ||
const expression = utils.parseMemberOrCallExpression(node) | ||
const code = expression.join('.').replace(/\.\[/g, '[') | ||
const MUTATION_REGEX = /(this.)((?!(concat|slice|map|filter)\().)[^\)]*((push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)\()/g | ||
|
||
if (MUTATION_REGEX.test(code)) { | ||
if (expression[0] === 'this') { | ||
mutatedNodes.push({ | ||
name: expression[1], | ||
node | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
utils.executeOnVue(context, (obj) => { | ||
props = utils.getComponentProps(obj) | ||
.filter(cp => cp.key) | ||
checkForMutations() | ||
}), | ||
|
||
utils.defineTemplateBodyVisitor(context, { | ||
'VExpressionContainer AssignmentExpression' (node) { | ||
checkTemplateProperty(node.left) | ||
}, | ||
// this.xxx <++|--> | ||
'VExpressionContainer UpdateExpression' (node) { | ||
checkTemplateProperty(node.argument) | ||
}, | ||
// this.xxx.func() | ||
'VExpressionContainer CallExpression' (node) { | ||
const expression = utils.parseMemberOrCallExpression(node) | ||
const code = expression.join('.').replace(/\.\[/g, '[') | ||
const MUTATION_REGEX = /(this.)?((?!(concat|slice|map|filter)\().)[^\)]*((push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)\()/g | ||
|
||
if (MUTATION_REGEX.test(code)) { | ||
mutatedNodes.push({ | ||
name: expression[0] === 'this' ? expression[1] : expression[0], | ||
node | ||
}) | ||
} | ||
}, | ||
|
||
"VAttribute[directive=true][key.name='model'] VExpressionContainer" (node) { | ||
checkTemplateProperty(node.expression) | ||
}, | ||
|
||
"VElement[name='template']:exit" () { | ||
checkForMutations() | ||
} | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.