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