@blumintinc/eslint-plugin-blumint 0.1.4-s → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +1 -1
- package/lib/rules/export-if-in-doubt.js +42 -8
- package/lib/utils/ASTHelpers.js +20 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.exportIfInDoubt = void 0;
|
|
4
|
+
/* eslint-disable @blumintinc/blumint/extract-global-constants */
|
|
5
|
+
// import { ASTHelpers } from '../utils/ASTHelpers';
|
|
4
6
|
const createRule_1 = require("../utils/createRule");
|
|
5
7
|
exports.exportIfInDoubt = (0, createRule_1.createRule)({
|
|
6
|
-
name: '
|
|
8
|
+
name: 'export-if-in-doubt',
|
|
7
9
|
meta: {
|
|
8
10
|
type: 'suggestion',
|
|
9
11
|
docs: {
|
|
@@ -17,18 +19,50 @@ exports.exportIfInDoubt = (0, createRule_1.createRule)({
|
|
|
17
19
|
},
|
|
18
20
|
defaultOptions: [],
|
|
19
21
|
create(context) {
|
|
22
|
+
// List of top-level declarations
|
|
23
|
+
// List of exported identifiers
|
|
24
|
+
const topLevelDeclarations = [];
|
|
25
|
+
const exportedIdentifiers = [];
|
|
20
26
|
return {
|
|
21
27
|
'Program > VariableDeclaration > VariableDeclarator, Program > FunctionDeclaration, Program > TSTypeAliasDeclaration'(node) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
topLevelDeclarations.push(node);
|
|
29
|
+
},
|
|
30
|
+
ExportNamedDeclaration: (node) => {
|
|
31
|
+
if (node.specifiers) {
|
|
32
|
+
node.specifiers.forEach((specifier) => {
|
|
33
|
+
if (specifier.type === 'ExportSpecifier') {
|
|
34
|
+
// Handle both normal and default export
|
|
35
|
+
const exportedName = specifier.exported.name;
|
|
36
|
+
if (!exportedIdentifiers.includes(exportedName)) {
|
|
37
|
+
exportedIdentifiers.push(exportedName);
|
|
38
|
+
}
|
|
39
|
+
// If the specifier is a default export
|
|
40
|
+
if (specifier.local.name !== exportedName) {
|
|
41
|
+
const localName = specifier.local.name;
|
|
42
|
+
if (!exportedIdentifiers.includes(localName)) {
|
|
43
|
+
exportedIdentifiers.push(localName);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
29
47
|
});
|
|
30
48
|
}
|
|
31
49
|
},
|
|
50
|
+
'Program:exit': () => {
|
|
51
|
+
topLevelDeclarations.forEach((node) => {
|
|
52
|
+
if ('id' in node &&
|
|
53
|
+
node.id &&
|
|
54
|
+
(node.type === 'VariableDeclarator' ||
|
|
55
|
+
node.type === 'FunctionDeclaration' ||
|
|
56
|
+
node.type === 'TSTypeAliasDeclaration') &&
|
|
57
|
+
node.id.type === 'Identifier' &&
|
|
58
|
+
!exportedIdentifiers.includes(node.id.name)) {
|
|
59
|
+
context.report({
|
|
60
|
+
node,
|
|
61
|
+
messageId: 'exportIfInDoubt',
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
},
|
|
32
66
|
};
|
|
33
67
|
},
|
|
34
68
|
});
|
package/lib/utils/ASTHelpers.js
CHANGED
|
@@ -99,6 +99,26 @@ class ASTHelpers {
|
|
|
99
99
|
}
|
|
100
100
|
return false;
|
|
101
101
|
}
|
|
102
|
+
static isNodeExported(node) {
|
|
103
|
+
// Checking if the node is exported as a named export.
|
|
104
|
+
if (node.parent && node.parent.type === 'ExportNamedDeclaration') {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
// Checking if the node is exported as default.
|
|
108
|
+
if (node.parent &&
|
|
109
|
+
node.parent.parent &&
|
|
110
|
+
node.parent.parent.type === 'ExportDefaultDeclaration') {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
// Checking if the node is exported in a list of exports.
|
|
114
|
+
if (node.parent &&
|
|
115
|
+
node.parent.parent &&
|
|
116
|
+
node.parent.parent.type === 'ExportSpecifier' &&
|
|
117
|
+
node.parent.parent.exported.name === node.name) {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
102
122
|
}
|
|
103
123
|
exports.ASTHelpers = ASTHelpers;
|
|
104
124
|
//# sourceMappingURL=ASTHelpers.js.map
|