@atlaspack/transformer-typescript-types 2.14.5-canary.24 → 2.14.5-canary.241
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/lib/shake.js
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.shake = shake;
|
|
7
|
-
var _TSModule = require("./TSModule");
|
|
8
7
|
function _typescript() {
|
|
9
8
|
const data = _interopRequireDefault(require("typescript"));
|
|
10
9
|
_typescript = function () {
|
|
@@ -43,6 +42,7 @@ function shake(moduleGraph, context, sourceFile) {
|
|
|
43
42
|
let _currentModule;
|
|
44
43
|
let visit = node => {
|
|
45
44
|
if (_typescript().default.isBundle(node)) {
|
|
45
|
+
// @ts-expect-error TS2345
|
|
46
46
|
return factory.updateBundle(node, _typescript().default.visitNodes(node.sourceFiles, visit));
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -53,14 +53,19 @@ function shake(moduleGraph, context, sourceFile) {
|
|
|
53
53
|
var _node$modifiers$;
|
|
54
54
|
// Since we are hoisting them to the top-level scope, we need to add a "declare" keyword to make them ambient.
|
|
55
55
|
// we also want the declare keyword to come after the export keyword to guarantee a valid typings file.
|
|
56
|
+
// @ts-expect-error TS2540
|
|
56
57
|
node.modifiers ??= [];
|
|
57
|
-
const index =
|
|
58
|
+
const index =
|
|
59
|
+
// @ts-expect-error TS18048
|
|
60
|
+
((_node$modifiers$ = node.modifiers[0]) === null || _node$modifiers$ === void 0 ? void 0 : _node$modifiers$.kind) === _typescript().default.SyntaxKind.ExportKeyword ? 1 : 0;
|
|
61
|
+
// @ts-expect-error TS18048
|
|
58
62
|
node.modifiers.splice(index, 0, factory.createModifier(_typescript().default.SyntaxKind.DeclareKeyword));
|
|
59
63
|
return node;
|
|
60
64
|
}
|
|
61
65
|
moduleStack.push(_currentModule);
|
|
62
66
|
let isFirstModule = !_currentModule;
|
|
63
67
|
_currentModule = moduleGraph.getModule(node.name.text);
|
|
68
|
+
// @ts-expect-error TS2532
|
|
64
69
|
let statements = _typescript().default.visitEachChild(node, visit, context).body.statements;
|
|
65
70
|
_currentModule = moduleStack.pop();
|
|
66
71
|
if (isFirstModule && !addedGeneratedImports) {
|
|
@@ -80,10 +85,13 @@ function shake(moduleGraph, context, sourceFile) {
|
|
|
80
85
|
let currentModule = (0, _nullthrows().default)(_currentModule);
|
|
81
86
|
// Remove exports from flattened modules
|
|
82
87
|
if (_typescript().default.isExportDeclaration(node)) {
|
|
83
|
-
if (!node.moduleSpecifier ||
|
|
88
|
+
if (!node.moduleSpecifier ||
|
|
89
|
+
// @ts-expect-error TS2339
|
|
90
|
+
moduleGraph.getModule(node.moduleSpecifier.text)) {
|
|
84
91
|
if (!node.moduleSpecifier && node.exportClause) {
|
|
85
92
|
// Filter exported elements to only external re-exports
|
|
86
93
|
let exported = [];
|
|
94
|
+
// @ts-expect-error TS2339
|
|
87
95
|
for (let element of node.exportClause.elements) {
|
|
88
96
|
let name = (element.propertyName ?? element.name).text;
|
|
89
97
|
if (exportedNames.get(name) === currentModule && !currentModule.hasBinding(name)) {
|
|
@@ -122,7 +130,9 @@ function shake(moduleGraph, context, sourceFile) {
|
|
|
122
130
|
}
|
|
123
131
|
|
|
124
132
|
// Remove original export modifiers
|
|
125
|
-
node.modifiers = (node.modifiers || []).filter(
|
|
133
|
+
node.modifiers = (node.modifiers || []).filter(
|
|
134
|
+
// @ts-expect-error TS7006
|
|
135
|
+
m => m.kind !== _typescript().default.SyntaxKind.ExportKeyword && m.kind !== _typescript().default.SyntaxKind.DefaultKeyword);
|
|
126
136
|
|
|
127
137
|
// Rename declarations
|
|
128
138
|
let newName = currentModule.getName(name);
|
|
@@ -137,6 +147,7 @@ function shake(moduleGraph, context, sourceFile) {
|
|
|
137
147
|
}
|
|
138
148
|
node.modifiers.unshift(factory.createModifier(_typescript().default.SyntaxKind.ExportKeyword));
|
|
139
149
|
} else if (_typescript().default.isFunctionDeclaration(node) || _typescript().default.isClassDeclaration(node)) {
|
|
150
|
+
// @ts-expect-error TS18048
|
|
140
151
|
node.modifiers.unshift(factory.createModifier(_typescript().default.SyntaxKind.DeclareKeyword));
|
|
141
152
|
}
|
|
142
153
|
}
|
|
@@ -149,10 +160,14 @@ function shake(moduleGraph, context, sourceFile) {
|
|
|
149
160
|
}
|
|
150
161
|
|
|
151
162
|
// Remove original export modifiers
|
|
152
|
-
node.modifiers = (node.modifiers || []).filter(
|
|
163
|
+
node.modifiers = (node.modifiers || []).filter(
|
|
164
|
+
// @ts-expect-error TS7006
|
|
165
|
+
m => m.kind !== _typescript().default.SyntaxKind.ExportKeyword && m.kind !== _typescript().default.SyntaxKind.DeclareKeyword);
|
|
153
166
|
|
|
154
167
|
// Add export modifier if all declarations are exported.
|
|
155
|
-
let isExported = node.declarationList.declarations.every(
|
|
168
|
+
let isExported = node.declarationList.declarations.every(
|
|
169
|
+
// @ts-expect-error TS7006
|
|
170
|
+
d => exportedNames.get(d.name.text) === currentModule);
|
|
156
171
|
if (isExported) {
|
|
157
172
|
node.modifiers.unshift(factory.createModifier(_typescript().default.SyntaxKind.ExportKeyword));
|
|
158
173
|
} else {
|
|
@@ -163,6 +178,7 @@ function shake(moduleGraph, context, sourceFile) {
|
|
|
163
178
|
}
|
|
164
179
|
if (_typescript().default.isVariableDeclaration(node)) {
|
|
165
180
|
// Remove unused variables
|
|
181
|
+
// @ts-expect-error TS2339
|
|
166
182
|
if (!currentModule.used.has(node.name.text)) {
|
|
167
183
|
return null;
|
|
168
184
|
}
|
|
@@ -198,10 +214,12 @@ function shake(moduleGraph, context, sourceFile) {
|
|
|
198
214
|
return _typescript().default.visitNode(sourceFile, visit);
|
|
199
215
|
}
|
|
200
216
|
function generateImports(factory, moduleGraph) {
|
|
217
|
+
// @ts-expect-error TS2304
|
|
201
218
|
let importStatements = [];
|
|
202
219
|
for (let [specifier, names] of moduleGraph.getAllImports()) {
|
|
203
220
|
let defaultSpecifier;
|
|
204
221
|
let namespaceSpecifier;
|
|
222
|
+
// @ts-expect-error TS2304
|
|
205
223
|
let namedSpecifiers = [];
|
|
206
224
|
for (let [name, imported] of names) {
|
|
207
225
|
if (imported === 'default') {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type Import = {
|
|
2
|
+
specifier: string;
|
|
3
|
+
imported: string;
|
|
4
|
+
};
|
|
5
|
+
export type Export = {
|
|
6
|
+
name: string;
|
|
7
|
+
imported: string;
|
|
8
|
+
specifier?: string | null | undefined;
|
|
9
|
+
} | {
|
|
10
|
+
specifier: string;
|
|
11
|
+
};
|
|
12
|
+
export declare class TSModule {
|
|
13
|
+
imports: Map<string, Import>;
|
|
14
|
+
exports: Array<Export>;
|
|
15
|
+
bindings: Map<string, Set<any>>;
|
|
16
|
+
names: Map<string, string>;
|
|
17
|
+
used: Set<string>;
|
|
18
|
+
constructor();
|
|
19
|
+
addImport(local: string, specifier: string, imported: string): void;
|
|
20
|
+
addExport(name: string, imported: string, specifier?: string | null): void;
|
|
21
|
+
addWildcardExport(specifier: string): void;
|
|
22
|
+
addLocal(name: string, node: any): void;
|
|
23
|
+
getName(name: string): string;
|
|
24
|
+
hasBinding(name: string): boolean;
|
|
25
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { TSModule, Export } from './TSModule';
|
|
2
|
+
export declare class TSModuleGraph {
|
|
3
|
+
modules: Map<string, TSModule>;
|
|
4
|
+
mainModuleName: string;
|
|
5
|
+
mainModule: TSModule | null | undefined;
|
|
6
|
+
syntheticImportCount: number;
|
|
7
|
+
constructor(mainModuleName: string);
|
|
8
|
+
addModule(name: string, module: TSModule): void;
|
|
9
|
+
getModule(name: string): TSModule | null | undefined;
|
|
10
|
+
markUsed(module: TSModule, name: string, context: any): void;
|
|
11
|
+
getExport(m: TSModule, e: Export): {
|
|
12
|
+
imported: string;
|
|
13
|
+
module: TSModule;
|
|
14
|
+
name: string;
|
|
15
|
+
} | null | undefined;
|
|
16
|
+
resolveImport(module: TSModule, local: string, imported?: string): {
|
|
17
|
+
imported: string;
|
|
18
|
+
module: TSModule;
|
|
19
|
+
name: string;
|
|
20
|
+
} | null | undefined;
|
|
21
|
+
resolveExport(module: TSModule, name: string): {
|
|
22
|
+
imported: string;
|
|
23
|
+
module: TSModule;
|
|
24
|
+
name: string;
|
|
25
|
+
} | null | undefined;
|
|
26
|
+
getAllExports(module?: TSModule, excludeDefault?: boolean): Array<{
|
|
27
|
+
imported: string;
|
|
28
|
+
module: TSModule;
|
|
29
|
+
name: string;
|
|
30
|
+
}>;
|
|
31
|
+
getAllImports(): Map<string, Map<string, string>>;
|
|
32
|
+
propagate(context: any): Map<string, TSModule>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ExportDeclaration, Expression, Identifier, ImportClause, ImportDeclaration, ImportSpecifier, Modifier, NamedImportBindings } from 'typescript';
|
|
2
|
+
type AssertClause = any;
|
|
3
|
+
type NamedExportBindings = any;
|
|
4
|
+
export declare const createImportClause: (factory: any, isTypeOnly: boolean, name: Identifier | undefined, namedBindings: NamedImportBindings | undefined) => ImportClause;
|
|
5
|
+
export declare const createImportDeclaration: (factory: any, modifiers: Modifier[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, assertClause: AssertClause) => ImportDeclaration;
|
|
6
|
+
export declare const createImportSpecifier: (factory: any, isTypeOnly: boolean, propertyName: Identifier | undefined, name: Identifier) => ImportSpecifier;
|
|
7
|
+
export declare const updateExportDeclaration: (factory: any, node: ExportDeclaration, modifiers: Modifier[] | undefined, isTypeOnly: boolean, exportClause: NamedExportBindings | undefined, moduleSpecifier: Expression | undefined, assertClause: AssertClause | undefined) => ExportDeclaration;
|
|
8
|
+
export {};
|
package/lib/utils.js
CHANGED
|
@@ -17,9 +17,13 @@ function getExportedName(node) {
|
|
|
17
17
|
if (!node.modifiers) {
|
|
18
18
|
return null;
|
|
19
19
|
}
|
|
20
|
+
|
|
21
|
+
// @ts-expect-error TS7006
|
|
20
22
|
if (!node.modifiers.some(m => m.kind === _typescript().default.SyntaxKind.ExportKeyword)) {
|
|
21
23
|
return null;
|
|
22
24
|
}
|
|
25
|
+
|
|
26
|
+
// @ts-expect-error TS7006
|
|
23
27
|
if (node.modifiers.some(m => m.kind === _typescript().default.SyntaxKind.DefaultKeyword)) {
|
|
24
28
|
return 'default';
|
|
25
29
|
}
|
package/lib/wrappers.js
CHANGED
|
@@ -20,6 +20,7 @@ function _assert() {
|
|
|
20
20
|
}
|
|
21
21
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
22
|
/* eslint-disable no-unused-vars */
|
|
23
|
+
|
|
23
24
|
const [majorVersion, minorVersion] = _typescript().default.versionMajorMinor.split('.').map(num => parseInt(num, 10));
|
|
24
25
|
|
|
25
26
|
// Everything below was generated using https://github.com/mischnic/tsc-version-wrapper
|
|
@@ -41,7 +42,7 @@ const createImportDeclaration = exports.createImportDeclaration = (() => {
|
|
|
41
42
|
} else if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 5) {
|
|
42
43
|
return (factory, modifiers, importClause, moduleSpecifier, assertClause) => factory.createImportDeclaration(undefined /* decorators */, modifiers, importClause, moduleSpecifier, assertClause);
|
|
43
44
|
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 0) {
|
|
44
|
-
return (factory, modifiers, importClause, moduleSpecifier) => factory.createImportDeclaration(undefined /* decorators */, modifiers, importClause, moduleSpecifier);
|
|
45
|
+
return (factory, modifiers, importClause, moduleSpecifier, assertClause) => factory.createImportDeclaration(undefined /* decorators */, modifiers, importClause, moduleSpecifier);
|
|
45
46
|
} else {
|
|
46
47
|
(0, _assert().default)(false);
|
|
47
48
|
}
|
|
@@ -61,11 +62,11 @@ const updateExportDeclaration = exports.updateExportDeclaration = (() => {
|
|
|
61
62
|
} else if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 5) {
|
|
62
63
|
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause);
|
|
63
64
|
} else if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 0) {
|
|
64
|
-
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, isTypeOnly, exportClause, moduleSpecifier);
|
|
65
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, isTypeOnly, exportClause, moduleSpecifier);
|
|
65
66
|
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 8) {
|
|
66
|
-
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, exportClause, moduleSpecifier, isTypeOnly);
|
|
67
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, exportClause, moduleSpecifier, isTypeOnly);
|
|
67
68
|
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 0) {
|
|
68
|
-
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, exportClause, moduleSpecifier);
|
|
69
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, exportClause, moduleSpecifier);
|
|
69
70
|
} else {
|
|
70
71
|
(0, _assert().default)(false);
|
|
71
72
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/transformer-typescript-types",
|
|
3
|
-
"version": "2.14.5-canary.
|
|
3
|
+
"version": "2.14.5-canary.241+816fbf634",
|
|
4
4
|
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -9,16 +9,17 @@
|
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/atlassian-labs/atlaspack.git"
|
|
11
11
|
},
|
|
12
|
-
"main": "lib/TSTypesTransformer.js",
|
|
13
|
-
"source": "src/TSTypesTransformer.
|
|
12
|
+
"main": "./lib/TSTypesTransformer.js",
|
|
13
|
+
"source": "./src/TSTypesTransformer.ts",
|
|
14
|
+
"types": "./lib/types/TSTypesTransformer.d.ts",
|
|
14
15
|
"engines": {
|
|
15
16
|
"node": ">= 16.0.0"
|
|
16
17
|
},
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"@atlaspack/diagnostic": "2.14.1-canary.
|
|
19
|
-
"@atlaspack/plugin": "2.14.5-canary.
|
|
20
|
-
"@atlaspack/ts-utils": "2.14.1-canary.
|
|
21
|
-
"@atlaspack/utils": "2.14.5-canary.
|
|
19
|
+
"@atlaspack/diagnostic": "2.14.1-canary.309+816fbf634",
|
|
20
|
+
"@atlaspack/plugin": "2.14.5-canary.241+816fbf634",
|
|
21
|
+
"@atlaspack/ts-utils": "2.14.1-canary.309+816fbf634",
|
|
22
|
+
"@atlaspack/utils": "2.14.5-canary.241+816fbf634",
|
|
22
23
|
"@parcel/source-map": "^2.1.1",
|
|
23
24
|
"nullthrows": "^1.1.1"
|
|
24
25
|
},
|
|
@@ -29,5 +30,8 @@
|
|
|
29
30
|
"typescript": ">=3.0.0"
|
|
30
31
|
},
|
|
31
32
|
"type": "commonjs",
|
|
32
|
-
"
|
|
33
|
-
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
35
|
+
},
|
|
36
|
+
"gitHead": "816fbf634125215c3b4f2d79c0cd4a976648f1dc"
|
|
37
|
+
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export type Import = {
|
|
2
|
+
specifier: string;
|
|
3
|
+
imported: string;
|
|
4
|
+
};
|
|
4
5
|
export type Export =
|
|
5
|
-
| {
|
|
6
|
-
|
|
6
|
+
| {
|
|
7
|
+
name: string;
|
|
8
|
+
imported: string;
|
|
9
|
+
specifier?: string | null | undefined;
|
|
10
|
+
}
|
|
11
|
+
| {
|
|
12
|
+
specifier: string;
|
|
13
|
+
};
|
|
7
14
|
|
|
8
15
|
export class TSModule {
|
|
9
16
|
imports: Map<string, Import>;
|
|
@@ -28,7 +35,7 @@ export class TSModule {
|
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
// if not a reexport: imported = local, name = exported
|
|
31
|
-
addExport(name: string, imported: string, specifier
|
|
38
|
+
addExport(name: string, imported: string, specifier?: string | null) {
|
|
32
39
|
this.exports.push({name, specifier, imported});
|
|
33
40
|
}
|
|
34
41
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import type {TSModule, Export} from './TSModule';
|
|
3
2
|
|
|
4
3
|
import nullthrows from 'nullthrows';
|
|
@@ -8,7 +7,7 @@ import ts from 'typescript';
|
|
|
8
7
|
export class TSModuleGraph {
|
|
9
8
|
modules: Map<string, TSModule>;
|
|
10
9
|
mainModuleName: string;
|
|
11
|
-
mainModule:
|
|
10
|
+
mainModule: TSModule | null | undefined;
|
|
12
11
|
syntheticImportCount: number;
|
|
13
12
|
|
|
14
13
|
constructor(mainModuleName: string) {
|
|
@@ -25,7 +24,7 @@ export class TSModuleGraph {
|
|
|
25
24
|
}
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
getModule(name: string):
|
|
27
|
+
getModule(name: string): TSModule | null | undefined {
|
|
29
28
|
return this.modules.get(name);
|
|
30
29
|
}
|
|
31
30
|
|
|
@@ -77,17 +76,28 @@ export class TSModuleGraph {
|
|
|
77
76
|
getExport(
|
|
78
77
|
m: TSModule,
|
|
79
78
|
e: Export,
|
|
80
|
-
):
|
|
79
|
+
):
|
|
80
|
+
| {
|
|
81
|
+
imported: string;
|
|
82
|
+
module: TSModule;
|
|
83
|
+
name: string;
|
|
84
|
+
}
|
|
85
|
+
| null
|
|
86
|
+
| undefined {
|
|
87
|
+
// @ts-expect-error TS2339
|
|
81
88
|
invariant(e.name != null);
|
|
89
|
+
// @ts-expect-error TS2339
|
|
82
90
|
let exportName = e.name;
|
|
83
91
|
|
|
84
92
|
// Re-export
|
|
93
|
+
// @ts-expect-error TS2339
|
|
85
94
|
if (e.specifier && e.imported) {
|
|
86
95
|
let m = this.getModule(e.specifier);
|
|
87
96
|
if (!m) {
|
|
88
97
|
return null;
|
|
89
98
|
}
|
|
90
99
|
|
|
100
|
+
// @ts-expect-error TS2339
|
|
91
101
|
let exp = this.resolveExport(m, e.imported);
|
|
92
102
|
if (!exp) {
|
|
93
103
|
return null;
|
|
@@ -114,6 +124,7 @@ export class TSModuleGraph {
|
|
|
114
124
|
return {
|
|
115
125
|
module: m,
|
|
116
126
|
name: exportName,
|
|
127
|
+
// @ts-expect-error TS2339
|
|
117
128
|
imported: e.imported != null ? m.getName(e.imported) : exportName,
|
|
118
129
|
};
|
|
119
130
|
}
|
|
@@ -122,7 +133,14 @@ export class TSModuleGraph {
|
|
|
122
133
|
module: TSModule,
|
|
123
134
|
local: string,
|
|
124
135
|
imported?: string,
|
|
125
|
-
):
|
|
136
|
+
):
|
|
137
|
+
| {
|
|
138
|
+
imported: string;
|
|
139
|
+
module: TSModule;
|
|
140
|
+
name: string;
|
|
141
|
+
}
|
|
142
|
+
| null
|
|
143
|
+
| undefined {
|
|
126
144
|
let i = module.imports.get(local);
|
|
127
145
|
if (!i) {
|
|
128
146
|
return null;
|
|
@@ -140,8 +158,16 @@ export class TSModuleGraph {
|
|
|
140
158
|
resolveExport(
|
|
141
159
|
module: TSModule,
|
|
142
160
|
name: string,
|
|
143
|
-
):
|
|
161
|
+
):
|
|
162
|
+
| {
|
|
163
|
+
imported: string;
|
|
164
|
+
module: TSModule;
|
|
165
|
+
name: string;
|
|
166
|
+
}
|
|
167
|
+
| null
|
|
168
|
+
| undefined {
|
|
144
169
|
for (let e of module.exports) {
|
|
170
|
+
// @ts-expect-error TS2339
|
|
145
171
|
if (e.name === name) {
|
|
146
172
|
return this.getExport(module, e);
|
|
147
173
|
} else if (e.specifier) {
|
|
@@ -159,9 +185,18 @@ export class TSModuleGraph {
|
|
|
159
185
|
getAllExports(
|
|
160
186
|
module: TSModule = nullthrows(this.mainModule),
|
|
161
187
|
excludeDefault: boolean = false,
|
|
162
|
-
): Array<{
|
|
163
|
-
|
|
188
|
+
): Array<{
|
|
189
|
+
imported: string;
|
|
190
|
+
module: TSModule;
|
|
191
|
+
name: string;
|
|
192
|
+
}> {
|
|
193
|
+
let res: Array<{
|
|
194
|
+
imported: string;
|
|
195
|
+
module: TSModule;
|
|
196
|
+
name: string;
|
|
197
|
+
}> = [];
|
|
164
198
|
for (let e of module.exports) {
|
|
199
|
+
// @ts-expect-error TS2339
|
|
165
200
|
if (e.name && (!excludeDefault || e.name !== 'default')) {
|
|
166
201
|
let exp = this.getExport(module, e);
|
|
167
202
|
if (exp) {
|
|
@@ -209,7 +244,7 @@ export class TSModuleGraph {
|
|
|
209
244
|
exportedNames.set(e.name, e.module);
|
|
210
245
|
}
|
|
211
246
|
|
|
212
|
-
let importedSymbolsToUpdate = [];
|
|
247
|
+
let importedSymbolsToUpdate: Array<Array<TSModule | string>> = [];
|
|
213
248
|
|
|
214
249
|
// Assign unique names across all modules
|
|
215
250
|
for (let m of this.modules.values()) {
|
|
@@ -240,11 +275,14 @@ export class TSModuleGraph {
|
|
|
240
275
|
let imports = new Map();
|
|
241
276
|
|
|
242
277
|
for (let [m, orig] of importedSymbolsToUpdate) {
|
|
278
|
+
// @ts-expect-error TS2339
|
|
243
279
|
let imp = nullthrows(m.imports.get(orig));
|
|
280
|
+
// @ts-expect-error TS2345
|
|
244
281
|
let imported = nullthrows(this.resolveImport(m, orig));
|
|
245
282
|
|
|
246
283
|
// If the module is bundled, map the local name to the original exported name.
|
|
247
284
|
if (this.modules.has(imp.specifier)) {
|
|
285
|
+
// @ts-expect-error TS2339
|
|
248
286
|
m.names.set(orig, imported.imported);
|
|
249
287
|
continue;
|
|
250
288
|
}
|
|
@@ -269,6 +307,7 @@ export class TSModuleGraph {
|
|
|
269
307
|
importedNames.set(imported.imported, name);
|
|
270
308
|
}
|
|
271
309
|
|
|
310
|
+
// @ts-expect-error TS2339
|
|
272
311
|
m.names.set(orig, name);
|
|
273
312
|
}
|
|
274
313
|
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// @flow strict-local
|
|
2
|
-
|
|
3
1
|
import {Transformer} from '@atlaspack/plugin';
|
|
4
2
|
import path from 'path';
|
|
5
3
|
import SourceMap from '@parcel/source-map';
|
|
@@ -15,7 +13,7 @@ import nullthrows from 'nullthrows';
|
|
|
15
13
|
import {collect} from './collect';
|
|
16
14
|
import {shake} from './shake';
|
|
17
15
|
|
|
18
|
-
export default
|
|
16
|
+
export default new Transformer({
|
|
19
17
|
loadConfig({config, options}) {
|
|
20
18
|
return loadTSConfig(config, options);
|
|
21
19
|
},
|
|
@@ -41,7 +39,7 @@ export default (new Transformer({
|
|
|
41
39
|
};
|
|
42
40
|
|
|
43
41
|
let host = new CompilerHost(options.inputFS, ts, logger);
|
|
44
|
-
//
|
|
42
|
+
// @ts-expect-error TS2345
|
|
45
43
|
let program = ts.createProgram([asset.filePath], opts, host);
|
|
46
44
|
|
|
47
45
|
for (let file of program.getSourceFiles()) {
|
|
@@ -54,6 +52,7 @@ export default (new Transformer({
|
|
|
54
52
|
|
|
55
53
|
let mainModuleName = normalizeSeparators(
|
|
56
54
|
path
|
|
55
|
+
// @ts-expect-error TS2339
|
|
57
56
|
.relative(program.getCommonSourceDirectory(), asset.filePath)
|
|
58
57
|
.slice(0, -path.extname(asset.filePath).length),
|
|
59
58
|
);
|
|
@@ -62,11 +61,11 @@ export default (new Transformer({
|
|
|
62
61
|
let emitResult = program.emit(undefined, undefined, undefined, true, {
|
|
63
62
|
afterDeclarations: [
|
|
64
63
|
// 1. Build module graph
|
|
65
|
-
(context) => (sourceFile) => {
|
|
64
|
+
(context: any) => (sourceFile: any) => {
|
|
66
65
|
return collect(moduleGraph, context, sourceFile);
|
|
67
66
|
},
|
|
68
67
|
// 2. Tree shake and rename types
|
|
69
|
-
(context) => (sourceFile) => {
|
|
68
|
+
(context: any) => (sourceFile: any) => {
|
|
70
69
|
return shake(moduleGraph, context, sourceFile);
|
|
71
70
|
},
|
|
72
71
|
],
|
|
@@ -77,7 +76,8 @@ export default (new Transformer({
|
|
|
77
76
|
.concat(emitResult.diagnostics);
|
|
78
77
|
|
|
79
78
|
let diagnosticIds = new Set();
|
|
80
|
-
|
|
79
|
+
// @ts-expect-error TS2304
|
|
80
|
+
let deduplicatedDiagnostics: Array<Diagnostic> = [];
|
|
81
81
|
for (let d of diagnostics) {
|
|
82
82
|
if (d.start != null && d.length != null && d.messageText != null) {
|
|
83
83
|
let id = `${d.start}:${d.length}:${ts.flattenDiagnosticMessageText(
|
|
@@ -102,14 +102,13 @@ export default (new Transformer({
|
|
|
102
102
|
'\n',
|
|
103
103
|
);
|
|
104
104
|
|
|
105
|
-
let codeframe:
|
|
105
|
+
let codeframe: DiagnosticCodeFrame | null | undefined;
|
|
106
106
|
if (file != null && diagnostic.start != null) {
|
|
107
107
|
let source = file.text || diagnostic.source;
|
|
108
108
|
if (file.fileName) {
|
|
109
109
|
filename = file.fileName;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
// $FlowFixMe
|
|
113
112
|
if (source) {
|
|
114
113
|
let lineChar = file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
115
114
|
let start = {
|
|
@@ -167,6 +166,7 @@ export default (new Transformer({
|
|
|
167
166
|
code = code.substring(0, code.lastIndexOf('//# sourceMappingURL'));
|
|
168
167
|
|
|
169
168
|
let map = JSON.parse(nullthrows(host.outputMap));
|
|
169
|
+
// @ts-expect-error TS7006
|
|
170
170
|
map.sources = map.sources.map((source) =>
|
|
171
171
|
path.join(path.dirname(asset.filePath), source),
|
|
172
172
|
);
|
|
@@ -182,4 +182,4 @@ export default (new Transformer({
|
|
|
182
182
|
asset.setMap(sourceMap);
|
|
183
183
|
return [asset];
|
|
184
184
|
},
|
|
185
|
-
})
|
|
185
|
+
}) as Transformer<unknown>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import type {TSModuleGraph} from './TSModuleGraph';
|
|
3
2
|
|
|
4
3
|
import nullthrows from 'nullthrows';
|
|
5
|
-
import ts, {
|
|
4
|
+
import ts, {EntityName} from 'typescript';
|
|
6
5
|
import {TSModule} from './TSModule';
|
|
7
6
|
import {getExportedName, isDeclaration} from './utils';
|
|
8
7
|
|
|
@@ -16,10 +15,11 @@ export function collect(
|
|
|
16
15
|
|
|
17
16
|
// When module definitions are nested inside each other (e.g with module augmentation),
|
|
18
17
|
// we want to keep track of the hierarchy so we can associated nodes with the right module.
|
|
19
|
-
const moduleStack: Array
|
|
20
|
-
let _currentModule:
|
|
18
|
+
const moduleStack: Array<TSModule | null | undefined> = [];
|
|
19
|
+
let _currentModule: TSModule | null | undefined;
|
|
21
20
|
let visit = (node: any): any => {
|
|
22
21
|
if (ts.isBundle(node)) {
|
|
22
|
+
// @ts-expect-error TS2345
|
|
23
23
|
return factory.updateBundle(node, ts.visitNodes(node.sourceFiles, visit));
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -36,17 +36,23 @@ export function collect(
|
|
|
36
36
|
let currentModule = nullthrows(_currentModule);
|
|
37
37
|
if (ts.isImportDeclaration(node) && node.importClause) {
|
|
38
38
|
if (node.importClause.namedBindings) {
|
|
39
|
+
// @ts-expect-error TS2339
|
|
39
40
|
if (node.importClause.namedBindings.elements) {
|
|
41
|
+
// @ts-expect-error TS2339
|
|
40
42
|
for (let element of node.importClause.namedBindings.elements) {
|
|
41
43
|
currentModule.addImport(
|
|
42
44
|
element.name.text,
|
|
45
|
+
// @ts-expect-error TS2339
|
|
43
46
|
node.moduleSpecifier.text,
|
|
44
47
|
(element.propertyName ?? element.name).text,
|
|
45
48
|
);
|
|
46
49
|
}
|
|
50
|
+
// @ts-expect-error TS2339
|
|
47
51
|
} else if (node.importClause.namedBindings.name) {
|
|
48
52
|
currentModule.addImport(
|
|
53
|
+
// @ts-expect-error TS2339
|
|
49
54
|
node.importClause.namedBindings.name.text,
|
|
55
|
+
// @ts-expect-error TS2339
|
|
50
56
|
node.moduleSpecifier.text,
|
|
51
57
|
'*',
|
|
52
58
|
);
|
|
@@ -56,6 +62,7 @@ export function collect(
|
|
|
56
62
|
if (node.importClause.name) {
|
|
57
63
|
currentModule.addImport(
|
|
58
64
|
node.importClause.name.text,
|
|
65
|
+
// @ts-expect-error TS2339
|
|
59
66
|
node.moduleSpecifier.text,
|
|
60
67
|
'default',
|
|
61
68
|
);
|
|
@@ -64,11 +71,13 @@ export function collect(
|
|
|
64
71
|
|
|
65
72
|
if (ts.isExportDeclaration(node)) {
|
|
66
73
|
if (node.exportClause) {
|
|
74
|
+
// @ts-expect-error TS2339
|
|
67
75
|
for (let element of node.exportClause.elements) {
|
|
68
76
|
if (node.moduleSpecifier) {
|
|
69
77
|
currentModule.addExport(
|
|
70
78
|
element.name.text,
|
|
71
79
|
(element.propertyName ?? element.name).text,
|
|
80
|
+
// @ts-expect-error TS2339
|
|
72
81
|
node.moduleSpecifier.text,
|
|
73
82
|
);
|
|
74
83
|
} else {
|
|
@@ -79,6 +88,7 @@ export function collect(
|
|
|
79
88
|
}
|
|
80
89
|
}
|
|
81
90
|
} else {
|
|
91
|
+
// @ts-expect-error TS18048
|
|
82
92
|
currentModule.addWildcardExport(node.moduleSpecifier.text);
|
|
83
93
|
}
|
|
84
94
|
}
|
|
@@ -118,8 +128,10 @@ export function collect(
|
|
|
118
128
|
(m) => m.kind === ts.SyntaxKind.ExportKeyword,
|
|
119
129
|
);
|
|
120
130
|
for (let v of node.declarationList.declarations) {
|
|
131
|
+
// @ts-expect-error TS2339
|
|
121
132
|
currentModule.addLocal(v.name.text, v);
|
|
122
133
|
if (isExported) {
|
|
134
|
+
// @ts-expect-error TS2339
|
|
123
135
|
currentModule.addExport(v.name.text, v.name.text);
|
|
124
136
|
}
|
|
125
137
|
}
|
|
@@ -139,19 +151,24 @@ export function collect(
|
|
|
139
151
|
// Traverse down an EntityName to the root identifier. Return that to use as the named import specifier,
|
|
140
152
|
// and collect the remaining parts into a new QualifiedName with the local replacement at the root.
|
|
141
153
|
// import('react').JSX.Element => import {JSX} from 'react'; JSX.Element
|
|
154
|
+
// @ts-expect-error TS7023
|
|
142
155
|
function getImportName(
|
|
143
|
-
qualifier:
|
|
156
|
+
qualifier: EntityName | null | undefined,
|
|
144
157
|
local: string,
|
|
145
158
|
factory: typeof ts,
|
|
146
159
|
) {
|
|
147
160
|
if (!qualifier) {
|
|
161
|
+
// @ts-expect-error TS2339
|
|
148
162
|
return ['*', factory.createIdentifier(local)];
|
|
149
163
|
}
|
|
150
164
|
|
|
151
165
|
if (qualifier.kind === ts.SyntaxKind.Identifier) {
|
|
166
|
+
// @ts-expect-error TS2339
|
|
152
167
|
return [qualifier.text, factory.createIdentifier(local)];
|
|
153
168
|
}
|
|
154
169
|
|
|
170
|
+
// @ts-expect-error TS7022
|
|
155
171
|
let [name, entity] = getImportName(qualifier.left, local, factory);
|
|
172
|
+
// @ts-expect-error TS2339
|
|
156
173
|
return [name, factory.createQualifiedName(entity, qualifier.right)];
|
|
157
174
|
}
|