Skip to content

Commit

Permalink
fix: without script and jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
ItMaga committed Jun 30, 2024
1 parent 426f5af commit 2daebd6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/rules/require-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,31 @@ module.exports = {
},
/** @param {RuleContext} context */
create(context) {
if (utils.isScriptSetup(context)) {
const sourceCode = context.getSourceCode()
const documentFragment = sourceCode.parserServices.getDocumentFragment?.()

const hasScript =
documentFragment &&
documentFragment.children.some(
(e) => utils.isVElement(e) && e.name === 'script'
)

if (utils.isScriptSetup(context) || !hasScript) {
return {}
}

let hasDefaultExport = false

return {
'Program > ExportDefaultDeclaration'() {
hasDefaultExport = true
},

/**
* @param {Program} node
*/
'Program:exit'(node) {
const hasDefaultExport = node.body.some(
(item) => item.type === 'ExportDefaultDeclaration'
)

if (!hasDefaultExport) {
if (!hasDefaultExport && node.body.length > 0) {
context.report({
loc: { line: 1, column: 0 },
messageId: 'missing'
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/require-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const tester = new RuleTester({

tester.run('require-default-export', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>Without script</template>
`
},
{
filename: 'test.vue',
code: `
Expand Down

0 comments on commit 2daebd6

Please sign in to comment.