Skip to content

Commit

Permalink
Add comment option.
Browse files Browse the repository at this point in the history
  • Loading branch information
fossamagna committed Dec 19, 2018
1 parent 6d30ada commit 8d2a0c4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
36 changes: 23 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function createBaseAST() {
return ast;
}

function createStubFunctionASTNode(functionName) {
function createStubFunctionASTNode(functionName, leadingComments) {
var node = {
type: 'FunctionDeclaration',
id: {
Expand All @@ -27,35 +27,45 @@ function createStubFunctionASTNode(functionName) {
generator: false,
expression: false
};
if (leadingComments) {
node.leadingComments = leadingComments;
}
return node;
}

function _generateStubs(data) {
var ast = esprima.parse(data);
function _generateStubs(data, options) {
var ast = esprima.parseScript(data, { attachComment: options.comment });
var stubs = [];
estraverse.traverse(ast, {
leave: function (node) {
if (node.type === 'AssignmentExpression'
&& node.operator === '='
&& node.left.type === 'MemberExpression'
&& node.left.object.type === 'Identifier'
&& node.left.object.name === 'global') {
var functionName = node.left.property.name;
stubs.push(createStubFunctionASTNode(functionName));
if (node.type === 'ExpressionStatement'
&& isGlobalAssignmentExpression(node.expression)) {
var functionName = node.expression.left.property.name;
stubs.push(createStubFunctionASTNode(functionName, node.leadingComments));
}
}
});

return stubs;
}

function generateStubs(source) {
function isGlobalAssignmentExpression(node) {
return node.type === 'AssignmentExpression'
&& node.operator === '='
&& node.left.type === 'MemberExpression'
&& node.left.object.type === 'Identifier'
&& node.left.object.name === 'global'
}

function generateStubs(source, options) {
options = options || {comment: false};
var comment = !!options.comment;
var baseAST = createBaseAST();
var stubs = _generateStubs(source);
var stubs = _generateStubs(source, options);
stubs.forEach(function (stub) {
baseAST.body.push(stub);
});
return escodegen.generate(baseAST);
return escodegen.generate(baseAST, { comment: comment });
}

module.exports = generateStubs;
10 changes: 10 additions & 0 deletions test/fixtures/expected.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
/**
* Leading Block Comment for foo.
*/
// leading line comment for foo
function foo() {
}
/**
* Leading Block Comment for boo.
*/
// leading line comment for boo
function boo() {
}
10 changes: 10 additions & 0 deletions test/fixtures/source.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
/**
* Leading Block Comment for foo.
*/
// leading line comment for foo
global.foo = function () {
};
/**
* Leading Block Comment for boo.
*/
// leading line comment for boo
global.boo = function () {
};
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var gasEntryGenerator = require('../');
test('gas-entry-generator', function(t) {
var source = fs.readFileSync(__dirname + '/fixtures/source.js', {encoding: 'utf8'});
var expected = fs.readFileSync(__dirname + '/fixtures/expected.js', {encoding: 'utf8'});
var entryFunctions = gasEntryGenerator(source);
var entryFunctions = gasEntryGenerator(source, {comment: true});
t.equal(entryFunctions.toString(), expected.toString(), 'actual output will match expected');
t.end();
});

0 comments on commit 8d2a0c4

Please sign in to comment.