forked from import-js/eslint-plugin-import
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathno-anonymous-default-export.js
103 lines (93 loc) · 3.05 KB
/
no-anonymous-default-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
/**
* @fileoverview Rule to disallow anonymous default exports.
* @author Duncan Beevers
*/
import docsUrl from '../docsUrl'
import has from 'has'
const defs = {
ArrayExpression: {
option: 'allowArray',
description: 'If `false`, will report default export of an array',
message: 'Assign array to a variable before exporting as module default',
},
ArrowFunctionExpression: {
option: 'allowArrowFunction',
description: 'If `false`, will report default export of an arrow function',
message: 'Assign arrow function to a variable before exporting as module default',
},
CallExpression: {
option: 'allowCallExpression',
description: 'If `false`, will report default export of a function call',
message: 'Assign call result to a variable before exporting as module default',
default: true,
},
ClassDeclaration: {
option: 'allowAnonymousClass',
description: 'If `false`, will report default export of an anonymous class',
message: 'Unexpected default export of anonymous class',
forbid: (node) => !node.declaration.id,
},
FunctionDeclaration: {
option: 'allowAnonymousFunction',
description: 'If `false`, will report default export of an anonymous function',
message: 'Unexpected default export of anonymous function',
forbid: (node) => !node.declaration.id,
},
Literal: {
option: 'allowLiteral',
description: 'If `false`, will report default export of a literal',
message: 'Assign literal to a variable before exporting as module default',
},
ObjectExpression: {
option: 'allowObject',
description: 'If `false`, will report default export of an object expression',
message: 'Assign object to a variable before exporting as module default',
},
TemplateLiteral: {
option: 'allowLiteral',
description: 'If `false`, will report default export of a literal',
message: 'Assign literal to a variable before exporting as module default',
},
}
const schemaProperties = Object.keys(defs)
.map((key) => defs[key])
.reduce((acc, def) => {
acc[def.option] = {
description: def.description,
type: 'boolean',
}
return acc
}, {})
const defaults = Object.keys(defs)
.map((key) => defs[key])
.reduce((acc, def) => {
acc[def.option] = has(def, 'default') ? def.default : false
return acc
}, {})
module.exports = {
meta: {
docs: {
url: docsUrl('no-anonymous-default-export'),
},
schema: [
{
type: 'object',
properties: schemaProperties,
'additionalProperties': false,
},
],
},
create: function (context) {
const options = Object.assign({}, defaults, context.options[0])
return {
'ExportDefaultDeclaration': (node) => {
const def = defs[node.declaration.type]
// Recognized node type and allowed by configuration,
// and has no forbid check, or forbid check return value is truthy
if (def && !options[def.option] && (!def.forbid || def.forbid(node))) {
context.report({ node, message: def.message })
}
},
}
},
}