Skip to content

Commit 6037a9f

Browse files
committed
Exclude with dynamic arguments from core rule wrapper validation target
1 parent c6c4927 commit 6037a9f

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/rules/array-bracket-spacing.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55

66
const { wrapCoreRule } = require('../utils')
77

8-
// eslint-disable-next-line
9-
module.exports = wrapCoreRule(require('eslint/lib/rules/array-bracket-spacing'))
8+
// eslint-disable-next-line no-invalid-meta
9+
module.exports = wrapCoreRule(
10+
require('eslint/lib/rules/array-bracket-spacing'),
11+
{ skipDynamicArguments: true }
12+
)

lib/utils/index.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ module.exports = {
6565
/**
6666
* Wrap a given core rule to apply it to Vue.js template.
6767
* @param {Rule} coreRule The core rule implementation to wrap.
68-
* @param {string|undefined} category The category of this rule.
68+
* @param {Object|undefined} options The option of this rule.
69+
* @param {string|undefined} options.category The category of this rule.
70+
* @param {boolean|undefined} options.skipDynamicArguments If `true`, skip validation within dynamic arguments.
6971
* @returns {Rule} The wrapped rule implementation.
7072
*/
71-
wrapCoreRule (coreRule, category) {
73+
wrapCoreRule (coreRule, options) {
74+
const { category, skipDynamicArguments } = options || {}
7275
return {
7376
create (context) {
7477
const tokenStore =
@@ -92,6 +95,19 @@ module.exports = {
9295
delete handlers['Program:exit']
9396
}
9497

98+
if (skipDynamicArguments) {
99+
let withinDynamicArguments = false
100+
for (const name of Object.keys(handlers)) {
101+
const original = handlers[name]
102+
handlers[name] = (...args) => {
103+
if (withinDynamicArguments) return
104+
original(...args)
105+
}
106+
}
107+
handlers['VDirectiveKey > VExpressionContainer'] = () => { withinDynamicArguments = true }
108+
handlers['VDirectiveKey > VExpressionContainer:exit'] = () => { withinDynamicArguments = false }
109+
}
110+
95111
// Apply the handlers to templates.
96112
return module.exports.defineTemplateBodyVisitor(context, handlers)
97113
},

tests/lib/rules/array-bracket-spacing.js

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ tester.run('array-bracket-spacing', rule, {
2121
{
2222
code: '<template><div :attr="[ a ]" /></template>',
2323
options: ['always']
24+
},
25+
'<template><div :[attr]="a" /></template>',
26+
{
27+
code: '<template><div :[attr]="a" /></template>',
28+
options: ['always']
29+
},
30+
'<template><div :[[attr]]="a" /></template>',
31+
{
32+
code: '<template><div :[[attr]]="a" /></template>',
33+
options: ['always']
2434
}
2535
],
2636
invalid: [

0 commit comments

Comments
 (0)