X Tutup
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
56 changes: 47 additions & 9 deletions src/transformation/visitors/modules/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,72 @@ export const transformExportAssignment: FunctionVisitor<ts.ExportAssignment> = (
}
};

function transformExportAllFrom(context: TransformationContext, node: ts.ExportDeclaration): lua.Statement | undefined {
function transformExportAll(context: TransformationContext, node: ts.ExportDeclaration): lua.Statement | undefined {
assert(node.moduleSpecifier);

if (!context.resolver.moduleExportsSomeValue(node.moduleSpecifier)) {
return undefined;
}

const moduleRequire = createModuleRequire(context, node.moduleSpecifier);
const tempModuleIdentifier = lua.createIdentifier("____export");

// export * as ns from "...";
// exports.ns = require(...)
if (node.exportClause && ts.isNamespaceExport(node.exportClause)) {
const assignToExports = lua.createAssignmentStatement(
lua.createTableIndexExpression(
createExportsIdentifier(),
lua.createStringLiteral(node.exportClause.name.text)
),
moduleRequire
);
return assignToExports;
}

// export * from "...";
// exports all values EXCEPT "default" from "..."
const result: lua.Statement[] = [];

// local ____export = require(...)
const tempModuleIdentifier = lua.createIdentifier("____export");
const declaration = lua.createVariableDeclarationStatement(tempModuleIdentifier, moduleRequire);
result.push(declaration);

// ____exports[____exportKey] = ____exportValue
const forKey = lua.createIdentifier("____exportKey");
const forValue = lua.createIdentifier("____exportValue");
const leftAssignment = lua.createAssignmentStatement(
lua.createTableIndexExpression(createExportsIdentifier(), forKey),
forValue
);

const body = lua.createBlock([
lua.createAssignmentStatement(lua.createTableIndexExpression(createExportsIdentifier(), forKey), forValue),
]);
// if key ~= "default" then
// -- export the value, do not export "default" values
// end
const ifBody = lua.createBlock([leftAssignment]);
const ifStatement = lua.createIfStatement(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment explaining what the purpose of this if statement is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more comments to transformExportAll, I think it should be much easier to read now

lua.createBinaryExpression(
lua.cloneIdentifier(forKey),
lua.createStringLiteral("default"),
lua.SyntaxKind.InequalityOperator
),
ifBody
);

// for ____exportKey, ____exportValue in ____export do
// -- export ____exportValue, unless ____exportKey is "default"
// end
const pairsIdentifier = lua.createIdentifier("pairs");
const forIn = lua.createForInStatement(
body,
lua.createBlock([ifStatement]),
[lua.cloneIdentifier(forKey), lua.cloneIdentifier(forValue)],
[lua.createCallExpression(pairsIdentifier, [lua.cloneIdentifier(tempModuleIdentifier)])]
);

result.push(forIn);

// Wrap this in a DoStatement to prevent polluting the scope.
return lua.createDoStatement([declaration, forIn], node);
return lua.createDoStatement(result, node);
}

const isDefaultExportSpecifier = (node: ts.ExportSpecifier) =>
Expand Down Expand Up @@ -127,7 +165,7 @@ export const getExported = (context: TransformationContext, exportSpecifiers: ts
export const transformExportDeclaration: FunctionVisitor<ts.ExportDeclaration> = (node, context) => {
if (!node.exportClause) {
// export * from "...";
return transformExportAllFrom(context, node);
return transformExportAll(context, node);
}

if (!context.resolver.isValueAliasDeclaration(node)) {
Expand All @@ -136,7 +174,7 @@ export const transformExportDeclaration: FunctionVisitor<ts.ExportDeclaration> =

if (ts.isNamespaceExport(node.exportClause)) {
// export * as ns from "...";
throw new Error("NamespaceExport is not supported");
return transformExportAll(context, node);
}

const exportSpecifiers = getExported(context, node.exportClause);
Expand Down
4 changes: 3 additions & 1 deletion test/translation/__snapshots__/transformation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ ____exports.uwv = xyz
do
local ____export = require(\\"xyz\\")
for ____exportKey, ____exportValue in pairs(____export) do
____exports[____exportKey] = ____exportValue
if ____exportKey ~= \\"default\\" then
____exports[____exportKey] = ____exportValue
end
end
end
do
Expand Down
29 changes: 29 additions & 0 deletions test/unit/modules/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,32 @@ test("export default function with future reference", () => {
.setReturnExport("result")
.expectToMatchJsResult();
});

const moduleFile = `
export default true;
export const foo = "bar";
`;

test("export all does not include default", () => {
util.testBundle`
export * from "./module";
`
.addExtraFile("module.ts", moduleFile)
.expectToEqual({ foo: "bar" });
});

test("namespace export does not include default", () => {
util.testBundle`
export * as result from "./module";
`
.addExtraFile("module.ts", moduleFile)
.expectToEqual({ result: { default: true, foo: "bar" } });
});

test("namespace export with unsafe Lua name", () => {
util.testBundle`
export * as $$$ from "./module";
`
.addExtraFile("module.ts", moduleFile)
.expectToEqual({ $$$: { default: true, foo: "bar" } });
});
X Tutup