Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transformation/visitors/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function transformPropertyAccessExpressionWithCapture(

let property = node.name.text;
const symbol = context.checker.getSymbolAtLocation(node.name);
const customName = getCustomNameFromSymbol(symbol);
const customName = getCustomNameFromSymbol(context, symbol);
if (customName) {
property = customName;
}
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function transformContextualCallExpression(
let name = left.name.text;

const symbol = context.checker.getSymbolAtLocation(left);
const customName = getCustomNameFromSymbol(symbol);
const customName = getCustomNameFromSymbol(context, symbol);

if (customName) {
name = customName;
Expand Down
16 changes: 14 additions & 2 deletions src/transformation/visitors/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function transformIdentifier(context: TransformationContext, identifier:
return transformNonValueIdentifier(context, identifier, context.checker.getSymbolAtLocation(identifier));
}

export function getCustomNameFromSymbol(symbol?: ts.Symbol): undefined | string {
export function getCustomNameFromSymbol(context: TransformationContext, symbol?: ts.Symbol): undefined | string {
let retVal: undefined | string;

if (symbol) {
Expand All @@ -33,6 +33,18 @@ export function getCustomNameFromSymbol(symbol?: ts.Symbol): undefined | string
customNameAnnotation = foundAnnotation;
break;
}

// If the symbol is an imported value, check the original declaration
// beware of declaration.propertyName, this is the import name alias and should not be renamed!
if (ts.isImportSpecifier(declaration) && !declaration.propertyName) {
const importedType = context.checker.getTypeAtLocation(declaration);
if (importedType.symbol) {
const importedCustomName = getCustomNameFromSymbol(context, importedType.symbol);
if (importedCustomName) {
return importedCustomName;
}
}
}
}

if (customNameAnnotation) {
Expand Down Expand Up @@ -82,7 +94,7 @@ function transformNonValueIdentifier(

let text = hasUnsafeIdentifierName(context, identifier, symbol) ? createSafeName(identifier.text) : identifier.text;

const customName = getCustomNameFromSymbol(symbol);
const customName = getCustomNameFromSymbol(context, symbol);
if (customName) text = customName;

const symbolId = getIdentifierSymbolId(context, identifier, symbol);
Expand Down
11 changes: 9 additions & 2 deletions src/transformation/visitors/modules/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createHoistableVariableDeclarationStatement } from "../../utils/lua-ast
import { importLuaLibFeature, LuaLibFeature } from "../../utils/lualib";
import { createSafeName } from "../../utils/safe-names";
import { peekScope } from "../../utils/scope";
import { transformIdentifier } from "../identifier";
import { getCustomNameFromSymbol, transformIdentifier } from "../identifier";
import { transformPropertyName } from "../literal";

function isNoResolutionPath(context: TransformationContext, moduleSpecifier: ts.Expression): boolean {
Expand Down Expand Up @@ -46,8 +46,15 @@ function transformImportSpecifier(
importSpecifier: ts.ImportSpecifier,
moduleTableName: lua.Identifier
): lua.VariableDeclarationStatement {
const type = context.checker.getTypeAtLocation(importSpecifier.name);

const leftIdentifier = transformIdentifier(context, importSpecifier.name);
const propertyName = transformPropertyName(context, importSpecifier.propertyName ?? importSpecifier.name);

// If imported value has a customName annotation use that, otherwise use regular property
const customName = getCustomNameFromSymbol(context, type.getSymbol());
const propertyName = customName
? lua.createStringLiteral(customName, importSpecifier.propertyName ?? importSpecifier.name)
: transformPropertyName(context, importSpecifier.propertyName ?? importSpecifier.name);

return lua.createVariableDeclarationStatement(
leftIdentifier,
Expand Down
64 changes: 64 additions & 0 deletions test/unit/identifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,67 @@ test("customName rename declared function", () => {
expect(mainFile.lua).toContain("Test2(");
expect(mainFile.lua).not.toContain("Test(");
});

test("customName rename import specifier", () => {
const testModule = util.testModule`
import { Test } from "./myimport";
import { Test as Aliased } from "./myimport";
Test();
Aliased();
`.addExtraFile(
"myimport.ts",
`
/** @customName Test2 **/
export function Test(this: void): void {}
`
);

testModule.expectToHaveNoDiagnostics();
const result = testModule.getLuaResult();
expect(result.transpiledFiles).not.toHaveLength(0);

const mainFile = result.transpiledFiles.find(f => f.outPath === "main.lua");
expect(mainFile).toBeDefined();

// avoid ts error "not defined", even though toBeDefined is being checked above
if (!mainFile) return;

expect(mainFile.lua).toBeDefined();
expect(mainFile.lua).toContain("Test2(");
expect(mainFile.lua).toContain("myimport.Test2");
expect(mainFile.lua).not.toContain("Test(");

testModule.expectNoExecutionError();
});

test("customName import specifier from declarations", () => {
const testModule = util.testModule`
import { Test } from "./myimport";
import { Test as Aliased } from "./myimport";
Test();
Aliased();
`
.addExtraFile(
"myimport.d.ts",
`
/** @customName Test2 **/
export declare function Test(this: void): void;
`
)
.setOptions({ noResolvePaths: ["./myimport"] });

testModule.expectToHaveNoDiagnostics();
const result = testModule.getLuaResult();
expect(result.transpiledFiles).not.toHaveLength(0);

const mainFile = result.transpiledFiles.find(f => f.outPath === "main.lua");
expect(mainFile).toBeDefined();

// avoid ts error "not defined", even though toBeDefined is being checked above
if (!mainFile) return;

expect(mainFile.lua).toBeDefined();
expect(mainFile.lua).toContain("Test2(");
expect(mainFile.lua).toContain("myimport.Test2");
expect(mainFile.lua).not.toContain("Test(");
});
Loading