Skip to content

Commit

Permalink
Fix crash for TSFunctionType with type-literal option in `vue/defin…
Browse files Browse the repository at this point in the history
…e-emits-declaration` rule (#2336)
  • Loading branch information
ota-meshi authored Nov 30, 2023
1 parent 73ee48f commit 9fe8b34
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
51 changes: 32 additions & 19 deletions lib/rules/define-emits-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const utils = require('../utils')

/**
* @typedef {import('@typescript-eslint/types').TSESTree.TSTypeLiteral} TSTypeLiteral
* @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode
*
*/

Expand Down Expand Up @@ -54,24 +54,7 @@ module.exports = {
}

case 'type-literal': {
if (node.arguments.length > 0) {
context.report({
node,
messageId: 'hasArg'
})
return
}

const typeArguments = node.typeArguments || node.typeParameters
const param = /** @type {TSTypeLiteral} */ (typeArguments.params[0])
for (const memberNode of param.members) {
if (memberNode.type !== 'TSPropertySignature') {
context.report({
node: memberNode,
messageId: 'hasTypeCallArg'
})
}
}
verifyTypeLiteral(node)
break
}

Expand All @@ -89,5 +72,35 @@ module.exports = {
}
}
})

/** @param {CallExpression} node */
function verifyTypeLiteral(node) {
if (node.arguments.length > 0) {
context.report({
node,
messageId: 'hasArg'
})
return
}

const typeArguments = node.typeArguments || node.typeParameters
const param = /** @type {TypeNode|undefined} */ (typeArguments?.params[0])
if (!param) return
if (param.type === 'TSTypeLiteral') {
for (const memberNode of param.members) {
if (memberNode.type !== 'TSPropertySignature') {
context.report({
node: memberNode,
messageId: 'hasTypeCallArg'
})
}
}
} else if (param.type === 'TSFunctionType') {
context.report({
node: param,
messageId: 'hasTypeCallArg'
})
}
}
}
}
19 changes: 19 additions & 0 deletions tests/lib/rules/define-emits-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,25 @@ tester.run('define-emits-declaration', rule, {
line: 5
}
]
},
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const emit = defineEmits<(e: 'change', id: number) => void>()
</script>
`,
options: ['type-literal'],
parserOptions: {
parser: require.resolve('@typescript-eslint/parser')
},
errors: [
{
message:
'Use new type literal declaration instead of the old call signature declaration.',
line: 3
}
]
}
]
})
4 changes: 2 additions & 2 deletions typings/eslint-plugin-vue/util-types/ast/es-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export interface CallExpression extends HasParentNode {
typeArguments?: TS.TSTypeParameterInstantiation

/* @deprecated */
typeParameters: never
typeParameters?: never
}
export interface Super extends HasParentNode {
type: 'Super'
Expand All @@ -534,7 +534,7 @@ export interface NewExpression extends HasParentNode {
typeArguments?: TSTypeParameterInstantiation

/* @deprecated */
typeParameters: never
typeParameters?: never
}
interface BaseMemberExpression extends HasParentNode {
type: 'MemberExpression'
Expand Down

0 comments on commit 9fe8b34

Please sign in to comment.