From b9ebc7e84752fb6413f06fdbbecdedc532086587 Mon Sep 17 00:00:00 2001 From: kzhrk Date: Sat, 8 Nov 2025 14:52:42 +0900 Subject: [PATCH] fix(no-v-html): add CallExpression support to ignorePattern option (#2949) This commit extends the `ignorePattern` option to support function call expressions like `$sanitize(test)`, not just simple variable identifiers like `htmlSafe`. Changes: - Add new `shouldIgnore()` helper function that handles both Identifier and CallExpression expression types - For Identifiers, use the `name` property directly for optimal performance - For CallExpressions and other expression types, use `sourceCode.getText()` to get the full expression text - Update the visitor to pass `sourceCode` to the new helper function This allows users to configure patterns like `^\$sanitize\(` to match function call expressions where the function name matches the pattern. Tests: - Add test case for CallExpression matching with ignorePattern `^\$sanitize\(` - All existing tests continue to pass --- .changeset/add-callexpression-support.md | 5 +++++ lib/rules/no-v-html.js | 28 +++++++++++++++++++++--- tests/lib/rules/no-v-html.js | 5 +++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .changeset/add-callexpression-support.md 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: [