/
require-direct-export.js
109 lines (101 loc) · 3.27 KB
/
require-direct-export.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @fileoverview require the component to be directly exported
* @author Hiroki Osame <hiroki.osame@gmail.com>
*/
'use strict'
const utils = require('../utils')
/**
* @typedef {import('vue-eslint-parser').AST.ESLintExportDefaultDeclaration} ExportDefaultDeclaration
* @typedef {import('vue-eslint-parser').AST.ESLintDeclaration} Declaration
* @typedef {import('vue-eslint-parser').AST.ESLintExpression} Expression
* @typedef {import('vue-eslint-parser').AST.ESLintReturnStatement} ReturnStatement
*
*/
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require the component to be directly exported',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/require-direct-export.html'
},
fixable: null, // or "code" or "whitespace"
schema: [{
type: 'object',
properties: {
disallowFunctionalComponentFunction: { type: 'boolean' }
},
additionalProperties: false
}]
},
create (context) {
const filePath = context.getFilename()
if (!utils.isVueFile(filePath)) return {}
const disallowFunctional = (context.options[0] || {}).disallowFunctionalComponentFunction
let maybeVue3Functional
let scopeStack = null
return {
/** @param {Declaration | Expression} node */
'ExportDefaultDeclaration > *' (node) {
if (node.type === 'ObjectExpression') {
// OK
return
}
if (!disallowFunctional) {
if (node.type === 'ArrowFunctionExpression') {
if (node.body.type !== 'BlockStatement') {
// OK
return
}
maybeVue3Functional = {
body: node.body
}
return
}
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') {
maybeVue3Functional = {
body: node.body
}
return
}
}
context.report({
node: node.parent,
message: `Expected the component literal to be directly exported.`
})
},
...(disallowFunctional ? {} : {
':function > BlockStatement' (node) {
if (!maybeVue3Functional) {
return
}
scopeStack = { upper: scopeStack, withinVue3FunctionalBody: maybeVue3Functional.body === node }
},
/** @param {ReturnStatement} node */
ReturnStatement (node) {
if (scopeStack && scopeStack.withinVue3FunctionalBody && node.argument) {
maybeVue3Functional.hasReturnArgument = true
}
},
':function > BlockStatement:exit' (node) {
scopeStack = scopeStack && scopeStack.upper
},
/** @param {ExportDefaultDeclaration} node */
'ExportDefaultDeclaration:exit' (node) {
if (!maybeVue3Functional) {
return
}
if (!maybeVue3Functional.hasReturnArgument) {
context.report({
node,
message: `Expected the component literal to be directly exported.`
})
}
}
})
}
}
}