Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

Commit 06c3a31

Browse files
committed
Inline fixDirectives and use for-loop
1 parent 5d32ad0 commit 06c3a31

1 file changed

Lines changed: 21 additions & 28 deletions

File tree

babylon-to-espree/toAST.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,27 @@ var astTransformVisitor = {
4646
exit (path, state) {
4747
var node = path.node;
4848

49-
fixDirectives(path, state);
49+
// fixDirectives
50+
if (path.isFunction() || path.isProgram()) {
51+
var directivesContainer = node;
52+
var body = node.body;
53+
if (node.type !== "Program") {
54+
directivesContainer = body;
55+
body = body.body;
56+
}
57+
if (directivesContainer.directives) {
58+
for (var i = directivesContainer.directives.length - 1; i >= 0; i--) {
59+
var directive = directivesContainer.directives[i];
60+
directive.type = "ExpressionStatement";
61+
directive.expression = directive.value;
62+
delete directive.value;
63+
directive.expression.type = "Literal";
64+
changeToLiteral(directive.expression, state);
65+
body.unshift(directive);
66+
}
67+
delete directivesContainer.directives;
68+
}
69+
}
5070

5171
if (path.isJSXText()) {
5272
node.type = "Literal";
@@ -212,30 +232,3 @@ var astTransformVisitor = {
212232
}
213233
}
214234
};
215-
216-
217-
function fixDirectives (path, state) {
218-
if (!(path.isProgram() || path.isFunction())) return;
219-
220-
var node = path.node;
221-
var directivesContainer = node;
222-
var body = node.body;
223-
224-
if (node.type !== "Program") {
225-
directivesContainer = body;
226-
body = body.body;
227-
}
228-
229-
if (!directivesContainer.directives) return;
230-
231-
directivesContainer.directives.reverse().forEach((directive) => {
232-
directive.type = "ExpressionStatement";
233-
directive.expression = directive.value;
234-
delete directive.value;
235-
directive.expression.type = "Literal";
236-
changeToLiteral(directive.expression, state);
237-
body.unshift(directive);
238-
});
239-
delete directivesContainer.directives;
240-
}
241-
// fixDirectives

0 commit comments

Comments
 (0)