Skip to content

Commit

Permalink
Merge pull request #175 from fossamagna/fix-173-no-assignment-default…
Browse files Browse the repository at this point in the history
…-global

fix: No generate entry function declaration from default export
  • Loading branch information
fossamagna committed Sep 9, 2020
2 parents 36e88b6 + e3990a3 commit 60191f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ function _generateStubs(ast, options) {
}
if (autoGlobalExports) {
if (node.type === 'ExpressionStatement'
&& isExportsAssignmentExpression(node.expression)) {
&& isNamedExportsAssignmentExpression(node.expression)) {
const functionName = node.expression.left.property.name;
entryPointFunctions.add(functionName, node.expression.right.params, node.leadingComments);
} else if (node.type === 'ExpressionStatement'
&& node.expression.type === 'SequenceExpression') {
node.expression.expressions.forEach(function (expression) {
if (isExportsAssignmentExpression(expression)) {
if (isNamedExportsAssignmentExpression(expression)) {
const functionName = expression.left.property.name;
entryPointFunctions.add(functionName, expression.right.params, expression.leadingComments ?
expression.leadingComments : node.leadingComments);
Expand All @@ -124,15 +124,17 @@ function isGlobalAssignmentExpression(node) {
&& node.operator === '='
&& node.left.type === 'MemberExpression'
&& node.left.object.type === 'Identifier'
&& node.left.object.name === 'global'
&& node.left.object.name === 'global';
}

function isExportsAssignmentExpression(node) {
function isNamedExportsAssignmentExpression(node) {
return node.type === 'AssignmentExpression'
&& node.operator === '='
&& node.left.type === 'MemberExpression'
&& node.left.object.type === 'Identifier'
&& node.left.object.name === 'exports'
&& node.left.property.type === 'Identifier'
&& node.left.property.name !== 'default';
}

function generateStubs(ast, options) {
Expand All @@ -147,13 +149,13 @@ function generateGlobalAssignments(ast) {
estraverse.traverse(ast, {
leave: (node) => {
if (node.type === 'ExpressionStatement'
&& isExportsAssignmentExpression(node.expression)) {
&& isNamedExportsAssignmentExpression(node.expression)) {
const functionName = node.expression.left.property.name;
globalAssignments.add(functionName);
} else if (node.type === 'ExpressionStatement'
&& node.expression.type === 'SequenceExpression') {
node.expression.expressions.forEach(function (expression) {
if (isExportsAssignmentExpression(expression)) {
if (isNamedExportsAssignmentExpression(expression)) {
const functionName = expression.left.property.name;
globalAssignments.add(functionName);
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/exports-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ function test() {
};
var X = 'x';
exports.test = test, exports.X = X;

exports.default = function noAssignmentToGlobal() {};

0 comments on commit 60191f1

Please sign in to comment.