Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/src/rules/no-mutable-exports.js
829 views
1
import docsUrl from '../docsUrl';
2
3
module.exports = {
4
meta: {
5
type: 'suggestion',
6
docs: {
7
url: docsUrl('no-mutable-exports'),
8
},
9
schema: [],
10
},
11
12
create(context) {
13
function checkDeclaration(node) {
14
const { kind } = node;
15
if (kind === 'var' || kind === 'let') {
16
context.report(node, `Exporting mutable '${kind}' binding, use 'const' instead.`);
17
}
18
}
19
20
function checkDeclarationsInScope({ variables }, name) {
21
for (const variable of variables) {
22
if (variable.name === name) {
23
for (const def of variable.defs) {
24
if (def.type === 'Variable' && def.parent) {
25
checkDeclaration(def.parent);
26
}
27
}
28
}
29
}
30
}
31
32
function handleExportDefault(node) {
33
const scope = context.getScope();
34
35
if (node.declaration.name) {
36
checkDeclarationsInScope(scope, node.declaration.name);
37
}
38
}
39
40
function handleExportNamed(node) {
41
const scope = context.getScope();
42
43
if (node.declaration) {
44
checkDeclaration(node.declaration);
45
} else if (!node.source) {
46
for (const specifier of node.specifiers) {
47
checkDeclarationsInScope(scope, specifier.local.name);
48
}
49
}
50
}
51
52
return {
53
'ExportDefaultDeclaration': handleExportDefault,
54
'ExportNamedDeclaration': handleExportNamed,
55
};
56
},
57
};
58
59