123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 'use strict'
- const utils = require('../utils')
- const regexp = require('../utils/regexp')
- function buildMatcher(str) {
- if (regexp.isRegExp(str)) {
- const re = regexp.toRegExp(str)
- return (s) => {
- re.lastIndex = 0
- return re.test(s)
- }
- }
- return (s) => s === str
- }
- function parseOption(option) {
- if (typeof option === 'string') {
- const matcher = buildMatcher(option)
- return {
- test(block) {
- return matcher(block.rawName)
- }
- }
- }
- const parsed = parseOption(option.element)
- parsed.message = option.message
- return parsed
- }
- module.exports = {
- meta: {
- type: 'suggestion',
- docs: {
- description: 'disallow specific block',
- categories: undefined,
- url: 'https://eslint.vuejs.org/rules/no-restricted-block.html'
- },
- fixable: null,
- schema: {
- type: 'array',
- items: {
- oneOf: [
- { type: 'string' },
- {
- type: 'object',
- properties: {
- element: { type: 'string' },
- message: { type: 'string', minLength: 1 }
- },
- required: ['element'],
- additionalProperties: false
- }
- ]
- },
- uniqueItems: true,
- minItems: 0
- },
- messages: {
-
- restrictedBlock: '{{message}}'
- }
- },
-
- create(context) {
-
- const options = context.options.map(parseOption)
- const documentFragment =
- context.parserServices.getDocumentFragment &&
- context.parserServices.getDocumentFragment()
- function getTopLevelHTMLElements() {
- if (documentFragment) {
- return documentFragment.children.filter(utils.isVElement)
- }
- return []
- }
- return {
-
- Program(node) {
- if (utils.hasInvalidEOF(node)) {
- return
- }
- for (const block of getTopLevelHTMLElements()) {
- for (const option of options) {
- if (option.test(block)) {
- const message = option.message || defaultMessage(block)
- context.report({
- node: block.startTag,
- messageId: 'restrictedBlock',
- data: { message }
- })
- break
- }
- }
- }
- }
- }
-
- function defaultMessage(block) {
- return `Using \`<${block.rawName}>\` is not allowed.`
- }
- }
- }
|