Skip to content

Commit

Permalink
Inline fixDirectives and use for-loop
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed Mar 25, 2017
1 parent 5d32ad0 commit 06c3a31
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions babylon-to-espree/toAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,27 @@ var astTransformVisitor = {
exit (path, state) {
var node = path.node;

fixDirectives(path, state);
// fixDirectives
if (path.isFunction() || path.isProgram()) {
var directivesContainer = node;
var body = node.body;
if (node.type !== "Program") {
directivesContainer = body;
body = body.body;
}
if (directivesContainer.directives) {
for (var i = directivesContainer.directives.length - 1; i >= 0; i--) {
var directive = directivesContainer.directives[i];
directive.type = "ExpressionStatement";
directive.expression = directive.value;
delete directive.value;
directive.expression.type = "Literal";
changeToLiteral(directive.expression, state);
body.unshift(directive);
}
delete directivesContainer.directives;
}
}

if (path.isJSXText()) {
node.type = "Literal";
Expand Down Expand Up @@ -212,30 +232,3 @@ var astTransformVisitor = {
}
}
};


function fixDirectives (path, state) {
if (!(path.isProgram() || path.isFunction())) return;

var node = path.node;
var directivesContainer = node;
var body = node.body;

if (node.type !== "Program") {
directivesContainer = body;
body = body.body;
}

if (!directivesContainer.directives) return;

directivesContainer.directives.reverse().forEach((directive) => {
directive.type = "ExpressionStatement";
directive.expression = directive.value;
delete directive.value;
directive.expression.type = "Literal";
changeToLiteral(directive.expression, state);
body.unshift(directive);
});
delete directivesContainer.directives;
}
// fixDirectives

0 comments on commit 06c3a31

Please sign in to comment.