Skip to content

Commit f16b6e4

Browse files
committed
save
1 parent bda9159 commit f16b6e4

18 files changed

+257
-139
lines changed

packages/type-compiler/dist/.gitkeep

Whitespace-only changes.

packages/type-compiler/src/compiler.ts

+32-30
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import {
8282
getNameAsString,
8383
getPropertyName,
8484
getRuntimeTypeName,
85-
hasModifier,
85+
hasModifier, hasSourceFile,
8686
isBuiltType,
8787
isNodeWithLocals,
8888
NodeConverter,
@@ -465,7 +465,7 @@ class CompilerProgram {
465465
return frame.variables.length - 1;
466466
}
467467

468-
pushTemplateParameter(name: string, withDefault: boolean = false): number {
468+
pushTemplateParameter(name: string, withDefault: boolean = false): number {
469469
this.pushOp(withDefault ? ReflectionOp.typeParameterDefault : ReflectionOp.typeParameter, this.findOrAddStackEntry(name));
470470
this.frame.variables.push({
471471
index: this.frame.variables.length,
@@ -1343,11 +1343,11 @@ export class ReflectionTransformer implements CustomTransformer {
13431343
const narrowed = node as ClassDeclaration | ClassExpression;
13441344
//class nodes have always their own program, so the start is always fresh, means we don't need a frame
13451345

1346-
if (this.external.isEmbeddingExternalLibraryImport()) {
1346+
/*if (this.external.isEmbeddingExternalLibraryImport()) {
13471347
const declaration = this.nodeConverter.convertClassToInterface(narrowed);
13481348
this.extractPackStructOfType(declaration, program);
13491349
break;
1350-
}
1350+
}*/
13511351

13521352
if (node) {
13531353
const members: ClassElement[] = [];
@@ -1363,7 +1363,7 @@ export class ReflectionTransformer implements CustomTransformer {
13631363
}
13641364
}
13651365

1366-
if (narrowed.heritageClauses) {
1366+
if (!this.external.isEmbeddingExternalLibraryImport() && narrowed.heritageClauses) {
13671367
for (const heritage of narrowed.heritageClauses) {
13681368
if (heritage.token === SyntaxKind.ExtendsKeyword) {
13691369
for (const extendType of heritage.types) {
@@ -1394,7 +1394,8 @@ export class ReflectionTransformer implements CustomTransformer {
13941394
this.extractPackStructOfType(member, program);
13951395
}
13961396

1397-
program.pushOp(ReflectionOp.class);
1397+
program.pushOp(ReflectionOp.class)
1398+
// program.pushOp(!this.external.isEmbeddingExternalLibraryImport() ? ReflectionOp.class : ReflectionOp.objectLiteral);
13981399

13991400
if (narrowed.heritageClauses) {
14001401
for (const heritageClause of narrowed.heritageClauses) {
@@ -1416,7 +1417,9 @@ export class ReflectionTransformer implements CustomTransformer {
14161417
}
14171418
}
14181419

1419-
if (narrowed.name) this.resolveTypeName(getIdentifierName(narrowed.name), program);
1420+
if (narrowed.name) {
1421+
this.resolveTypeName(getIdentifierName(narrowed.name), program);
1422+
}
14201423

14211424
const description = extractJSDocAttribute(narrowed, 'description');
14221425
if (description) program.pushOp(ReflectionOp.description, program.findOrAddStackEntry(description));
@@ -1696,10 +1699,6 @@ export class ReflectionTransformer implements CustomTransformer {
16961699
const narrowed = node as MethodSignature | MethodDeclaration | CallSignatureDeclaration | ConstructorTypeNode
16971700
| ConstructSignatureDeclaration | ConstructorDeclaration | ArrowFunction | FunctionExpression | FunctionTypeNode | FunctionDeclaration;
16981701

1699-
if(this.external.isEmbeddingExternalLibraryImport() && (isFunctionDeclaration(node) || isFunctionExpression(node) || isMethodDeclaration(node))) {
1700-
console.log(getNameAsString(narrowed.name));
1701-
}
1702-
17031702
const config = this.findReflectionConfig(narrowed, program);
17041703
if (config.mode === 'never') return;
17051704

@@ -2034,7 +2033,7 @@ export class ReflectionTransformer implements CustomTransformer {
20342033
if (importDeclaration.importClause && importDeclaration.importClause.isTypeOnly) typeOnly = true;
20352034
declaration = this.resolveImportSpecifier(typeName.escapedText, importDeclaration, this.sourceFile);
20362035
//might be an external library
2037-
if (!declaration) {
2036+
if (!declaration && hasSourceFile(importDeclaration)) {
20382037
sourceFile = importDeclaration.getSourceFile();
20392038
declaration = this.resolveImportSpecifier(typeName.escapedText, importDeclaration, sourceFile);
20402039
}
@@ -2096,6 +2095,7 @@ export class ReflectionTransformer implements CustomTransformer {
20962095
// check if the referenced declaration has reflection disabled
20972096
const declarationReflection = this.findReflectionConfig(declaration, program);
20982097
if (declarationReflection.mode !== 'never') {
2098+
if (!hasSourceFile(importDeclaration)) return expression;
20992099
const declarationSourceFile = importDeclaration.getSourceFile();
21002100

21012101
const runtimeTypeName = this.getRuntimeTypeName(typeName);
@@ -2106,10 +2106,11 @@ export class ReflectionTransformer implements CustomTransformer {
21062106

21072107
if (isEmbeddingExternalLibraryImport || (!builtType && this.external.shouldInlineExternalLibraryImport(importDeclaration, typeName, declarationReflection))) {
21082108
const { module } = this.external.processExternalLibraryImport(typeName, declaration, declarationSourceFile, importDeclaration);
2109-
const newExpression = this.f.createPropertyAccessExpression(this.f.createIdentifier(getExternalRuntimeTypeName(module.packageId.name)), typeName);
2110-
return !isEmbeddingExternalLibraryImport && (isClassDeclaration(declaration) || isFunctionDeclaration(declaration))
2111-
? this.wrapWithAssignType(expression, newExpression)
2112-
: newExpression
2109+
return this.f.createPropertyAccessExpression(this.f.createIdentifier(getExternalRuntimeTypeName(module.packageId.name)), typeName);
2110+
// const newExpression = this.f.createPropertyAccessExpression(this.f.createIdentifier(getExternalRuntimeTypeName(module.packageId.name)), typeName);
2111+
// return !isEmbeddingExternalLibraryImport && (isClassDeclaration(declaration) || isFunctionDeclaration(declaration))
2112+
// ? this.wrapWithAssignType(expression, newExpression)
2113+
// : newExpression
21132114
}
21142115
}
21152116

@@ -2215,7 +2216,6 @@ export class ReflectionTransformer implements CustomTransformer {
22152216
if (isModuleDeclaration(declaration) && resolved.importDeclaration) {
22162217
let expression: Expression = serializeEntityNameAsExpression(this.f, typeName);
22172218
if (isIdentifier(typeName)) {
2218-
console.log(2, getNameAsString(typeName));
22192219
expression = this.resolveImportExpression(declaration, resolved.importDeclaration, typeName, expression, program)
22202220
}
22212221

@@ -2309,19 +2309,20 @@ export class ReflectionTransformer implements CustomTransformer {
23092309
return;
23102310
}
23112311

2312-
const builtType = isBuiltType(runtimeTypeName, found);
2313-
if (!builtType) {
2314-
if (!this.external.shouldInlineExternalLibraryImport(resolved.importDeclaration, typeName, declarationReflection)) return;
2315-
this.external.processExternalLibraryImport(typeName, declaration, declarationSourceFile, resolved.importDeclaration);
2312+
const reflection = this.findReflectionFromPath(found.fileName);
2313+
if (reflection.mode !== 'never') {
2314+
this.addImports.push({ identifier: runtimeTypeName, from: resolved.importDeclaration.moduleSpecifier });
2315+
return;
23162316
} else {
2317-
//check if the referenced file has reflection info emitted. if not, any is emitted for that reference
2318-
const reflection = this.findReflectionFromPath(found.fileName);
2319-
if (reflection.mode === 'never') {
2320-
program.pushOp(ReflectionOp.any);
2321-
return;
2317+
const builtType = isBuiltType(runtimeTypeName, found);
2318+
if (!builtType) {
2319+
if (this.external.shouldInlineExternalLibraryImport(resolved.importDeclaration, typeName, declarationReflection)) {
2320+
this.external.processExternalLibraryImport(typeName, declaration, declarationSourceFile, resolved.importDeclaration);
2321+
} else {
2322+
program.pushOp(ReflectionOp.any);
2323+
return;
2324+
}
23222325
}
2323-
2324-
this.addImports.push({ identifier: runtimeTypeName, from: resolved.importDeclaration.moduleSpecifier });
23252326
}
23262327
}
23272328
} else {
@@ -2384,14 +2385,15 @@ export class ReflectionTransformer implements CustomTransformer {
23842385
this.extractPackStructOfType(typeArgument, program);
23852386
}
23862387
}
2387-
if (this.external.isEmbeddingExternalLibraryImport()) {
2388+
/*if (this.external.isEmbeddingExternalLibraryImport()) {
23882389
if (isClassDeclaration(declaration)) {
23892390
program.pushOp(ReflectionOp.objectLiteral);
23902391
} else {
23912392
const index = program.pushStack(this.f.createArrowFunction(undefined, undefined, [], undefined, undefined, body));
23922393
program.pushOp(ReflectionOp.function, index);
23932394
}
2394-
} else {
2395+
this.resolveTypeName(getNameAsString(typeName), program);
2396+
} else */{
23952397
const index = program.pushStack(this.f.createArrowFunction(undefined, undefined, [], undefined, undefined, body));
23962398
program.pushOp(isClassDeclaration(declaration) ? ReflectionOp.classReference : ReflectionOp.functionReference, index);
23972399
}

packages/type-compiler/src/external.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
isStringLiteral,
99
} = ts;
1010

11-
import { getEntityName, getNameAsString } from './reflection-ast.js';
11+
import { getEntityName, getNameAsString, hasSourceFile } from './reflection-ast.js';
1212
import { ReflectionConfig } from './compiler.js';
1313
import { Resolver } from './resolver.js';
1414

@@ -68,6 +68,7 @@ export class External {
6868

6969
public shouldInlineExternalLibraryImport(importDeclaration: ImportDeclaration, entityName: EntityName, config: ReflectionConfig): boolean {
7070
if (!isStringLiteral(importDeclaration.moduleSpecifier)) return false;
71+
if (!hasSourceFile(importDeclaration)) return false;
7172
if (config.options.inlineExternalLibraryImports === true) return true;
7273
const resolvedModule = this.resolver.resolveExternalLibraryImport(importDeclaration);
7374
const imports = config.options.inlineExternalLibraryImports?.[resolvedModule.packageId.name];

packages/type-compiler/src/reflection-ast.ts

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export function getIdentifierName(node: Identifier | PrivateIdentifier): string
6464
return ts.unescapeLeadingUnderscores(node.escapedText);
6565
}
6666

67+
export function hasSourceFile(node: Node): boolean {
68+
return typeof node.getSourceFile === 'function';
69+
}
70+
6771
export function joinQualifiedName(name: EntityName): string {
6872
if (isIdentifier(name)) return getIdentifierName(name);
6973
return joinQualifiedName(name.left) + '_' + getIdentifierName(name.right);
@@ -162,6 +166,7 @@ export class NodeConverter {
162166
node.type,
163167
);
164168
} else if (isConstructorDeclaration(node)) {
169+
// return this.f.createm
165170
return this.f.createConstructSignature(node.typeParameters, node.parameters, node.type);
166171
}
167172

0 commit comments

Comments
 (0)