@christiango/unbarrel 0.10.2 → 0.10.3
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixIssuesInBarrelFile.d.ts","sourceRoot":"","sources":["../src/fixIssuesInBarrelFile.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fixIssuesInBarrelFile.d.ts","sourceRoot":"","sources":["../src/fixIssuesInBarrelFile.ts"],"names":[],"mappings":"AA2DA;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,gBAAgB,EAAE,MAAM,QA+F7D"}
|
|
@@ -47,56 +47,37 @@ const parseUtils_1 = require("./parseUtils");
|
|
|
47
47
|
* Groups resolved exports by their import path
|
|
48
48
|
*/
|
|
49
49
|
function groupExportsByPath(exports) {
|
|
50
|
-
const
|
|
50
|
+
const grouped = new Map();
|
|
51
51
|
for (const exp of exports) {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
const list = grouped.get(exp.importPath) || [];
|
|
53
|
+
list.push(exp);
|
|
54
|
+
grouped.set(exp.importPath, list);
|
|
55
55
|
}
|
|
56
|
-
return
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Creates export named declaration AST nodes from grouped exports
|
|
60
|
-
*/
|
|
61
|
-
function createExportStatements(exportsByPath) {
|
|
62
|
-
const statements = [];
|
|
63
|
-
for (const [importPath, exps] of exportsByPath) {
|
|
64
|
-
const specifiers = exps.map((e) => {
|
|
65
|
-
const local = t.identifier(e.importedName);
|
|
66
|
-
const exported = t.identifier(e.exportedName);
|
|
67
|
-
const specifier = t.exportSpecifier(local, exported);
|
|
68
|
-
// Set exportKind to 'type' for type-only exports
|
|
69
|
-
if (e.typeOnly) {
|
|
70
|
-
specifier.exportKind = 'type';
|
|
71
|
-
}
|
|
72
|
-
return specifier;
|
|
73
|
-
});
|
|
74
|
-
const exportDecl = t.exportNamedDeclaration(null, specifiers, t.stringLiteral(importPath));
|
|
75
|
-
statements.push(exportDecl);
|
|
76
|
-
}
|
|
77
|
-
return statements;
|
|
56
|
+
return grouped;
|
|
78
57
|
}
|
|
79
58
|
/**
|
|
80
59
|
* Deduplicates exports by exportedName.
|
|
81
|
-
*
|
|
82
|
-
* take precedence over type-only exports (typeOnly: true), because a value export
|
|
83
|
-
* can be used as both a value and a type (via typeof).
|
|
60
|
+
* Value exports take precedence over type-only exports.
|
|
84
61
|
*/
|
|
85
62
|
function deduplicateExports(exports) {
|
|
86
|
-
const
|
|
63
|
+
const byName = new Map();
|
|
87
64
|
for (const exp of exports) {
|
|
88
|
-
const existing =
|
|
89
|
-
if (!existing) {
|
|
90
|
-
|
|
91
|
-
exportsByName.set(exp.exportedName, exp);
|
|
92
|
-
}
|
|
93
|
-
else if (existing.typeOnly && !exp.typeOnly) {
|
|
94
|
-
// Replace type-only export with value export (value takes precedence)
|
|
95
|
-
exportsByName.set(exp.exportedName, exp);
|
|
65
|
+
const existing = byName.get(exp.exportedName);
|
|
66
|
+
if (!existing || (existing.typeOnly && !exp.typeOnly)) {
|
|
67
|
+
byName.set(exp.exportedName, exp);
|
|
96
68
|
}
|
|
97
|
-
// Otherwise keep the existing export (either both are same typeOnly, or existing is value and new is type)
|
|
98
69
|
}
|
|
99
|
-
return Array.from(
|
|
70
|
+
return Array.from(byName.values());
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Creates an export specifier AST node from a resolved export
|
|
74
|
+
*/
|
|
75
|
+
function createExportSpecifier(exp) {
|
|
76
|
+
const specifier = t.exportSpecifier(t.identifier(exp.importedName), t.identifier(exp.exportedName));
|
|
77
|
+
if (exp.typeOnly) {
|
|
78
|
+
specifier.exportKind = 'type';
|
|
79
|
+
}
|
|
80
|
+
return specifier;
|
|
100
81
|
}
|
|
101
82
|
/**
|
|
102
83
|
* Fixes any issues in the referenced barrel file.
|
|
@@ -104,16 +85,33 @@ function deduplicateExports(exports) {
|
|
|
104
85
|
*/
|
|
105
86
|
function fixIssuesInBarrelFile(absoluteFilePath) {
|
|
106
87
|
const ast = (0, parseUtils_1.parseTypescriptFile)(absoluteFilePath);
|
|
107
|
-
// Collect all
|
|
88
|
+
// Collect all exports in a single traverse pass
|
|
89
|
+
const existingExportsByName = new Map();
|
|
90
|
+
const existingStatementsByPath = new Map();
|
|
108
91
|
const exportStarPaths = [];
|
|
109
92
|
const allFlattenedExports = [];
|
|
110
93
|
(0, traverse_1.default)(ast, {
|
|
94
|
+
ExportNamedDeclaration(path) {
|
|
95
|
+
if (!path.node.source)
|
|
96
|
+
return;
|
|
97
|
+
const namedExport = {
|
|
98
|
+
path,
|
|
99
|
+
sourcePath: path.node.source.value,
|
|
100
|
+
};
|
|
101
|
+
existingStatementsByPath.set(namedExport.sourcePath, namedExport);
|
|
102
|
+
path.node.specifiers.forEach((specifier, i) => {
|
|
103
|
+
if (specifier.type === 'ExportSpecifier' && specifier.exported.type === 'Identifier') {
|
|
104
|
+
existingExportsByName.set(specifier.exported.name, {
|
|
105
|
+
namedExport,
|
|
106
|
+
specifierIndex: i,
|
|
107
|
+
typeOnly: specifier.exportKind === 'type' || path.node.exportKind === 'type',
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
},
|
|
111
112
|
ExportAllDeclaration(path) {
|
|
112
|
-
const importPath = path.node.source.value;
|
|
113
|
-
// Check if this is `export type *` (type-only export star)
|
|
114
113
|
const isExportTypeStar = path.node.exportKind === 'type';
|
|
115
|
-
|
|
116
|
-
const flattened = (0, flattenExportStar_1.flattenExportStar)(absoluteFilePath, importPath, isExportTypeStar);
|
|
114
|
+
const flattened = (0, flattenExportStar_1.flattenExportStar)(absoluteFilePath, path.node.source.value, isExportTypeStar);
|
|
117
115
|
exportStarPaths.push(path);
|
|
118
116
|
allFlattenedExports.push(...flattened);
|
|
119
117
|
},
|
|
@@ -121,23 +119,55 @@ function fixIssuesInBarrelFile(absoluteFilePath) {
|
|
|
121
119
|
if (exportStarPaths.length === 0) {
|
|
122
120
|
return;
|
|
123
121
|
}
|
|
124
|
-
// Deduplicate
|
|
122
|
+
// Deduplicate and filter exports
|
|
125
123
|
const deduplicated = deduplicateExports(allFlattenedExports);
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
124
|
+
const toRemove = [];
|
|
125
|
+
const filteredExports = deduplicated.filter((exp) => {
|
|
126
|
+
const existing = existingExportsByName.get(exp.exportedName);
|
|
127
|
+
if (!existing)
|
|
128
|
+
return true;
|
|
129
|
+
// Upgrade type-only to value export
|
|
130
|
+
if (existing.typeOnly && !exp.typeOnly) {
|
|
131
|
+
toRemove.push(existing);
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
});
|
|
136
|
+
// Remove specifiers being upgraded from type to value
|
|
137
|
+
for (const { namedExport, specifierIndex } of toRemove) {
|
|
138
|
+
const specifiers = namedExport.path.node.specifiers;
|
|
139
|
+
if (specifiers.length === 1) {
|
|
140
|
+
namedExport.path.remove();
|
|
141
|
+
existingStatementsByPath.delete(namedExport.sourcePath);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
specifiers.splice(specifierIndex, 1);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Group filtered exports and create/update statements
|
|
148
|
+
const grouped = groupExportsByPath(filteredExports);
|
|
149
|
+
const newStatements = [];
|
|
150
|
+
for (const [importPath, exps] of grouped) {
|
|
151
|
+
const existing = existingStatementsByPath.get(importPath);
|
|
152
|
+
if (existing) {
|
|
153
|
+
existing.path.node.specifiers.push(...exps.map(createExportSpecifier));
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
newStatements.push(t.exportNamedDeclaration(null, exps.map(createExportSpecifier), t.stringLiteral(importPath)));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Replace export stars
|
|
160
|
+
if (newStatements.length > 0) {
|
|
161
|
+
exportStarPaths[0].replaceWithMultiple(newStatements);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
exportStarPaths[0].remove();
|
|
165
|
+
}
|
|
133
166
|
for (let i = 1; i < exportStarPaths.length; i++) {
|
|
134
167
|
exportStarPaths[i].remove();
|
|
135
168
|
}
|
|
136
|
-
//
|
|
137
|
-
const output = (0, generator_1.default)(ast, {
|
|
138
|
-
retainLines: false,
|
|
139
|
-
comments: true,
|
|
140
|
-
});
|
|
169
|
+
// Write output
|
|
170
|
+
const output = (0, generator_1.default)(ast, { retainLines: false, comments: true });
|
|
141
171
|
fs.writeFileSync(absoluteFilePath, output.code, 'utf8');
|
|
142
172
|
}
|
|
143
173
|
//# sourceMappingURL=fixIssuesInBarrelFile.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixIssuesInBarrelFile.js","sourceRoot":"","sources":["../src/fixIssuesInBarrelFile.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"fixIssuesInBarrelFile.js","sourceRoot":"","sources":["../src/fixIssuesInBarrelFile.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,sDA+FC;AA9JD,4CAA8B;AAE9B,+DAAuC;AACvC,iEAAwC;AACxC,gDAAkC;AAClC,2DAAwD;AAExD,6CAAmD;AAEnD;;GAEG;AACH,SAAS,kBAAkB,CAAC,OAAmC;IAC7D,MAAM,OAAO,GAAG,IAAI,GAAG,EAAsC,CAAC;IAC9D,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;QAC1B,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;QAC/C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACf,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC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