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

Commit

Permalink
Remove comment attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Jan 10, 2019
1 parent a861223 commit d741cf0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 105 deletions.
59 changes: 0 additions & 59 deletions lib/babylon-to-espree/attachComments.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const t = require("@babel/core").types;
const convertComments = require("./convertComments");
const convertProgramNode = require("./convertProgramNode");

module.exports = function(ast, traverse, code) {
const state = { source: code };
Expand All @@ -20,6 +20,8 @@ module.exports = function(ast, traverse, code) {

delete t.VISITOR_KEYS.Property;
delete t.VISITOR_KEYS.MethodDefinition;

convertProgramNode(ast);
};

const astTransformVisitor = {
Expand All @@ -31,16 +33,15 @@ const astTransformVisitor = {
node._babelType = node.type;

if (node.innerComments) {
node.trailingComments = node.innerComments;
delete node.innerComments;
}

if (node.trailingComments) {
convertComments(node.trailingComments);
delete node.trailingComments;
}

if (node.leadingComments) {
convertComments(node.leadingComments);
delete node.leadingComments;
}
},
exit(path) {
Expand Down
40 changes: 40 additions & 0 deletions lib/babylon-to-espree/convertProgramNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";

module.exports = function(ast) {
ast.type = "Program";
ast.sourceType = ast.program.sourceType;
ast.directives = ast.program.directives;
ast.body = ast.program.body;
delete ast.program;

if (ast.comments.length) {
const lastComment = ast.comments[ast.comments.length - 1];

if (!ast.tokens.length) {
// if no tokens, the program starts at the end of the last comment
ast.start = lastComment.end;
ast.loc.start.line = lastComment.loc.end.line;
ast.loc.start.column = lastComment.loc.end.column;
} else {
const lastToken = ast.tokens[ast.tokens.length - 1];

if (lastComment.end > lastToken.end) {
// If there is a comment after the last token, the program ends at the
// last token and not the comment
ast.range[1] = lastToken.end;
ast.loc.end.line = lastToken.loc.end.line;
ast.loc.end.column = lastToken.loc.end.column;
}
}
} else {
if (!ast.tokens.length) {
ast.loc.start.line = 1;
ast.loc.end.line = 1;
}
}

if (ast.body && ast.body.length > 0) {
ast.loc.start.line = ast.body[0].loc.start.line;
ast.range[0] = ast.body[0].start;
}
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";

const convertTemplateType = require("./convertTemplateType");
const toToken = require("./toToken");
const convertToken = require("./convertToken");

module.exports = function(tokens, tt, code) {
return convertTemplateType(tokens, tt)
.filter(t => t.type !== "CommentLine" && t.type !== "CommentBlock")
.map(t => toToken(t, tt, code));
.map(t => convertToken(t, tt, code));
};
27 changes: 4 additions & 23 deletions lib/babylon-to-espree/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
"use strict";

const attachComments = require("./attachComments");
const convertTokens = require("./convertTokens");
const convertComments = require("./convertComments");
const toTokens = require("./toTokens");
const toAST = require("./toAST");
const convertAST = require("./convertAST");

module.exports = function(ast, traverse, tt, code) {
// convert tokens
ast.tokens = toTokens(ast.tokens, tt, code);

// add comments
ast.tokens = convertTokens(ast.tokens, tt, code);
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;

attachComments(ast, ast.comments, ast.tokens);
convertAST(ast, traverse, code);
};
36 changes: 19 additions & 17 deletions test/specs/babel-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ function lookup(obj, keypath, backwardsDepth) {
return obj;
}

return keypath
.split(".")
.slice(0, -1 * backwardsDepth)
.reduce((base, segment) => {
return base && base[segment], obj;
});
let pathSegments = keypath.split(".");

if (pathSegments.length > 1) {
pathSegments = pathSegments.slice(0, -1 * backwardsDepth);
}

return pathSegments.reduce((base, segment) => {
return base && base[segment], obj;
});
}

function parseAndAssertSame(code) {
Expand All @@ -38,7 +41,6 @@ function parseAndAssertSame(code) {
loc: true,
range: true,
comment: true,
attachComment: true,
ecmaVersion: 2018,
sourceType: "module",
});
Expand All @@ -50,18 +52,18 @@ function parseAndAssertSame(code) {
assertImplementsAST(esAST, babylonAST);
} catch (err) {
const traversal = err.message.slice(3, err.message.indexOf(":"));
err.message += unpad(`
err.message += `\n${unpad(`
espree:
${util.inspect(lookup(esAST, traversal, 2), {
depth: err.depth,
colors: true,
})}
${util.inspect(lookup(esAST, traversal, 2), {
depth: err.depth,
colors: true,
})}
babel-eslint:
${util.inspect(lookup(babylonAST, traversal, 2), {
depth: err.depth,
colors: true,
})}
`);
${util.inspect(lookup(babylonAST, traversal, 2), {
depth: err.depth,
colors: true,
})}
`)}`;
throw err;
}
//assert.equal(esAST, babylonAST);
Expand Down

0 comments on commit d741cf0

Please sign in to comment.