@@ -16,7 +16,7 @@ import * as ts from "typescript";
16
16
import { ScriptTransformationContext , HierarchyFacts } from "../../../types" ;
17
17
import { NodeVisitor } from "../../visitor" ;
18
18
import { SCRIPT_HELPER_MODULE , SCRIPT_VROES_VAR , SCRIPT_LAZY_IMPORT_NAME , SCRIPT_VRO_GLOBAL } from "../helpers/VROES" ;
19
- import { getIdentifierTextOrNull , hasModifier , isRequireCall , getStringTextOrNull } from "../helpers/node" ;
19
+ import { getIdentifierTextOrNull , hasModifier , isRequireCall , getStringTextOrNull , isJestMethodCallWithModuleName } from "../helpers/node" ;
20
20
import { system } from "../../../system/system" ;
21
21
import { createModulePrologueStatements } from "./prologueStatements" ;
22
22
import { createEpilogueStatements } from "./epilogueStatements" ;
@@ -263,7 +263,7 @@ export function transformModuleSystem(sourceFile: ts.SourceFile, context: Script
263
263
const moduleName = resolveFullModuleName ( node . moduleSpecifier . text ) ;
264
264
const requireCall = ts . setSourceMapRange ( ts . factory . createCallExpression (
265
265
ts . factory . createIdentifier ( "require" ) ,
266
- /*typeArguments*/ undefined ,
266
+ /*typeArguments*/ undefined ,
267
267
[ ts . factory . createStringLiteral ( moduleName ) ]
268
268
) , ts . getSourceMapRange ( node ) ) ;
269
269
const importVarPrefix = moduleName . substring ( 1 + Math . max ( moduleName . lastIndexOf ( "." ) , moduleName . lastIndexOf ( "/" ) ) ) ;
@@ -301,7 +301,7 @@ export function transformModuleSystem(sourceFile: ts.SourceFile, context: Script
301
301
const moduleName = resolveFullModuleName ( node . moduleSpecifier . text ) ;
302
302
const requireCall = ts . setSourceMapRange ( ts . factory . createCallExpression (
303
303
ts . factory . createIdentifier ( "require" ) ,
304
- /*typeArguments*/ undefined ,
304
+ /*typeArguments*/ undefined ,
305
305
[ ts . factory . createStringLiteral ( moduleName ) ]
306
306
) , ts . getSourceMapRange ( node ) ) ;
307
307
if ( node . exportClause && ts . isNamespaceExport ( node . exportClause ) ) {
@@ -311,15 +311,15 @@ export function transformModuleSystem(sourceFile: ts.SourceFile, context: Script
311
311
ts . factory . createToken ( ts . SyntaxKind . EqualsToken ) ,
312
312
ts . factory . createCallExpression (
313
313
ts . factory . createPropertyAccessExpression ( ts . factory . createIdentifier ( tslibVarName ) , "__importStar" ) ,
314
- /*typeArguments*/ undefined ,
314
+ /*typeArguments*/ undefined ,
315
315
[ requireCall , ts . factory . createIdentifier ( "exports" ) ]
316
316
) ,
317
317
) ;
318
318
return ts . setSourceMapRange ( ts . factory . createExpressionStatement ( exportAssignmentNode ) , ts . getSourceMapRange ( node ) ) ;
319
319
} else {
320
320
const exportCall = ts . setSourceMapRange ( ts . factory . createCallExpression (
321
321
ts . factory . createPropertyAccessExpression ( ts . factory . createIdentifier ( tslibVarName ) , "__exportStar" ) ,
322
- /*typeArguments*/ undefined ,
322
+ /*typeArguments*/ undefined ,
323
323
[ requireCall , ts . factory . createIdentifier ( "exports" ) ]
324
324
) , ts . getSourceMapRange ( node ) ) ;
325
325
return ts . setSourceMapRange ( ts . factory . createExpressionStatement ( exportCall ) , ts . getSourceMapRange ( node ) ) ;
@@ -372,12 +372,12 @@ export function transformModuleSystem(sourceFile: ts.SourceFile, context: Script
372
372
const isDefault = hasModifier ( node . modifiers , ts . SyntaxKind . DefaultKeyword ) ;
373
373
const exportName = isDefault || ! node . name ? "default" : node . name . text ;
374
374
const funcExp = ts . setSourceMapRange ( ts . factory . createFunctionExpression (
375
- /*modifiers*/ undefined ,
376
- /*asteriskToken*/ undefined ,
377
- /*name*/ undefined ,
378
- /*typeParameters*/ undefined ,
375
+ /*modifiers*/ undefined ,
376
+ /*asteriskToken*/ undefined ,
377
+ /*name*/ undefined ,
378
+ /*typeParameters*/ undefined ,
379
379
node . parameters ,
380
- /*type*/ undefined ,
380
+ /*type*/ undefined ,
381
381
body
382
382
) , ts . getSourceMapRange ( node ) ) ;
383
383
result . push ( ts . setSourceMapRange ( ts . factory . createExpressionStatement ( ts . factory . createBinaryExpression (
@@ -389,12 +389,12 @@ export function transformModuleSystem(sourceFile: ts.SourceFile, context: Script
389
389
result . push (
390
390
ts . factory . updateFunctionDeclaration (
391
391
node ,
392
- /*modifiers*/ undefined ,
393
- /*asteriskToken*/ undefined ,
392
+ /*modifiers*/ undefined ,
393
+ /*asteriskToken*/ undefined ,
394
394
node . name ,
395
- /*typeParameters*/ undefined ,
395
+ /*typeParameters*/ undefined ,
396
396
node . parameters ,
397
- /*type*/ undefined ,
397
+ /*type*/ undefined ,
398
398
body
399
399
) ) ;
400
400
if ( isExported ) {
@@ -449,6 +449,21 @@ export function transformModuleSystem(sourceFile: ts.SourceFile, context: Script
449
449
node = tryUpdateLocalRequireCall ( node ) ;
450
450
context . file . hierarchyFacts |= HierarchyFacts . ContainsRequire ;
451
451
}
452
+ if ( isJestMethodCallWithModuleName ( node ) ) {
453
+ const moduleSpecifier = ( node . arguments [ 0 ] as ts . StringLiteral ) . text ;
454
+ const moduleName = system . normalizePath ( system . joinPath ( context . file . relativeDirPath , moduleSpecifier ) ) ;
455
+ const actionNamespaceDirs = context . actionsNamespace ? context . actionsNamespace . split ( '.' ) : [ ] ;
456
+ const relativeDirPathDirs = context . file . relativeDirPath ? context . file . relativeDirPath . split ( '/' ) : [ ] ;
457
+ const subfolderLevels = 1 + actionNamespaceDirs . length + relativeDirPathDirs . length ;
458
+ const newPath = "../" . repeat ( subfolderLevels ) + 'src/' + actionNamespaceDirs . join ( '/' ) + '/' + moduleName ;
459
+
460
+ const newArguments : ts . Expression [ ] = [ ts . factory . createStringLiteral ( newPath ) ] ;
461
+ for ( let i = 1 ; i < node . arguments . length ; i ++ ) {
462
+ newArguments . push ( node . arguments [ i ] ) ;
463
+ }
464
+ const newNode = ts . factory . updateCallExpression ( node , node . expression , node . typeArguments , newArguments ) ;
465
+ return visitor . visitEachChild ( newNode ) ;
466
+ }
452
467
return visitor . visitEachChild ( node ) ;
453
468
}
454
469
@@ -545,7 +560,7 @@ export function transformModuleSystem(sourceFile: ts.SourceFile, context: Script
545
560
requireCallNode = ts . factory . updateCallExpression (
546
561
requireCallNode ,
547
562
ts . factory . createPropertyAccessExpression ( ts . factory . createIdentifier ( SCRIPT_VROES_VAR ) , SCRIPT_LAZY_IMPORT_NAME ) ,
548
- /* typeArguments */ undefined ,
563
+ /* typeArguments */ undefined ,
549
564
requireCallNode . arguments ) ;
550
565
node = ts . factory . createVariableDeclaration (
551
566
node . name ,
@@ -606,7 +621,7 @@ export function transformModuleSystem(sourceFile: ts.SourceFile, context: Script
606
621
requireCallNode = ts . factory . updateCallExpression (
607
622
requireCallNode ,
608
623
requireCallNode . expression ,
609
- /* typeArguments */ undefined ,
624
+ /* typeArguments */ undefined ,
610
625
[ ts . factory . createStringLiteral ( moduleName ) ] ) ;
611
626
}
612
627
}
0 commit comments