Skip to content

Commit a6d72b0

Browse files
authored
Fix unsafe name default export (#1368)
* Fix unsafe name default export * Nicely unpack preceding statements * Make functional tests instead of snapshots
1 parent df6b4d0 commit a6d72b0

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/transformation/visitors/class/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ import { createMethodDecoratingExpression, transformMethodDeclaration } from "./
2323
import { getExtendedNode, getExtendedType, isStaticNode } from "./utils";
2424
import { createClassSetup } from "./setup";
2525
import { LuaTarget } from "../../../CompilerOptions";
26+
import { transformInPrecedingStatementScope } from "../../utils/preceding-statements";
2627

2728
export const transformClassDeclaration: FunctionVisitor<ts.ClassLikeDeclaration> = (declaration, context) => {
2829
// If declaration is a default export, transform to export variable assignment instead
2930
if (hasDefaultExportModifier(declaration)) {
30-
const left = createDefaultExportExpression(declaration);
31-
const right = transformClassAsExpression(declaration, context);
32-
return [lua.createAssignmentStatement(left, right, declaration)];
31+
// Class declaration including assignment to ____exports.default are in preceding statements
32+
const [precedingStatements] = transformInPrecedingStatementScope(context, () => {
33+
transformClassAsExpression(declaration, context);
34+
return [];
35+
});
36+
return precedingStatements;
3337
}
3438

3539
const { statements } = transformClassLikeDeclaration(declaration, context);

test/unit/identifiers.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,38 @@ describe("unicode identifiers in supporting environments (luajit)", () => {
285285
});
286286
});
287287

288+
test("unicode export class", () => {
289+
util.testModule`
290+
import { 你好 } from "./utfclass";
291+
export const result = new 你好().hello();
292+
`
293+
.addExtraFile(
294+
"utfclass.ts",
295+
`export class 你好 {
296+
hello() {
297+
return "你好";
298+
}
299+
}`
300+
)
301+
.expectToEqual({ result: "你好" });
302+
});
303+
304+
test("unicode export default class", () => {
305+
util.testModule`
306+
import c from "./utfclass";
307+
export const result = new c().hello();
308+
`
309+
.addExtraFile(
310+
"utfclass.ts",
311+
`export default class 你好 {
312+
hello() {
313+
return "你好";
314+
}
315+
}`
316+
)
317+
.expectToEqual({ result: "你好" });
318+
});
319+
288320
describe("lua keyword as identifier doesn't interfere with lua's value", () => {
289321
test("variable (nil)", () => {
290322
util.testFunction`

0 commit comments

Comments
 (0)