File tree Expand file tree Collapse file tree 2 files changed +23
-6
lines changed Expand file tree Collapse file tree 2 files changed +23
-6
lines changed Original file line number Diff line number Diff line change @@ -22,20 +22,31 @@ module.exports = {
22
22
} ,
23
23
/** @param {RuleContext } context */
24
24
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 ) {
26
35
return { }
27
36
}
28
37
38
+ let hasDefaultExport = false
39
+
29
40
return {
41
+ 'Program > ExportDefaultDeclaration' ( ) {
42
+ hasDefaultExport = true
43
+ } ,
44
+
30
45
/**
31
46
* @param {Program } node
32
47
*/
33
48
'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 ) {
39
50
context . report ( {
40
51
loc : { line : 1 , column : 0 } ,
41
52
messageId : 'missing'
Original file line number Diff line number Diff line change @@ -17,6 +17,12 @@ const tester = new RuleTester({
17
17
18
18
tester . run ( 'require-default-export' , rule , {
19
19
valid : [
20
+ {
21
+ filename : 'test.vue' ,
22
+ code : `
23
+ <template>Without script</template>
24
+ `
25
+ } ,
20
26
{
21
27
filename : 'test.vue' ,
22
28
code : `
You can’t perform that action at this time.
0 commit comments