|
| 1 | +/** |
| 2 | + * @fileoverview Prevent overwrite reserved keys |
| 3 | + * @author Armano |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const assert = require('assert') |
| 9 | + |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | +// Rule Definition |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +const RESERVER_NAMES = new Set(require('../utils/vue-reserved.json')) |
| 15 | +const SCOPE_NAMES = new Set(['props', 'computed', 'data', 'methods']) |
| 16 | + |
| 17 | +function create (context) { |
| 18 | + const reservedNames = RESERVER_NAMES |
| 19 | + const scopeNames = SCOPE_NAMES |
| 20 | + |
| 21 | + const options = context.options[0] || {} |
| 22 | + if (options.reserved) { |
| 23 | + options.reserved.forEach(name => { |
| 24 | + reservedNames.add(name) |
| 25 | + }) |
| 26 | + } |
| 27 | + if (options.scope) { |
| 28 | + options.scope.forEach(name => { |
| 29 | + scopeNames.add(name) |
| 30 | + }) |
| 31 | + } |
| 32 | + |
| 33 | + // ---------------------------------------------------------------------- |
| 34 | + // Helpers |
| 35 | + // ---------------------------------------------------------------------- |
| 36 | + |
| 37 | + function checkUsedNames (name, node, groupName) { |
| 38 | + assert(typeof name === 'string') |
| 39 | + |
| 40 | + if (groupName === 'data' && name.substr(0, 1) === '_') { |
| 41 | + context.report({ |
| 42 | + node: node, |
| 43 | + message: "Field '{{name}}' which start with _ in data is reserved.", |
| 44 | + data: { |
| 45 | + name |
| 46 | + } |
| 47 | + }) |
| 48 | + } else if (reservedNames.has(name)) { |
| 49 | + context.report({ |
| 50 | + node: node, |
| 51 | + message: "Reserved key '{{name}}'.", |
| 52 | + data: { |
| 53 | + name |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + function checkArrayExpression (node, groupName) { |
| 60 | + node.elements.forEach(item => { |
| 61 | + const name = utils.getStaticPropertyName(item) |
| 62 | + if (name) { |
| 63 | + checkUsedNames(name, item, groupName) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + function checkObjectExpression (node, groupName) { |
| 69 | + node.properties.forEach(item => { |
| 70 | + const name = utils.getStaticPropertyName(item) |
| 71 | + if (name) { |
| 72 | + checkUsedNames(name, item.key, groupName) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + function checkFunctionExpression (node, groupName) { |
| 78 | + if (node.body.type === 'BlockStatement') { |
| 79 | + node.body.body.forEach(item => { |
| 80 | + if (item.type === 'ReturnStatement' && item.argument.type === 'ObjectExpression') { |
| 81 | + checkObjectExpression(item.argument, groupName) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // ---------------------------------------------------------------------- |
| 88 | + // Public |
| 89 | + // ---------------------------------------------------------------------- |
| 90 | + |
| 91 | + return utils.executeOnVueComponent(context, (obj) => { |
| 92 | + obj.properties |
| 93 | + .filter(p => p.type === 'Property' && p.key.type === 'Identifier' && scopeNames.has(p.key.name)) |
| 94 | + .forEach(node => { |
| 95 | + if (node.value.type === 'ArrayExpression') { |
| 96 | + checkArrayExpression(node.value, node.key.name) |
| 97 | + } else if (node.value.type === 'ObjectExpression') { |
| 98 | + checkObjectExpression(node.value, node.key.name) |
| 99 | + } else if (node.value.type === 'FunctionExpression') { |
| 100 | + checkFunctionExpression(node.value, node.key.name) |
| 101 | + } |
| 102 | + }) |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +module.exports = { |
| 107 | + meta: { |
| 108 | + docs: { |
| 109 | + description: 'Prevent overwrite reserved keys', |
| 110 | + category: 'Fill me in', |
| 111 | + recommended: false |
| 112 | + }, |
| 113 | + fixable: null, // or "code" or "whitespace" |
| 114 | + schema: [ |
| 115 | + { |
| 116 | + type: 'object', |
| 117 | + properties: { |
| 118 | + reserved: { |
| 119 | + type: 'array' |
| 120 | + }, |
| 121 | + scope: { |
| 122 | + type: 'array' |
| 123 | + } |
| 124 | + }, |
| 125 | + additionalProperties: false |
| 126 | + } |
| 127 | + ] |
| 128 | + }, |
| 129 | + |
| 130 | + create |
| 131 | +} |
0 commit comments