@travetto/transformer
Advanced tools
Comparing version 3.1.0-rc.3 to 3.1.0-rc.4
{ | ||
"name": "@travetto/transformer", | ||
"version": "3.1.0-rc.3", | ||
"version": "3.1.0-rc.4", | ||
"description": "Functionality for AST transformations, with transformer registration, and general utils", | ||
@@ -27,3 +27,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/manifest": "^3.1.0-rc.2", | ||
"@travetto/manifest": "^3.1.0-rc.3", | ||
"tslib": "^2.5.0", | ||
@@ -30,0 +30,0 @@ "typescript": "^5.0.2" |
@@ -5,3 +5,3 @@ import ts from 'typescript'; | ||
import { AnyType, TransformResolver, ExternalType } from './resolver/types'; | ||
import { AnyType, TransformResolver, ManagedType } from './resolver/types'; | ||
import { ImportUtil } from './util/import'; | ||
@@ -132,7 +132,7 @@ import { CoreUtil } from './util/core'; | ||
for (const type of types) { | ||
if (type.key === 'external' && type.importName && type.importName !== this.#importName) { | ||
if (type.key === 'managed' && type.importName && type.importName !== this.#importName) { | ||
this.importFile(type.importName); | ||
} | ||
switch (type.key) { | ||
case 'external': | ||
case 'managed': | ||
case 'literal': this.importFromResolved(...type.typeArguments || []); break; | ||
@@ -222,3 +222,3 @@ case 'union': | ||
*/ | ||
getOrImport(factory: ts.NodeFactory, type: ExternalType): ts.Identifier | ts.PropertyAccessExpression { | ||
getOrImport(factory: ts.NodeFactory, type: ManagedType): ts.Identifier | ts.PropertyAccessExpression { | ||
if (type.importName === this.#importName) { | ||
@@ -225,0 +225,0 @@ return factory.createIdentifier(type.name!); |
@@ -36,3 +36,3 @@ /* eslint-disable no-bitwise */ | ||
type Category = 'void' | 'undefined' | 'concrete' | 'unknown' | 'tuple' | 'shape' | 'literal' | 'external' | 'union'; | ||
type Category = 'void' | 'undefined' | 'concrete' | 'unknown' | 'tuple' | 'shape' | 'literal' | 'managed' | 'union' | 'foreign'; | ||
@@ -57,3 +57,9 @@ /** | ||
} else if (objectFlags & ts.ObjectFlags.Anonymous) { | ||
return { category: 'shape', type }; | ||
const source = DeclarationUtil.getPrimaryDeclarationNode(type).getSourceFile(); | ||
const sourceFile = source.fileName; | ||
if (sourceFile?.endsWith('.d.ts') && !resolver.isKnownFile(sourceFile)) { | ||
return { category: 'foreign', type }; | ||
} else { | ||
return { category: 'shape', type }; | ||
} | ||
} else if (objectFlags & (ts.ObjectFlags.Reference | ts.ObjectFlags.Class | ts.ObjectFlags.Interface)) { | ||
@@ -71,10 +77,10 @@ let resolvedType = type; | ||
const sourceFile = source.fileName; | ||
if (sourceFile?.includes('@types/node/globals') || sourceFile?.includes('typescript/lib')) { | ||
if (sourceFile?.includes('typescript/lib')) { | ||
return { category: 'literal', type }; | ||
} else if (sourceFile?.endsWith('.d.ts') && !resolver.isKnownFile(sourceFile)) { | ||
return { category: 'unknown', type }; | ||
return { category: 'foreign', type: resolvedType }; | ||
} else if (!resolvedType.isClass()) { // Not a real type | ||
return { category: 'shape', type: resolvedType }; | ||
} else { | ||
return { category: 'external', type: resolvedType }; | ||
return { category: 'managed', type: resolvedType }; | ||
} | ||
@@ -148,8 +154,15 @@ } else if (flags & ( | ||
}, | ||
external: { | ||
foreign: { | ||
build: (resolver, type) => { | ||
const name = CoreUtil.getSymbol(type)?.getName(); | ||
const source = DeclarationUtil.getPrimaryDeclarationNode(type).getSourceFile(); | ||
return { key: 'foreign', name, source: source.fileName }; | ||
} | ||
}, | ||
managed: { | ||
build: (resolver, type) => { | ||
const name = CoreUtil.getSymbol(type)?.getName(); | ||
const importName = resolver.getTypeImportName(type)!; | ||
const tsTypeArguments = resolver.getAllTypeArguments(type); | ||
return { key: 'external', name, importName, tsTypeArguments }; | ||
return { key: 'managed', name, importName, tsTypeArguments }; | ||
} | ||
@@ -233,3 +246,3 @@ }, | ||
} | ||
return { key: 'external', name, importName }; | ||
return { key: 'managed', name, importName }; | ||
} | ||
@@ -236,0 +249,0 @@ } |
@@ -19,3 +19,3 @@ import ts from 'typescript'; | ||
case 'shape': | ||
case 'external': { | ||
case 'managed': { | ||
if (this.storage.has(tsType)) { | ||
@@ -22,0 +22,0 @@ const target = this.storage.get(tsType)!; |
@@ -34,5 +34,5 @@ import type ts from 'typescript'; | ||
/** | ||
* A type that is not defined in the scope of the given file | ||
* A type that is not defined in the scope of the given file, but is importable from the project | ||
*/ | ||
export interface ExternalType extends Type<'external'> { | ||
export interface ManagedType extends Type<'managed'> { | ||
/** | ||
@@ -146,2 +146,17 @@ * Location the type came from, for class references | ||
/** | ||
* Foreign type, outside of framework | ||
*/ | ||
export interface ForeignType extends Type<'foreign'> { | ||
/** | ||
* Identifier for type | ||
*/ | ||
name: string; | ||
/** | ||
* Primary source file | ||
*/ | ||
source: string; | ||
} | ||
/** | ||
* Unknown type, should default to object | ||
@@ -151,3 +166,3 @@ */ | ||
export type AnyType = TupleType | ShapeType | UnionType | LiteralType | ExternalType | PointerType | UnknownType; | ||
export type AnyType = TupleType | ShapeType | UnionType | LiteralType | ManagedType | PointerType | UnknownType | ForeignType; | ||
@@ -154,0 +169,0 @@ /** |
@@ -5,3 +5,3 @@ import ts from 'typescript'; | ||
import { ExternalType, AnyType } from './resolver/types'; | ||
import { ManagedType, AnyType } from './resolver/types'; | ||
import { State, DecoratorMeta, Transformer, ModuleNameⲐ } from './types/visitor'; | ||
@@ -57,3 +57,3 @@ import { SimpleResolver } from './resolver/service'; | ||
*/ | ||
getOrImport(type: ExternalType): ts.Identifier | ts.PropertyAccessExpression { | ||
getOrImport(type: ManagedType): ts.Identifier | ts.PropertyAccessExpression { | ||
return this.#imports.getOrImport(this.factory, type); | ||
@@ -81,5 +81,5 @@ } | ||
*/ | ||
resolveExternalType(node: ts.Node): ExternalType { | ||
resolveManagedType(node: ts.Node): ManagedType { | ||
const resolved = this.resolveType(node); | ||
if (resolved.key !== 'external') { | ||
if (resolved.key !== 'managed') { | ||
const file = node.getSourceFile().fileName; | ||
@@ -99,3 +99,3 @@ const src = this.#resolver.getFileImportName(file); | ||
case 'literal': return this.factory.createIdentifier(type.ctor!.name); | ||
case 'external': return this.getOrImport(type); | ||
case 'managed': return this.getOrImport(type); | ||
case 'shape': return; | ||
@@ -355,2 +355,10 @@ } | ||
} | ||
/** | ||
* Get import name for a given file | ||
* @param file | ||
*/ | ||
getFileImportName(file: string): string { | ||
return this.#resolver.getFileImportName(file); | ||
} | ||
} |
89877
2261