|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | +const regexp = require('../utils/regexp') |
| 13 | +const casing = require('../utils/casing') |
| 14 | + |
| 15 | +/** |
| 16 | + * @typedef {import('vue-eslint-parser').AST.VAttribute} VAttribute |
| 17 | + * @typedef {import('vue-eslint-parser').AST.VDirective} VDirective |
| 18 | + * @typedef {import('vue-eslint-parser').AST.VElement} VElement |
| 19 | + * @typedef {import('vue-eslint-parser').AST.VIdentifier} VIdentifier |
| 20 | + * @typedef {import('vue-eslint-parser').AST.VExpressionContainer} VExpressionContainer |
| 21 | + * @typedef {import('vue-eslint-parser').AST.VText} VText |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * @typedef { { names: { [tagName in string]: Set<string> }, regexps: { name: RegExp, attrs: Set<string> }[], cache: { [tagName in string]: Set<string> } } } TargetAttrs |
| 26 | + * @typedef { { upper: ElementStack, name: string, attrs: Set<string> } } ElementStack |
| 27 | + */ |
| 28 | + |
| 29 | +// ------------------------------------------------------------------------------ |
| 30 | +// Constants |
| 31 | +// ------------------------------------------------------------------------------ |
| 32 | + |
| 33 | +// https://dev.w3.org/html5/html-author/charref |
| 34 | +const DEFAULT_WHITELIST = [ |
| 35 | + '(', |
| 36 | + ')', |
| 37 | + ',', |
| 38 | + '.', |
| 39 | + '&', |
| 40 | + '+', |
| 41 | + '-', |
| 42 | + '=', |
| 43 | + '*', |
| 44 | + '/', |
| 45 | + '#', |
| 46 | + '%', |
| 47 | + '!', |
| 48 | + '?', |
| 49 | + ':', |
| 50 | + '[', |
| 51 | + ']', |
| 52 | + '{', |
| 53 | + '}', |
| 54 | + '<', |
| 55 | + '>', |
| 56 | + '\u00b7', // "·" |
| 57 | + '\u2022', // "•" |
| 58 | + '\u2010', // "‐" |
| 59 | + '\u2013', // "–" |
| 60 | + '\u2014', // "—" |
| 61 | + '\u2212', // "−" |
| 62 | + '|' |
| 63 | +] |
| 64 | + |
| 65 | +const DEFAULT_ATTRIBUTES = { |
| 66 | + '/.+/': [ |
| 67 | + 'title', |
| 68 | + 'aria-label', |
| 69 | + 'aria-placeholder', |
| 70 | + 'aria-roledescription', |
| 71 | + 'aria-valuetext' |
| 72 | + ], |
| 73 | + input: ['placeholder'], |
| 74 | + img: ['alt'] |
| 75 | +} |
| 76 | + |
| 77 | +const DEFAULT_DIRECTIVES = ['v-text'] |
| 78 | + |
| 79 | +// -------------------------------------------------------------------------- |
| 80 | +// Helpers |
| 81 | +// -------------------------------------------------------------------------- |
| 82 | + |
| 83 | +/** |
| 84 | + * Parse attributes option |
| 85 | + * @returns {TargetAttrs} |
| 86 | + */ |
| 87 | +function parseTargetAttrs(options) { |
| 88 | + /** @type {TargetAttrs} */ |
| 89 | + const result = { names: {}, regexps: [], cache: {} } |
| 90 | + for (const tagName of Object.keys(options)) { |
| 91 | + /** @type { Set<string> } */ |
| 92 | + const attrs = new Set(options[tagName]) |
| 93 | + if (regexp.isRegExp(tagName)) { |
| 94 | + result.regexps.push({ |
| 95 | + name: regexp.toRegExp(tagName), |
| 96 | + attrs |
| 97 | + }) |
| 98 | + } else { |
| 99 | + result.names[tagName] = attrs |
| 100 | + } |
| 101 | + } |
| 102 | + return result |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Get a string from given expression container node |
| 107 | + * @param {VExpressionContainer} node |
| 108 | + * @returns { string | null } |
| 109 | + */ |
| 110 | +function getStringValue(value) { |
| 111 | + const expression = value.expression |
| 112 | + if (!expression) { |
| 113 | + return null |
| 114 | + } |
| 115 | + if (expression.type !== 'Literal') { |
| 116 | + return null |
| 117 | + } |
| 118 | + if (typeof expression.value === 'string') { |
| 119 | + return expression.value |
| 120 | + } |
| 121 | + return null |
| 122 | +} |
| 123 | + |
| 124 | +// ------------------------------------------------------------------------------ |
| 125 | +// Rule Definition |
| 126 | +// ------------------------------------------------------------------------------ |
| 127 | + |
| 128 | +module.exports = { |
| 129 | + meta: { |
| 130 | + type: 'suggestion', |
| 131 | + docs: { |
| 132 | + description: 'disallow the use of bare strings in `<template>`', |
| 133 | + categories: undefined, |
| 134 | + url: 'https://eslint.vuejs.org/rules/no-bare-strings-in-template.html' |
| 135 | + }, |
| 136 | + schema: [ |
| 137 | + { |
| 138 | + type: 'object', |
| 139 | + properties: { |
| 140 | + whitelist: { |
| 141 | + type: 'array', |
| 142 | + items: { type: 'string' }, |
| 143 | + uniqueItems: true |
| 144 | + }, |
| 145 | + attributes: { |
| 146 | + type: 'object', |
| 147 | + patternProperties: { |
| 148 | + '^(?:\\S+|/.*/[a-z]*)$': { |
| 149 | + type: 'array', |
| 150 | + items: { type: 'string' }, |
| 151 | + uniqueItems: true |
| 152 | + } |
| 153 | + }, |
| 154 | + additionalProperties: false |
| 155 | + }, |
| 156 | + directives: { |
| 157 | + type: 'array', |
| 158 | + items: { type: 'string', pattern: '^v-' }, |
| 159 | + uniqueItems: true |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + ], |
| 164 | + messages: { |
| 165 | + unexpected: 'Unexpected non-translated string used.', |
| 166 | + unexpectedInAttr: 'Unexpected non-translated string used in `{{attr}}`.' |
| 167 | + } |
| 168 | + }, |
| 169 | + create(context) { |
| 170 | + const opts = context.options[0] || {} |
| 171 | + const whitelist = opts.whitelist || DEFAULT_WHITELIST |
| 172 | + const attributes = parseTargetAttrs(opts.attributes || DEFAULT_ATTRIBUTES) |
| 173 | + const directives = opts.directives || DEFAULT_DIRECTIVES |
| 174 | + |
| 175 | + const whitelistRe = new RegExp( |
| 176 | + whitelist.map((w) => regexp.escape(w)).join('|'), |
| 177 | + 'gu' |
| 178 | + ) |
| 179 | + |
| 180 | + /** @type {ElementStack | null} */ |
| 181 | + let elementStack = null |
| 182 | + /** |
| 183 | + * Gets the bare string from given string |
| 184 | + * @param {string} str |
| 185 | + */ |
| 186 | + function getBareString(str) { |
| 187 | + return str.trim().replace(whitelistRe, '').trim() |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Get the attribute to be verified from the element name. |
| 192 | + * @param {string} tagName |
| 193 | + * @returns {Set<string>} |
| 194 | + */ |
| 195 | + function getTargetAttrs(tagName) { |
| 196 | + if (attributes.cache[tagName]) { |
| 197 | + return attributes.cache[tagName] |
| 198 | + } |
| 199 | + /** @type {string[]} */ |
| 200 | + const result = [] |
| 201 | + if (attributes.names[tagName]) { |
| 202 | + result.push(...attributes.names[tagName]) |
| 203 | + } |
| 204 | + for (const { name, attrs } of attributes.regexps) { |
| 205 | + name.lastIndex = 0 |
| 206 | + if (name.test(tagName)) { |
| 207 | + result.push(...attrs) |
| 208 | + } |
| 209 | + } |
| 210 | + if (casing.isKebabCase(tagName)) { |
| 211 | + result.push(...getTargetAttrs(casing.pascalCase(tagName))) |
| 212 | + } |
| 213 | + |
| 214 | + return (attributes.cache[tagName] = new Set(result)) |
| 215 | + } |
| 216 | + |
| 217 | + return utils.defineTemplateBodyVisitor(context, { |
| 218 | + /** @param {VText} node */ |
| 219 | + VText(node) { |
| 220 | + if (getBareString(node.value)) { |
| 221 | + context.report({ |
| 222 | + node, |
| 223 | + messageId: 'unexpected' |
| 224 | + }) |
| 225 | + } |
| 226 | + }, |
| 227 | + /** |
| 228 | + * @param {VElement} node |
| 229 | + */ |
| 230 | + VElement(node) { |
| 231 | + elementStack = { |
| 232 | + upper: elementStack, |
| 233 | + name: node.rawName, |
| 234 | + attrs: getTargetAttrs(node.rawName) |
| 235 | + } |
| 236 | + }, |
| 237 | + 'VElement:exit'() { |
| 238 | + elementStack = elementStack.upper |
| 239 | + }, |
| 240 | + /** @param {VAttribute|VDirective} node */ |
| 241 | + VAttribute(node) { |
| 242 | + if (!node.value) { |
| 243 | + return |
| 244 | + } |
| 245 | + if (node.directive === false) { |
| 246 | + const attrs = elementStack.attrs |
| 247 | + if (!attrs.has(node.key.rawName)) { |
| 248 | + return |
| 249 | + } |
| 250 | + |
| 251 | + if (getBareString(node.value.value)) { |
| 252 | + context.report({ |
| 253 | + node: node.value, |
| 254 | + messageId: 'unexpectedInAttr', |
| 255 | + data: { |
| 256 | + attr: node.key.rawName |
| 257 | + } |
| 258 | + }) |
| 259 | + } |
| 260 | + } else { |
| 261 | + const directive = `v-${node.key.name.name}` |
| 262 | + if (!directives.includes(directive)) { |
| 263 | + return |
| 264 | + } |
| 265 | + const str = getStringValue(node.value) |
| 266 | + if (str && getBareString(str)) { |
| 267 | + context.report({ |
| 268 | + node: node.value, |
| 269 | + messageId: 'unexpectedInAttr', |
| 270 | + data: { |
| 271 | + attr: directive |
| 272 | + } |
| 273 | + }) |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + }) |
| 278 | + } |
| 279 | +} |
0 commit comments