Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/src/rules/no-anonymous-default-export.js
829 views
1
/**
2
* @fileoverview Rule to disallow anonymous default exports.
3
* @author Duncan Beevers
4
*/
5
6
import docsUrl from '../docsUrl';
7
import has from 'has';
8
9
const defs = {
10
ArrayExpression: {
11
option: 'allowArray',
12
description: 'If `false`, will report default export of an array',
13
message: 'Assign array to a variable before exporting as module default',
14
},
15
ArrowFunctionExpression: {
16
option: 'allowArrowFunction',
17
description: 'If `false`, will report default export of an arrow function',
18
message: 'Assign arrow function to a variable before exporting as module default',
19
},
20
CallExpression: {
21
option: 'allowCallExpression',
22
description: 'If `false`, will report default export of a function call',
23
message: 'Assign call result to a variable before exporting as module default',
24
default: true,
25
},
26
ClassDeclaration: {
27
option: 'allowAnonymousClass',
28
description: 'If `false`, will report default export of an anonymous class',
29
message: 'Unexpected default export of anonymous class',
30
forbid: (node) => !node.declaration.id,
31
},
32
FunctionDeclaration: {
33
option: 'allowAnonymousFunction',
34
description: 'If `false`, will report default export of an anonymous function',
35
message: 'Unexpected default export of anonymous function',
36
forbid: (node) => !node.declaration.id,
37
},
38
Literal: {
39
option: 'allowLiteral',
40
description: 'If `false`, will report default export of a literal',
41
message: 'Assign literal to a variable before exporting as module default',
42
},
43
ObjectExpression: {
44
option: 'allowObject',
45
description: 'If `false`, will report default export of an object expression',
46
message: 'Assign object to a variable before exporting as module default',
47
},
48
TemplateLiteral: {
49
option: 'allowLiteral',
50
description: 'If `false`, will report default export of a literal',
51
message: 'Assign literal to a variable before exporting as module default',
52
},
53
};
54
55
const schemaProperties = Object.keys(defs)
56
.map((key) => defs[key])
57
.reduce((acc, def) => {
58
acc[def.option] = {
59
description: def.description,
60
type: 'boolean',
61
};
62
63
return acc;
64
}, {});
65
66
const defaults = Object.keys(defs)
67
.map((key) => defs[key])
68
.reduce((acc, def) => {
69
acc[def.option] = has(def, 'default') ? def.default : false;
70
return acc;
71
}, {});
72
73
module.exports = {
74
meta: {
75
type: 'suggestion',
76
docs: {
77
url: docsUrl('no-anonymous-default-export'),
78
},
79
80
schema: [
81
{
82
type: 'object',
83
properties: schemaProperties,
84
'additionalProperties': false,
85
},
86
],
87
},
88
89
create(context) {
90
const options = Object.assign({}, defaults, context.options[0]);
91
92
return {
93
'ExportDefaultDeclaration': (node) => {
94
const def = defs[node.declaration.type];
95
96
// Recognized node type and allowed by configuration,
97
// and has no forbid check, or forbid check return value is truthy
98
if (def && !options[def.option] && (!def.forbid || def.forbid(node))) {
99
context.report({ node, message: def.message });
100
}
101
},
102
};
103
},
104
};
105
106