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

Commit

Permalink
Add Prettier (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism committed Jun 17, 2017
1 parent 945f00a commit 37f9242
Show file tree
Hide file tree
Showing 14 changed files with 856 additions and 383 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
@@ -1,9 +1,13 @@
module.exports = {
root: true,
extends: "babel",
"plugins": [
"prettier"
],
rules: {
"no-var": 0,
"max-len": 0
"max-len": 0,
"prettier/prettier": ["error", { "trailingComma": "es5" }],
},
env: {
node: true,
Expand Down
2 changes: 1 addition & 1 deletion babylon-to-espree/attachComments.js
@@ -1,7 +1,7 @@
"use strict";

// comment fixes
module.exports = function (ast, comments, tokens) {
module.exports = function(ast, comments, tokens) {
if (comments.length) {
var firstComment = comments[0];
var lastComment = comments[comments.length - 1];
Expand Down
2 changes: 1 addition & 1 deletion babylon-to-espree/convertComments.js
@@ -1,6 +1,6 @@
"use strict";

module.exports = function (comments) {
module.exports = function(comments) {
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
if (comment.type === "CommentBlock") {
Expand Down
30 changes: 17 additions & 13 deletions babylon-to-espree/convertTemplateType.js
@@ -1,24 +1,25 @@
"use strict";

module.exports = function (tokens, tt) {
var startingToken = 0;
var currentToken = 0;
var numBraces = 0; // track use of {}
var numBackQuotes = 0; // track number of nested templates
module.exports = function(tokens, tt) {
var startingToken = 0;
var currentToken = 0;
var numBraces = 0; // track use of {}
var numBackQuotes = 0; // track number of nested templates

function isBackQuote(token) {
return tokens[token].type === tt.backQuote;
}

function isTemplateStarter(token) {
return isBackQuote(token) ||
// only can be a template starter when in a template already
tokens[token].type === tt.braceR && numBackQuotes > 0;
return (
isBackQuote(token) ||
// only can be a template starter when in a template already
(tokens[token].type === tt.braceR && numBackQuotes > 0)
);
}

function isTemplateEnder(token) {
return isBackQuote(token) ||
tokens[token].type === tt.dollarBraceL;
return isBackQuote(token) || tokens[token].type === tt.dollarBraceL;
}

// append the values between start and end
Expand All @@ -44,8 +45,8 @@ module.exports = function (tokens, tt) {
end: tokens[end].end,
loc: {
start: tokens[start].loc.start,
end: tokens[end].loc.end
}
end: tokens[end].loc.end,
},
};

// put new token in place of old tokens
Expand All @@ -70,7 +71,10 @@ module.exports = function (tokens, tt) {
currentToken = startingToken + 1;

// check if token after template start is "template"
if (currentToken >= tokens.length - 1 || tokens[currentToken].type !== tt.template) {
if (
currentToken >= tokens.length - 1 ||
tokens[currentToken].type !== tt.template
) {
break;
}

Expand Down
8 changes: 4 additions & 4 deletions babylon-to-espree/index.js
@@ -1,11 +1,11 @@
"use strict";

var attachComments = require("./attachComments");
var attachComments = require("./attachComments");
var convertComments = require("./convertComments");
var toTokens = require("./toTokens");
var toAST = require("./toAST");
var toTokens = require("./toTokens");
var toAST = require("./toAST");

module.exports = function (ast, traverse, tt, code) {
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
Expand Down
28 changes: 21 additions & 7 deletions babylon-to-espree/toAST.js
Expand Up @@ -3,12 +3,18 @@
var t = require("babel-types");
var convertComments = require("./convertComments");

module.exports = function (ast, traverse, code) {
module.exports = function(ast, traverse, code) {
var state = { source: code };

// Monkey patch visitor keys in order to be able to traverse the estree nodes
t.VISITOR_KEYS.Property = t.VISITOR_KEYS.ObjectProperty;
t.VISITOR_KEYS.MethodDefinition = ["key", "value", "decorators", "returnType", "typeParameters"];
t.VISITOR_KEYS.MethodDefinition = [
"key",
"value",
"decorators",
"returnType",
"typeParameters",
];

traverse(ast, astTransformVisitor, null, state);

Expand All @@ -18,7 +24,7 @@ module.exports = function (ast, traverse, code) {

var astTransformVisitor = {
noScope: true,
enter (path) {
enter(path) {
var node = path.node;

// private var to track original node type
Expand All @@ -37,7 +43,7 @@ var astTransformVisitor = {
convertComments(node.leadingComments);
}
},
exit (path) {
exit(path) {
var node = path.node;

if (path.isJSXText()) {
Expand All @@ -49,11 +55,19 @@ var astTransformVisitor = {
if (!node.shorthand) node.shorthand = false;
}

if (path.isRestElement() && path.parent && path.parent.type === "ObjectPattern") {
if (
path.isRestElement() &&
path.parent &&
path.parent.type === "ObjectPattern"
) {
node.type = "ExperimentalRestProperty";
}

if (path.isSpreadElement() && path.parent && path.parent.type === "ObjectExpression") {
if (
path.isSpreadElement() &&
path.parent &&
path.parent.type === "ObjectExpression"
) {
node.type = "ExperimentalSpreadProperty";
}

Expand Down Expand Up @@ -105,5 +119,5 @@ var astTransformVisitor = {
}
}
}
}
},
};
58 changes: 38 additions & 20 deletions babylon-to-espree/toToken.js
@@ -1,29 +1,47 @@
"use strict";

module.exports = function (token, tt, source) {
module.exports = function(token, tt, source) {
var type = token.type;
token.range = [token.start, token.end];

if (type === tt.name) {
token.type = "Identifier";
} else if (type === tt.semi || type === tt.comma ||
type === tt.parenL || type === tt.parenR ||
type === tt.braceL || type === tt.braceR ||
type === tt.slash || type === tt.dot ||
type === tt.bracketL || type === tt.bracketR ||
type === tt.ellipsis || type === tt.arrow ||
type === tt.star || type === tt.incDec ||
type === tt.colon || type === tt.question ||
type === tt.template || type === tt.backQuote ||
type === tt.dollarBraceL || type === tt.at ||
type === tt.logicalOR || type === tt.logicalAND ||
type === tt.bitwiseOR || type === tt.bitwiseXOR ||
type === tt.bitwiseAND || type === tt.equality ||
type === tt.relational || type === tt.bitShift ||
type === tt.plusMin || type === tt.modulo ||
type === tt.exponent || type === tt.prefix ||
type === tt.doubleColon ||
type.isAssign) {
} else if (
type === tt.semi ||
type === tt.comma ||
type === tt.parenL ||
type === tt.parenR ||
type === tt.braceL ||
type === tt.braceR ||
type === tt.slash ||
type === tt.dot ||
type === tt.bracketL ||
type === tt.bracketR ||
type === tt.ellipsis ||
type === tt.arrow ||
type === tt.star ||
type === tt.incDec ||
type === tt.colon ||
type === tt.question ||
type === tt.template ||
type === tt.backQuote ||
type === tt.dollarBraceL ||
type === tt.at ||
type === tt.logicalOR ||
type === tt.logicalAND ||
type === tt.bitwiseOR ||
type === tt.bitwiseXOR ||
type === tt.bitwiseAND ||
type === tt.equality ||
type === tt.relational ||
type === tt.bitShift ||
type === tt.plusMin ||
type === tt.modulo ||
type === tt.exponent ||
type === tt.prefix ||
type === tt.doubleColon ||
type.isAssign
) {
token.type = "Punctuator";
if (!token.value) token.value = type.label;
} else if (type === tt.jsxTagStart) {
Expand Down Expand Up @@ -53,7 +71,7 @@ module.exports = function (token, tt, source) {
var value = token.value;
token.regex = {
pattern: value.pattern,
flags: value.flags
flags: value.flags,
};
token.value = `/${value.pattern}/${value.flags}`;
}
Expand Down
2 changes: 1 addition & 1 deletion babylon-to-espree/toTokens.js
Expand Up @@ -3,7 +3,7 @@
var convertTemplateType = require("./convertTemplateType");
var toToken = require("./toToken");

module.exports = function (tokens, tt, code) {
module.exports = function(tokens, tt, code) {
// transform tokens to type "Template"
convertTemplateType(tokens, tt);

Expand Down

0 comments on commit 37f9242

Please sign in to comment.