Skip to content

Commit 8a7b31b

Browse files
authored
Added support for importequals declaration (#598)
* Added support for importequals declaration * made importequalsdeclaration hoistable * Got rid of a visitor method and used getIdentifierText * Changed transformIdentifier to transformIdentifierExpression * Added test with import equals and require * Prettier format
1 parent 440ef27 commit 8a7b31b

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/LuaTransformer.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ export class LuaTransformer {
159159
return this.transformExportDeclaration(node as ts.ExportDeclaration);
160160
case ts.SyntaxKind.ImportDeclaration:
161161
return this.transformImportDeclaration(node as ts.ImportDeclaration);
162+
case ts.SyntaxKind.ImportEqualsDeclaration:
163+
return this.transformImportEqualsDeclaration(node as ts.ImportEqualsDeclaration);
162164
case ts.SyntaxKind.ClassDeclaration:
163165
return this.transformClassDeclaration(node as ts.ClassDeclaration);
164166
case ts.SyntaxKind.ModuleDeclaration:
@@ -458,6 +460,38 @@ export class LuaTransformer {
458460
}
459461
}
460462

463+
public transformImportEqualsDeclaration(declaration: ts.ImportEqualsDeclaration): StatementVisitResult {
464+
const name = this.transformIdentifier(declaration.name);
465+
const expression = ts.isExternalModuleReference(declaration.moduleReference)
466+
? this.transformExternalModuleReference(declaration.moduleReference)
467+
: this.transformEntityName(declaration.moduleReference);
468+
469+
return this.createHoistableVariableDeclarationStatement(name, expression, declaration);
470+
}
471+
472+
public transformExternalModuleReference(
473+
externalModuleReference: ts.ExternalModuleReference
474+
): ExpressionVisitResult {
475+
return tstl.createCallExpression(
476+
tstl.createIdentifier("require"),
477+
[this.transformExpression(externalModuleReference.expression)],
478+
externalModuleReference
479+
);
480+
}
481+
482+
private transformEntityName(entityName: ts.EntityName): ExpressionVisitResult {
483+
return ts.isQualifiedName(entityName)
484+
? this.transformQualifiedName(entityName)
485+
: this.transformIdentifierExpression(entityName);
486+
}
487+
488+
public transformQualifiedName(qualifiedName: ts.QualifiedName): ExpressionVisitResult {
489+
const right = tstl.createStringLiteral(this.getIdentifierText(qualifiedName.right), qualifiedName.right);
490+
const left = this.transformEntityName(qualifiedName.left);
491+
492+
return tstl.createTableIndexExpression(left, right, qualifiedName);
493+
}
494+
461495
public transformClassDeclaration(
462496
statement: ts.ClassLikeDeclaration,
463497
nameOverride?: tstl.Identifier
@@ -3755,7 +3789,7 @@ export class LuaTransformer {
37553789
}
37563790

37573791
public transformPropertyAccessExpression(expression: ts.PropertyAccessExpression): ExpressionVisitResult {
3758-
const property = expression.name.text;
3792+
const property = this.getIdentifierText(expression.name);
37593793

37603794
// Check for primitive types to override
37613795
const type = this.checker.getTypeAtLocation(expression.expression);

test/unit/declarations.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,51 @@ test("Declaration instance function callback", () => {
9292
const result = util.transpileAndExecute(source, undefined, libLua, tsHeader);
9393
expect(result).toBe(20);
9494
});
95+
96+
test("ImportEquals declaration", () => {
97+
const header = `
98+
namespace outerNamespace {
99+
export namespace innerNamespace {
100+
export function func() { return "foo" }
101+
}
102+
};
103+
104+
import importedFunc = outerNamespace.innerNamespace.func;
105+
`;
106+
107+
const execution = `return importedFunc();`;
108+
109+
const result = util.transpileAndExecute(execution, undefined, undefined, header);
110+
expect(result).toEqual("foo");
111+
});
112+
113+
test("ImportEquals declaration ambient", () => {
114+
const header = `
115+
declare namespace outerNamespace {
116+
namespace innerNamespace {
117+
function func(): string;
118+
}
119+
};
120+
121+
import importedFunc = outerNamespace.innerNamespace.func;
122+
`;
123+
124+
const luaHeader = `outerNamespace = {
125+
innerNamespace = {
126+
func = function() return "foo" end
127+
}
128+
}
129+
`;
130+
131+
const execution = `return importedFunc();`;
132+
133+
const result = util.transpileAndExecute(execution, undefined, luaHeader, header);
134+
expect(result).toEqual("foo");
135+
});
136+
137+
test("ImportEquals declaration require", () => {
138+
const source = `import foo = require("bar");`;
139+
140+
const result = util.transpileString(source);
141+
expect(result.includes(`local foo = require("bar")`)).toBeTruthy();
142+
});

0 commit comments

Comments
 (0)