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