@@ -3,62 +3,15 @@ import * as ts from "typescript";
33
44import { Expect } from "alsatian" ;
55
6- import { CompilerOptions } from "../../src/CommandLineParser " ;
7- import { createTranspiler } from "../../src/Compiler " ;
8- import { LuaLibImportKind , LuaTarget , LuaTranspiler } from "../../src/Transpiler " ;
6+ import { transpileString } from "../../src/Compiler " ;
7+ import { LuaTarget , LuaTranspiler } from "../../src/Transpiler " ;
8+ import { createTranspiler } from "../../src/TranspilerFactory " ;
99
1010import { lauxlib , lua , lualib , to_jsstring , to_luastring } from "fengari" ;
1111
12- const fs = require ( "fs" ) ;
13-
14- const libSource = fs . readFileSync ( path . join ( path . dirname ( require . resolve ( "typescript" ) ) , "lib.es6.d.ts" ) ) . toString ( ) ;
15-
16- export function transpileString ( str : string , options : CompilerOptions = { luaLibImport : LuaLibImportKind . Require , luaTarget : LuaTarget . Lua53 } ) : string {
17- const compilerHost = {
18- directoryExists : ( ) => true ,
19- fileExists : ( fileName ) : boolean => true ,
20- getCanonicalFileName : fileName => fileName ,
21- getCurrentDirectory : ( ) => "" ,
22- getDefaultLibFileName : ( ) => "lib.es6.d.ts" ,
23- getDirectories : ( ) => [ ] ,
24- getNewLine : ( ) => "\n" ,
25-
26- getSourceFile : ( filename , languageVersion ) => {
27- if ( filename === "file.ts" ) {
28- return ts . createSourceFile ( filename , str , ts . ScriptTarget . Latest , false ) ;
29- }
30- if ( filename === "lib.es6.d.ts" ) {
31- return ts . createSourceFile ( filename , libSource , ts . ScriptTarget . Latest , false ) ;
32- }
33- return undefined ;
34- } ,
35-
36- readFile : ( ) => "" ,
37-
38- useCaseSensitiveFileNames : ( ) => false ,
39- // Don't write output
40- writeFile : ( name , text , writeByteOrderMark ) => null ,
41- } ;
42- const program = ts . createProgram ( [ "file.ts" ] , options , compilerHost ) ;
43-
44- const result = createTranspiler ( program . getTypeChecker ( ) ,
45- options ,
46- program . getSourceFile ( "file.ts" ) ) . transpileSourceFile ( ) ;
47- return result . trim ( ) ;
48- }
49-
50- export function transpileFile ( filePath : string ) : string {
51- const program = ts . createProgram ( [ filePath ] , { } ) ;
52- const checker = program . getTypeChecker ( ) ;
12+ import * as fs from "fs" ;
5313
54- // Output errors
55- const diagnostics = ts . getPreEmitDiagnostics ( program ) . filter ( diag => diag . code !== 6054 ) ;
56- diagnostics . forEach ( diagnostic => console . log ( `${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) } ` ) ) ;
57-
58- const options : ts . CompilerOptions = { luaLibImport : "none" } ;
59- const result = createTranspiler ( checker , options , program . getSourceFile ( filePath ) ) . transpileSourceFile ( ) ;
60- return result . trim ( ) ;
61- }
14+ export { transpileString } ;
6215
6316export function executeLua ( luaStr : string , withLib = true ) : any {
6417 if ( withLib ) {
@@ -91,7 +44,7 @@ export function executeLua(luaStr: string, withLib = true): any {
9144 }
9245}
9346
94- export function expectCodeEqual ( code1 : string , code2 : string ) {
47+ export function expectCodeEqual ( code1 : string , code2 : string ) : void {
9548 // Trim leading/trailing whitespace
9649 let c1 = code1 . trim ( ) ;
9750 let c2 = code2 . trim ( ) ;
@@ -104,14 +57,14 @@ export function expectCodeEqual(code1: string, code2: string) {
10457}
10558
10659// Get a mock transpiler to use for testing
107- export function makeTestTranspiler ( target : LuaTarget = LuaTarget . Lua53 ) {
60+ export function makeTestTranspiler ( target : LuaTarget = LuaTarget . Lua53 ) : LuaTranspiler {
10861 return createTranspiler ( { } as ts . TypeChecker ,
10962 { luaLibImport : "none" , luaTarget : target } as any ,
11063 { statements : [ ] } as any as ts . SourceFile ) ;
11164}
11265
113- export function transpileAndExecute ( ts : string ) : any {
114- return executeLua ( transpileString ( ts ) ) ;
66+ export function transpileAndExecute ( tsStr : string ) : any {
67+ return executeLua ( transpileString ( tsStr ) ) ;
11568}
11669
11770const jsonlib = fs . readFileSync ( "test/src/json.lua" ) + "\n" ;
0 commit comments