|
| 1 | +/** |
| 2 | + * @fileoverview Check if component props are not mutated |
| 3 | + * @author 2018 Armano |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +module.exports = { |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + description: 'disallow mutation of props', |
| 17 | + category: undefined, |
| 18 | + url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/no-mutating-props.md' |
| 19 | + }, |
| 20 | + fixable: null, // or "code" or "whitespace" |
| 21 | + schema: [ |
| 22 | + // fill in your schema |
| 23 | + ] |
| 24 | + }, |
| 25 | + |
| 26 | + create (context) { |
| 27 | + let mutatedNodes = [] |
| 28 | + let props = [] |
| 29 | + |
| 30 | + function checkForMutations () { |
| 31 | + for (const prop of props) { |
| 32 | + const propName = utils.getStaticPropertyName(prop.key) |
| 33 | + |
| 34 | + for (const node of mutatedNodes) { |
| 35 | + if (propName === node.name) { |
| 36 | + context.report({ |
| 37 | + node: node.node, |
| 38 | + message: 'Unexpected mutation of "{{key}}" prop.', |
| 39 | + data: { key: node.name } |
| 40 | + }) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + mutatedNodes = [] |
| 45 | + } |
| 46 | + |
| 47 | + function checkTemplateProperty (node) { |
| 48 | + if (node.type === 'MemberExpression') { |
| 49 | + const expression = utils.parseMemberExpression(node) |
| 50 | + mutatedNodes.push({ |
| 51 | + name: expression[0] === 'this' ? expression[1] : expression[0], |
| 52 | + node |
| 53 | + }) |
| 54 | + } else if (node.type === 'Identifier') { |
| 55 | + mutatedNodes.push({ |
| 56 | + name: node.name, |
| 57 | + node |
| 58 | + }) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return Object.assign({}, |
| 63 | + { |
| 64 | + // this.xxx <=|+=|-=> |
| 65 | + 'AssignmentExpression' (node) { |
| 66 | + if (node.left.type !== 'MemberExpression') return |
| 67 | + const expression = utils.parseMemberExpression(node.left) |
| 68 | + if (expression[0] === 'this') { |
| 69 | + mutatedNodes.push({ |
| 70 | + name: expression[1], |
| 71 | + node |
| 72 | + }) |
| 73 | + } |
| 74 | + }, |
| 75 | + // this.xxx <++|--> |
| 76 | + 'UpdateExpression > MemberExpression' (node) { |
| 77 | + const expression = utils.parseMemberExpression(node) |
| 78 | + if (expression[0] === 'this') { |
| 79 | + mutatedNodes.push({ |
| 80 | + name: expression[1], |
| 81 | + node |
| 82 | + }) |
| 83 | + } |
| 84 | + }, |
| 85 | + // this.xxx.func() |
| 86 | + 'CallExpression' (node) { |
| 87 | + const expression = utils.parseMemberOrCallExpression(node) |
| 88 | + const code = expression.join('.').replace(/\.\[/g, '[') |
| 89 | + const MUTATION_REGEX = /(this.)((?!(concat|slice|map|filter)\().)[^\)]*((push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)\()/g |
| 90 | + |
| 91 | + if (MUTATION_REGEX.test(code)) { |
| 92 | + if (expression[0] === 'this') { |
| 93 | + mutatedNodes.push({ |
| 94 | + name: expression[1], |
| 95 | + node |
| 96 | + }) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + utils.executeOnVue(context, (obj) => { |
| 102 | + props = utils.getComponentProps(obj) |
| 103 | + .filter(cp => cp.key) |
| 104 | + checkForMutations() |
| 105 | + }), |
| 106 | + |
| 107 | + utils.defineTemplateBodyVisitor(context, { |
| 108 | + 'VExpressionContainer AssignmentExpression' (node) { |
| 109 | + checkTemplateProperty(node.left) |
| 110 | + }, |
| 111 | + // this.xxx <++|--> |
| 112 | + 'VExpressionContainer UpdateExpression' (node) { |
| 113 | + checkTemplateProperty(node.argument) |
| 114 | + }, |
| 115 | + // this.xxx.func() |
| 116 | + 'VExpressionContainer CallExpression' (node) { |
| 117 | + const expression = utils.parseMemberOrCallExpression(node) |
| 118 | + const code = expression.join('.').replace(/\.\[/g, '[') |
| 119 | + const MUTATION_REGEX = /(this.)?((?!(concat|slice|map|filter)\().)[^\)]*((push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)\()/g |
| 120 | + |
| 121 | + if (MUTATION_REGEX.test(code)) { |
| 122 | + mutatedNodes.push({ |
| 123 | + name: expression[0] === 'this' ? expression[1] : expression[0], |
| 124 | + node |
| 125 | + }) |
| 126 | + } |
| 127 | + }, |
| 128 | + |
| 129 | + "VAttribute[directive=true][key.name='model'] VExpressionContainer" (node) { |
| 130 | + checkTemplateProperty(node.expression) |
| 131 | + }, |
| 132 | + |
| 133 | + "VElement[name='template']:exit" () { |
| 134 | + checkForMutations() |
| 135 | + } |
| 136 | + }) |
| 137 | + ) |
| 138 | + } |
| 139 | +} |
0 commit comments