Skip to content

Commit

Permalink
rename external to inlineExternalImports
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa committed Dec 5, 2023
1 parent 98cafff commit 1978d31
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
23 changes: 12 additions & 11 deletions packages/type-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export interface ReflectionOptions {
/**
* External imports to reflect
*/
external?: Record<string, '*' | string[]>; // '*' or true?
inlineExternalImports?: true | Record<string, true | string[]>;
}

export interface ReflectionConfig {
Expand Down Expand Up @@ -537,7 +537,7 @@ export class EmbedDeclarations extends Map<Node, EmbedDeclaration> {
*/
export class ReflectionTransformer implements CustomTransformer {
sourceFile!: SourceFile;
public f: NodeFactory;
protected f: NodeFactory;
protected currentReflectionConfig: ReflectionConfig = { mode: 'never', options: {}, baseDir: '' };

public defaultExcluded: string[] = [
Expand Down Expand Up @@ -594,7 +594,7 @@ export class ReflectionTransformer implements CustomTransformer {
protected context: TransformationContext,
) {
this.f = context.factory;
this.nodeConverter = new NodeConverter(this);
this.nodeConverter = new NodeConverter(this.f, this);
//it is important to not have undefined values like {paths: undefined} because it would override the read tsconfig.json
this.compilerOptions = filterUndefined(context.getCompilerOptions());
this.host = createCompilerHost(this.compilerOptions);
Expand Down Expand Up @@ -2026,14 +2026,15 @@ export class ReflectionTransformer implements CustomTransformer {
return this.f.createIdentifier('__Ω' + joinQualifiedName(typeName));
}

// TODO: what to do when the external type depends on another external type? should that be automatically resolved, or should the user specify that explicitly as well?
protected isImportMarkedAsExternal(importDeclaration: ImportDeclaration, entityName: EntityName, config: ReflectionConfig): boolean {
// 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?
protected shouldInlineExternalImport(importDeclaration: ImportDeclaration, entityName: EntityName, config: ReflectionConfig): boolean {
if (!ts.isStringLiteral(importDeclaration.moduleSpecifier)) return false;
const external = config.options.external?.[importDeclaration.moduleSpecifier.text];
if (!external) return false;
if (external === '*') return true;
if (config.options.inlineExternalImports === true) return true;
const externalImport = config.options.inlineExternalImports?.[importDeclaration.moduleSpecifier.text];
if (!externalImport) return false;
if (externalImport === true) return true;
const typeName = getEntityName(entityName);
return external.includes(typeName);
return externalImport.includes(typeName);
}

protected isExcluded(filePath: string): boolean {
Expand Down Expand Up @@ -2230,7 +2231,7 @@ export class ReflectionTransformer implements CustomTransformer {
const builtType = isBuiltType(typeVar, found);

if (!builtType) {
if (!this.isImportMarkedAsExternal(resolved.importDeclaration, typeName, declarationReflection)) return;
if (!this.shouldInlineExternalImport(resolved.importDeclaration, typeName, declarationReflection)) return;
this.embedDeclarations.set(declaration, {
name: typeName,
sourceFile: declarationSourceFile
Expand Down Expand Up @@ -2307,7 +2308,7 @@ export class ReflectionTransformer implements CustomTransformer {
// //check if typeVar is exported in referenced file
const builtType = isBuiltType(typeVar, declarationSourceFile);

if (!builtType && this.isImportMarkedAsExternal(resolved.importDeclaration, typeName, declarationReflection)) {
if (!builtType && this.shouldInlineExternalImport(resolved.importDeclaration, typeName, declarationReflection)) {
this.embedDeclarations.set(declaration, {
name: typeName,
sourceFile: declarationSourceFile,
Expand Down
6 changes: 1 addition & 5 deletions packages/type-compiler/src/reflection-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ const cloneHook = <T extends Node>(node: T, payload: { depth: number }): CloneNo
};

export class NodeConverter {
protected f: NodeFactory

constructor(protected transformer: ReflectionTransformer) {
this.f = transformer.f;
}
constructor(protected f: NodeFactory, protected transformer: ReflectionTransformer) {}

toExpression<T extends PackExpression | PackExpression[]>(node?: T): Expression {
if (node === undefined) return this.f.createIdentifier('undefined');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('class type', () => {
type A = Observable<unknown>;
`
}, undefined, {
external: {
inlineExternalImports: {
'rxjs': ['Observable'],
},
});
Expand All @@ -25,7 +25,7 @@ test('class typeOf', () => {
typeOf<Observable>();
`
}, undefined, {
external: {
inlineExternalImports: {
'rxjs': ['Subject'],
},
});
Expand All @@ -42,7 +42,7 @@ test.todo('function'/*, () => {
typeOf<typeof preview>();
`
}, undefined, {
external: {
inlineExternalImports: {
'vite': ['preview'],
},
});
Expand All @@ -57,7 +57,7 @@ test('only a single type is transformed', () => {
type B = CorsOrigin;
`
}, undefined, {
external: {
inlineExternalImports: {
'vite': ['ConfigEnv'],
},
});
Expand All @@ -73,7 +73,7 @@ test('interface typeOf', () => {
typeOf<ConfigEnv>();
`
}, undefined, {
external: {
inlineExternalImports: {
'vite': ['ConfigEnv'],
},
});
Expand Down Expand Up @@ -130,7 +130,7 @@ test('interface typeOf', () => {
`);
});

test('all exports marked as external', () => {
test('inline all external imports for package', () => {
const res = transpile({
app: `import { ConfigEnv, CorsOrigin } from 'vite';
import { typeOf } from '@deepkit/type';
Expand All @@ -139,8 +139,8 @@ test('all exports marked as external', () => {
type B = CorsOrigin;
`
}, undefined, {
external: {
'vite': '*',
inlineExternalImports: {
'vite': true,
},
});
expect(res.app).toContain('const __ΩConfigEnv = [');
Expand Down

0 comments on commit 1978d31

Please sign in to comment.