Skip to content

Commit 6c5beec

Browse files
committedMar 25, 2017
Move ast convert steps to babylon-to-espree
1 parent d2ce789 commit 6c5beec

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed
 

‎babylon-to-espree/index.js

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1-
exports.attachComments = require("./attachComments");
1+
var attachComments = require("./attachComments");
2+
var convertComments = require("./convertComments");
3+
var toTokens = require("./toTokens");
4+
var toAST = require("./toAST");
25

3-
exports.toTokens = require("./toTokens");
4-
exports.toAST = require("./toAST");
6+
module.exports = function (ast, traverse, tt, code) {
7+
// remove EOF token, eslint doesn't use this for anything and it interferes
8+
// with some rules see https://github.com/babel/babel-eslint/issues/2
9+
// todo: find a more elegant way to do this
10+
ast.tokens.pop();
511

6-
exports.convertComments = require("./convertComments");
12+
// convert tokens
13+
ast.tokens = toTokens(ast.tokens, tt, code);
14+
15+
// add comments
16+
convertComments(ast.comments);
17+
18+
// transform esprima and acorn divergent nodes
19+
toAST(ast, traverse, code);
20+
21+
// ast.program.tokens = ast.tokens;
22+
// ast.program.comments = ast.comments;
23+
// ast = ast.program;
24+
25+
// remove File
26+
ast.type = "Program";
27+
ast.sourceType = ast.program.sourceType;
28+
ast.directives = ast.program.directives;
29+
ast.body = ast.program.body;
30+
delete ast.program;
31+
delete ast._paths;
32+
33+
attachComments(ast, ast.comments, ast.tokens);
34+
};

‎index.js

+1-27
Original file line numberDiff line numberDiff line change
@@ -428,33 +428,7 @@ exports.parseNoPatch = function (code, options) {
428428
throw err;
429429
}
430430

431-
// remove EOF token, eslint doesn't use this for anything and it interferes with some rules
432-
// see https://github.com/babel/babel-eslint/issues/2 for more info
433-
// todo: find a more elegant way to do this
434-
ast.tokens.pop();
435-
436-
// convert tokens
437-
ast.tokens = babylonToEspree.toTokens(ast.tokens, tt, code);
438-
439-
// add comments
440-
babylonToEspree.convertComments(ast.comments);
441-
442-
// transform esprima and acorn divergent nodes
443-
babylonToEspree.toAST(ast, traverse, code);
444-
445-
// ast.program.tokens = ast.tokens;
446-
// ast.program.comments = ast.comments;
447-
// ast = ast.program;
448-
449-
// remove File
450-
ast.type = "Program";
451-
ast.sourceType = ast.program.sourceType;
452-
ast.directives = ast.program.directives;
453-
ast.body = ast.program.body;
454-
delete ast.program;
455-
delete ast._paths;
456-
457-
babylonToEspree.attachComments(ast, ast.comments, ast.tokens);
431+
babylonToEspree(ast, traverse, tt, code);
458432

459433
return ast;
460434
};

0 commit comments

Comments
 (0)
Please sign in to comment.