Skip to content

Commit ed320a7

Browse files
committed
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
1 parent 5ec36ce commit ed320a7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/rules/no-v-html.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,37 @@ module.exports = {
3636
? new RegExp(options.ignorePattern, 'u')
3737
: undefined
3838

39+
/**
40+
* Check if the expression matches the ignore pattern
41+
* @param {VExpressionContainer['expression']} expression
42+
* @param {SourceCode} sourceCode
43+
* @returns {boolean}
44+
*/
45+
function shouldIgnore(expression, sourceCode) {
46+
if (!ignoreRegEx || !expression) {
47+
return false
48+
}
49+
50+
// For simple identifiers, use the name property directly (optimized)
51+
if (expression.type === 'Identifier') {
52+
return ignoreRegEx.test(expression.name)
53+
}
54+
55+
// For other expression types (e.g., CallExpression), get the full text
56+
const expressionText = sourceCode.getText(expression)
57+
return ignoreRegEx.test(expressionText)
58+
}
59+
3960
return utils.defineTemplateBodyVisitor(context, {
4061
/** @param {VDirective} node */
4162
"VAttribute[directive=true][key.name.name='html']"(node) {
63+
const sourceCode = context.sourceCode
64+
4265
if (
43-
ignoreRegEx &&
4466
node.value &&
4567
node.value.expression &&
46-
node.value.expression.type === 'Identifier' &&
47-
ignoreRegEx.test(node.value.expression.name)
68+
sourceCode &&
69+
shouldIgnore(node.value.expression, sourceCode)
4870
) {
4971
return
5072
}

tests/lib/rules/no-v-html.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ ruleTester.run('no-v-html', rule, {
3333
filename: 'test.vue',
3434
code: '<template><div v-html="htmlKnownToBeSafe"></div></template>',
3535
options: [{ ignorePattern: '^html' }]
36+
},
37+
{
38+
filename: 'test.vue',
39+
code: '<template><div v-html="$sanitize(test)"></div></template>',
40+
options: [{ ignorePattern: String.raw`^\$sanitize\(` }]
3641
}
3742
],
3843
invalid: [

0 commit comments

Comments
 (0)