-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add vue/require-default-export
rule
#2494
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
426f5af
Add `vue/require-default-export` rule
ItMaga 2daebd6
fix: without script and jsx
ItMaga 178441a
Merge remote-tracking branch 'origin/master' into feat/require-defaul…
ItMaga 3f5f4b4
docs: add related rules and revert non-relevant changes
ItMaga dd353c2
feat: revert categories and update description
ItMaga 77f1126
feat: add test suites
ItMaga 7f98644
refactor: improve error message
ItMaga 73e00c8
Discard changes to docs/rules/object-curly-newline.md
FloEdelmann b42646a
Discard changes to docs/rules/object-property-newline.md
FloEdelmann 4a66d2e
feat: improve messages and accurate loc
ItMaga c0dd101
docs: add related rule to one-component-per-file
ItMaga 1c706a1
chore: remove empty line
ItMaga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/require-default-export | ||
description: require components to be the default export | ||
--- | ||
|
||
# vue/require-default-export | ||
|
||
> require components to be the default export | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports when a Vue component does not have a default export, if the component is not defined as `<script setup>`. | ||
|
||
<eslint-code-block :rules="{'vue/require-default-export': ['error']}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<script> | ||
const foo = 'foo'; | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/require-default-export': ['error']}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<script> | ||
export default { | ||
data() { | ||
return { | ||
foo: 'foo' | ||
}; | ||
} | ||
}; | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :couple: Related Rules | ||
|
||
- [vue/one-component-per-file](./one-component-per-file.md) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-default-export.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/require-default-export.js) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @author ItMaga | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'require components to be the default export', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/require-default-export.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
missing: 'Missing default export.', | ||
mustBeDefaultExport: 'Component must be the default export.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(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 | ||
let hasDefinedComponent = false | ||
|
||
return utils.compositingVisitors( | ||
utils.defineVueVisitor(context, { | ||
onVueObjectExit() { | ||
hasDefinedComponent = true | ||
} | ||
}), | ||
|
||
{ | ||
'Program > ExportDefaultDeclaration'() { | ||
hasDefaultExport = true | ||
}, | ||
|
||
/** | ||
* @param {Program} node | ||
*/ | ||
'Program:exit'(node) { | ||
if (!hasDefaultExport && node.body.length > 0) { | ||
context.report({ | ||
loc: node.tokens[node.tokens.length - 1].loc, | ||
messageId: hasDefinedComponent ? 'mustBeDefaultExport' : 'missing' | ||
}) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/** | ||
* @author ItMaga | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('../../eslint-compat').RuleTester | ||
const rule = require('../../../lib/rules/require-default-export') | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('require-default-export', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template>Without script</template> | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import { ref } from 'vue'; | ||
|
||
export default {} | ||
</script> | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script setup> | ||
const foo = 'foo'; | ||
</script> | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
const component = {}; | ||
|
||
export default component; | ||
</script> | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import {defineComponent} from 'vue'; | ||
|
||
export default defineComponent({}); | ||
</script> | ||
` | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
const foo = 'foo'; | ||
export const bar = 'bar'; | ||
` | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
import {defineComponent} from 'vue'; | ||
defineComponent({}); | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
const foo = 'foo'; | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missing', | ||
line: 4, | ||
endLine: 4, | ||
column: 7, | ||
endColumn: 16 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
export const foo = 'foo'; | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missing', | ||
line: 4, | ||
endLine: 4, | ||
column: 7, | ||
endColumn: 16 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
const foo = 'foo'; | ||
|
||
export { foo }; | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missing', | ||
line: 6, | ||
endLine: 6, | ||
column: 7, | ||
endColumn: 16 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
export const foo = 'foo'; | ||
export const bar = 'bar'; | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missing', | ||
line: 5, | ||
endLine: 5, | ||
column: 7, | ||
endColumn: 16 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import { defineComponent } from 'vue'; | ||
|
||
export const component = defineComponent({}); | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'mustBeDefaultExport', | ||
line: 6, | ||
endLine: 6, | ||
column: 7, | ||
endColumn: 16 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import Vue from 'vue'; | ||
|
||
const component = Vue.component('foo', {}); | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'mustBeDefaultExport', | ||
line: 6, | ||
endLine: 6, | ||
column: 7, | ||
endColumn: 16 | ||
} | ||
] | ||
} | ||
] | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.