Skip to content

Commit

Permalink
Merge remote-tracking branch 'babel-eslint/7.x' into prerelease/3.0.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	babylon-to-espree/toAST.js
#	index.js
#	package.json
#	test/non-regression.js
#	yarn.lock
  • Loading branch information
wcjohnson committed Oct 12, 2017
2 parents 5721d25 + 54ed517 commit 1c30620
Show file tree
Hide file tree
Showing 19 changed files with 561 additions and 540 deletions.
4 changes: 0 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
module.exports = {
root: true,
extends: "babel",
parserOptions: {
ecmaVersion: 7,
sourceType: "module"
},
rules: {
"no-var": 0,
"max-len": 0
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

2 changes: 2 additions & 0 deletions babylon-to-espree/attachComments.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

// comment fixes
module.exports = function (ast, comments, tokens) {
if (comments.length) {
Expand Down
17 changes: 17 additions & 0 deletions babylon-to-espree/convertComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

module.exports = function (comments) {
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
if (comment.type === "CommentBlock") {
comment.type = "Block";
} else if (comment.type === "CommentLine") {
comment.type = "Line";
}
// sometimes comments don't get ranges computed,
// even with options.ranges === true
if (!comment.range) {
comment.range = [comment.start, comment.end];
}
}
};
2 changes: 2 additions & 0 deletions babylon-to-espree/convertTemplateType.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

module.exports = function (tokens, tt) {
var startingToken = 0;
var currentToken = 0;
Expand Down
54 changes: 35 additions & 19 deletions babylon-to-espree/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
exports.attachComments = require("./attachComments");

exports.toTokens = require("./toTokens");
exports.toAST = require("./toAST");

exports.convertComments = function (comments) {
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
if (comment.type === "CommentBlock") {
comment.type = "Block";
} else if (comment.type === "CommentLine") {
comment.type = "Line";
}
// sometimes comments don't get ranges computed,
// even with options.ranges === true
if (!comment.range) {
comment.range = [comment.start, comment.end];
}
}
"use strict";

var attachComments = require("./attachComments");
var convertComments = require("./convertComments");
var toTokens = require("./toTokens");
var 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();

// 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);
};
98 changes: 41 additions & 57 deletions babylon-to-espree/toAST.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
var source;
"use strict";

var convertComments = require("./convertComments");
var cloneDeep = require("lodash/cloneDeep");

module.exports = function (ast, traverse, code) {
source = code;
var state = { source: code };
ast.range = [ast.start, ast.end];
traverse(ast, astTransformVisitor);
traverse(ast, astTransformVisitor, null, state);
};

function changeToLiteral(node) {
function changeToLiteral(node, state) {
node.type = "Literal";
if (!node.raw) {
if (node.extra && node.extra.raw) {
node.raw = node.extra.raw;
} else {
node.raw = source.slice(node.start, node.end);
}
}
}

function changeComments(nodeComments) {
for (var i = 0; i < nodeComments.length; i++) {
var comment = nodeComments[i];
if (comment.type === "CommentLine") {
comment.type = "Line";
} else if (comment.type === "CommentBlock") {
comment.type = "Block";
node.raw = state.source.slice(node.start, node.end);
}
comment.range = [comment.start, comment.end];
}
}

Expand Down Expand Up @@ -76,24 +66,40 @@ var astTransformVisitor = {
}

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

if (node.leadingComments) {
changeComments(node.leadingComments);
convertComments(node.leadingComments);
}

// make '_paths' non-enumerable (babel-eslint #200)
Object.defineProperty(node, "_paths", { value: node._paths, writable: true });
},
exit (path) {
exit (path, state) {
var node = path.node;

[
fixDirectives,
].forEach((fixer) => {
fixer(path);
});
// 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 All @@ -102,7 +108,7 @@ var astTransformVisitor = {

if (path.isNumericLiteral() ||
path.isStringLiteral()) {
changeToLiteral(node);
changeToLiteral(node, state);
}

if (path.isBooleanLiteral()) {
Expand All @@ -119,7 +125,11 @@ var astTransformVisitor = {
if (path.isRegExpLiteral()) {
node.type = "Literal";
node.raw = node.extra.raw;
node.value = {};
try {
node.value = new RegExp(node.pattern, node.flags);
} catch (err) {
node.value = null;
}
node.regex = {
pattern: node.pattern,
flags: node.flags
Expand All @@ -135,7 +145,7 @@ var astTransformVisitor = {
}

if (path.isClassMethod() || path.isObjectMethod()) {
var code = source.slice(node.key.end, node.body.start);
var code = state.source.slice(node.key.end, node.body.start);
var offset = code.indexOf("(");

node.value = {
Expand Down Expand Up @@ -242,7 +252,8 @@ var astTransformVisitor = {

// template string range fixes
if (path.isTemplateLiteral()) {
node.quasis.forEach((q) => {
for (var j = 0; j < node.quasis.length; j++) {
var q = node.quasis[j];
q.range[0] -= 1;
if (q.tail) {
q.range[1] += 1;
Expand All @@ -255,34 +266,7 @@ var astTransformVisitor = {
} else {
q.loc.end.column += 2;
}
});
}
}
}
};


function fixDirectives (path) {
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);
body.unshift(directive);
});
delete directivesContainer.directives;
}
// fixDirectives
2 changes: 2 additions & 0 deletions babylon-to-espree/toToken.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

module.exports = function (token, tt, source) {
var type = token.type;
token.range = [token.start, token.end];
Expand Down
13 changes: 8 additions & 5 deletions babylon-to-espree/toTokens.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"use strict";

var convertTemplateType = require("./convertTemplateType");
var toToken = require("./toToken");

module.exports = function (tokens, tt, code) {
// transform tokens to type "Template"
convertTemplateType(tokens, tt);
var transformedTokens = tokens.filter((token) => {
return token.type !== "CommentLine" && token.type !== "CommentBlock";
});

for (var i = 0, l = transformedTokens.length; i < l; i++) {
transformedTokens[i] = toToken(transformedTokens[i], tt, code);
var transformedTokens = [];
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type !== "CommentLine" && token.type !== "CommentBlock") {
transformedTokens.push(toToken(token, tt, code));
}
}

return transformedTokens;
Expand Down
1 change: 0 additions & 1 deletion eslint
Submodule eslint deleted from fdce86
6 changes: 0 additions & 6 deletions eslint-tester.js

This file was deleted.

Loading

0 comments on commit 1c30620

Please sign in to comment.