@@ -90,9 +90,8 @@ import { existsSync, readFileSync } from 'fs';
90
90
import { dirname , isAbsolute , join , resolve } from 'path' ;
91
91
import stripJsonComments from 'strip-json-comments' ;
92
92
import { MappedModifier , ReflectionOp , TypeNumberBrand } from '@deepkit/type-spec' ;
93
- import { Resolver } from './resolver.js' ;
93
+ import { patternMatch , ReflectionMode , reflectionModeMatcher , reflectionModes , Resolver } from './resolver.js' ;
94
94
import { knownLibFilesForCompilerOptions } from '@typescript/vfs' ;
95
- import * as micromatch from 'micromatch' ;
96
95
97
96
// don't use from @deepkit/core since we don't want to have a dependency to @deepkit/core
98
97
export function isObject ( obj : any ) : obj is { [ key : string ] : any } {
@@ -186,7 +185,6 @@ const serverEnv = 'undefined' !== typeof process;
186
185
* It can't be more ops than this given number
187
186
*/
188
187
export const packSize : number = 2 ** packSizeByte ; //64
189
- const reflectionModes = [ 'always' , 'default' , 'never' ] as const ;
190
188
191
189
interface ReflectionOptions {
192
190
/**
@@ -695,7 +693,7 @@ export class ReflectionTransformer implements CustomTransformer {
695
693
}
696
694
}
697
695
698
- debug ( `Transform file ${ sourceFile . fileName } via config ${ this . compilerOptions . configFilePath || 'none' } , reflection=${ this . reflectionMode } .` ) ;
696
+ debug ( `Transform file ${ sourceFile . fileName } via config ${ this . compilerOptions . configFilePath || 'none' } , reflection=${ this . reflectionMode } ( ${ this . getModuleType ( ) } ) .` ) ;
699
697
700
698
if ( this . reflectionMode === 'never' ) {
701
699
return sourceFile ;
@@ -992,15 +990,15 @@ export class ReflectionTransformer implements CustomTransformer {
992
990
this . sourceFile = visitNode ( this . sourceFile , compileDeclarations ) ;
993
991
994
992
if ( this . addImports . length ) {
995
- const compilerOptions = this . compilerOptions ;
996
993
const handledIdentifier : string [ ] = [ ] ;
997
994
for ( const imp of this . addImports ) {
998
995
if ( handledIdentifier . includes ( getIdentifierName ( imp . identifier ) ) ) continue ;
999
996
handledIdentifier . push ( getIdentifierName ( imp . identifier ) ) ;
1000
- if ( compilerOptions . module === ModuleKind . CommonJS ) {
997
+ if ( this . getModuleType ( ) === 'cjs' ) {
1001
998
//var {identifier} = require('./bar')
999
+ const test = this . f . createIdentifier ( getIdentifierName ( imp . identifier ) ) ;
1002
1000
const variable = this . f . createVariableStatement ( undefined , this . f . createVariableDeclarationList ( [ this . f . createVariableDeclaration (
1003
- this . f . createObjectBindingPattern ( [ this . f . createBindingElement ( undefined , undefined , imp . identifier ) ] ) ,
1001
+ this . f . createObjectBindingPattern ( [ this . f . createBindingElement ( undefined , undefined , test ) ] ) ,
1004
1002
undefined , undefined ,
1005
1003
this . f . createCallExpression ( this . f . createIdentifier ( 'require' ) , undefined , [ imp . from ] )
1006
1004
) ] , NodeFlags . Const ) ) ;
@@ -1015,7 +1013,7 @@ export class ReflectionTransformer implements CustomTransformer {
1015
1013
//import {identifier} from './bar.js'
1016
1014
// import { identifier as identifier } is used to avoid automatic elision of imports (in angular builds for example)
1017
1015
// that's probably a bit unstable.
1018
- const specifier = this . f . createImportSpecifier ( false , imp . identifier , imp . identifier ) ;
1016
+ const specifier = this . f . createImportSpecifier ( false , undefined , imp . identifier ) ;
1019
1017
const namedImports = this . f . createNamedImports ( [ specifier ] ) ;
1020
1018
const importStatement = this . f . createImportDeclaration ( undefined ,
1021
1019
this . f . createImportClause ( false , undefined , namedImports ) , imp . from
@@ -1111,6 +1109,10 @@ export class ReflectionTransformer implements CustomTransformer {
1111
1109
return this . sourceFile ;
1112
1110
}
1113
1111
1112
+ protected getModuleType ( ) : 'cjs' | 'esm' {
1113
+ return this . compilerOptions . module === ModuleKind . CommonJS ? 'cjs' : 'esm' ;
1114
+ }
1115
+
1114
1116
protected injectResetΩ < T extends FunctionDeclaration | FunctionExpression | MethodDeclaration | ConstructorDeclaration > ( node : T ) : T {
1115
1117
let hasReceiveType = false ;
1116
1118
for ( const param of node . parameters ) {
@@ -1997,12 +1999,7 @@ export class ReflectionTransformer implements CustomTransformer {
1997
1999
1998
2000
protected isExcluded ( filePath : string ) : boolean {
1999
2001
if ( ! this . currentReflectionConfig . options . exclude ) return false ;
2000
-
2001
- const excluded = micromatch . contains ( filePath , this . currentReflectionConfig . options . exclude , {
2002
- basename : true ,
2003
- cwd : this . currentReflectionConfig . baseDir
2004
- } ) ;
2005
- return excluded ;
2002
+ return patternMatch ( filePath , this . currentReflectionConfig . options . exclude , this . currentReflectionConfig . baseDir ) ;
2006
2003
}
2007
2004
2008
2005
protected extractPackStructOfTypeReference ( type : TypeReferenceNode | ExpressionWithTypeArguments , program : CompilerProgram ) : void {
@@ -2144,12 +2141,15 @@ export class ReflectionTransformer implements CustomTransformer {
2144
2141
return ;
2145
2142
}
2146
2143
2144
+ const runtimeTypeName = this . getDeclarationVariableName ( typeName ) ;
2145
+
2147
2146
//to break recursion, we track which declaration has already been compiled
2148
2147
if ( ! this . compiledDeclarations . has ( declaration ) && ! this . compileDeclarations . has ( declaration ) ) {
2149
2148
const declarationSourceFile = findSourceFile ( declaration ) || this . sourceFile ;
2150
2149
const isGlobal = resolved . importDeclaration === undefined && declarationSourceFile . fileName !== this . sourceFile . fileName ;
2151
2150
const isFromImport = resolved . importDeclaration !== undefined ;
2152
2151
2152
+
2153
2153
if ( this . isExcluded ( declarationSourceFile . fileName ) ) {
2154
2154
program . pushOp ( ReflectionOp . any ) ;
2155
2155
return ;
@@ -2185,7 +2185,7 @@ export class ReflectionTransformer implements CustomTransformer {
2185
2185
2186
2186
// check if this is a viable option:
2187
2187
// //check if the referenced file has reflection info emitted. if not, any is emitted for that reference
2188
- // const typeVar = this.getDeclarationVariableName(typeName) ;
2188
+ // const typeVar = runtimeRypeName ;
2189
2189
// //check if typeVar is exported in referenced file
2190
2190
// const builtType = isNodeWithLocals(found) && found.locals && found.locals.has(typeVar.escapedText);
2191
2191
// if (!builtType) {
@@ -2201,7 +2201,7 @@ export class ReflectionTransformer implements CustomTransformer {
2201
2201
}
2202
2202
2203
2203
// this.addImports.push({ identifier: typeVar, from: resolved.importDeclaration.moduleSpecifier });
2204
- this . addImports . push ( { identifier : this . getDeclarationVariableName ( typeName ) , from : resolved . importDeclaration . moduleSpecifier } ) ;
2204
+ this . addImports . push ( { identifier : runtimeTypeName , from : resolved . importDeclaration . moduleSpecifier } ) ;
2205
2205
}
2206
2206
} else {
2207
2207
//it's a reference type inside the same file. Make sure its type is reflected
@@ -2219,7 +2219,7 @@ export class ReflectionTransformer implements CustomTransformer {
2219
2219
}
2220
2220
2221
2221
const index = program . pushStack (
2222
- program . forNode === declaration ? 0 : this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , this . getDeclarationVariableName ( typeName ) )
2222
+ program . forNode === declaration ? 0 : this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , runtimeTypeName )
2223
2223
) ;
2224
2224
if ( type . typeArguments ) {
2225
2225
for ( const argument of type . typeArguments ) {
@@ -2647,18 +2647,8 @@ export class ReflectionTransformer implements CustomTransformer {
2647
2647
) ;
2648
2648
}
2649
2649
2650
- protected parseReflectionMode ( mode : typeof reflectionModes [ number ] | '' | boolean | string | string [ ] | undefined , configPathDir : string ) : typeof reflectionModes [ number ] {
2651
- if ( Array . isArray ( mode ) ) {
2652
- if ( ! configPathDir ) return 'never' ;
2653
- const matches = micromatch . contains ( this . sourceFile . fileName , mode , {
2654
- cwd : configPathDir
2655
- } ) ;
2656
-
2657
- return matches ? 'default' : 'never' ;
2658
- }
2659
- if ( 'boolean' === typeof mode ) return mode ? 'default' : 'never' ;
2660
- if ( mode === 'default' || mode === 'always' ) return mode ;
2661
- return 'never' ;
2650
+ protected parseReflectionMode ( mode : ReflectionMode , configPathDir : string ) : typeof reflectionModes [ number ] {
2651
+ return reflectionModeMatcher ( this . sourceFile . fileName , mode , configPathDir ) ;
2662
2652
}
2663
2653
2664
2654
protected resolvedTsConfig : { [ path : string ] : { data : Record < string , any > , exists : boolean } } = { } ;
0 commit comments