@@ -74,12 +74,14 @@ import ts from 'typescript';
74
74
75
75
import {
76
76
ensureImportIsEmitted ,
77
- extractJSDocAttribute , getEntityName ,
77
+ extractJSDocAttribute ,
78
+ getEntityName ,
78
79
getGlobalsOfSourceFile ,
79
80
getIdentifierName ,
80
81
getNameAsString ,
81
82
getPropertyName ,
82
- hasModifier , isBuiltType ,
83
+ hasModifier ,
84
+ isBuiltType ,
83
85
isNodeWithLocals ,
84
86
NodeConverter ,
85
87
PackExpression ,
@@ -90,9 +92,8 @@ import { existsSync, readFileSync } from 'fs';
90
92
import { dirname , isAbsolute , join , resolve } from 'path' ;
91
93
import stripJsonComments from 'strip-json-comments' ;
92
94
import { MappedModifier , ReflectionOp , TypeNumberBrand } from '@deepkit/type-spec' ;
93
- import { Resolver } from './resolver.js' ;
95
+ import { patternMatch , ReflectionMode , reflectionModeMatcher , reflectionModes , Resolver } from './resolver.js' ;
94
96
import { knownLibFilesForCompilerOptions } from '@typescript/vfs' ;
95
- import * as micromatch from 'micromatch' ;
96
97
97
98
// don't use from @deepkit/core since we don't want to have a dependency to @deepkit/core
98
99
export function isObject ( obj : any ) : obj is { [ key : string ] : any } {
@@ -186,7 +187,6 @@ const serverEnv = 'undefined' !== typeof process;
186
187
* It can't be more ops than this given number
187
188
*/
188
189
export const packSize : number = 2 ** packSizeByte ; //64
189
- const reflectionModes = [ 'always' , 'default' , 'never' ] as const ;
190
190
191
191
export interface ReflectionOptions {
192
192
/**
@@ -546,7 +546,7 @@ export class ReflectionTransformer implements CustomTransformer {
546
546
'lib.es2017.typedarrays.d.ts' ,
547
547
] ;
548
548
549
- public embedAssignType : boolean = false ;
549
+ protected embedAssignType : boolean = false ;
550
550
551
551
protected reflectionMode ?: typeof reflectionModes [ number ] ;
552
552
protected reflectionOptions ?: ReflectionOptions ;
@@ -726,7 +726,7 @@ export class ReflectionTransformer implements CustomTransformer {
726
726
}
727
727
}
728
728
729
- debug ( `Transform file ${ sourceFile . fileName } via config ${ this . compilerOptions . configFilePath || 'none' } , reflection=${ this . reflectionMode } .` ) ;
729
+ debug ( `Transform file ${ sourceFile . fileName } via config ${ this . compilerOptions . configFilePath || 'none' } , reflection=${ this . reflectionMode } ( ${ this . getModuleType ( ) } ) .` ) ;
730
730
731
731
if ( this . reflectionMode === 'never' ) {
732
732
return sourceFile ;
@@ -1023,15 +1023,15 @@ export class ReflectionTransformer implements CustomTransformer {
1023
1023
this . sourceFile = visitNode ( this . sourceFile , compileDeclarations ) ;
1024
1024
1025
1025
if ( this . addImports . length ) {
1026
- const compilerOptions = this . compilerOptions ;
1027
1026
const handledIdentifier : string [ ] = [ ] ;
1028
1027
for ( const imp of this . addImports ) {
1029
1028
if ( handledIdentifier . includes ( getIdentifierName ( imp . identifier ) ) ) continue ;
1030
1029
handledIdentifier . push ( getIdentifierName ( imp . identifier ) ) ;
1031
- if ( compilerOptions . module === ModuleKind . CommonJS ) {
1030
+ if ( this . getModuleType ( ) === 'cjs' ) {
1032
1031
//var {identifier} = require('./bar')
1032
+ const test = this . f . createIdentifier ( getIdentifierName ( imp . identifier ) ) ;
1033
1033
const variable = this . f . createVariableStatement ( undefined , this . f . createVariableDeclarationList ( [ this . f . createVariableDeclaration (
1034
- this . f . createObjectBindingPattern ( [ this . f . createBindingElement ( undefined , undefined , imp . identifier ) ] ) ,
1034
+ this . f . createObjectBindingPattern ( [ this . f . createBindingElement ( undefined , undefined , test ) ] ) ,
1035
1035
undefined , undefined ,
1036
1036
this . f . createCallExpression ( this . f . createIdentifier ( 'require' ) , undefined , [ imp . from ] )
1037
1037
) ] , NodeFlags . Const ) ) ;
@@ -1046,7 +1046,7 @@ export class ReflectionTransformer implements CustomTransformer {
1046
1046
//import {identifier} from './bar.js'
1047
1047
// import { identifier as identifier } is used to avoid automatic elision of imports (in angular builds for example)
1048
1048
// that's probably a bit unstable.
1049
- const specifier = this . f . createImportSpecifier ( false , imp . identifier , imp . identifier ) ;
1049
+ const specifier = this . f . createImportSpecifier ( false , undefined , imp . identifier ) ;
1050
1050
const namedImports = this . f . createNamedImports ( [ specifier ] ) ;
1051
1051
const importStatement = this . f . createImportDeclaration ( undefined ,
1052
1052
this . f . createImportClause ( false , undefined , namedImports ) , imp . from
@@ -1142,6 +1142,10 @@ export class ReflectionTransformer implements CustomTransformer {
1142
1142
return this . sourceFile ;
1143
1143
}
1144
1144
1145
+ protected getModuleType ( ) : 'cjs' | 'esm' {
1146
+ return this . compilerOptions . module === ModuleKind . CommonJS ? 'cjs' : 'esm' ;
1147
+ }
1148
+
1145
1149
protected injectResetΩ < T extends FunctionDeclaration | FunctionExpression | MethodDeclaration | ConstructorDeclaration > ( node : T ) : T {
1146
1150
let hasReceiveType = false ;
1147
1151
for ( const param of node . parameters ) {
@@ -2039,12 +2043,7 @@ export class ReflectionTransformer implements CustomTransformer {
2039
2043
2040
2044
protected isExcluded ( filePath : string ) : boolean {
2041
2045
if ( ! this . currentReflectionConfig . options . exclude ) return false ;
2042
-
2043
- const excluded = micromatch . contains ( filePath , this . currentReflectionConfig . options . exclude , {
2044
- basename : true ,
2045
- cwd : this . currentReflectionConfig . baseDir
2046
- } ) ;
2047
- return excluded ;
2046
+ return patternMatch ( filePath , this . currentReflectionConfig . options . exclude , this . currentReflectionConfig . baseDir ) ;
2048
2047
}
2049
2048
2050
2049
protected extractPackStructOfTypeReference ( type : TypeReferenceNode | ExpressionWithTypeArguments , program : CompilerProgram ) : void {
@@ -2186,6 +2185,8 @@ export class ReflectionTransformer implements CustomTransformer {
2186
2185
return ;
2187
2186
}
2188
2187
2188
+ const runtimeTypeName = this . getDeclarationVariableName ( typeName ) ;
2189
+
2189
2190
//to break recursion, we track which declaration has already been compiled
2190
2191
if ( ! this . compiledDeclarations . has ( declaration ) && ! this . compileDeclarations . has ( declaration ) ) {
2191
2192
const declarationSourceFile = findSourceFile ( declaration ) || this . sourceFile ;
@@ -2225,11 +2226,7 @@ export class ReflectionTransformer implements CustomTransformer {
2225
2226
return ;
2226
2227
}
2227
2228
2228
- //check if the referenced file has reflection info emitted. if not, any is emitted for that reference
2229
- const typeVar = this . getDeclarationVariableName ( typeName ) ;
2230
- //check if typeVar is exported in referenced file
2231
- const builtType = isBuiltType ( typeVar , found ) ;
2232
-
2229
+ const builtType = isBuiltType ( runtimeTypeName , found ) ;
2233
2230
if ( ! builtType ) {
2234
2231
if ( ! this . shouldInlineExternalImport ( resolved . importDeclaration , typeName , declarationReflection ) ) return ;
2235
2232
this . embedDeclarations . set ( declaration , {
@@ -2244,7 +2241,7 @@ export class ReflectionTransformer implements CustomTransformer {
2244
2241
return ;
2245
2242
}
2246
2243
2247
- this . addImports . push ( { identifier : this . getDeclarationVariableName ( typeName ) , from : resolved . importDeclaration . moduleSpecifier } ) ;
2244
+ this . addImports . push ( { identifier : runtimeTypeName , from : resolved . importDeclaration . moduleSpecifier } ) ;
2248
2245
}
2249
2246
}
2250
2247
} else {
@@ -2263,7 +2260,7 @@ export class ReflectionTransformer implements CustomTransformer {
2263
2260
}
2264
2261
2265
2262
const index = program . pushStack (
2266
- program . forNode === declaration ? 0 : this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , this . getDeclarationVariableName ( typeName ) )
2263
+ program . forNode === declaration ? 0 : this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , runtimeTypeName )
2267
2264
) ;
2268
2265
if ( type . typeArguments ) {
2269
2266
for ( const argument of type . typeArguments ) {
@@ -2696,7 +2693,7 @@ export class ReflectionTransformer implements CustomTransformer {
2696
2693
*
2697
2694
* where we embed assignType() at the beginning of the type.
2698
2695
*/
2699
- public wrapWithAssignType ( fn : Expression , type : Expression ) {
2696
+ protected wrapWithAssignType ( fn : Expression , type : Expression ) {
2700
2697
this . embedAssignType = true ;
2701
2698
2702
2699
return this . f . createCallExpression (
@@ -2709,18 +2706,8 @@ export class ReflectionTransformer implements CustomTransformer {
2709
2706
) ;
2710
2707
}
2711
2708
2712
- protected parseReflectionMode ( mode : typeof reflectionModes [ number ] | '' | boolean | string | string [ ] | undefined , configPathDir : string ) : typeof reflectionModes [ number ] {
2713
- if ( Array . isArray ( mode ) ) {
2714
- if ( ! configPathDir ) return 'never' ;
2715
- const matches = micromatch . contains ( this . sourceFile . fileName , mode , {
2716
- cwd : configPathDir
2717
- } ) ;
2718
-
2719
- return matches ? 'default' : 'never' ;
2720
- }
2721
- if ( 'boolean' === typeof mode ) return mode ? 'default' : 'never' ;
2722
- if ( mode === 'default' || mode === 'always' ) return mode ;
2723
- return 'never' ;
2709
+ protected parseReflectionMode ( mode : ReflectionMode , configPathDir : string ) : typeof reflectionModes [ number ] {
2710
+ return reflectionModeMatcher ( this . sourceFile . fileName , mode , configPathDir ) ;
2724
2711
}
2725
2712
2726
2713
protected resolvedTsConfig : { [ path : string ] : { data : Record < string , any > , exists : boolean } } = { } ;
@@ -2850,19 +2837,19 @@ export class ReflectionTransformer implements CustomTransformer {
2850
2837
}
2851
2838
}
2852
2839
2853
- if ( reflection === undefined && packageJson . deepkitTypeCompilerOptions !== undefined ) {
2840
+ if ( reflection === undefined && packageJson . reflection !== undefined ) {
2854
2841
return {
2855
- mode : this . parseReflectionMode ( packageJson . deepkitTypeCompilerOptions . reflection , currentDir ) ,
2842
+ mode : this . parseReflectionMode ( packageJson . reflection , currentDir ) ,
2856
2843
baseDir : currentDir ,
2857
- options : this . parseReflectionOptionsDefaults ( packageJson . deepkitTypeCompilerOptions || { } )
2844
+ options : this . parseReflectionOptionsDefaults ( packageJson . reflectionOptions || { } )
2858
2845
} ;
2859
2846
}
2860
2847
2861
- if ( reflection === undefined && tsConfig . deepkitTypeCompilerOptions !== undefined ) {
2848
+ if ( reflection === undefined && tsConfig . reflection !== undefined ) {
2862
2849
return {
2863
- mode : this . parseReflectionMode ( tsConfig . deepkitTypeCompilerOptions . reflection , currentDir ) ,
2850
+ mode : this . parseReflectionMode ( tsConfig . reflection , currentDir ) ,
2864
2851
baseDir : currentDir ,
2865
- options : this . parseReflectionOptionsDefaults ( tsConfig . deepkitTypeCompilerOptions || { } )
2852
+ options : this . parseReflectionOptionsDefaults ( tsConfig . reflectionOptions || { } )
2866
2853
} ;
2867
2854
}
2868
2855
0 commit comments