Skip to content

Commit d523648

Browse files
25867405552586740555
authored andcommitted
feat: Add allowVariableNamePattern to valid-define-options
1 parent 305100d commit d523648

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

docs/rules/valid-define-options.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,18 @@ defineOptions({ props: { msg: String } })
108108

109109
## :wrench: Options
110110

111-
Nothing.
111+
```json
112+
{
113+
"vue/valid-define-options": [
114+
"error",
115+
{
116+
"allowVariableNamePattern": "^[A-Z_]+$"
117+
}
118+
]
119+
}
120+
```
121+
122+
- `allowVariableNamePattern` Allow reference to declared variables if variable name matches patterns, default `null`.
112123

113124
## :couple: Related Rules
114125

lib/rules/valid-define-options.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ module.exports = {
1616
url: 'https://eslint.vuejs.org/rules/valid-define-options.html'
1717
},
1818
fixable: null,
19-
schema: [],
19+
schema: [
20+
{
21+
type: 'object',
22+
properties: {
23+
allowVariableNamePattern: {
24+
type: 'string'
25+
}
26+
},
27+
additionalProperties: false
28+
}
29+
],
2030
messages: {
2131
referencingLocally:
2232
'`defineOptions` is referencing locally declared variables.',
@@ -29,6 +39,13 @@ module.exports = {
2939
},
3040
/** @param {RuleContext} context */
3141
create(context) {
42+
const option = context.options[0] || {}
43+
const allowVariableNamePattern = option.allowVariableNamePattern
44+
/** @type {RegExp | null} */
45+
let allowVariableNamePatternRegEx = null
46+
if (allowVariableNamePattern) {
47+
allowVariableNamePatternRegEx = new RegExp(allowVariableNamePattern, 'u')
48+
}
3249
const scriptSetup = utils.getScriptSetupElement(context)
3350
if (!scriptSetup) {
3451
return {}
@@ -93,7 +110,9 @@ module.exports = {
93110
(def) =>
94111
def.type !== 'ImportBinding' &&
95112
utils.inRange(scriptSetup.range, def.name) &&
96-
!utils.inRange(defineOptions.range, def.name)
113+
!utils.inRange(defineOptions.range, def.name) &&
114+
(allowVariableNamePatternRegEx === null ||
115+
!allowVariableNamePatternRegEx.test(variable.name))
97116
)
98117
) {
99118
if (utils.withinTypeNode(node)) {

tests/lib/rules/valid-define-options.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ tester.run('valid-define-options', rule, {
7474
7575
defineOptions(def);
7676
</script>`
77+
},
78+
{
79+
filename: 'test.vue',
80+
code: `
81+
<script setup>
82+
const COMPONENT_NAME = 'TestComponent'
83+
defineOptions({ name: COMPONENT_NAME })
84+
</script>`,
85+
options: [{ allowVariableNamePattern: '^[A-Z_]+$' }]
7786
}
7887
],
7988
invalid: [
@@ -205,6 +214,21 @@ tester.run('valid-define-options', rule, {
205214
line: 3
206215
}
207216
]
217+
},
218+
{
219+
filename: 'test.vue',
220+
code: `
221+
<script setup>
222+
const componentName = "TestComponent"
223+
defineOptions({ name: componentName });
224+
</script>`,
225+
options: [{ allowVariableNamePattern: '^[A-Z_]+$' }],
226+
errors: [
227+
{
228+
message: '`defineOptions` is referencing locally declared variables.',
229+
line: 4
230+
}
231+
]
208232
}
209233
]
210234
})

0 commit comments

Comments
 (0)