Skip to content

Added support for defineOptions to vue/multi-word-component-names rule #2157

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

Merged
merged 1 commit into from
May 12, 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
11 changes: 11 additions & 0 deletions lib/rules/multi-word-component-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ module.exports = {
hasName = true
validateName(node.value)
}),
utils.defineScriptSetupVisitor(context, {
onDefineOptionsEnter(node) {
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
hasName = true
validateName(nameNode.value)
}
}),
{
/** @param {Program} node */
'Program:exit'(node) {
Expand Down
111 changes: 76 additions & 35 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,10 @@ module.exports = {
* - `onDefinePropsExit` ... Event when defineProps visit ends.
* - `onDefineEmitsEnter` ... Event when defineEmits is found.
* - `onDefineEmitsExit` ... Event when defineEmits visit ends.
* - `onDefineOptionsEnter` ... Event when defineOptions is found.
* - `onDefineOptionsExit` ... Event when defineOptions visit ends.
* - `onDefineSlotsEnter` ... Event when defineSlots is found.
* - `onDefineSlotsExit` ... Event when defineSlots visit ends.
*
* @param {RuleContext} context The ESLint rule context object.
* @param {ScriptSetupVisitor} visitor The visitor to traverse the AST nodes.
Expand Down Expand Up @@ -1186,11 +1190,58 @@ module.exports = {
scriptSetupVisitor[key] = (node) => callVisitor(key, node)
}

const hasPropsEvent =
visitor.onDefinePropsEnter || visitor.onDefinePropsExit
const hasEmitsEvent =
visitor.onDefineEmitsEnter || visitor.onDefineEmitsExit
if (hasPropsEvent || hasEmitsEvent) {
class MacroListener {
/**
* @param {string} name
* @param {string} enterName
* @param {string} exitName
* @param {(candidateMacro: Expression | null, node: CallExpression) => boolean} isMacroNode
* @param {(context: RuleContext, node: CallExpression) => unknown} buildParam
*/
constructor(name, enterName, exitName, isMacroNode, buildParam) {
this.name = name
this.enterName = enterName
this.exitName = exitName
this.isMacroNode = isMacroNode
this.buildParam = buildParam
this.hasListener = Boolean(
visitor[this.enterName] || visitor[this.exitName]
)
this.paramsMap = new Map()
}
}
const macroListenerList = [
new MacroListener(
'defineProps',
'onDefinePropsEnter',
'onDefinePropsExit',
(candidateMacro, node) =>
candidateMacro === node || candidateMacro === getWithDefaults(node),
getComponentPropsFromDefineProps
),
new MacroListener(
'defineEmits',
'onDefineEmitsEnter',
'onDefineEmitsExit',
(candidateMacro, node) => candidateMacro === node,
getComponentEmitsFromDefineEmits
),
new MacroListener(
'defineOptions',
'onDefineOptionsEnter',
'onDefineOptionsExit',
(candidateMacro, node) => candidateMacro === node,
() => undefined
),
new MacroListener(
'defineSlots',
'onDefineSlotsEnter',
'onDefineSlotsExit',
(candidateMacro, node) => candidateMacro === node,
() => undefined
)
].filter((m) => m.hasListener)
if (macroListenerList.length > 0) {
/** @type {Expression | null} */
let candidateMacro = null
/** @param {VariableDeclarator|ExpressionStatement} node */
Expand All @@ -1213,8 +1264,6 @@ module.exports = {
candidateMacro = null
}
}
const definePropsMap = new Map()
const defineEmitsMap = new Map()
/**
* @param {CallExpression} node
*/
Expand All @@ -1224,40 +1273,32 @@ module.exports = {
inScriptSetup(node) &&
node.callee.type === 'Identifier'
) {
if (
hasPropsEvent &&
(candidateMacro === node ||
candidateMacro === getWithDefaults(node)) &&
node.callee.name === 'defineProps'
) {
/** @type {ComponentProp[]} */
const props = getComponentPropsFromDefineProps(context, node)

callVisitor('onDefinePropsEnter', node, props)
definePropsMap.set(node, props)
} else if (
hasEmitsEvent &&
candidateMacro === node &&
node.callee.name === 'defineEmits'
) {
/** @type {ComponentEmit[]} */
const emits = getComponentEmitsFromDefineEmits(context, node)

callVisitor('onDefineEmitsEnter', node, emits)
defineEmitsMap.set(node, emits)
for (const macroListener of macroListenerList) {
if (
node.callee.name !== macroListener.name ||
!macroListener.isMacroNode(candidateMacro, node)
) {
continue
}
const param = macroListener.buildParam(context, node)
callVisitor(macroListener.enterName, node, param)
macroListener.paramsMap.set(node, param)
break
}
}
callVisitor('CallExpression', node)
}
scriptSetupVisitor['CallExpression:exit'] = (node) => {
callVisitor('CallExpression:exit', node)
if (definePropsMap.has(node)) {
callVisitor('onDefinePropsExit', node, definePropsMap.get(node))
definePropsMap.delete(node)
}
if (defineEmitsMap.has(node)) {
callVisitor('onDefineEmitsExit', node, defineEmitsMap.get(node))
defineEmitsMap.delete(node)
for (const macroListener of macroListenerList) {
if (macroListener.paramsMap.has(node)) {
callVisitor(
macroListener.exitName,
node,
macroListener.paramsMap.get(node)
)
macroListener.paramsMap.delete(node)
}
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion tests/lib/rules/multi-word-component-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,18 @@ tester.run('multi-word-component-names', rule, {
<template>
<AppButton />
</template>

<script setup lang="ts">
import AppButton from "@/components/AppButton.vue";
</script>`
},
{
filename: 'Single.vue',
code: `
<script setup>
defineOptions({name: 'MultiWord'})
</script>
`
}
],
invalid: [
Expand Down Expand Up @@ -333,6 +341,20 @@ tester.run('multi-word-component-names', rule, {
line: 1
}
]
},
{
filename: 'MultiWord.vue',
code: `
<script setup>
defineOptions({name: 'Single'})
</script>
`,
errors: [
{
message: 'Component name "Single" should always be multi-word.',
line: 3
}
]
}
]
})
4 changes: 4 additions & 0 deletions typings/eslint-plugin-vue/util-types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export interface ScriptSetupVisitor extends ScriptSetupVisitorBase {
onDefinePropsExit?(node: CallExpression, props: ComponentProp[]): void
onDefineEmitsEnter?(node: CallExpression, emits: ComponentEmit[]): void
onDefineEmitsExit?(node: CallExpression, emits: ComponentEmit[]): void
onDefineOptionsEnter?(node: CallExpression): void
onDefineOptionsExit?(node: CallExpression): void
onDefineSlotsEnter?(node: CallExpression): void
onDefineSlotsExit?(node: CallExpression): void
[query: string]:
| ((node: VAST.ParamNode) => void)
| ((node: CallExpression, props: ComponentProp[]) => void)
Expand Down