@atlaspack/transformer-typescript-types 2.14.5-canary.33 → 2.14.5-canary.331

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.
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ const plugin_1 = require("@atlaspack/plugin");
40
+ const path_1 = __importDefault(require("path"));
41
+ const source_map_1 = __importDefault(require("@atlaspack/source-map"));
42
+ const typescript_1 = __importDefault(require("typescript"));
43
+ const ts_utils_1 = require("@atlaspack/ts-utils");
44
+ const utils_1 = require("@atlaspack/utils");
45
+ const diagnostic_1 = __importStar(require("@atlaspack/diagnostic"));
46
+ const TSModuleGraph_1 = require("./TSModuleGraph");
47
+ const nullthrows_1 = __importDefault(require("nullthrows"));
48
+ const collect_1 = require("./collect");
49
+ const shake_1 = require("./shake");
50
+ exports.default = new plugin_1.Transformer({
51
+ loadConfig({ config, options }) {
52
+ return (0, ts_utils_1.loadTSConfig)(config, options);
53
+ },
54
+ transform({ asset, config, options, logger }) {
55
+ let opts = {
56
+ // React is the default. Users can override this by supplying their own tsconfig,
57
+ // which many TypeScript users will already have for typechecking, etc.
58
+ jsx: typescript_1.default.JsxEmit.React,
59
+ moduleResolution: typescript_1.default.ModuleResolutionKind.NodeJs,
60
+ ...config,
61
+ // Always emit output
62
+ noEmit: false,
63
+ noEmitOnError: false,
64
+ declaration: true,
65
+ declarationMap: true,
66
+ isolatedModules: false,
67
+ emitDeclarationOnly: true,
68
+ outFile: 'index.d.ts',
69
+ // createProgram doesn't support incremental mode
70
+ composite: false,
71
+ incremental: false,
72
+ };
73
+ let host = new ts_utils_1.CompilerHost(options.inputFS, typescript_1.default, logger);
74
+ // @ts-expect-error TS2345
75
+ let program = typescript_1.default.createProgram([asset.filePath], opts, host);
76
+ for (let file of program.getSourceFiles()) {
77
+ if (path_1.default.normalize(file.fileName) !== asset.filePath) {
78
+ asset.invalidateOnFileChange(host.redirectTypes.get(file.fileName) ?? file.fileName);
79
+ }
80
+ }
81
+ let mainModuleName = (0, utils_1.normalizeSeparators)(path_1.default
82
+ // @ts-expect-error TS2339
83
+ .relative(program.getCommonSourceDirectory(), asset.filePath)
84
+ .slice(0, -path_1.default.extname(asset.filePath).length));
85
+ let moduleGraph = new TSModuleGraph_1.TSModuleGraph(mainModuleName);
86
+ let emitResult = program.emit(undefined, undefined, undefined, true, {
87
+ afterDeclarations: [
88
+ // 1. Build module graph
89
+ (context) => (sourceFile) => {
90
+ return (0, collect_1.collect)(moduleGraph, context, sourceFile);
91
+ },
92
+ // 2. Tree shake and rename types
93
+ (context) => (sourceFile) => {
94
+ return (0, shake_1.shake)(moduleGraph, context, sourceFile);
95
+ },
96
+ ],
97
+ });
98
+ let diagnostics = typescript_1.default
99
+ .getPreEmitDiagnostics(program)
100
+ .concat(emitResult.diagnostics);
101
+ let diagnosticIds = new Set();
102
+ // @ts-expect-error TS2304
103
+ let deduplicatedDiagnostics = [];
104
+ for (let d of diagnostics) {
105
+ if (d.start != null && d.length != null && d.messageText != null) {
106
+ let id = `${d.start}:${d.length}:${typescript_1.default.flattenDiagnosticMessageText(d.messageText, '\n')}`;
107
+ if (!diagnosticIds.has(id)) {
108
+ deduplicatedDiagnostics.push(d);
109
+ }
110
+ diagnosticIds.add(id);
111
+ }
112
+ else {
113
+ deduplicatedDiagnostics.push(d);
114
+ }
115
+ }
116
+ let parcelDiagnostics = deduplicatedDiagnostics.map((diagnostic) => {
117
+ let filename = asset.filePath;
118
+ let { file } = diagnostic;
119
+ let diagnosticMessage = typescript_1.default.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
120
+ let codeframe;
121
+ if (file != null && diagnostic.start != null) {
122
+ let source = file.text || diagnostic.source;
123
+ if (file.fileName) {
124
+ filename = file.fileName;
125
+ }
126
+ if (source) {
127
+ let lineChar = file.getLineAndCharacterOfPosition(diagnostic.start);
128
+ let start = {
129
+ line: lineChar.line + 1,
130
+ column: lineChar.character + 1,
131
+ };
132
+ let end = {
133
+ line: start.line,
134
+ column: start.column + 1,
135
+ };
136
+ if (typeof diagnostic.start === 'number' &&
137
+ typeof diagnostic.length === 'number') {
138
+ let endCharPosition = file.getLineAndCharacterOfPosition(diagnostic.start + diagnostic.length);
139
+ end = {
140
+ line: endCharPosition.line + 1,
141
+ column: endCharPosition.character,
142
+ };
143
+ }
144
+ codeframe = {
145
+ filePath: filename,
146
+ code: source,
147
+ codeHighlights: [
148
+ {
149
+ start,
150
+ end,
151
+ message: (0, diagnostic_1.escapeMarkdown)(diagnosticMessage),
152
+ },
153
+ ],
154
+ };
155
+ }
156
+ }
157
+ return {
158
+ message: (0, diagnostic_1.escapeMarkdown)(diagnosticMessage),
159
+ codeFrames: codeframe ? [codeframe] : undefined,
160
+ };
161
+ });
162
+ if (host.outputCode == null) {
163
+ throw new diagnostic_1.default({ diagnostic: parcelDiagnostics });
164
+ }
165
+ else {
166
+ for (let d of parcelDiagnostics) {
167
+ logger.warn(d);
168
+ }
169
+ }
170
+ let code = (0, nullthrows_1.default)(host.outputCode);
171
+ code = code.substring(0, code.lastIndexOf('//# sourceMappingURL'));
172
+ let map = JSON.parse((0, nullthrows_1.default)(host.outputMap));
173
+ // @ts-expect-error TS7006
174
+ map.sources = map.sources.map((source) => path_1.default.join(path_1.default.dirname(asset.filePath), source));
175
+ let sourceMap = null;
176
+ if (map.mappings) {
177
+ sourceMap = new source_map_1.default(options.projectRoot);
178
+ sourceMap.addVLQMap(map);
179
+ }
180
+ asset.type = 'ts';
181
+ asset.setCode(code);
182
+ asset.setMap(sourceMap);
183
+ return [asset];
184
+ },
185
+ });
@@ -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
+ }
@@ -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
+ })();