UNPKG

2.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.typeReferenceToIdentifier = void 0;
4const plugin_debug_logger_1 = require("../plugin-debug-logger");
5const plugin_utils_1 = require("./plugin-utils");
6function typeReferenceToIdentifier(typeReferenceDescriptor, hostFilename, options, factory, type, typeImports, importsToAdd) {
7 if (options.readonly) {
8 assertReferenceableType(type, typeReferenceDescriptor.typeName, hostFilename, options);
9 }
10 const { typeReference, importPath, typeName } = (0, plugin_utils_1.replaceImportPath)(typeReferenceDescriptor.typeName, hostFilename, options);
11 let identifier;
12 if (importPath && !options.readonly) {
13 // Add top-level import to eagarly load class metadata
14 importsToAdd.add(importPath);
15 }
16 if (options.readonly && typeReference?.includes('import')) {
17 if (!typeImports[importPath]) {
18 typeImports[importPath] = typeReference;
19 }
20 let ref = `t["${importPath}"].${typeName}`;
21 if (typeReferenceDescriptor.isArray) {
22 ref = wrapTypeInArray(ref, typeReferenceDescriptor.arrayDepth);
23 }
24 identifier = factory.createIdentifier(ref);
25 }
26 else {
27 let ref = typeReference;
28 if (typeReferenceDescriptor.isArray) {
29 ref = wrapTypeInArray(ref, typeReferenceDescriptor.arrayDepth);
30 }
31 identifier = factory.createIdentifier(ref);
32 }
33 return identifier;
34}
35exports.typeReferenceToIdentifier = typeReferenceToIdentifier;
36function wrapTypeInArray(typeRef, arrayDepth) {
37 for (let i = 0; i < arrayDepth; i++) {
38 typeRef = `[${typeRef}]`;
39 }
40 return typeRef;
41}
42function assertReferenceableType(type, parsedTypeName, hostFilename, options) {
43 if (!type.symbol) {
44 return true;
45 }
46 if (!type.symbol.isReferenced) {
47 return true;
48 }
49 if (parsedTypeName.includes('import')) {
50 return true;
51 }
52 const errorMessage = `Type "${parsedTypeName}" is not referenceable ("${hostFilename}"). To fix this, make sure to export this type.`;
53 if (options.debug) {
54 plugin_debug_logger_1.pluginDebugLogger.debug(errorMessage);
55 }
56 throw new Error(errorMessage);
57}