Skip to content

Commit beab438

Browse files
committed
improve
1 parent de8b473 commit beab438

File tree

3 files changed

+363
-94
lines changed

3 files changed

+363
-94
lines changed

packages/type-compiler/src/compiler.ts

+38-23
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,6 @@ export class ReflectionTransformer implements CustomTransformer {
826826
} else if (isArrowFunction(node)) {
827827
return this.decorateArrow(node);
828828
} else if ((isNewExpression(node) || isCallExpression(node)) && node.typeArguments && node.typeArguments.length > 0) {
829-
830829
if (isCallExpression(node)) {
831830
const autoTypeFunctions = ['valuesOf', 'propertiesOf', 'typeOf'];
832831
if (isIdentifier(node.expression) && autoTypeFunctions.includes(getIdentifierName(node.expression))) {
@@ -1823,7 +1822,7 @@ export class ReflectionTransformer implements CustomTransformer {
18231822
if (isIdentifier(narrowed.exprName)) {
18241823
const resolved = this.resolveDeclaration(narrowed.exprName);
18251824
if (resolved && findSourceFile(resolved.declaration) !== this.sourceFile && resolved.importDeclaration) {
1826-
ensureImportIsEmitted(resolved.importDeclaration, narrowed.exprName);
1825+
this.resolveImport(resolved.declaration, resolved.importDeclaration, narrowed.exprName, program);
18271826
}
18281827
}
18291828

@@ -1982,6 +1981,8 @@ export class ReflectionTransformer implements CustomTransformer {
19821981
importDeclaration = declaration.parent;
19831982
}
19841983

1984+
// TODO: check if it's a built type here?
1985+
19851986
if (importDeclaration) {
19861987
if (importDeclaration.importClause && importDeclaration.importClause.isTypeOnly) typeOnly = true;
19871988
declaration = this.resolveImportSpecifier(typeName.escapedText, importDeclaration, this.sourceFile);
@@ -2030,7 +2031,37 @@ export class ReflectionTransformer implements CustomTransformer {
20302031
return this.f.createIdentifier('__Ω' + joinQualifiedName(typeName));
20312032
}
20322033

2033-
// TODO: what to do when the inlineExternalImports type depends on another inlineExternalImports type? should that be automatically resolved, or should the user specify that explicitly as well?
2034+
protected resolveImport(declaration: Node, importDeclaration: ImportDeclaration, typeName: Identifier, program: CompilerProgram) {
2035+
if (isVariableDeclaration(declaration)) {
2036+
if (declaration.type) {
2037+
declaration = declaration.type;
2038+
} else if (declaration.initializer) {
2039+
declaration = declaration.initializer;
2040+
}
2041+
}
2042+
2043+
ensureImportIsEmitted(importDeclaration, typeName);
2044+
2045+
// check if the referenced declaration has reflection disabled
2046+
const declarationReflection = this.findReflectionConfig(declaration, program);
2047+
if (declarationReflection.mode !== 'never') {
2048+
const declarationSourceFile = importDeclaration.getSourceFile();
2049+
2050+
const runtimeTypeName = this.getDeclarationVariableName(typeName);
2051+
2052+
const builtType = isBuiltType(runtimeTypeName, declarationSourceFile);
2053+
2054+
if (!builtType && this.shouldInlineExternalImport(importDeclaration, typeName, declarationReflection)) {
2055+
this.embedDeclarations.set(declaration, {
2056+
name: typeName,
2057+
sourceFile: declarationSourceFile,
2058+
assignType: true,
2059+
});
2060+
}
2061+
}
2062+
}
2063+
2064+
// TODO: what to do when the external type depends on another external type? should that be automatically resolved, or should the user explicitly specify that as well?
20342065
protected shouldInlineExternalImport(importDeclaration: ImportDeclaration, entityName: EntityName, config: ReflectionConfig): boolean {
20352066
if (!ts.isStringLiteral(importDeclaration.moduleSpecifier)) return false;
20362067
if (config.options.inlineExternalImports === true) return true;
@@ -2138,7 +2169,9 @@ export class ReflectionTransformer implements CustomTransformer {
21382169
}
21392170

21402171
if (isModuleDeclaration(declaration) && resolved.importDeclaration) {
2141-
if (isIdentifier(typeName)) ensureImportIsEmitted(resolved.importDeclaration, typeName);
2172+
if (isIdentifier(typeName)) {
2173+
this.resolveImport(declaration, resolved.importDeclaration, typeName, program);
2174+
}
21422175

21432176
//we can not infer from module declaration, so do `typeof T` in runtime
21442177
program.pushOp(
@@ -2294,25 +2327,7 @@ export class ReflectionTransformer implements CustomTransformer {
22942327
}
22952328

22962329
if (resolved.importDeclaration && isIdentifier(typeName)) {
2297-
ensureImportIsEmitted(resolved.importDeclaration, typeName);
2298-
2299-
// check if the referenced declaration has reflection disabled
2300-
const declarationReflection = this.findReflectionConfig(declaration, program);
2301-
if (declarationReflection.mode !== 'never') {
2302-
const declarationSourceFile = resolved.importDeclaration.getSourceFile();
2303-
// //check if the referenced file has reflection info emitted. if not, any is emitted for that reference
2304-
const typeVar = this.getDeclarationVariableName(typeName);
2305-
// //check if typeVar is exported in referenced file
2306-
const builtType = isBuiltType(typeVar, declarationSourceFile);
2307-
2308-
if (!builtType && this.shouldInlineExternalImport(resolved.importDeclaration, typeName, declarationReflection)) {
2309-
this.embedDeclarations.set(declaration, {
2310-
name: typeName,
2311-
sourceFile: declarationSourceFile,
2312-
assignType: true,
2313-
});
2314-
}
2315-
}
2330+
this.resolveImport(resolved.declaration, resolved.importDeclaration, typeName, program);
23162331
}
23172332
program.pushFrame();
23182333
if (type.typeArguments) {

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ export class NodeConverter {
139139

140140
if (node.pos === -1 && node.end === -1 && node.parent === undefined) {
141141
if (isArrowFunction(node)) {
142-
// if (node.body.pos === -1 && node.body.end === -1 && node.body.parent === undefined) return node;
143142
return this.f.createArrowFunction(node.modifiers, node.typeParameters, node.parameters, node.type, node.equalsGreaterThanToken, this.toExpression(node.body as Expression));
144143
}
145-
return node;
146144
}
145+
147146
switch (node.kind) {
148147
case SyntaxKind.Identifier:
149148
const typeName = getIdentifierName(node as Identifier);

0 commit comments

Comments
 (0)