|
| 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 | + |
| 9 | +/** |
| 10 | + * @typedef {import('eslint').Rule.RuleContext} RuleContext |
| 11 | + * @typedef {import('vue-eslint-parser').AST.VExpressionContainer} VExpressionContainer |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * Strip quotes string |
| 16 | + * @param {string} text |
| 17 | + * @returns {string} |
| 18 | + */ |
| 19 | +function stripQuotesForHTML(text) { |
| 20 | + if ( |
| 21 | + (text[0] === '"' || text[0] === "'" || text[0] === '`') && |
| 22 | + text[0] === text[text.length - 1] |
| 23 | + ) { |
| 24 | + return text.slice(1, -1) |
| 25 | + } |
| 26 | + |
| 27 | + const re = /^(?:&(?:quot|apos|#\d+|#x[\da-f]+);|["'`])([\s\S]*)(?:&(?:quot|apos|#\d+|#x[\da-f]+);|["'`])$/u.exec( |
| 28 | + text |
| 29 | + ) |
| 30 | + if (!re) { |
| 31 | + return null |
| 32 | + } |
| 33 | + return re[1] |
| 34 | +} |
| 35 | + |
| 36 | +module.exports = { |
| 37 | + meta: { |
| 38 | + docs: { |
| 39 | + description: 'disallow unnecessary mustache interpolations', |
| 40 | + categories: undefined, |
| 41 | + url: 'https://eslint.vuejs.org/rules/no-useless-mustaches.html' |
| 42 | + }, |
| 43 | + fixable: 'code', |
| 44 | + messages: { |
| 45 | + unexpected: |
| 46 | + 'Unexpected mustache interpolation with a string literal value.' |
| 47 | + }, |
| 48 | + schema: [ |
| 49 | + { |
| 50 | + type: 'object', |
| 51 | + properties: { |
| 52 | + ignoreIncludesComment: { |
| 53 | + type: 'boolean' |
| 54 | + }, |
| 55 | + ignoreStringEscape: { |
| 56 | + type: 'boolean' |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + ], |
| 61 | + type: 'suggestion' |
| 62 | + }, |
| 63 | + /** @param {RuleContext} context */ |
| 64 | + create(context) { |
| 65 | + const opts = context.options[0] || {} |
| 66 | + const ignoreIncludesComment = opts.ignoreIncludesComment |
| 67 | + const ignoreStringEscape = opts.ignoreStringEscape |
| 68 | + const sourceCode = context.getSourceCode() |
| 69 | + |
| 70 | + /** |
| 71 | + * Report if the value expression is string literals |
| 72 | + * @param {VExpressionContainer} node the node to check |
| 73 | + */ |
| 74 | + function verify(node) { |
| 75 | + const { expression } = node |
| 76 | + if (!expression) { |
| 77 | + return |
| 78 | + } |
| 79 | + let strValue, rawValue |
| 80 | + if (expression.type === 'Literal') { |
| 81 | + if (typeof expression.value !== 'string') { |
| 82 | + return |
| 83 | + } |
| 84 | + strValue = expression.value |
| 85 | + rawValue = expression.raw.slice(1, -1) |
| 86 | + } else if (expression.type === 'TemplateLiteral') { |
| 87 | + if (expression.expressions.length > 0) { |
| 88 | + return |
| 89 | + } |
| 90 | + strValue = expression.quasis[0].value.cooked |
| 91 | + rawValue = expression.quasis[0].value.raw |
| 92 | + } else { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + const tokenStore = context.parserServices.getTemplateBodyTokenStore() |
| 97 | + const hasComment = tokenStore |
| 98 | + .getTokens(node, { includeComments: true }) |
| 99 | + .some((t) => t.type === 'Block' || t.type === 'Line') |
| 100 | + if (ignoreIncludesComment && hasComment) { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + let hasEscape = false |
| 105 | + if (rawValue !== strValue) { |
| 106 | + // check escapes |
| 107 | + const chars = [...rawValue] |
| 108 | + let c = chars.shift() |
| 109 | + while (c) { |
| 110 | + if (c === '\\') { |
| 111 | + c = chars.shift() |
| 112 | + if ( |
| 113 | + c == null || |
| 114 | + // ignore "\\", '"', "'", "`" and "$" |
| 115 | + 'nrvtbfux'.includes(c) |
| 116 | + ) { |
| 117 | + // has useful escape. |
| 118 | + hasEscape = true |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + c = chars.shift() |
| 123 | + } |
| 124 | + } |
| 125 | + if (ignoreStringEscape && hasEscape) { |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + context.report({ |
| 130 | + // @ts-ignore |
| 131 | + node, |
| 132 | + messageId: 'unexpected', |
| 133 | + fix(fixer) { |
| 134 | + if (hasComment || hasEscape) { |
| 135 | + // cannot fix |
| 136 | + return null |
| 137 | + } |
| 138 | + context.parserServices.getDocumentFragment() |
| 139 | + const text = stripQuotesForHTML(sourceCode.getText(expression)) |
| 140 | + if (text == null) { |
| 141 | + // unknowns |
| 142 | + return null |
| 143 | + } |
| 144 | + if (text.includes('\n') || /^\s|\s$/u.test(text)) { |
| 145 | + // It doesn't autofix because another rule like indent or eol space might remove spaces. |
| 146 | + return null |
| 147 | + } |
| 148 | + |
| 149 | + return [fixer.replaceText(node, text.replace(/\\([\s\S])/g, '$1'))] |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | + |
| 154 | + return utils.defineTemplateBodyVisitor(context, { |
| 155 | + 'VElement > VExpressionContainer': verify |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
0 commit comments