|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +// @ts-check |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const utils = require('../utils') |
| 9 | +const regexp = require('../utils/regexp') |
| 10 | + |
| 11 | +/** |
| 12 | + * @typedef {import('vue-eslint-parser').AST.VDirectiveKey} VDirectiveKey |
| 13 | + * @typedef {import('vue-eslint-parser').AST.VIdentifier} VIdentifier |
| 14 | + */ |
| 15 | +/** |
| 16 | + * @typedef {object} ParsedOption |
| 17 | + * @property { (key: VDirectiveKey) => boolean } test |
| 18 | + * @property {string[]} modifiers |
| 19 | + * @property {boolean} [useElement] |
| 20 | + * @property {string} [message] |
| 21 | + */ |
| 22 | + |
| 23 | +const DEFAULT_OPTIONS = [ |
| 24 | + { |
| 25 | + argument: '/^v-/', |
| 26 | + message: |
| 27 | + 'Using `:v-xxx` is not allowed. Instead, remove `:` and use it as directive.' |
| 28 | + } |
| 29 | +] |
| 30 | + |
| 31 | +/** |
| 32 | + * @param {string} str |
| 33 | + * @returns {(str: string) => boolean} |
| 34 | + */ |
| 35 | +function buildMatcher(str) { |
| 36 | + if (regexp.isRegExp(str)) { |
| 37 | + const re = regexp.toRegExp(str) |
| 38 | + return (s) => { |
| 39 | + re.lastIndex = 0 |
| 40 | + return re.test(s) |
| 41 | + } |
| 42 | + } |
| 43 | + return (s) => s === str |
| 44 | +} |
| 45 | +/** |
| 46 | + * @param {any} option |
| 47 | + * @returns {ParsedOption} |
| 48 | + */ |
| 49 | +function parseOption(option) { |
| 50 | + if (typeof option === 'string') { |
| 51 | + const matcher = buildMatcher(option) |
| 52 | + return { |
| 53 | + test(key) { |
| 54 | + return ( |
| 55 | + key.argument && |
| 56 | + key.argument.type === 'VIdentifier' && |
| 57 | + matcher(key.argument.rawName) |
| 58 | + ) |
| 59 | + }, |
| 60 | + modifiers: [] |
| 61 | + } |
| 62 | + } |
| 63 | + if (option === null) { |
| 64 | + return { |
| 65 | + test(key) { |
| 66 | + return key.argument === null |
| 67 | + }, |
| 68 | + modifiers: [] |
| 69 | + } |
| 70 | + } |
| 71 | + const parsed = parseOption(option.argument) |
| 72 | + if (option.modifiers) { |
| 73 | + const argTest = parsed.test |
| 74 | + parsed.test = (key) => { |
| 75 | + if (!argTest(key)) { |
| 76 | + return false |
| 77 | + } |
| 78 | + return option.modifiers.every((modName) => { |
| 79 | + return key.modifiers.some((mid) => mid.name === modName) |
| 80 | + }) |
| 81 | + } |
| 82 | + parsed.modifiers = option.modifiers |
| 83 | + } |
| 84 | + if (option.element) { |
| 85 | + const argTest = parsed.test |
| 86 | + const tagMatcher = buildMatcher(option.element) |
| 87 | + parsed.test = (key) => { |
| 88 | + if (!argTest(key)) { |
| 89 | + return false |
| 90 | + } |
| 91 | + const element = key.parent.parent.parent |
| 92 | + return tagMatcher(element.rawName) |
| 93 | + } |
| 94 | + parsed.useElement = true |
| 95 | + } |
| 96 | + parsed.message = option.message |
| 97 | + return parsed |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = { |
| 101 | + meta: { |
| 102 | + type: 'suggestion', |
| 103 | + docs: { |
| 104 | + description: 'disallow specific argument in `v-bind`', |
| 105 | + categories: undefined, |
| 106 | + url: 'https://eslint.vuejs.org/rules/no-restricted-v-bind.html' |
| 107 | + }, |
| 108 | + fixable: null, |
| 109 | + schema: { |
| 110 | + type: 'array', |
| 111 | + items: { |
| 112 | + oneOf: [ |
| 113 | + { type: ['string', 'null'] }, |
| 114 | + { |
| 115 | + type: 'object', |
| 116 | + properties: { |
| 117 | + argument: { type: ['string', 'null'] }, |
| 118 | + modifiers: { |
| 119 | + type: 'array', |
| 120 | + items: { |
| 121 | + type: 'string', |
| 122 | + enum: ['prop', 'camel', 'sync'] |
| 123 | + }, |
| 124 | + uniqueItems: true |
| 125 | + }, |
| 126 | + element: { type: 'string' }, |
| 127 | + message: { type: 'string', minLength: 1 } |
| 128 | + }, |
| 129 | + required: ['argument'], |
| 130 | + additionalProperties: false |
| 131 | + } |
| 132 | + ] |
| 133 | + }, |
| 134 | + uniqueItems: true, |
| 135 | + minItems: 0 |
| 136 | + }, |
| 137 | + |
| 138 | + messages: { |
| 139 | + // eslint-disable-next-line eslint-plugin/report-message-format |
| 140 | + restrictedVBind: '{{message}}' |
| 141 | + } |
| 142 | + }, |
| 143 | + create(context) { |
| 144 | + /** @type {ParsedOption[]} */ |
| 145 | + const options = (context.options.length === 0 |
| 146 | + ? DEFAULT_OPTIONS |
| 147 | + : context.options |
| 148 | + ).map(parseOption) |
| 149 | + |
| 150 | + return utils.defineTemplateBodyVisitor(context, { |
| 151 | + /** |
| 152 | + * @param {VDirectiveKey} node |
| 153 | + */ |
| 154 | + "VAttribute[directive=true][key.name.name='bind'] > VDirectiveKey"(node) { |
| 155 | + for (const option of options) { |
| 156 | + if (option.test(node)) { |
| 157 | + const message = option.message || defaultMessage(node, option) |
| 158 | + context.report({ |
| 159 | + node, |
| 160 | + messageId: 'restrictedVBind', |
| 161 | + data: { message } |
| 162 | + }) |
| 163 | + return |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + }) |
| 168 | + |
| 169 | + /** |
| 170 | + * @param {VDirectiveKey} key |
| 171 | + * @param {ParsedOption} option |
| 172 | + */ |
| 173 | + function defaultMessage(key, option) { |
| 174 | + const vbind = key.name.rawName === ':' ? '' : 'v-bind' |
| 175 | + const arg = |
| 176 | + key.argument != null && key.argument.type === 'VIdentifier' |
| 177 | + ? `:${key.argument.rawName}` |
| 178 | + : '' |
| 179 | + const mod = option.modifiers.length |
| 180 | + ? `.${option.modifiers.join('.')}` |
| 181 | + : '' |
| 182 | + let on = '' |
| 183 | + if (option.useElement) { |
| 184 | + on = ` on \`<${key.parent.parent.parent.rawName}>\`` |
| 185 | + } |
| 186 | + return `Using \`${vbind + arg + mod}\`${on} is not allowed.` |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments