|
| 1 | +/** |
| 2 | + * @author Flo Edelmann |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | +const { defineTemplateBodyVisitor } = require('../utils') |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {object} RuleOption |
| 10 | + * @property {string[]} additionalDirectives |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * @param {VNode | Token} node |
| 15 | + * @returns {boolean} |
| 16 | + */ |
| 17 | +function isWhiteSpaceTextNode(node) { |
| 18 | + return node.type === 'VText' && node.value.trim() === '' |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {Position} pos1 |
| 23 | + * @param {Position} pos2 |
| 24 | + * @returns {'less' | 'equal' | 'greater'} |
| 25 | + */ |
| 26 | +function comparePositions(pos1, pos2) { |
| 27 | + if ( |
| 28 | + pos1.line < pos2.line || |
| 29 | + (pos1.line === pos2.line && pos1.column < pos2.column) |
| 30 | + ) { |
| 31 | + return 'less' |
| 32 | + } |
| 33 | + |
| 34 | + if ( |
| 35 | + pos1.line > pos2.line || |
| 36 | + (pos1.line === pos2.line && pos1.column > pos2.column) |
| 37 | + ) { |
| 38 | + return 'greater' |
| 39 | + } |
| 40 | + |
| 41 | + return 'equal' |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @param {(VNode | Token)[]} nodes |
| 46 | + * @returns {SourceLocation | undefined} |
| 47 | + */ |
| 48 | +function getLocationRange(nodes) { |
| 49 | + /** @type {Position | undefined} */ |
| 50 | + let start |
| 51 | + /** @type {Position | undefined} */ |
| 52 | + let end |
| 53 | + |
| 54 | + for (const node of nodes) { |
| 55 | + if (!start || comparePositions(node.loc.start, start) === 'less') { |
| 56 | + start = node.loc.start |
| 57 | + } |
| 58 | + |
| 59 | + if (!end || comparePositions(node.loc.end, end) === 'greater') { |
| 60 | + end = node.loc.end |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (start === undefined || end === undefined) { |
| 65 | + return undefined |
| 66 | + } |
| 67 | + |
| 68 | + return { start, end } |
| 69 | +} |
| 70 | + |
| 71 | +// ------------------------------------------------------------------------------ |
| 72 | +// Rule Definition |
| 73 | +// ------------------------------------------------------------------------------ |
| 74 | + |
| 75 | +module.exports = { |
| 76 | + meta: { |
| 77 | + hasSuggestions: true, |
| 78 | + type: 'problem', |
| 79 | + docs: { |
| 80 | + description: |
| 81 | + "disallow element's child contents which would be overwritten by a directive like `v-html` or `v-text`", |
| 82 | + categories: undefined, |
| 83 | + url: 'https://eslint.vuejs.org/rules/no-child-content.html' |
| 84 | + }, |
| 85 | + fixable: null, |
| 86 | + schema: [ |
| 87 | + { |
| 88 | + type: 'object', |
| 89 | + additionalProperties: false, |
| 90 | + properties: { |
| 91 | + additionalDirectives: { |
| 92 | + type: 'array', |
| 93 | + uniqueItems: true, |
| 94 | + minItems: 1, |
| 95 | + items: { |
| 96 | + type: 'string' |
| 97 | + } |
| 98 | + } |
| 99 | + }, |
| 100 | + required: ['additionalDirectives'] |
| 101 | + } |
| 102 | + ] |
| 103 | + }, |
| 104 | + /** @param {RuleContext} context */ |
| 105 | + create(context) { |
| 106 | + const directives = new Set(['html', 'text']) |
| 107 | + |
| 108 | + /** @type {RuleOption | undefined} */ |
| 109 | + const option = context.options[0] |
| 110 | + if (option !== undefined) { |
| 111 | + for (const directive of option.additionalDirectives) { |
| 112 | + directives.add(directive) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return defineTemplateBodyVisitor(context, { |
| 117 | + /** @param {VDirective} directiveNode */ |
| 118 | + 'VAttribute[directive=true]'(directiveNode) { |
| 119 | + const directiveName = directiveNode.key.name.name |
| 120 | + const elementNode = directiveNode.parent.parent |
| 121 | + |
| 122 | + if (elementNode.endTag === null) { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + const tokenStore = context.parserServices.getTemplateBodyTokenStore() |
| 127 | + const elementComments = tokenStore.getTokensBetween( |
| 128 | + elementNode.startTag, |
| 129 | + elementNode.endTag, |
| 130 | + { |
| 131 | + includeComments: true, |
| 132 | + filter: (token) => token.type === 'HTMLComment' |
| 133 | + } |
| 134 | + ) |
| 135 | + |
| 136 | + const childNodes = [...elementNode.children, ...elementComments] |
| 137 | + |
| 138 | + if ( |
| 139 | + directives.has(directiveName) && |
| 140 | + childNodes.length > 0 && |
| 141 | + childNodes.some((childNode) => !isWhiteSpaceTextNode(childNode)) |
| 142 | + ) { |
| 143 | + context.report({ |
| 144 | + node: elementNode, |
| 145 | + loc: getLocationRange(childNodes), |
| 146 | + message: |
| 147 | + 'Child content is disallowed because it will be overwritten by the v-{{ directiveName }} directive.', |
| 148 | + data: { directiveName }, |
| 149 | + suggest: [ |
| 150 | + { |
| 151 | + desc: 'Remove child content.', |
| 152 | + *fix(fixer) { |
| 153 | + for (const childNode of childNodes) { |
| 154 | + yield fixer.remove(childNode) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + ] |
| 159 | + }) |
| 160 | + } |
| 161 | + } |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments