Skip to content

Commit 1978d31

Browse files
committed
rename external to inlineExternalImports
1 parent 98cafff commit 1978d31

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

packages/type-compiler/src/compiler.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export interface ReflectionOptions {
197197
/**
198198
* External imports to reflect
199199
*/
200-
external?: Record<string, '*' | string[]>; // '*' or true?
200+
inlineExternalImports?: true | Record<string, true | string[]>;
201201
}
202202

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

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

2029-
// 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?
2030-
protected isImportMarkedAsExternal(importDeclaration: ImportDeclaration, entityName: EntityName, config: ReflectionConfig): boolean {
2029+
// 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?
2030+
protected shouldInlineExternalImport(importDeclaration: ImportDeclaration, entityName: EntityName, config: ReflectionConfig): boolean {
20312031
if (!ts.isStringLiteral(importDeclaration.moduleSpecifier)) return false;
2032-
const external = config.options.external?.[importDeclaration.moduleSpecifier.text];
2033-
if (!external) return false;
2034-
if (external === '*') return true;
2032+
if (config.options.inlineExternalImports === true) return true;
2033+
const externalImport = config.options.inlineExternalImports?.[importDeclaration.moduleSpecifier.text];
2034+
if (!externalImport) return false;
2035+
if (externalImport === true) return true;
20352036
const typeName = getEntityName(entityName);
2036-
return external.includes(typeName);
2037+
return externalImport.includes(typeName);
20372038
}
20382039

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

22322233
if (!builtType) {
2233-
if (!this.isImportMarkedAsExternal(resolved.importDeclaration, typeName, declarationReflection)) return;
2234+
if (!this.shouldInlineExternalImport(resolved.importDeclaration, typeName, declarationReflection)) return;
22342235
this.embedDeclarations.set(declaration, {
22352236
name: typeName,
22362237
sourceFile: declarationSourceFile
@@ -2307,7 +2308,7 @@ export class ReflectionTransformer implements CustomTransformer {
23072308
// //check if typeVar is exported in referenced file
23082309
const builtType = isBuiltType(typeVar, declarationSourceFile);
23092310

2310-
if (!builtType && this.isImportMarkedAsExternal(resolved.importDeclaration, typeName, declarationReflection)) {
2311+
if (!builtType && this.shouldInlineExternalImport(resolved.importDeclaration, typeName, declarationReflection)) {
23112312
this.embedDeclarations.set(declaration, {
23122313
name: typeName,
23132314
sourceFile: declarationSourceFile,

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ const cloneHook = <T extends Node>(node: T, payload: { depth: number }): CloneNo
123123
};
124124

125125
export class NodeConverter {
126-
protected f: NodeFactory
127-
128-
constructor(protected transformer: ReflectionTransformer) {
129-
this.f = transformer.f;
130-
}
126+
constructor(protected f: NodeFactory, protected transformer: ReflectionTransformer) {}
131127

132128
toExpression<T extends PackExpression | PackExpression[]>(node?: T): Expression {
133129
if (node === undefined) return this.f.createIdentifier('undefined');

packages/type-compiler/tests/inline-external-types.spec.ts renamed to packages/type-compiler/tests/inline-external-imports.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test('class type', () => {
99
type A = Observable<unknown>;
1010
`
1111
}, undefined, {
12-
external: {
12+
inlineExternalImports: {
1313
'rxjs': ['Observable'],
1414
},
1515
});
@@ -25,7 +25,7 @@ test('class typeOf', () => {
2525
typeOf<Observable>();
2626
`
2727
}, undefined, {
28-
external: {
28+
inlineExternalImports: {
2929
'rxjs': ['Subject'],
3030
},
3131
});
@@ -42,7 +42,7 @@ test.todo('function'/*, () => {
4242
typeOf<typeof preview>();
4343
`
4444
}, undefined, {
45-
external: {
45+
inlineExternalImports: {
4646
'vite': ['preview'],
4747
},
4848
});
@@ -57,7 +57,7 @@ test('only a single type is transformed', () => {
5757
type B = CorsOrigin;
5858
`
5959
}, undefined, {
60-
external: {
60+
inlineExternalImports: {
6161
'vite': ['ConfigEnv'],
6262
},
6363
});
@@ -73,7 +73,7 @@ test('interface typeOf', () => {
7373
typeOf<ConfigEnv>();
7474
`
7575
}, undefined, {
76-
external: {
76+
inlineExternalImports: {
7777
'vite': ['ConfigEnv'],
7878
},
7979
});
@@ -130,7 +130,7 @@ test('interface typeOf', () => {
130130
`);
131131
});
132132

133-
test('all exports marked as external', () => {
133+
test('inline all external imports for package', () => {
134134
const res = transpile({
135135
app: `import { ConfigEnv, CorsOrigin } from 'vite';
136136
import { typeOf } from '@deepkit/type';
@@ -139,8 +139,8 @@ test('all exports marked as external', () => {
139139
type B = CorsOrigin;
140140
`
141141
}, undefined, {
142-
external: {
143-
'vite': '*',
142+
inlineExternalImports: {
143+
'vite': true,
144144
},
145145
});
146146
expect(res.app).toContain('const __ΩConfigEnv = [');

0 commit comments

Comments
 (0)