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

Commit 37f9242

Browse files
Add Prettier (#491)
1 parent 945f00a commit 37f9242

14 files changed

+856
-383
lines changed

.eslintrc.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
module.exports = {
22
root: true,
33
extends: "babel",
4+
"plugins": [
5+
"prettier"
6+
],
47
rules: {
58
"no-var": 0,
6-
"max-len": 0
9+
"max-len": 0,
10+
"prettier/prettier": ["error", { "trailingComma": "es5" }],
711
},
812
env: {
913
node: true,

babylon-to-espree/attachComments.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
// comment fixes
4-
module.exports = function (ast, comments, tokens) {
4+
module.exports = function(ast, comments, tokens) {
55
if (comments.length) {
66
var firstComment = comments[0];
77
var lastComment = comments[comments.length - 1];

babylon-to-espree/convertComments.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
module.exports = function (comments) {
3+
module.exports = function(comments) {
44
for (var i = 0; i < comments.length; i++) {
55
var comment = comments[i];
66
if (comment.type === "CommentBlock") {

babylon-to-espree/convertTemplateType.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
"use strict";
22

3-
module.exports = function (tokens, tt) {
4-
var startingToken = 0;
5-
var currentToken = 0;
6-
var numBraces = 0; // track use of {}
7-
var numBackQuotes = 0; // track number of nested templates
3+
module.exports = function(tokens, tt) {
4+
var startingToken = 0;
5+
var currentToken = 0;
6+
var numBraces = 0; // track use of {}
7+
var numBackQuotes = 0; // track number of nested templates
88

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

1313
function isTemplateStarter(token) {
14-
return isBackQuote(token) ||
15-
// only can be a template starter when in a template already
16-
tokens[token].type === tt.braceR && numBackQuotes > 0;
14+
return (
15+
isBackQuote(token) ||
16+
// only can be a template starter when in a template already
17+
(tokens[token].type === tt.braceR && numBackQuotes > 0)
18+
);
1719
}
1820

1921
function isTemplateEnder(token) {
20-
return isBackQuote(token) ||
21-
tokens[token].type === tt.dollarBraceL;
22+
return isBackQuote(token) || tokens[token].type === tt.dollarBraceL;
2223
}
2324

2425
// append the values between start and end
@@ -44,8 +45,8 @@ module.exports = function (tokens, tt) {
4445
end: tokens[end].end,
4546
loc: {
4647
start: tokens[start].loc.start,
47-
end: tokens[end].loc.end
48-
}
48+
end: tokens[end].loc.end,
49+
},
4950
};
5051

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

7273
// check if token after template start is "template"
73-
if (currentToken >= tokens.length - 1 || tokens[currentToken].type !== tt.template) {
74+
if (
75+
currentToken >= tokens.length - 1 ||
76+
tokens[currentToken].type !== tt.template
77+
) {
7478
break;
7579
}
7680

babylon-to-espree/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22

3-
var attachComments = require("./attachComments");
3+
var attachComments = require("./attachComments");
44
var convertComments = require("./convertComments");
5-
var toTokens = require("./toTokens");
6-
var toAST = require("./toAST");
5+
var toTokens = require("./toTokens");
6+
var toAST = require("./toAST");
77

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

babylon-to-espree/toAST.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
var t = require("babel-types");
44
var convertComments = require("./convertComments");
55

6-
module.exports = function (ast, traverse, code) {
6+
module.exports = function(ast, traverse, code) {
77
var state = { source: code };
88

99
// Monkey patch visitor keys in order to be able to traverse the estree nodes
1010
t.VISITOR_KEYS.Property = t.VISITOR_KEYS.ObjectProperty;
11-
t.VISITOR_KEYS.MethodDefinition = ["key", "value", "decorators", "returnType", "typeParameters"];
11+
t.VISITOR_KEYS.MethodDefinition = [
12+
"key",
13+
"value",
14+
"decorators",
15+
"returnType",
16+
"typeParameters",
17+
];
1218

1319
traverse(ast, astTransformVisitor, null, state);
1420

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

1925
var astTransformVisitor = {
2026
noScope: true,
21-
enter (path) {
27+
enter(path) {
2228
var node = path.node;
2329

2430
// private var to track original node type
@@ -37,7 +43,7 @@ var astTransformVisitor = {
3743
convertComments(node.leadingComments);
3844
}
3945
},
40-
exit (path) {
46+
exit(path) {
4147
var node = path.node;
4248

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

52-
if (path.isRestElement() && path.parent && path.parent.type === "ObjectPattern") {
58+
if (
59+
path.isRestElement() &&
60+
path.parent &&
61+
path.parent.type === "ObjectPattern"
62+
) {
5363
node.type = "ExperimentalRestProperty";
5464
}
5565

56-
if (path.isSpreadElement() && path.parent && path.parent.type === "ObjectExpression") {
66+
if (
67+
path.isSpreadElement() &&
68+
path.parent &&
69+
path.parent.type === "ObjectExpression"
70+
) {
5771
node.type = "ExperimentalSpreadProperty";
5872
}
5973

@@ -105,5 +119,5 @@ var astTransformVisitor = {
105119
}
106120
}
107121
}
108-
}
122+
},
109123
};

babylon-to-espree/toToken.js

+38-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
"use strict";
22

3-
module.exports = function (token, tt, source) {
3+
module.exports = function(token, tt, source) {
44
var type = token.type;
55
token.range = [token.start, token.end];
66

77
if (type === tt.name) {
88
token.type = "Identifier";
9-
} else if (type === tt.semi || type === tt.comma ||
10-
type === tt.parenL || type === tt.parenR ||
11-
type === tt.braceL || type === tt.braceR ||
12-
type === tt.slash || type === tt.dot ||
13-
type === tt.bracketL || type === tt.bracketR ||
14-
type === tt.ellipsis || type === tt.arrow ||
15-
type === tt.star || type === tt.incDec ||
16-
type === tt.colon || type === tt.question ||
17-
type === tt.template || type === tt.backQuote ||
18-
type === tt.dollarBraceL || type === tt.at ||
19-
type === tt.logicalOR || type === tt.logicalAND ||
20-
type === tt.bitwiseOR || type === tt.bitwiseXOR ||
21-
type === tt.bitwiseAND || type === tt.equality ||
22-
type === tt.relational || type === tt.bitShift ||
23-
type === tt.plusMin || type === tt.modulo ||
24-
type === tt.exponent || type === tt.prefix ||
25-
type === tt.doubleColon ||
26-
type.isAssign) {
9+
} else if (
10+
type === tt.semi ||
11+
type === tt.comma ||
12+
type === tt.parenL ||
13+
type === tt.parenR ||
14+
type === tt.braceL ||
15+
type === tt.braceR ||
16+
type === tt.slash ||
17+
type === tt.dot ||
18+
type === tt.bracketL ||
19+
type === tt.bracketR ||
20+
type === tt.ellipsis ||
21+
type === tt.arrow ||
22+
type === tt.star ||
23+
type === tt.incDec ||
24+
type === tt.colon ||
25+
type === tt.question ||
26+
type === tt.template ||
27+
type === tt.backQuote ||
28+
type === tt.dollarBraceL ||
29+
type === tt.at ||
30+
type === tt.logicalOR ||
31+
type === tt.logicalAND ||
32+
type === tt.bitwiseOR ||
33+
type === tt.bitwiseXOR ||
34+
type === tt.bitwiseAND ||
35+
type === tt.equality ||
36+
type === tt.relational ||
37+
type === tt.bitShift ||
38+
type === tt.plusMin ||
39+
type === tt.modulo ||
40+
type === tt.exponent ||
41+
type === tt.prefix ||
42+
type === tt.doubleColon ||
43+
type.isAssign
44+
) {
2745
token.type = "Punctuator";
2846
if (!token.value) token.value = type.label;
2947
} else if (type === tt.jsxTagStart) {
@@ -53,7 +71,7 @@ module.exports = function (token, tt, source) {
5371
var value = token.value;
5472
token.regex = {
5573
pattern: value.pattern,
56-
flags: value.flags
74+
flags: value.flags,
5775
};
5876
token.value = `/${value.pattern}/${value.flags}`;
5977
}

babylon-to-espree/toTokens.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var convertTemplateType = require("./convertTemplateType");
44
var toToken = require("./toToken");
55

6-
module.exports = function (tokens, tt, code) {
6+
module.exports = function(tokens, tt, code) {
77
// transform tokens to type "Template"
88
convertTemplateType(tokens, tt);
99

0 commit comments

Comments
 (0)