Skip to content

Commit

Permalink
fix: generate single entry function declaration from TypeScript named…
Browse files Browse the repository at this point in the history
… export

fix #172
  • Loading branch information
fossamagna committed Sep 8, 2020
1 parent 075a806 commit e0c8a2c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
72 changes: 55 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,87 @@ function createStubFunctionASTNode(functionName, leadingComments, params) {
return node;
}

class EntryPointFunctions {
constructor() {
this.stubs = new Map();
this.functionNames = [];
}

add(functionName, params, comments) {
let index = this.functionNames.indexOf(functionName);
if (index === -1) {
index = this.functionNames.push(functionName) - 1;
}
this.stubs.set(index, createStubFunctionASTNode(functionName, comments, params));
}

getEntryPointFunctions() {
const entryPointFunctions = [];
for (let index = 0; index < this.functionNames.length; index++) {
entryPointFunctions.push(this.stubs.get(index));
}
return entryPointFunctions;
}
}

class GlobalAssignments {
constructor() {
this.stubs = [];
this.functionNames = new Set();
}

add(functionName) {
if (this.functionNames.has(functionName)) {
return;
}
this.functionNames.add(functionName);
this.stubs.push(createGlobalAssignmentASTNode(functionName));
}

getGlobalAssignments() {
return this.stubs;
}
}

function _generateStubs(ast, options) {
const autoGlobalExports = options.autoGlobalExports;
const stubs = [];
const entryPointFunctions = new EntryPointFunctions();
estraverse.traverse(ast, {
leave: function (node) {
if (node.type === 'ExpressionStatement'
&& isGlobalAssignmentExpression(node.expression)) {
const functionName = node.expression.left.property.name;
stubs.push(createStubFunctionASTNode(functionName, node.leadingComments, node.expression.right.params));
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 (isGlobalAssignmentExpression(expression)) {
const functionName = expression.left.property.name;
stubs.push(createStubFunctionASTNode(functionName, expression.leadingComments ?
expression.leadingComments : node.leadingComments, expression.right.params));
entryPointFunctions.add(functionName, expression.right.params, expression.leadingComments ?
expression.leadingComments : node.leadingComments);
}
});
}
if (autoGlobalExports) {
if (node.type === 'ExpressionStatement'
&& isExportsAssignmentExpression(node.expression)) {
const functionName = node.expression.left.property.name;
stubs.push(createStubFunctionASTNode(functionName, node.leadingComments, node.expression.right.params));
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)) {
const functionName = expression.left.property.name;
stubs.push(createStubFunctionASTNode(functionName, expression.leadingComments ?
expression.leadingComments : node.leadingComments, expression.right.params));
entryPointFunctions.add(functionName, expression.right.params, expression.leadingComments ?
expression.leadingComments : node.leadingComments);
}
});
}
}
}
});

return stubs;
return entryPointFunctions.getEntryPointFunctions();
}

function isGlobalAssignmentExpression(node) {
Expand All @@ -96,35 +138,31 @@ function isExportsAssignmentExpression(node) {
function generateStubs(ast, options) {
const baseAST = createBaseAST();
const stubs = _generateStubs(ast, options);
stubs.forEach(function (stub) {
baseAST.body.push(stub);
});
baseAST.body.push(...stubs);
return escodegen.generate(baseAST, { comment: !!options.comment });
}

function generateGlobalAssignments(ast) {
const stubs = [];
const globalAssignments = new GlobalAssignments();
estraverse.traverse(ast, {
leave: (node) => {
if (node.type === 'ExpressionStatement'
&& isExportsAssignmentExpression(node.expression)) {
const functionName = node.expression.left.property.name;
stubs.push(createGlobalAssignmentASTNode(functionName));
globalAssignments.add(functionName);
} else if (node.type === 'ExpressionStatement'
&& node.expression.type === 'SequenceExpression') {
node.expression.expressions.forEach(function (expression) {
if (isExportsAssignmentExpression(expression)) {
const functionName = expression.left.property.name;
stubs.push(createGlobalAssignmentASTNode(functionName));
globalAssignments.add(functionName);
}
});
}
}
});
const baseAST = createBaseAST();
stubs.forEach(function (stub) {
baseAST.body.push(stub);
});
baseAST.body.push(...globalAssignments.getGlobalAssignments());
return escodegen.generate(baseAST);
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/exports-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
exports.foo = function() {}

exports.boo = void 0;
/**
* This is boo.
*/
Expand Down

0 comments on commit e0c8a2c

Please sign in to comment.