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

Fix crash for TSFunctionType with type-literal option in vue/define-emits-declaration rule #2336

Merged
merged 1 commit into from
Nov 30, 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
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
Loading