Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for defineOptions to vue/match-component-file-name rule #2161

Merged
merged 1 commit into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/rules/match-component-file-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ module.exports = {
}
}

return Object.assign(
{},
return utils.compositingVisitors(
utils.executeOnCallVueComponent(context, (node) => {
if (node.arguments.length === 2) {
const argument = node.arguments[0]
Expand All @@ -139,6 +138,18 @@ module.exports = {
if (!canVerify(node.value)) return
verifyName(node.value)
}),
utils.defineScriptSetupVisitor(context, {
onDefineOptionsEnter(node) {
componentCount++
if (node.arguments.length === 0) return
const define = node.arguments[0]
if (define.type !== 'ObjectExpression') return
const nameNode = utils.findProperty(define, 'name')
if (!nameNode) return
if (!canVerify(nameNode.value)) return
verifyName(nameNode.value)
}
}),
{
'Program:exit'() {
if (componentCount > 1) return
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/match-component-file-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,13 @@ ruleTester.run('match-component-file-name', rule, {
filename: 'test.jsx',
code: `fn1(component.data)`,
parserOptions
},
{
filename: 'MyComponent.vue',
code: `<script setup> defineOptions({name: 'MyComponent'}) </script>`,
options: [{ extensions: ['vue'] }],
parser: require.resolve('vue-eslint-parser'),
parserOptions
}
],

Expand Down Expand Up @@ -1081,6 +1088,25 @@ ruleTester.run('match-component-file-name', rule, {
]
}
]
},
{
filename: 'MyComponent.vue',
code: `<script setup> defineOptions({name: 'CoolComponent'}) </script>`,
options: [{ extensions: ['vue'] }],
parser: require.resolve('vue-eslint-parser'),
parserOptions,
errors: [
{
message:
'Component name `CoolComponent` should match file name `MyComponent`.',
suggestions: [
{
desc: 'Rename component to match file name.',
output: `<script setup> defineOptions({name: 'MyComponent'}) </script>`
}
]
}
]
}
]
})