Skip to content

Commit

Permalink
Fix function declarations immediately following a default export (#648)
Browse files Browse the repository at this point in the history
Use ContextualKeyword._async here to make sure that we check if it's an async function in default exports.

Fixes #543
  • Loading branch information
benjdlambert authored Oct 7, 2021
1 parent 5cdeb62 commit 978e138
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/transformers/CJSImportTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,11 @@ export default class CJSImportTransformer extends Transformer {
if (
this.tokens.matches4(tt._export, tt._default, tt._function, tt.name) ||
// export default async function
this.tokens.matches5(tt._export, tt._default, tt.name, tt._function, tt.name)
(this.tokens.matches5(tt._export, tt._default, tt.name, tt._function, tt.name) &&
this.tokens.matchesContextualAtIndex(
this.tokens.currentIndex() + 2,
ContextualKeyword._async,
))
) {
this.tokens.removeInitialToken();
this.tokens.removeToken();
Expand Down
6 changes: 5 additions & 1 deletion src/transformers/ESMImportTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ export default class ESMImportTransformer extends Transformer {
const alreadyHasName =
this.tokens.matches4(tt._export, tt._default, tt._function, tt.name) ||
// export default async function
this.tokens.matches5(tt._export, tt._default, tt.name, tt._function, tt.name) ||
(this.tokens.matches5(tt._export, tt._default, tt.name, tt._function, tt.name) &&
this.tokens.matchesContextualAtIndex(
this.tokens.currentIndex() + 2,
ContextualKeyword._async,
)) ||
this.tokens.matches4(tt._export, tt._default, tt._class, tt.name) ||
this.tokens.matches5(tt._export, tt._default, tt._abstract, tt._class, tt.name);

Expand Down
30 changes: 30 additions & 0 deletions test/imports-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,36 @@ module.exports = exports.default;
);
});

it("should transform export function blocks", () => {
assertResult(
`
const a = {}
export default a
function b() {}
`,
`"use strict";${ESMODULE_PREFIX}
const a = {}
exports. default = a
function b() {}
`,
{transforms: ["imports", "typescript"]},
);
});

it("should recognize 'export default async function'", () => {
assertResult(
`
export default async function blah() {}
blah();
`,
`"use strict";${ESMODULE_PREFIX}
async function blah() {} exports.default = blah;
blah();
`,
{transforms: ["imports", "typescript"]},
);
});

it("properly handles shadowing for simple arrow functions", () => {
assertResult(
`
Expand Down
27 changes: 27 additions & 0 deletions test/react-hot-loader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,33 @@ describe("transform react-hot-loader", () => {
);
});

it("should register unnamed default export followed by function declaration", () => {
assertESMResult(
`
const a = 1
export default a
function f() {}
`,
`${RHL_PREFIX}
const a = 1
let _default; export default _default = a
function f() {}
;(function () {
var reactHotLoader = require('react-hot-loader').default;
var leaveModule = require('react-hot-loader').leaveModule;
if (!reactHotLoader) {
return;
}
reactHotLoader.register(a, "a", "sample.tsx");
reactHotLoader.register(f, "f", "sample.tsx");
reactHotLoader.register(_default, "default", "sample.tsx");
leaveModule(module);
})();`,
["typescript"],
);
});

it("guards against ASI issues by starting the suffix with a semicolon", () => {
assertESMResult(
`
Expand Down

0 comments on commit 978e138

Please sign in to comment.