|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 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 | + * @typedef {object} ParsedOption |
| 11 | + * @property { (block: VElement) => boolean } test |
| 12 | + * @property {string} [message] |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {string} str |
| 17 | + * @returns {(str: string) => boolean} |
| 18 | + */ |
| 19 | +function buildMatcher(str) { |
| 20 | + if (regexp.isRegExp(str)) { |
| 21 | + const re = regexp.toRegExp(str) |
| 22 | + return (s) => { |
| 23 | + re.lastIndex = 0 |
| 24 | + return re.test(s) |
| 25 | + } |
| 26 | + } |
| 27 | + return (s) => s === str |
| 28 | +} |
| 29 | +/** |
| 30 | + * @param {any} option |
| 31 | + * @returns {ParsedOption} |
| 32 | + */ |
| 33 | +function parseOption(option) { |
| 34 | + if (typeof option === 'string') { |
| 35 | + const matcher = buildMatcher(option) |
| 36 | + return { |
| 37 | + test(block) { |
| 38 | + return matcher(block.rawName) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + const parsed = parseOption(option.element) |
| 43 | + parsed.message = option.message |
| 44 | + return parsed |
| 45 | +} |
| 46 | + |
| 47 | +module.exports = { |
| 48 | + meta: { |
| 49 | + type: 'suggestion', |
| 50 | + docs: { |
| 51 | + description: 'disallow specific block', |
| 52 | + categories: undefined, |
| 53 | + url: 'https://eslint.vuejs.org/rules/no-restricted-block.html' |
| 54 | + }, |
| 55 | + fixable: null, |
| 56 | + schema: { |
| 57 | + type: 'array', |
| 58 | + items: { |
| 59 | + oneOf: [ |
| 60 | + { type: 'string' }, |
| 61 | + { |
| 62 | + type: 'object', |
| 63 | + properties: { |
| 64 | + element: { type: 'string' }, |
| 65 | + message: { type: 'string', minLength: 1 } |
| 66 | + }, |
| 67 | + required: ['element'], |
| 68 | + additionalProperties: false |
| 69 | + } |
| 70 | + ] |
| 71 | + }, |
| 72 | + uniqueItems: true, |
| 73 | + minItems: 0 |
| 74 | + }, |
| 75 | + |
| 76 | + messages: { |
| 77 | + // eslint-disable-next-line eslint-plugin/report-message-format |
| 78 | + restrictedBlock: '{{message}}' |
| 79 | + } |
| 80 | + }, |
| 81 | + /** @param {RuleContext} context */ |
| 82 | + create(context) { |
| 83 | + /** @type {ParsedOption[]} */ |
| 84 | + const options = context.options.map(parseOption) |
| 85 | + |
| 86 | + const documentFragment = |
| 87 | + context.parserServices.getDocumentFragment && |
| 88 | + context.parserServices.getDocumentFragment() |
| 89 | + |
| 90 | + function getTopLevelHTMLElements() { |
| 91 | + if (documentFragment) { |
| 92 | + return documentFragment.children.filter(utils.isVElement) |
| 93 | + } |
| 94 | + return [] |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + /** @param {Program} node */ |
| 99 | + Program(node) { |
| 100 | + if (utils.hasInvalidEOF(node)) { |
| 101 | + return |
| 102 | + } |
| 103 | + for (const block of getTopLevelHTMLElements()) { |
| 104 | + for (const option of options) { |
| 105 | + if (option.test(block)) { |
| 106 | + const message = option.message || defaultMessage(block) |
| 107 | + context.report({ |
| 108 | + node: block.startTag, |
| 109 | + messageId: 'restrictedBlock', |
| 110 | + data: { message } |
| 111 | + }) |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param {VElement} block |
| 121 | + */ |
| 122 | + function defaultMessage(block) { |
| 123 | + return `Using \`<${block.rawName}>\` is not allowed.` |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments