diff --git a/.changeset/add-callexpression-support.md b/.changeset/add-callexpression-support.md new file mode 100644 index 000000000..22c748649 --- /dev/null +++ b/.changeset/add-callexpression-support.md @@ -0,0 +1,5 @@ +--- +'eslint-plugin-vue': patch +--- + +fix(no-v-html): add CallExpression support to ignorePattern option (#2949) diff --git a/lib/rules/no-v-html.js b/lib/rules/no-v-html.js index 512cf4c71..7690ba60b 100644 --- a/lib/rules/no-v-html.js +++ b/lib/rules/no-v-html.js @@ -36,15 +36,37 @@ module.exports = { ? new RegExp(options.ignorePattern, 'u') : undefined + /** + * Check if the expression matches the ignore pattern + * @param {VExpressionContainer['expression']} expression + * @param {SourceCode} sourceCode + * @returns {boolean} + */ + function shouldIgnore(expression, sourceCode) { + if (!ignoreRegEx || !expression) { + return false + } + + // For simple identifiers, use the name property directly (optimized) + if (expression.type === 'Identifier') { + return ignoreRegEx.test(expression.name) + } + + // For other expression types (e.g., CallExpression), get the full text + const expressionText = sourceCode.getText(expression) + return ignoreRegEx.test(expressionText) + } + return utils.defineTemplateBodyVisitor(context, { /** @param {VDirective} node */ "VAttribute[directive=true][key.name.name='html']"(node) { + const sourceCode = context.sourceCode + if ( - ignoreRegEx && node.value && node.value.expression && - node.value.expression.type === 'Identifier' && - ignoreRegEx.test(node.value.expression.name) + sourceCode && + shouldIgnore(node.value.expression, sourceCode) ) { return } diff --git a/tests/lib/rules/no-v-html.js b/tests/lib/rules/no-v-html.js index 44d3a79c5..16b606a63 100644 --- a/tests/lib/rules/no-v-html.js +++ b/tests/lib/rules/no-v-html.js @@ -33,6 +33,11 @@ ruleTester.run('no-v-html', rule, { filename: 'test.vue', code: '', options: [{ ignorePattern: '^html' }] + }, + { + filename: 'test.vue', + code: '', + options: [{ ignorePattern: String.raw`^\$sanitize\(` }] } ], invalid: [