11import * as path from "path" ;
22import * as ts from "typescript" ;
3- import { getSourceDir } from "../../src" ;
3+ import { getEmitPath , getSourceDir } from "../../src" ;
44import * as util from "../util" ;
55
66const cwd = process . cwd ( ) ;
@@ -12,31 +12,24 @@ describe("getSourceDir", () => {
1212 test ( "with rootDir" , ( ) => {
1313 const program = ts . createProgram ( [ "main.ts" , "src/otherfile.ts" ] , { configFilePath, rootDir : "src" } ) ;
1414
15- // getCommonSourceDirectory does not work right so mock it
16- jest . spyOn ( program , "getCommonSourceDirectory" ) . mockReturnValue ( cwd ) ;
17-
15+ // If rootdir is specified, rootDir is the sourceDir
1816 expect ( getSourceDir ( program ) ) . toBe ( path . join ( cwd , "src" ) ) ;
1917 } ) ;
2018
2119 test ( "without rootDir" , ( ) => {
2220 const program = ts . createProgram ( [ "main.ts" , "src/otherfile.ts" ] , { configFilePath } ) ;
2321
24- // getCommonSourceDirectory does not work right so mock it
25- jest . spyOn ( program , "getCommonSourceDirectory" ) . mockReturnValue ( cwd ) ;
26-
27- // Common sources directory is project root
22+ // If rootDir is not specified, root dir is where the config file is
2823 expect ( normalize ( getSourceDir ( program ) ) ) . toBe ( cwd ) ;
2924 } ) ;
3025
31- test ( "without rootDir in src dir" , ( ) => {
32- const program = ts . createProgram ( [ path . join ( cwd , "src" , "main.ts" ) , path . join ( cwd , "src" , "otherfile.ts" ) ] , {
33- configFilePath,
34- } ) ;
26+ test ( "without config file in src dir" , ( ) => {
27+ const program = ts . createProgram ( [ path . join ( cwd , "src" , "main.ts" ) , path . join ( cwd , "src" , "otherfile.ts" ) ] , { } ) ;
3528
3629 // getCommonSourceDirectory does not work right so mock it
3730 jest . spyOn ( program , "getCommonSourceDirectory" ) . mockReturnValue ( path . join ( cwd , "src" ) ) ;
3831
39- // Common sources directory is src
32+ // If there is no config file, return the common source directory
4033 expect ( normalize ( getSourceDir ( program ) ) ) . toBe ( path . join ( cwd , "src" ) ) ;
4134 } ) ;
4235} ) ;
@@ -145,6 +138,21 @@ describe("getEmitPath", () => {
145138 expect ( fileNames ) . toHaveLength ( 1 ) ;
146139 expect ( fileNames ) . toContain ( path . join ( cwd , "out1" , "out2" , "bundle.scar" ) ) ;
147140 } ) ;
141+
142+ // https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1540
143+ test ( "puts files next to their source if no config is given (#1540)" , ( ) => {
144+ const file1 = path . join ( "src" , "main.ts" ) ;
145+ const file2 = path . join ( "src" , "otherfile.ts" ) ;
146+ const file3 = path . join ( "src" , "nested" , "nestedfile.ts" ) ;
147+ const program = ts . createProgram ( [ file1 , file2 , file3 ] , { configFilePath } ) ;
148+
149+ // If rootDir is not specified, root dir is where the config file is
150+ const configRoot = path . dirname ( configFilePath ) ;
151+ const replaceExtension = ( f : string ) => f . replace ( / \. t s $ / , ".lua" ) ;
152+ expect ( getEmitPath ( file1 , program ) ) . toBe ( replaceExtension ( path . join ( configRoot , file1 ) ) ) ;
153+ expect ( getEmitPath ( file2 , program ) ) . toBe ( replaceExtension ( path . join ( configRoot , file2 ) ) ) ;
154+ expect ( getEmitPath ( file3 , program ) ) . toBe ( replaceExtension ( path . join ( configRoot , file3 ) ) ) ;
155+ } ) ;
148156} ) ;
149157
150158function normalize ( path : string ) {
0 commit comments