@@ -67,6 +67,54 @@ export function transpileAndExecute(tsStr: string): any {
6767 return executeLua ( transpileString ( tsStr ) ) ;
6868}
6969
70+ export function parseTypeScript ( typescript : string , target : LuaTarget = LuaTarget . Lua53 )
71+ : [ ts . SourceFile , ts . TypeChecker ] {
72+ const compilerHost = {
73+ directoryExists : ( ) => true ,
74+ fileExists : ( fileName ) : boolean => true ,
75+ getCanonicalFileName : fileName => fileName ,
76+ getCurrentDirectory : ( ) => "" ,
77+ getDefaultLibFileName : ( ) => "lib.es6.d.ts" ,
78+ getDirectories : ( ) => [ ] ,
79+ getNewLine : ( ) => "\n" ,
80+
81+ getSourceFile : ( filename , languageVersion ) => {
82+ if ( filename === "file.ts" ) {
83+ return ts . createSourceFile ( filename , typescript , ts . ScriptTarget . Latest , false ) ;
84+ }
85+ if ( filename === "lib.es6.d.ts" ) {
86+ const libPath = path . join ( path . dirname ( require . resolve ( "typescript" ) ) , "lib.es6.d.ts" ) ;
87+ const libSource = fs . readFileSync ( libPath ) . toString ( ) ;
88+ return ts . createSourceFile ( filename , libSource , ts . ScriptTarget . Latest , false ) ;
89+ }
90+ return undefined ;
91+ } ,
92+
93+ readFile : ( ) => "" ,
94+
95+ useCaseSensitiveFileNames : ( ) => false ,
96+ // Don't write output
97+ writeFile : ( name , text , writeByteOrderMark ) => null ,
98+ } ;
99+
100+ const program = ts . createProgram ( [ "file.ts" ] , { luaTarget : target } , compilerHost ) ;
101+ return [ program . getSourceFile ( "file.ts" ) , program . getTypeChecker ( ) ] ;
102+ }
103+
104+ export function findFirstChild ( node : ts . Node , predicate : ( node : ts . Node ) => boolean ) : ts . Node | undefined {
105+ for ( const child of node . getChildren ( ) ) {
106+ if ( predicate ( child ) ) {
107+ return child ;
108+ }
109+
110+ const childChild = findFirstChild ( child , predicate ) ;
111+ if ( childChild !== undefined ) {
112+ return childChild ;
113+ }
114+ }
115+ return undefined ;
116+ }
117+
70118const jsonlib = fs . readFileSync ( "test/src/json.lua" ) + "\n" ;
71119
72120export const minimalTestLib = jsonlib ;
0 commit comments