Skip to content

Commit

Permalink
fix(exports): fixes a bug where exports = require(...)() would lead t…
Browse files Browse the repository at this point in the history
…o SyntaxErrors
  • Loading branch information
wessberg committed May 9, 2019
1 parent 4884474 commit 1803b54
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/transformer/before/visitor/visit/visit-binary-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {nodeContainsSuper} from "../../../util/node-contains-super";
import {addExportModifier} from "../../../util/add-export-modifier";
import {isRequireCall} from "../../../util/is-require-call";
import {getModuleExportsFromRequireDataInContext} from "../../../util/get-module-exports-from-require-data-in-context";
import {isExpression} from "../../../util/is-expression";

/**
* Visits the given BinaryExpression
* @param {BeforeVisitorOptions<BinaryExpression>} options
* @returns {VisitResult<BinaryExpression>}
*/
export function visitBinaryExpression ({node, sourceFile, context}: BeforeVisitorOptions<BinaryExpression>): VisitResult<Node> {
export function visitBinaryExpression ({node, sourceFile, context, continuation}: BeforeVisitorOptions<BinaryExpression>): VisitResult<Node> {
// Check if the left-hand side contains exports. For example: 'exports = ...' or 'exports.foo = 1' or event 'module.exports = 1'
const exportsData = getExportsData(node.left);
const right = walkThroughFillerNodes(node.right);
Expand Down Expand Up @@ -158,14 +159,22 @@ export function visitBinaryExpression ({node, sourceFile, context}: BeforeVisito

// Convert it into an ExportAssignment instead if possible
else {

// Check if the rightvalue represents a require(...) call.
const requireData = isRequireCall(node.right, sourceFile, context);

// If it doesn't, export the right side
if (!requireData.match) {
if (!context.isDefaultExported) {
context.markDefaultAsExported();
context.addTrailingStatements(createExportAssignment(undefined, undefined, false, node.right));
const continuationResult = continuation(node.right);
if (continuationResult == null || Array.isArray(continuationResult) || !isExpression(continuationResult)) {
return undefined;
}

else {
context.addTrailingStatements(createExportAssignment(undefined, undefined, false, continuationResult));
}
}
return undefined;
}
Expand Down
47 changes: 47 additions & 0 deletions test/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,33 @@ test("Converts 'exports = require(...)' syntax into namespace re-exports if the
);
});

test("Converts 'exports = require(...)()' syntax into namespace import along with a default export if the required module has named exports. #1", t => {
const bundle = generateTransformerResult([
{
entry: true,
fileName: "index.ts",
text: `
module.exports = require("./foo")();
`
},
{
entry: true,
fileName: "foo.ts",
text: `
exports.readFileSync = () => {};
`
}
]);
const [file] = bundle;
t.deepEqual(
formatCode(file.text),
formatCode(`\
import * as foo from "./foo";
export default foo();
`)
);
});

test("Converts 'exports = require(...)' syntax into a default export if the required module has one. #1", t => {
const bundle = generateTransformerResult([
{
Expand Down Expand Up @@ -526,4 +553,24 @@ test("Converts 'exports = require(...)' syntax into a default export if the requ
export {default} from "./foo";
`)
);
});

test("Converts 'exports = require(...)()' syntax into a default export if the required module is unknown. #2", t => {
const bundle = generateTransformerResult([
{
entry: true,
fileName: "index.ts",
text: `
module.exports = require("./foo")();
`
}
]);
const [file] = bundle;
t.deepEqual(
formatCode(file.text),
formatCode(`\
import foo from "./foo";
export default foo();
`)
);
});

0 comments on commit 1803b54

Please sign in to comment.