Skip to content

Commit 2daebd6

Browse files
committed
fix: without script and jsx
1 parent 426f5af commit 2daebd6

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lib/rules/require-default-export.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,31 @@ module.exports = {
2222
},
2323
/** @param {RuleContext} context */
2424
create(context) {
25-
if (utils.isScriptSetup(context)) {
25+
const sourceCode = context.getSourceCode()
26+
const documentFragment = sourceCode.parserServices.getDocumentFragment?.()
27+
28+
const hasScript =
29+
documentFragment &&
30+
documentFragment.children.some(
31+
(e) => utils.isVElement(e) && e.name === 'script'
32+
)
33+
34+
if (utils.isScriptSetup(context) || !hasScript) {
2635
return {}
2736
}
2837

38+
let hasDefaultExport = false
39+
2940
return {
41+
'Program > ExportDefaultDeclaration'() {
42+
hasDefaultExport = true
43+
},
44+
3045
/**
3146
* @param {Program} node
3247
*/
3348
'Program:exit'(node) {
34-
const hasDefaultExport = node.body.some(
35-
(item) => item.type === 'ExportDefaultDeclaration'
36-
)
37-
38-
if (!hasDefaultExport) {
49+
if (!hasDefaultExport && node.body.length > 0) {
3950
context.report({
4051
loc: { line: 1, column: 0 },
4152
messageId: 'missing'

tests/lib/rules/require-default-export.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ const tester = new RuleTester({
1717

1818
tester.run('require-default-export', rule, {
1919
valid: [
20+
{
21+
filename: 'test.vue',
22+
code: `
23+
<template>Without script</template>
24+
`
25+
},
2026
{
2127
filename: 'test.vue',
2228
code: `

0 commit comments

Comments
 (0)