Skip to content

Commit 1447444

Browse files
authored
Update vue/require-emit-validator rule to support <script setup> (#1557)
1 parent 4e575ea commit 1447444

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

Diff for: lib/rules/require-emit-validator.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const utils = require('../utils')
99
/**
1010
* @typedef {import('../utils').ComponentArrayEmit} ComponentArrayEmit
1111
* @typedef {import('../utils').ComponentObjectEmit} ComponentObjectEmit
12+
* @typedef {import('../utils').ComponentTypeEmit} ComponentTypeEmit
1213
*/
1314

1415
// ------------------------------------------------------------------------------
@@ -83,8 +84,20 @@ module.exports = {
8384
// Public
8485
// ----------------------------------------------------------------------
8586

86-
return utils.executeOnVue(context, (obj) => {
87-
utils.getComponentEmits(obj).forEach(checker)
88-
})
87+
return utils.compositingVisitors(
88+
utils.executeOnVue(context, (obj) => {
89+
utils.getComponentEmits(obj).forEach(checker)
90+
}),
91+
utils.defineScriptSetupVisitor(context, {
92+
onDefineEmitsEnter(_node, emits) {
93+
for (const emit of emits) {
94+
if (emit.type === 'type') {
95+
continue
96+
}
97+
checker(emit)
98+
}
99+
}
100+
})
101+
)
89102
}
90103
}

Diff for: tests/lib/rules/require-emit-validator.js

+54
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ ruleTester.run('require-emit-validator', rule, {
163163
}
164164
`,
165165
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
166+
},
167+
{
168+
filename: 'test.vue',
169+
code: `
170+
<script setup lang="ts">
171+
const emit = defineEmits<(e: 'foo')=>void>()
172+
</script>
173+
`,
174+
parser: require.resolve('vue-eslint-parser'),
175+
parserOptions: {
176+
ecmaVersion: 6,
177+
sourceType: 'module',
178+
parser: require.resolve('@typescript-eslint/parser')
179+
}
166180
}
167181
],
168182

@@ -325,6 +339,46 @@ ruleTester.run('require-emit-validator', rule, {
325339
line: 4
326340
}
327341
]
342+
},
343+
{
344+
filename: 'test.vue',
345+
code: `
346+
<script setup>
347+
const emit = defineEmits(['foo'])
348+
</script>
349+
`,
350+
parser: require.resolve('vue-eslint-parser'),
351+
parserOptions: {
352+
ecmaVersion: 6,
353+
sourceType: 'module'
354+
},
355+
errors: [
356+
{
357+
messageId: 'missing',
358+
data: { name: 'foo' },
359+
line: 3
360+
}
361+
]
362+
},
363+
{
364+
filename: 'test.vue',
365+
code: `
366+
<script setup>
367+
const emit = defineEmits({foo:null})
368+
</script>
369+
`,
370+
parser: require.resolve('vue-eslint-parser'),
371+
parserOptions: {
372+
ecmaVersion: 6,
373+
sourceType: 'module'
374+
},
375+
errors: [
376+
{
377+
messageId: 'skipped',
378+
data: { name: 'foo' },
379+
line: 3
380+
}
381+
]
328382
}
329383
]
330384
})

0 commit comments

Comments
 (0)