Skip to content

⭐️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

Merged
merged 7 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/rules/require-direct-export.md
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...
Copy link
Member

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?


:-1: Examples of **incorrect** code:

```js
const ComponentA = {
name: 'ComponentA',
data() {
return {
state: 1
}
}
}

export default Component A
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwanted space in Component A

```

:+1: Examples of **correct** code:

```js
export default {
name: 'ComponentA',
data() {
return {
state: 1
}
}
}
```
1 change: 1 addition & 0 deletions lib/configs/essential.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
'vue/no-use-v-if-with-v-for': 'error',
'vue/require-component-is': 'error',
'vue/require-prop-type-constructor': 'error',
'vue/require-direct-export': 'error',
'vue/require-render-return': 'error',
'vue/require-v-for-key': 'error',
'vue/require-valid-default-prop': 'error',
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
'prop-name-casing': require('./rules/prop-name-casing'),
'require-component-is': require('./rules/require-component-is'),
'require-default-prop': require('./rules/require-default-prop'),
'require-direct-export': require('./rules/require-direct-export'),
'require-prop-type-constructor': require('./rules/require-prop-type-constructor'),
'require-prop-types': require('./rules/require-prop-types'),
'require-render-return': require('./rules/require-render-return'),
Expand Down
47 changes: 47 additions & 0 deletions lib/rules/require-direct-export.js
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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We add every new rule as uncategorized to have a nice period for testing it and introducing breaking changes if necessary, without having to wait for another major release. So I think we should change the category to undefined for the time being :) We're still in v5 beta though, so it might land in v5 after all - we'll see.

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 strongly-recommended :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.`
})
}
}
}
}
}
7 changes: 5 additions & 2 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ module.exports = {
})
},

isVueFile (path) {
return path.endsWith('.vue') || path.endsWith('.jsx')
},

/**
* Check whether the given node is a Vue component based
* on the filename and default export type
Expand All @@ -413,8 +417,7 @@ module.exports = {
* @returns {boolean}
*/
isVueComponentFile (node, path) {
const isVueFile = path.endsWith('.vue') || path.endsWith('.jsx')
return isVueFile &&
return this.isVueFile(path) &&
node.type === 'ExportDefaultDeclaration' &&
node.declaration.type === 'ObjectExpression'
},
Expand Down
56 changes: 56 additions & 0 deletions tests/lib/rules/require-direct-export.js
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
}]
}
]
})