@atlaspack/transformer-typescript-types 2.14.5-canary.24 → 2.14.5-canary.240
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/CHANGELOG.md +239 -0
- package/dist/TSModule.js +40 -0
- package/dist/TSModuleGraph.js +233 -0
- package/dist/TSTypesTransformer.js +185 -0
- package/dist/collect.js +137 -0
- package/dist/shake.js +219 -0
- package/dist/utils.js +29 -0
- package/dist/wrappers.js +71 -0
- package/lib/TSModuleGraph.js +13 -0
- package/lib/TSTypesTransformer.js +6 -4
- package/lib/collect.js +28 -4
- package/lib/shake.js +24 -6
- package/lib/types/TSModule.d.ts +25 -0
- package/lib/types/TSModuleGraph.d.ts +33 -0
- package/lib/types/TSTypesTransformer.d.ts +3 -0
- package/lib/types/collect.d.ts +2 -0
- package/lib/types/shake.d.ts +2 -0
- package/lib/types/utils.d.ts +2 -0
- package/lib/types/wrappers.d.ts +8 -0
- package/lib/utils.js +4 -0
- package/lib/wrappers.js +5 -4
- package/package.json +13 -9
- package/src/{TSModule.js → TSModule.ts} +13 -6
- package/src/{TSModuleGraph.js → TSModuleGraph.ts} +48 -9
- package/src/{TSTypesTransformer.js → TSTypesTransformer.ts} +10 -10
- package/src/{collect.js → collect.ts} +22 -5
- package/src/{shake.js → shake.ts} +19 -6
- package/src/{utils.js → utils.ts} +3 -2
- package/src/{wrappers.js → wrappers.ts} +95 -58
- package/tsconfig.json +21 -0
- package/tsconfig.tsbuildinfo +1 -0
package/dist/collect.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.collect = collect;
|
|
7
|
+
const nullthrows_1 = __importDefault(require("nullthrows"));
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
const TSModule_1 = require("./TSModule");
|
|
10
|
+
const utils_1 = require("./utils");
|
|
11
|
+
function collect(moduleGraph, context, sourceFile) {
|
|
12
|
+
// Factory only exists on TS >= 4.0
|
|
13
|
+
const { factory = typescript_1.default } = context;
|
|
14
|
+
// When module definitions are nested inside each other (e.g with module augmentation),
|
|
15
|
+
// we want to keep track of the hierarchy so we can associated nodes with the right module.
|
|
16
|
+
const moduleStack = [];
|
|
17
|
+
let _currentModule;
|
|
18
|
+
let visit = (node) => {
|
|
19
|
+
if (typescript_1.default.isBundle(node)) {
|
|
20
|
+
// @ts-expect-error TS2345
|
|
21
|
+
return factory.updateBundle(node, typescript_1.default.visitNodes(node.sourceFiles, visit));
|
|
22
|
+
}
|
|
23
|
+
if (typescript_1.default.isModuleDeclaration(node)) {
|
|
24
|
+
moduleStack.push(_currentModule);
|
|
25
|
+
_currentModule = new TSModule_1.TSModule();
|
|
26
|
+
moduleGraph.addModule(node.name.text, _currentModule);
|
|
27
|
+
}
|
|
28
|
+
if (!_currentModule) {
|
|
29
|
+
return typescript_1.default.visitEachChild(node, visit, context);
|
|
30
|
+
}
|
|
31
|
+
let currentModule = (0, nullthrows_1.default)(_currentModule);
|
|
32
|
+
if (typescript_1.default.isImportDeclaration(node) && node.importClause) {
|
|
33
|
+
if (node.importClause.namedBindings) {
|
|
34
|
+
// @ts-expect-error TS2339
|
|
35
|
+
if (node.importClause.namedBindings.elements) {
|
|
36
|
+
// @ts-expect-error TS2339
|
|
37
|
+
for (let element of node.importClause.namedBindings.elements) {
|
|
38
|
+
currentModule.addImport(element.name.text,
|
|
39
|
+
// @ts-expect-error TS2339
|
|
40
|
+
node.moduleSpecifier.text, (element.propertyName ?? element.name).text);
|
|
41
|
+
}
|
|
42
|
+
// @ts-expect-error TS2339
|
|
43
|
+
}
|
|
44
|
+
else if (node.importClause.namedBindings.name) {
|
|
45
|
+
currentModule.addImport(
|
|
46
|
+
// @ts-expect-error TS2339
|
|
47
|
+
node.importClause.namedBindings.name.text,
|
|
48
|
+
// @ts-expect-error TS2339
|
|
49
|
+
node.moduleSpecifier.text, '*');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (node.importClause.name) {
|
|
53
|
+
currentModule.addImport(node.importClause.name.text,
|
|
54
|
+
// @ts-expect-error TS2339
|
|
55
|
+
node.moduleSpecifier.text, 'default');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (typescript_1.default.isExportDeclaration(node)) {
|
|
59
|
+
if (node.exportClause) {
|
|
60
|
+
// @ts-expect-error TS2339
|
|
61
|
+
for (let element of node.exportClause.elements) {
|
|
62
|
+
if (node.moduleSpecifier) {
|
|
63
|
+
currentModule.addExport(element.name.text, (element.propertyName ?? element.name).text,
|
|
64
|
+
// @ts-expect-error TS2339
|
|
65
|
+
node.moduleSpecifier.text);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
currentModule.addExport(element.name.text, (element.propertyName ?? element.name).text);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// @ts-expect-error TS18048
|
|
74
|
+
currentModule.addWildcardExport(node.moduleSpecifier.text);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
node = typescript_1.default.visitEachChild(node, visit, context);
|
|
78
|
+
if (typescript_1.default.isImportTypeNode(node) &&
|
|
79
|
+
typescript_1.default.isLiteralTypeNode(node.argument) &&
|
|
80
|
+
typescript_1.default.isStringLiteral(node.argument.literal)) {
|
|
81
|
+
let local = `$$parcel$import$${moduleGraph.syntheticImportCount++}`;
|
|
82
|
+
let [specifier, entity] = getImportName(node.qualifier, local, factory);
|
|
83
|
+
currentModule.addImport(local, node.argument.literal.text, specifier);
|
|
84
|
+
return factory.createTypeReferenceNode(entity, node.typeArguments);
|
|
85
|
+
}
|
|
86
|
+
// Handle `export default name;`
|
|
87
|
+
if (typescript_1.default.isExportAssignment(node) && typescript_1.default.isIdentifier(node.expression)) {
|
|
88
|
+
currentModule.addExport('default', node.expression.text);
|
|
89
|
+
}
|
|
90
|
+
if ((0, utils_1.isDeclaration)(node)) {
|
|
91
|
+
if (node.name) {
|
|
92
|
+
currentModule.addLocal(node.name.text, node);
|
|
93
|
+
}
|
|
94
|
+
let name = (0, utils_1.getExportedName)(node);
|
|
95
|
+
if (name) {
|
|
96
|
+
currentModule.addLocal(name, node);
|
|
97
|
+
currentModule.addExport(name, name);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (typescript_1.default.isVariableStatement(node) && node.modifiers) {
|
|
101
|
+
let isExported = node.modifiers.some((m) => m.kind === typescript_1.default.SyntaxKind.ExportKeyword);
|
|
102
|
+
for (let v of node.declarationList.declarations) {
|
|
103
|
+
// @ts-expect-error TS2339
|
|
104
|
+
currentModule.addLocal(v.name.text, v);
|
|
105
|
+
if (isExported) {
|
|
106
|
+
// @ts-expect-error TS2339
|
|
107
|
+
currentModule.addExport(v.name.text, v.name.text);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// After we finish traversing the children of a module definition,
|
|
112
|
+
// we need to make sure that subsequent nodes get associated with the next-highest level module.
|
|
113
|
+
if (typescript_1.default.isModuleDeclaration(node)) {
|
|
114
|
+
_currentModule = moduleStack.pop();
|
|
115
|
+
}
|
|
116
|
+
return node;
|
|
117
|
+
};
|
|
118
|
+
return typescript_1.default.visitNode(sourceFile, visit);
|
|
119
|
+
}
|
|
120
|
+
// Traverse down an EntityName to the root identifier. Return that to use as the named import specifier,
|
|
121
|
+
// and collect the remaining parts into a new QualifiedName with the local replacement at the root.
|
|
122
|
+
// import('react').JSX.Element => import {JSX} from 'react'; JSX.Element
|
|
123
|
+
// @ts-expect-error TS7023
|
|
124
|
+
function getImportName(qualifier, local, factory) {
|
|
125
|
+
if (!qualifier) {
|
|
126
|
+
// @ts-expect-error TS2339
|
|
127
|
+
return ['*', factory.createIdentifier(local)];
|
|
128
|
+
}
|
|
129
|
+
if (qualifier.kind === typescript_1.default.SyntaxKind.Identifier) {
|
|
130
|
+
// @ts-expect-error TS2339
|
|
131
|
+
return [qualifier.text, factory.createIdentifier(local)];
|
|
132
|
+
}
|
|
133
|
+
// @ts-expect-error TS7022
|
|
134
|
+
let [name, entity] = getImportName(qualifier.left, local, factory);
|
|
135
|
+
// @ts-expect-error TS2339
|
|
136
|
+
return [name, factory.createQualifiedName(entity, qualifier.right)];
|
|
137
|
+
}
|
package/dist/shake.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.shake = shake;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const nullthrows_1 = __importDefault(require("nullthrows"));
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const wrappers_1 = require("./wrappers");
|
|
11
|
+
function shake(moduleGraph, context, sourceFile) {
|
|
12
|
+
// Factory only exists on TS >= 4.0
|
|
13
|
+
const { factory = typescript_1.default } = context;
|
|
14
|
+
// We traverse things out of order which messes with typescript's internal state.
|
|
15
|
+
// We don't rely on the lexical environment, so just overwrite with noops to avoid errors.
|
|
16
|
+
context.suspendLexicalEnvironment = () => { };
|
|
17
|
+
context.resumeLexicalEnvironment = () => { };
|
|
18
|
+
// Propagate exports from the main module to determine what types should be included
|
|
19
|
+
let exportedNames = moduleGraph.propagate(context);
|
|
20
|
+
// When module definitions are nested inside each other (e.g with module augmentation),
|
|
21
|
+
// we want to keep track of the hierarchy so we can associated nodes with the right module.
|
|
22
|
+
const moduleStack = [];
|
|
23
|
+
let addedGeneratedImports = false;
|
|
24
|
+
let _currentModule;
|
|
25
|
+
let visit = (node) => {
|
|
26
|
+
if (typescript_1.default.isBundle(node)) {
|
|
27
|
+
// @ts-expect-error TS2345
|
|
28
|
+
return factory.updateBundle(node, typescript_1.default.visitNodes(node.sourceFiles, visit));
|
|
29
|
+
}
|
|
30
|
+
// Flatten all module declarations into the top-level scope
|
|
31
|
+
if (typescript_1.default.isModuleDeclaration(node)) {
|
|
32
|
+
// Deeply nested module declarations are assumed to be module augmentations and left alone.
|
|
33
|
+
if (moduleStack.length >= 1) {
|
|
34
|
+
// Since we are hoisting them to the top-level scope, we need to add a "declare" keyword to make them ambient.
|
|
35
|
+
// we also want the declare keyword to come after the export keyword to guarantee a valid typings file.
|
|
36
|
+
// @ts-expect-error TS2540
|
|
37
|
+
node.modifiers ?? (node.modifiers = []);
|
|
38
|
+
const index =
|
|
39
|
+
// @ts-expect-error TS18048
|
|
40
|
+
node.modifiers[0]?.kind === typescript_1.default.SyntaxKind.ExportKeyword ? 1 : 0;
|
|
41
|
+
// @ts-expect-error TS18048
|
|
42
|
+
node.modifiers.splice(index, 0, factory.createModifier(typescript_1.default.SyntaxKind.DeclareKeyword));
|
|
43
|
+
return node;
|
|
44
|
+
}
|
|
45
|
+
moduleStack.push(_currentModule);
|
|
46
|
+
let isFirstModule = !_currentModule;
|
|
47
|
+
_currentModule = moduleGraph.getModule(node.name.text);
|
|
48
|
+
// @ts-expect-error TS2532
|
|
49
|
+
let statements = typescript_1.default.visitEachChild(node, visit, context).body.statements;
|
|
50
|
+
_currentModule = moduleStack.pop();
|
|
51
|
+
if (isFirstModule && !addedGeneratedImports) {
|
|
52
|
+
statements.unshift(...generateImports(factory, moduleGraph));
|
|
53
|
+
addedGeneratedImports = true;
|
|
54
|
+
}
|
|
55
|
+
return statements;
|
|
56
|
+
}
|
|
57
|
+
if (!_currentModule) {
|
|
58
|
+
return typescript_1.default.visitEachChild(node, visit, context);
|
|
59
|
+
}
|
|
60
|
+
// Remove inline imports. They are hoisted to the top of the output.
|
|
61
|
+
if (typescript_1.default.isImportDeclaration(node)) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
let currentModule = (0, nullthrows_1.default)(_currentModule);
|
|
65
|
+
// Remove exports from flattened modules
|
|
66
|
+
if (typescript_1.default.isExportDeclaration(node)) {
|
|
67
|
+
if (!node.moduleSpecifier ||
|
|
68
|
+
// @ts-expect-error TS2339
|
|
69
|
+
moduleGraph.getModule(node.moduleSpecifier.text)) {
|
|
70
|
+
if (!node.moduleSpecifier && node.exportClause) {
|
|
71
|
+
// Filter exported elements to only external re-exports
|
|
72
|
+
let exported = [];
|
|
73
|
+
// @ts-expect-error TS2339
|
|
74
|
+
for (let element of node.exportClause.elements) {
|
|
75
|
+
let name = (element.propertyName ?? element.name).text;
|
|
76
|
+
if (exportedNames.get(name) === currentModule &&
|
|
77
|
+
!currentModule.hasBinding(name)) {
|
|
78
|
+
exported.push(element);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (exported.length > 0) {
|
|
82
|
+
return (0, wrappers_1.updateExportDeclaration)(factory, node, undefined, // modifiers
|
|
83
|
+
false, // isTypeOnly
|
|
84
|
+
factory.updateNamedExports(node.exportClause, exported), undefined, // moduleSpecifier
|
|
85
|
+
undefined);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Remove export assignment if unused.
|
|
92
|
+
if (typescript_1.default.isExportAssignment(node)) {
|
|
93
|
+
let name = currentModule.getName('default');
|
|
94
|
+
if (exportedNames.get(name) !== currentModule) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if ((0, utils_1.isDeclaration)(node)) {
|
|
99
|
+
let name = (0, utils_1.getExportedName)(node) || node.name.text;
|
|
100
|
+
// Remove unused declarations
|
|
101
|
+
if (!currentModule.used.has(name)) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
// Remove original export modifiers
|
|
105
|
+
node.modifiers = (node.modifiers || []).filter(
|
|
106
|
+
// @ts-expect-error TS7006
|
|
107
|
+
(m) => m.kind !== typescript_1.default.SyntaxKind.ExportKeyword &&
|
|
108
|
+
m.kind !== typescript_1.default.SyntaxKind.DefaultKeyword);
|
|
109
|
+
// Rename declarations
|
|
110
|
+
let newName = currentModule.getName(name);
|
|
111
|
+
if (newName !== name && newName !== 'default') {
|
|
112
|
+
node.name = factory.createIdentifier(newName);
|
|
113
|
+
}
|
|
114
|
+
// Export declarations that should be exported
|
|
115
|
+
if (exportedNames.get(newName) === currentModule) {
|
|
116
|
+
if (newName === 'default') {
|
|
117
|
+
node.modifiers.unshift(factory.createModifier(typescript_1.default.SyntaxKind.DefaultKeyword));
|
|
118
|
+
}
|
|
119
|
+
node.modifiers.unshift(factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword));
|
|
120
|
+
}
|
|
121
|
+
else if (typescript_1.default.isFunctionDeclaration(node) ||
|
|
122
|
+
typescript_1.default.isClassDeclaration(node)) {
|
|
123
|
+
// @ts-expect-error TS18048
|
|
124
|
+
node.modifiers.unshift(factory.createModifier(typescript_1.default.SyntaxKind.DeclareKeyword));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (typescript_1.default.isVariableStatement(node)) {
|
|
128
|
+
node = typescript_1.default.visitEachChild(node, visit, context);
|
|
129
|
+
// Remove empty variable statements
|
|
130
|
+
if (node.declarationList.declarations.length === 0) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
// Remove original export modifiers
|
|
134
|
+
node.modifiers = (node.modifiers || []).filter(
|
|
135
|
+
// @ts-expect-error TS7006
|
|
136
|
+
(m) => m.kind !== typescript_1.default.SyntaxKind.ExportKeyword &&
|
|
137
|
+
m.kind !== typescript_1.default.SyntaxKind.DeclareKeyword);
|
|
138
|
+
// Add export modifier if all declarations are exported.
|
|
139
|
+
let isExported = node.declarationList.declarations.every(
|
|
140
|
+
// @ts-expect-error TS7006
|
|
141
|
+
(d) => exportedNames.get(d.name.text) === currentModule);
|
|
142
|
+
if (isExported) {
|
|
143
|
+
node.modifiers.unshift(factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword));
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
// Otherwise, add `declare` modifier (required for top-level declarations in d.ts files).
|
|
147
|
+
node.modifiers.unshift(factory.createModifier(typescript_1.default.SyntaxKind.DeclareKeyword));
|
|
148
|
+
}
|
|
149
|
+
return node;
|
|
150
|
+
}
|
|
151
|
+
if (typescript_1.default.isVariableDeclaration(node)) {
|
|
152
|
+
// Remove unused variables
|
|
153
|
+
// @ts-expect-error TS2339
|
|
154
|
+
if (!currentModule.used.has(node.name.text)) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Rename references
|
|
159
|
+
if (typescript_1.default.isIdentifier(node) && currentModule.names.has(node.text)) {
|
|
160
|
+
let newName = (0, nullthrows_1.default)(currentModule.getName(node.text));
|
|
161
|
+
if (newName !== 'default') {
|
|
162
|
+
return factory.createIdentifier(newName);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// Replace namespace references with final names
|
|
166
|
+
if (typescript_1.default.isQualifiedName(node) && typescript_1.default.isIdentifier(node.left)) {
|
|
167
|
+
let resolved = moduleGraph.resolveImport(currentModule, node.left.text, node.right.text);
|
|
168
|
+
if (resolved && resolved.module.hasBinding(resolved.name)) {
|
|
169
|
+
return factory.createIdentifier(resolved.name);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
return factory.updateQualifiedName(node, factory.createIdentifier(currentModule.getName(node.left.text)), node.right);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// Remove private properties
|
|
176
|
+
if (typescript_1.default.isPropertyDeclaration(node)) {
|
|
177
|
+
let isPrivate = node.modifiers &&
|
|
178
|
+
node.modifiers.some((m) => m.kind === typescript_1.default.SyntaxKind.PrivateKeyword);
|
|
179
|
+
if (isPrivate) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return typescript_1.default.visitEachChild(node, visit, context);
|
|
184
|
+
};
|
|
185
|
+
return typescript_1.default.visitNode(sourceFile, visit);
|
|
186
|
+
}
|
|
187
|
+
function generateImports(factory, moduleGraph) {
|
|
188
|
+
// @ts-expect-error TS2304
|
|
189
|
+
let importStatements = [];
|
|
190
|
+
for (let [specifier, names] of moduleGraph.getAllImports()) {
|
|
191
|
+
let defaultSpecifier;
|
|
192
|
+
let namespaceSpecifier;
|
|
193
|
+
// @ts-expect-error TS2304
|
|
194
|
+
let namedSpecifiers = [];
|
|
195
|
+
for (let [name, imported] of names) {
|
|
196
|
+
if (imported === 'default') {
|
|
197
|
+
defaultSpecifier = factory.createIdentifier(name);
|
|
198
|
+
}
|
|
199
|
+
else if (imported === '*') {
|
|
200
|
+
namespaceSpecifier = factory.createNamespaceImport(factory.createIdentifier(name));
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
namedSpecifiers.push((0, wrappers_1.createImportSpecifier)(factory, false, name === imported ? undefined : factory.createIdentifier(imported), factory.createIdentifier(name)));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (namespaceSpecifier) {
|
|
207
|
+
let importClause = (0, wrappers_1.createImportClause)(factory, false, defaultSpecifier, namespaceSpecifier);
|
|
208
|
+
importStatements.push((0, wrappers_1.createImportDeclaration)(factory, undefined, importClause, factory.createStringLiteral(specifier), undefined));
|
|
209
|
+
defaultSpecifier = undefined;
|
|
210
|
+
}
|
|
211
|
+
if (defaultSpecifier || namedSpecifiers.length > 0) {
|
|
212
|
+
let importClause = (0, wrappers_1.createImportClause)(factory, false, defaultSpecifier, namedSpecifiers.length > 0
|
|
213
|
+
? factory.createNamedImports(namedSpecifiers)
|
|
214
|
+
: undefined);
|
|
215
|
+
importStatements.push((0, wrappers_1.createImportDeclaration)(factory, undefined, importClause, factory.createStringLiteral(specifier), undefined));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return importStatements;
|
|
219
|
+
}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getExportedName = getExportedName;
|
|
7
|
+
exports.isDeclaration = isDeclaration;
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
function getExportedName(node) {
|
|
10
|
+
if (!node.modifiers) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
// @ts-expect-error TS7006
|
|
14
|
+
if (!node.modifiers.some((m) => m.kind === typescript_1.default.SyntaxKind.ExportKeyword)) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
// @ts-expect-error TS7006
|
|
18
|
+
if (node.modifiers.some((m) => m.kind === typescript_1.default.SyntaxKind.DefaultKeyword)) {
|
|
19
|
+
return 'default';
|
|
20
|
+
}
|
|
21
|
+
return node.name.text;
|
|
22
|
+
}
|
|
23
|
+
function isDeclaration(node) {
|
|
24
|
+
return (typescript_1.default.isFunctionDeclaration(node) ||
|
|
25
|
+
typescript_1.default.isClassDeclaration(node) ||
|
|
26
|
+
typescript_1.default.isInterfaceDeclaration(node) ||
|
|
27
|
+
typescript_1.default.isEnumDeclaration(node) ||
|
|
28
|
+
typescript_1.default.isTypeAliasDeclaration(node));
|
|
29
|
+
}
|
package/dist/wrappers.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.updateExportDeclaration = exports.createImportSpecifier = exports.createImportDeclaration = exports.createImportClause = void 0;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const assert_1 = __importDefault(require("assert"));
|
|
9
|
+
const [majorVersion, minorVersion] = typescript_1.default.versionMajorMinor
|
|
10
|
+
.split('.')
|
|
11
|
+
.map((num) => parseInt(num, 10));
|
|
12
|
+
// Everything below was generated using https://github.com/mischnic/tsc-version-wrapper
|
|
13
|
+
exports.createImportClause = (() => {
|
|
14
|
+
if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 0)) {
|
|
15
|
+
return (factory, isTypeOnly, name, namedBindings) => factory.createImportClause(isTypeOnly, name, namedBindings);
|
|
16
|
+
}
|
|
17
|
+
else if (majorVersion > 3 || (majorVersion === 3 && minorVersion >= 8)) {
|
|
18
|
+
return (factory, isTypeOnly, name, namedBindings) => factory.createImportClause(name, namedBindings, isTypeOnly);
|
|
19
|
+
}
|
|
20
|
+
else if (majorVersion > 3 || (majorVersion === 3 && minorVersion >= 0)) {
|
|
21
|
+
return (factory, isTypeOnly, name, namedBindings) => factory.createImportClause(name, namedBindings);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
(0, assert_1.default)(false);
|
|
25
|
+
}
|
|
26
|
+
})();
|
|
27
|
+
exports.createImportDeclaration = (() => {
|
|
28
|
+
if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 8)) {
|
|
29
|
+
return (factory, modifiers, importClause, moduleSpecifier, assertClause) => factory.createImportDeclaration(modifiers, importClause, moduleSpecifier, assertClause);
|
|
30
|
+
}
|
|
31
|
+
else if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 5)) {
|
|
32
|
+
return (factory, modifiers, importClause, moduleSpecifier, assertClause) => factory.createImportDeclaration(undefined /* decorators */, modifiers, importClause, moduleSpecifier, assertClause);
|
|
33
|
+
}
|
|
34
|
+
else if (majorVersion > 3 || (majorVersion === 3 && minorVersion >= 0)) {
|
|
35
|
+
return (factory, modifiers, importClause, moduleSpecifier, assertClause) => factory.createImportDeclaration(undefined /* decorators */, modifiers, importClause, moduleSpecifier);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
(0, assert_1.default)(false);
|
|
39
|
+
}
|
|
40
|
+
})();
|
|
41
|
+
exports.createImportSpecifier = (() => {
|
|
42
|
+
if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 5)) {
|
|
43
|
+
return (factory, isTypeOnly, propertyName, name) => factory.createImportSpecifier(isTypeOnly, propertyName, name);
|
|
44
|
+
}
|
|
45
|
+
else if (majorVersion > 3 || (majorVersion === 3 && minorVersion >= 0)) {
|
|
46
|
+
return (factory, isTypeOnly, propertyName, name) => factory.createImportSpecifier(propertyName, name);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
(0, assert_1.default)(false);
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
exports.updateExportDeclaration = (() => {
|
|
53
|
+
if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 8)) {
|
|
54
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause);
|
|
55
|
+
}
|
|
56
|
+
else if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 5)) {
|
|
57
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause);
|
|
58
|
+
}
|
|
59
|
+
else if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 0)) {
|
|
60
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, isTypeOnly, exportClause, moduleSpecifier);
|
|
61
|
+
}
|
|
62
|
+
else if (majorVersion > 3 || (majorVersion === 3 && minorVersion >= 8)) {
|
|
63
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, exportClause, moduleSpecifier, isTypeOnly);
|
|
64
|
+
}
|
|
65
|
+
else if (majorVersion > 3 || (majorVersion === 3 && minorVersion >= 0)) {
|
|
66
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, exportClause, moduleSpecifier);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
(0, assert_1.default)(false);
|
|
70
|
+
}
|
|
71
|
+
})();
|
package/lib/TSModuleGraph.js
CHANGED
|
@@ -78,15 +78,20 @@ class TSModuleGraph {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
getExport(m, e) {
|
|
81
|
+
// @ts-expect-error TS2339
|
|
81
82
|
(0, _assert().default)(e.name != null);
|
|
83
|
+
// @ts-expect-error TS2339
|
|
82
84
|
let exportName = e.name;
|
|
83
85
|
|
|
84
86
|
// Re-export
|
|
87
|
+
// @ts-expect-error TS2339
|
|
85
88
|
if (e.specifier && e.imported) {
|
|
86
89
|
let m = this.getModule(e.specifier);
|
|
87
90
|
if (!m) {
|
|
88
91
|
return null;
|
|
89
92
|
}
|
|
93
|
+
|
|
94
|
+
// @ts-expect-error TS2339
|
|
90
95
|
let exp = this.resolveExport(m, e.imported);
|
|
91
96
|
if (!exp) {
|
|
92
97
|
return null;
|
|
@@ -115,6 +120,7 @@ class TSModuleGraph {
|
|
|
115
120
|
return {
|
|
116
121
|
module: m,
|
|
117
122
|
name: exportName,
|
|
123
|
+
// @ts-expect-error TS2339
|
|
118
124
|
imported: e.imported != null ? m.getName(e.imported) : exportName
|
|
119
125
|
};
|
|
120
126
|
}
|
|
@@ -136,6 +142,7 @@ class TSModuleGraph {
|
|
|
136
142
|
}
|
|
137
143
|
resolveExport(module, name) {
|
|
138
144
|
for (let e of module.exports) {
|
|
145
|
+
// @ts-expect-error TS2339
|
|
139
146
|
if (e.name === name) {
|
|
140
147
|
return this.getExport(module, e);
|
|
141
148
|
} else if (e.specifier) {
|
|
@@ -149,6 +156,7 @@ class TSModuleGraph {
|
|
|
149
156
|
getAllExports(module = (0, _nullthrows().default)(this.mainModule), excludeDefault = false) {
|
|
150
157
|
let res = [];
|
|
151
158
|
for (let e of module.exports) {
|
|
159
|
+
// @ts-expect-error TS2339
|
|
152
160
|
if (e.name && (!excludeDefault || e.name !== 'default')) {
|
|
153
161
|
let exp = this.getExport(module, e);
|
|
154
162
|
if (exp) {
|
|
@@ -218,11 +226,14 @@ class TSModuleGraph {
|
|
|
218
226
|
// Map of imported specifiers -> map of imported names to local names
|
|
219
227
|
let imports = new Map();
|
|
220
228
|
for (let [m, orig] of importedSymbolsToUpdate) {
|
|
229
|
+
// @ts-expect-error TS2339
|
|
221
230
|
let imp = (0, _nullthrows().default)(m.imports.get(orig));
|
|
231
|
+
// @ts-expect-error TS2345
|
|
222
232
|
let imported = (0, _nullthrows().default)(this.resolveImport(m, orig));
|
|
223
233
|
|
|
224
234
|
// If the module is bundled, map the local name to the original exported name.
|
|
225
235
|
if (this.modules.has(imp.specifier)) {
|
|
236
|
+
// @ts-expect-error TS2339
|
|
226
237
|
m.names.set(orig, imported.imported);
|
|
227
238
|
continue;
|
|
228
239
|
}
|
|
@@ -244,6 +255,8 @@ class TSModuleGraph {
|
|
|
244
255
|
}
|
|
245
256
|
importedNames.set(imported.imported, name);
|
|
246
257
|
}
|
|
258
|
+
|
|
259
|
+
// @ts-expect-error TS2339
|
|
247
260
|
m.names.set(orig, name);
|
|
248
261
|
}
|
|
249
262
|
return exportedNames;
|
|
@@ -98,14 +98,16 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
98
98
|
incremental: false
|
|
99
99
|
};
|
|
100
100
|
let host = new (_tsUtils().CompilerHost)(options.inputFS, _typescript().default, logger);
|
|
101
|
-
//
|
|
101
|
+
// @ts-expect-error TS2345
|
|
102
102
|
let program = _typescript().default.createProgram([asset.filePath], opts, host);
|
|
103
103
|
for (let file of program.getSourceFiles()) {
|
|
104
104
|
if (_path().default.normalize(file.fileName) !== asset.filePath) {
|
|
105
105
|
asset.invalidateOnFileChange(host.redirectTypes.get(file.fileName) ?? file.fileName);
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
|
-
let mainModuleName = (0, _utils().normalizeSeparators)(_path().default
|
|
108
|
+
let mainModuleName = (0, _utils().normalizeSeparators)(_path().default
|
|
109
|
+
// @ts-expect-error TS2339
|
|
110
|
+
.relative(program.getCommonSourceDirectory(), asset.filePath).slice(0, -_path().default.extname(asset.filePath).length));
|
|
109
111
|
let moduleGraph = new _TSModuleGraph.TSModuleGraph(mainModuleName);
|
|
110
112
|
let emitResult = program.emit(undefined, undefined, undefined, true, {
|
|
111
113
|
afterDeclarations: [
|
|
@@ -120,6 +122,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
120
122
|
});
|
|
121
123
|
let diagnostics = _typescript().default.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
122
124
|
let diagnosticIds = new Set();
|
|
125
|
+
// @ts-expect-error TS2304
|
|
123
126
|
let deduplicatedDiagnostics = [];
|
|
124
127
|
for (let d of diagnostics) {
|
|
125
128
|
if (d.start != null && d.length != null && d.messageText != null) {
|
|
@@ -144,8 +147,6 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
144
147
|
if (file.fileName) {
|
|
145
148
|
filename = file.fileName;
|
|
146
149
|
}
|
|
147
|
-
|
|
148
|
-
// $FlowFixMe
|
|
149
150
|
if (source) {
|
|
150
151
|
let lineChar = file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
151
152
|
let start = {
|
|
@@ -191,6 +192,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
191
192
|
let code = (0, _nullthrows().default)(host.outputCode);
|
|
192
193
|
code = code.substring(0, code.lastIndexOf('//# sourceMappingURL'));
|
|
193
194
|
let map = JSON.parse((0, _nullthrows().default)(host.outputMap));
|
|
195
|
+
// @ts-expect-error TS7006
|
|
194
196
|
map.sources = map.sources.map(source => _path().default.join(_path().default.dirname(asset.filePath), source));
|
|
195
197
|
let sourceMap = null;
|
|
196
198
|
if (map.mappings) {
|
package/lib/collect.js
CHANGED
|
@@ -33,6 +33,7 @@ function collect(moduleGraph, context, sourceFile) {
|
|
|
33
33
|
let _currentModule;
|
|
34
34
|
let visit = node => {
|
|
35
35
|
if (_typescript().default.isBundle(node)) {
|
|
36
|
+
// @ts-expect-error TS2345
|
|
36
37
|
return factory.updateBundle(node, _typescript().default.visitNodes(node.sourceFiles, visit));
|
|
37
38
|
}
|
|
38
39
|
if (_typescript().default.isModuleDeclaration(node)) {
|
|
@@ -46,28 +47,43 @@ function collect(moduleGraph, context, sourceFile) {
|
|
|
46
47
|
let currentModule = (0, _nullthrows().default)(_currentModule);
|
|
47
48
|
if (_typescript().default.isImportDeclaration(node) && node.importClause) {
|
|
48
49
|
if (node.importClause.namedBindings) {
|
|
50
|
+
// @ts-expect-error TS2339
|
|
49
51
|
if (node.importClause.namedBindings.elements) {
|
|
52
|
+
// @ts-expect-error TS2339
|
|
50
53
|
for (let element of node.importClause.namedBindings.elements) {
|
|
51
|
-
currentModule.addImport(element.name.text,
|
|
54
|
+
currentModule.addImport(element.name.text,
|
|
55
|
+
// @ts-expect-error TS2339
|
|
56
|
+
node.moduleSpecifier.text, (element.propertyName ?? element.name).text);
|
|
52
57
|
}
|
|
58
|
+
// @ts-expect-error TS2339
|
|
53
59
|
} else if (node.importClause.namedBindings.name) {
|
|
54
|
-
currentModule.addImport(
|
|
60
|
+
currentModule.addImport(
|
|
61
|
+
// @ts-expect-error TS2339
|
|
62
|
+
node.importClause.namedBindings.name.text,
|
|
63
|
+
// @ts-expect-error TS2339
|
|
64
|
+
node.moduleSpecifier.text, '*');
|
|
55
65
|
}
|
|
56
66
|
}
|
|
57
67
|
if (node.importClause.name) {
|
|
58
|
-
currentModule.addImport(node.importClause.name.text,
|
|
68
|
+
currentModule.addImport(node.importClause.name.text,
|
|
69
|
+
// @ts-expect-error TS2339
|
|
70
|
+
node.moduleSpecifier.text, 'default');
|
|
59
71
|
}
|
|
60
72
|
}
|
|
61
73
|
if (_typescript().default.isExportDeclaration(node)) {
|
|
62
74
|
if (node.exportClause) {
|
|
75
|
+
// @ts-expect-error TS2339
|
|
63
76
|
for (let element of node.exportClause.elements) {
|
|
64
77
|
if (node.moduleSpecifier) {
|
|
65
|
-
currentModule.addExport(element.name.text, (element.propertyName ?? element.name).text,
|
|
78
|
+
currentModule.addExport(element.name.text, (element.propertyName ?? element.name).text,
|
|
79
|
+
// @ts-expect-error TS2339
|
|
80
|
+
node.moduleSpecifier.text);
|
|
66
81
|
} else {
|
|
67
82
|
currentModule.addExport(element.name.text, (element.propertyName ?? element.name).text);
|
|
68
83
|
}
|
|
69
84
|
}
|
|
70
85
|
} else {
|
|
86
|
+
// @ts-expect-error TS18048
|
|
71
87
|
currentModule.addWildcardExport(node.moduleSpecifier.text);
|
|
72
88
|
}
|
|
73
89
|
}
|
|
@@ -96,8 +112,10 @@ function collect(moduleGraph, context, sourceFile) {
|
|
|
96
112
|
if (_typescript().default.isVariableStatement(node) && node.modifiers) {
|
|
97
113
|
let isExported = node.modifiers.some(m => m.kind === _typescript().default.SyntaxKind.ExportKeyword);
|
|
98
114
|
for (let v of node.declarationList.declarations) {
|
|
115
|
+
// @ts-expect-error TS2339
|
|
99
116
|
currentModule.addLocal(v.name.text, v);
|
|
100
117
|
if (isExported) {
|
|
118
|
+
// @ts-expect-error TS2339
|
|
101
119
|
currentModule.addExport(v.name.text, v.name.text);
|
|
102
120
|
}
|
|
103
121
|
}
|
|
@@ -116,13 +134,19 @@ function collect(moduleGraph, context, sourceFile) {
|
|
|
116
134
|
// Traverse down an EntityName to the root identifier. Return that to use as the named import specifier,
|
|
117
135
|
// and collect the remaining parts into a new QualifiedName with the local replacement at the root.
|
|
118
136
|
// import('react').JSX.Element => import {JSX} from 'react'; JSX.Element
|
|
137
|
+
// @ts-expect-error TS7023
|
|
119
138
|
function getImportName(qualifier, local, factory) {
|
|
120
139
|
if (!qualifier) {
|
|
140
|
+
// @ts-expect-error TS2339
|
|
121
141
|
return ['*', factory.createIdentifier(local)];
|
|
122
142
|
}
|
|
123
143
|
if (qualifier.kind === _typescript().default.SyntaxKind.Identifier) {
|
|
144
|
+
// @ts-expect-error TS2339
|
|
124
145
|
return [qualifier.text, factory.createIdentifier(local)];
|
|
125
146
|
}
|
|
147
|
+
|
|
148
|
+
// @ts-expect-error TS7022
|
|
126
149
|
let [name, entity] = getImportName(qualifier.left, local, factory);
|
|
150
|
+
// @ts-expect-error TS2339
|
|
127
151
|
return [name, factory.createQualifiedName(entity, qualifier.right)];
|
|
128
152
|
}
|