-
-
Notifications
You must be signed in to change notification settings - Fork 682
⭐️New: Add vue/require-direct-export
rule
#581
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
Changes from 3 commits
a3ef3a0
ceeb75a
ec4f7ce
18a6ed2
1f37abf
68c56ae
01680a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# require the component to be directly exported (require-direct-export) | ||
|
||
- :gear: This rule is included in `"plugin:vue/essentials"`. | ||
|
||
## Rule Details | ||
|
||
This rule aims to... | ||
|
||
:-1: Examples of **incorrect** code: | ||
|
||
```js | ||
const ComponentA = { | ||
name: 'ComponentA', | ||
data() { | ||
return { | ||
state: 1 | ||
} | ||
} | ||
} | ||
|
||
export default Component A | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unwanted space in |
||
``` | ||
|
||
:+1: Examples of **correct** code: | ||
|
||
```js | ||
export default { | ||
name: 'ComponentA', | ||
data() { | ||
return { | ||
state: 1 | ||
} | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @fileoverview require the component to be directly exported | ||
* @author Hiroki Osame <[email protected]> | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'require the component to be directly exported', | ||
category: 'essential', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We add every new rule as In theory it's perfectly fine to have the component assigned to a variable and then exported. You can use a special comment for determining that this is a Vue component and it will be linted properly: // @vue/component
const ComponentA = {
...
}
export default ComponentA; The way I see it - essentials are here to prevent errors. And this is technically not an error-situation so I think it would better fit in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh I didn't know about the comment. Good to know, thank you! |
||
recommended: false, | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/require-direct-export.md' | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [] | ||
}, | ||
|
||
create (context) { | ||
const filePath = context.getFilename() | ||
|
||
return { | ||
'ExportDefaultDeclaration:exit' (node) { | ||
const isVueFile = utils.isVueFile(filePath) | ||
if (!isVueFile) { return } | ||
|
||
const isObjectExpression = ( | ||
node.type === 'ExportDefaultDeclaration' && | ||
node.declaration.type === 'ObjectExpression' | ||
) | ||
|
||
if (!isObjectExpression) { | ||
context.report({ | ||
node, | ||
message: `Expected the component literal to be directly exported.` | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @fileoverview require the component to be directly exported | ||
* @author Hiroki Osame <[email protected]> | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/require-direct-export') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { jsx: true } | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester() | ||
ruleTester.run('require-direct-export', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default {} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
|
||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
const A = {}; | ||
export default A`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Expected the component literal to be directly exported.', | ||
type: 'ExportDefaultDeclaration', | ||
line: 3 | ||
}] | ||
} | ||
] | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please provide the short description?