@@ -7,6 +7,8 @@ import { DecoratorKind } from "./Decorator";
77import { TSTLErrors } from "./Errors" ;
88import { TSHelper as tsHelper } from "./TSHelper" ;
99
10+ import { LuaTransformer } from "./Transformer" ;
11+
1012/* tslint:disable */
1113const packageJSON = require ( "../package.json" ) ;
1214/* tslint:enable */
@@ -159,31 +161,6 @@ export abstract class LuaTranspiler {
159161 this . luaLibFeatureSet . add ( feature ) ;
160162 }
161163
162- public getAbsoluteImportPath ( relativePath : string ) : string {
163- if ( relativePath . charAt ( 0 ) !== "." && this . options . baseUrl ) {
164- return path . resolve ( this . options . baseUrl , relativePath ) ;
165- }
166- return path . resolve ( path . dirname ( this . sourceFile . fileName ) , relativePath ) ;
167- }
168-
169- public getImportPath ( relativePath : string ) : string {
170- // Calculate absolute path to import
171- const absolutePathToImport = this . getAbsoluteImportPath ( relativePath ) ;
172- if ( this . options . rootDir ) {
173- // Calculate path relative to project root
174- // and replace path.sep with dots (lua doesn't know paths)
175- const relativePathToRoot =
176- this . pathToLuaRequirePath ( absolutePathToImport . replace ( this . options . rootDir , "" ) . slice ( 1 ) ) ;
177- return `"${ relativePathToRoot } "` ;
178- }
179-
180- return `"${ this . pathToLuaRequirePath ( relativePath ) } "` ;
181- }
182-
183- public pathToLuaRequirePath ( filePath : string ) : string {
184- return filePath . replace ( new RegExp ( "\\\\|\/" , "g" ) , "." ) ;
185- }
186-
187164 public computeEnumMembers ( node : ts . EnumDeclaration ) : Array < { name : string , value : string | number } > {
188165 let val : number | string = 0 ;
189166 let hasStringInitializers = false ;
@@ -221,6 +198,9 @@ export abstract class LuaTranspiler {
221198 }
222199 let result = header ;
223200
201+ const transformer = new LuaTransformer ( this . checker , this . options ) ;
202+ this . sourceFile = transformer . transform ( this . sourceFile ) ;
203+
224204 // Transpile content first to gather some info on dependencies
225205 let fileStatements = "" ;
226206 this . exportStack . push ( [ ] ) ;
@@ -285,8 +265,6 @@ export abstract class LuaTranspiler {
285265 }
286266
287267 switch ( node . kind ) {
288- case ts . SyntaxKind . ImportDeclaration :
289- return this . transpileImport ( node as ts . ImportDeclaration ) ;
290268 case ts . SyntaxKind . ClassDeclaration :
291269 return this . transpileClass ( node as ts . ClassDeclaration ) ;
292270 case ts . SyntaxKind . ModuleDeclaration :
@@ -341,53 +319,6 @@ export abstract class LuaTranspiler {
341319 return `__TS__${ func } (${ params . join ( ", " ) } )` ;
342320 }
343321
344- public transpileImport ( node : ts . ImportDeclaration ) : string {
345- const importPath = this . transpileExpression ( node . moduleSpecifier ) ;
346- const importPathWithoutQuotes = importPath . replace ( new RegExp ( "\"" , "g" ) , "" ) ;
347-
348- if ( ! node . importClause || ! node . importClause . namedBindings ) {
349- throw TSTLErrors . DefaultImportsNotSupported ( node ) ;
350- }
351-
352- const imports = node . importClause . namedBindings ;
353-
354- const requireKeyword = "require" ;
355-
356- if ( ts . isNamedImports ( imports ) ) {
357- const fileImportTable = path . basename ( importPathWithoutQuotes ) + this . importCount ;
358- const resolvedImportPath = this . getImportPath ( importPathWithoutQuotes ) ;
359-
360- let result = `local ${ fileImportTable } = ${ requireKeyword } (${ resolvedImportPath } )\n` ;
361- this . importCount ++ ;
362-
363- const filteredElements = imports . elements . filter ( e => {
364- const decorators = tsHelper . getCustomDecorators ( this . checker . getTypeAtLocation ( e ) , this . checker ) ;
365- return ! decorators . has ( DecoratorKind . Extension ) && ! decorators . has ( DecoratorKind . MetaExtension ) ;
366- } ) ;
367-
368- if ( filteredElements . length === 0 ) {
369- return "" ;
370- }
371-
372- filteredElements . forEach ( element => {
373- const nameText = this . transpileIdentifier ( element . name ) ;
374- if ( element . propertyName ) {
375- const propertyText = this . transpileIdentifier ( element . propertyName ) ;
376- result += `local ${ nameText } = ${ fileImportTable } .${ propertyText } \n` ;
377- } else {
378- result += `local ${ nameText } = ${ fileImportTable } .${ nameText } \n` ;
379- }
380- } ) ;
381-
382- return result ;
383- } else if ( ts . isNamespaceImport ( imports ) ) {
384- const resolvedImportPath = this . getImportPath ( importPathWithoutQuotes ) ;
385- return `local ${ this . transpileIdentifier ( imports . name ) } = ${ requireKeyword } (${ resolvedImportPath } )\n` ;
386- } else {
387- throw TSTLErrors . UnsupportedImportType ( imports ) ;
388- }
389- }
390-
391322 public transpileNamespace ( node : ts . ModuleDeclaration ) : string {
392323 const decorators = tsHelper . getCustomDecorators ( this . checker . getTypeAtLocation ( node ) , this . checker ) ;
393324 // If phantom namespace just transpile the body as normal
0 commit comments