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

Commit

Permalink
Move ast convert steps to babylon-to-espree
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed Mar 25, 2017
1 parent d2ce789 commit 6c5beec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
36 changes: 32 additions & 4 deletions babylon-to-espree/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
exports.attachComments = require("./attachComments");
var attachComments = require("./attachComments");
var convertComments = require("./convertComments");
var toTokens = require("./toTokens");
var toAST = require("./toAST");

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

exports.convertComments = require("./convertComments");
// convert tokens
ast.tokens = toTokens(ast.tokens, tt, code);

// add comments
convertComments(ast.comments);

// transform esprima and acorn divergent nodes
toAST(ast, traverse, code);

// ast.program.tokens = ast.tokens;
// ast.program.comments = ast.comments;
// ast = ast.program;

// remove File
ast.type = "Program";
ast.sourceType = ast.program.sourceType;
ast.directives = ast.program.directives;
ast.body = ast.program.body;
delete ast.program;
delete ast._paths;

attachComments(ast, ast.comments, ast.tokens);
};
28 changes: 1 addition & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,33 +428,7 @@ exports.parseNoPatch = function (code, options) {
throw err;
}

// remove EOF token, eslint doesn't use this for anything and it interferes with some rules
// see https://github.com/babel/babel-eslint/issues/2 for more info
// todo: find a more elegant way to do this
ast.tokens.pop();

// convert tokens
ast.tokens = babylonToEspree.toTokens(ast.tokens, tt, code);

// add comments
babylonToEspree.convertComments(ast.comments);

// transform esprima and acorn divergent nodes
babylonToEspree.toAST(ast, traverse, code);

// ast.program.tokens = ast.tokens;
// ast.program.comments = ast.comments;
// ast = ast.program;

// remove File
ast.type = "Program";
ast.sourceType = ast.program.sourceType;
ast.directives = ast.program.directives;
ast.body = ast.program.body;
delete ast.program;
delete ast._paths;

babylonToEspree.attachComments(ast, ast.comments, ast.tokens);
babylonToEspree(ast, traverse, tt, code);

return ast;
};

0 comments on commit 6c5beec

Please sign in to comment.