Skip to content

Commit

Permalink
Fix exported default class not being accessible by name (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov authored Aug 8, 2024
1 parent 3a280d3 commit f7ac208
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
8 changes: 5 additions & 3 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1465,12 +1465,14 @@ func (c *compiler) compileLexicalDeclarations(list []ast.Statement, scopeDeclare
c.createLexicalBindings(lex.LexicalDeclaration)
} else if lex.ClassDeclaration != nil {
declareScope()
if lex.IsDefault {
c.createLexicalIdBinding("default", false, int(lex.Idx0())-1)
} else {
if lex.ClassDeclaration.Class.Name != nil {
cls := lex.ClassDeclaration
c.createLexicalIdBinding(cls.Class.Name.Name, false, int(cls.Class.Name.Idx)-1)
}

if lex.IsDefault {
c.createLexicalIdBinding("default", false, int(lex.Idx0())-1)
}
} else if lex.IsDefault {
switch {
case lex.HoistableDeclaration != nil:
Expand Down
7 changes: 6 additions & 1 deletion compiler_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,12 @@ func (c *compiler) compileExportDeclaration(expr *ast.ExportDeclaration) {
case expr.ClassDeclaration != nil:
cls := expr.ClassDeclaration
if expr.IsDefault {
c.emitLexicalAssign("default", int(cls.Class.Class)-1, c.compileClassLiteral(cls.Class, false))
if cls.Class.Name == nil {
c.emitLexicalAssign("default", int(cls.Class.Class)-1, c.compileClassLiteral(cls.Class, false))
return
}
c.compileClassDeclaration(cls)
c.emitLexicalAssign("default", int(cls.Class.Class)-1, c.compileIdentifierExpression(cls.Class.Name))
} else {
c.compileClassDeclaration(cls)
}
Expand Down
6 changes: 5 additions & 1 deletion modules_sourcetext.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,13 @@ func exportEntriesFromAst(declarations []*ast.ExportDeclaration) []exportEntry {
} else if exportDeclaration.ClassDeclaration != nil {
cls := exportDeclaration.ClassDeclaration.Class
if exportDeclaration.IsDefault {
localName := "default"
if cls.Name != nil {
localName = cls.Name.Name.String()
}
result = append(result, exportEntry{
exportName: "default",
localName: "default",
localName: localName,
lex: true,
offset: int(exportDeclaration.Idx0()),
})
Expand Down
43 changes: 43 additions & 0 deletions modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,49 @@ func TestSimpleModule(t *testing.T) {
`,
"dep.js": `export default () => {return 5 };`,
},
"default export Class usage": {
"a.js": `
import b from "dep.js";
export default class m {
some() {
return b();
}
}
let l = new m();
globalThis.s = l.some();
`,
"dep.js": `export default () => {return 5 };`,
},
"default export function usage": {
"a.js": `
import b from "dep.js";
export default function some () {
return b();
}
globalThis.s = some();
`,
"dep.js": `export default () => {return 5 };`,
},
"default export generator usage": {
"a.js": `
import b from "dep.js";
export default function * some () {
yield b();
}
globalThis.s = some().next().value;
`,
"dep.js": `export default () => {return 5 };`,
},
"default export async function usage": {
"a.js": `
import b from "dep.js";
export default async function some () {
return b();
}
globalThis.s = await some();
`,
"dep.js": `export default () => {return 5 };`,
},
"default export arrow async": {
"a.js": `
import b from "dep.js";
Expand Down

0 comments on commit f7ac208

Please sign in to comment.