prettier 0.12.2 → 0.12.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +49 -1
- data/CONTRIBUTING.md +1 -1
- data/README.md +34 -18
- data/node_modules/prettier/bin-prettier.js +22 -13
- data/node_modules/prettier/index.js +22 -13
- data/package.json +2 -2
- data/src/nodes.js +10 -576
- data/src/nodes/alias.js +1 -1
- data/src/nodes/args.js +88 -0
- data/src/nodes/arrays.js +1 -1
- data/src/nodes/assign.js +39 -0
- data/src/nodes/blocks.js +18 -3
- data/src/nodes/calls.js +37 -3
- data/src/nodes/case.js +1 -1
- data/src/nodes/commands.js +2 -1
- data/src/nodes/conditionals.js +118 -57
- data/src/nodes/constants.js +25 -0
- data/src/nodes/flow.js +64 -0
- data/src/nodes/hashes.js +3 -2
- data/src/nodes/hooks.js +1 -1
- data/src/nodes/ints.js +24 -0
- data/src/nodes/lambdas.js +1 -1
- data/src/nodes/loops.js +1 -1
- data/src/nodes/massign.js +70 -0
- data/src/nodes/methods.js +40 -2
- data/src/nodes/operators.js +44 -0
- data/src/nodes/params.js +14 -3
- data/src/nodes/regexp.js +1 -1
- data/src/nodes/rescue.js +1 -1
- data/src/nodes/scopes.js +61 -0
- data/src/nodes/statements.js +105 -0
- data/src/nodes/strings.js +9 -2
- data/src/{builders.js → prettier.js} +9 -2
- data/src/ripper.rb +412 -353
- data/src/utils.js +1 -1
- metadata +40 -3
@@ -0,0 +1,25 @@
|
|
1
|
+
const { concat, group, indent, join, softline } = require("../prettier");
|
2
|
+
const { first, makeCall, prefix } = require("../utils");
|
3
|
+
|
4
|
+
module.exports = {
|
5
|
+
const_path_field: (path, opts, print) => join("::", path.map(print, "body")),
|
6
|
+
const_path_ref: (path, opts, print) => join("::", path.map(print, "body")),
|
7
|
+
const_ref: first,
|
8
|
+
defined: (path, opts, print) =>
|
9
|
+
group(
|
10
|
+
concat([
|
11
|
+
"defined?(",
|
12
|
+
indent(concat([softline, path.call(print, "body", 0)])),
|
13
|
+
concat([softline, ")"])
|
14
|
+
])
|
15
|
+
),
|
16
|
+
field: (path, opts, print) =>
|
17
|
+
group(
|
18
|
+
concat([
|
19
|
+
path.call(print, "body", 0),
|
20
|
+
concat([makeCall(path, opts, print), path.call(print, "body", 2)])
|
21
|
+
])
|
22
|
+
),
|
23
|
+
top_const_field: prefix("::"),
|
24
|
+
top_const_ref: prefix("::")
|
25
|
+
};
|
data/src/nodes/flow.js
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
const { concat, join } = require("../prettier");
|
2
|
+
const { literal } = require("../utils");
|
3
|
+
|
4
|
+
module.exports = {
|
5
|
+
break: (path, opts, print) => {
|
6
|
+
const content = path.getValue().body[0];
|
7
|
+
|
8
|
+
if (content.body.length === 0) {
|
9
|
+
return "break";
|
10
|
+
}
|
11
|
+
|
12
|
+
if (content.body[0].body[0].type === "paren") {
|
13
|
+
return concat([
|
14
|
+
"break ",
|
15
|
+
path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)
|
16
|
+
]);
|
17
|
+
}
|
18
|
+
|
19
|
+
return concat(["break ", join(", ", path.call(print, "body", 0))]);
|
20
|
+
},
|
21
|
+
next: (path, opts, print) => {
|
22
|
+
const args = path.getValue().body[0].body[0];
|
23
|
+
|
24
|
+
if (!args) {
|
25
|
+
return "next";
|
26
|
+
}
|
27
|
+
|
28
|
+
if (args.body[0].type === "paren") {
|
29
|
+
// Ignoring the parens node and just going straight to the content
|
30
|
+
return concat([
|
31
|
+
"next ",
|
32
|
+
path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)
|
33
|
+
]);
|
34
|
+
}
|
35
|
+
|
36
|
+
return concat(["next ", join(", ", path.call(print, "body", 0))]);
|
37
|
+
},
|
38
|
+
return: (path, opts, print) => {
|
39
|
+
const args = path.getValue().body[0].body[0];
|
40
|
+
|
41
|
+
if (!args) {
|
42
|
+
return "return";
|
43
|
+
}
|
44
|
+
|
45
|
+
if (args.body[0] && args.body[0].type === "paren") {
|
46
|
+
// Ignoring the parens node and just going straight to the content
|
47
|
+
return concat([
|
48
|
+
"return ",
|
49
|
+
path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)
|
50
|
+
]);
|
51
|
+
}
|
52
|
+
|
53
|
+
return concat(["return ", join(", ", path.call(print, "body", 0))]);
|
54
|
+
},
|
55
|
+
return0: literal("return"),
|
56
|
+
yield: (path, opts, print) => {
|
57
|
+
if (path.getValue().body[0].type === "paren") {
|
58
|
+
return concat(["yield", path.call(print, "body", 0)]);
|
59
|
+
}
|
60
|
+
|
61
|
+
return concat(["yield ", join(", ", path.call(print, "body", 0))]);
|
62
|
+
},
|
63
|
+
yield0: literal("yield")
|
64
|
+
};
|
data/src/nodes/hashes.js
CHANGED
@@ -6,8 +6,8 @@ const {
|
|
6
6
|
join,
|
7
7
|
line,
|
8
8
|
literalline
|
9
|
-
} = require("../
|
10
|
-
const { skipAssignIndent } = require("../utils");
|
9
|
+
} = require("../prettier");
|
10
|
+
const { prefix, skipAssignIndent } = require("../utils");
|
11
11
|
|
12
12
|
const nodeDive = (node, steps) => {
|
13
13
|
let current = node;
|
@@ -67,6 +67,7 @@ module.exports = {
|
|
67
67
|
|
68
68
|
return group(concat(parts));
|
69
69
|
},
|
70
|
+
assoc_splat: prefix("**"),
|
70
71
|
assoclist_from_args: (path, opts, print) => {
|
71
72
|
const { addTrailingCommas } = opts;
|
72
73
|
|
data/src/nodes/hooks.js
CHANGED
data/src/nodes/ints.js
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module.exports = {
|
2
|
+
"@int": (path, _opts, _print) => {
|
3
|
+
const { body } = path.getValue();
|
4
|
+
|
5
|
+
// If the number is octal and does not contain the optional "o" character
|
6
|
+
// after the leading 0, add it in.
|
7
|
+
if (/^0[0-9]/.test(body)) {
|
8
|
+
return `0o${body.slice(1)}`;
|
9
|
+
}
|
10
|
+
|
11
|
+
// If the number is a base 10 number, is sufficiently large, and is not
|
12
|
+
// already formatted with underscores, then add them in in between the
|
13
|
+
// numbers every three characters starting from the right.
|
14
|
+
if (!body.startsWith("0") && body.length >= 4 && !body.includes("_")) {
|
15
|
+
return ` ${body}`
|
16
|
+
.slice((body.length + 2) % 3)
|
17
|
+
.match(/.{3}/g)
|
18
|
+
.join("_")
|
19
|
+
.trim();
|
20
|
+
}
|
21
|
+
|
22
|
+
return body;
|
23
|
+
}
|
24
|
+
};
|
data/src/nodes/lambdas.js
CHANGED
data/src/nodes/loops.js
CHANGED
@@ -0,0 +1,70 @@
|
|
1
|
+
const { concat, group, indent, join, line, softline } = require("../prettier");
|
2
|
+
const { makeList } = require("../utils");
|
3
|
+
|
4
|
+
module.exports = {
|
5
|
+
massign: (path, opts, print) => {
|
6
|
+
let right = path.call(print, "body", 1);
|
7
|
+
|
8
|
+
if (
|
9
|
+
["mrhs_add_star", "mrhs_new_from_args"].includes(
|
10
|
+
path.getValue().body[1].type
|
11
|
+
)
|
12
|
+
) {
|
13
|
+
right = group(join(concat([",", line]), right));
|
14
|
+
}
|
15
|
+
|
16
|
+
return group(
|
17
|
+
concat([
|
18
|
+
group(join(concat([",", line]), path.call(print, "body", 0))),
|
19
|
+
" =",
|
20
|
+
indent(concat([line, right]))
|
21
|
+
])
|
22
|
+
);
|
23
|
+
},
|
24
|
+
mlhs: makeList,
|
25
|
+
mlhs_add_post: (path, opts, print) =>
|
26
|
+
path.call(print, "body", 0).concat(path.call(print, "body", 1)),
|
27
|
+
mlhs_add_star: (path, opts, print) =>
|
28
|
+
path
|
29
|
+
.call(print, "body", 0)
|
30
|
+
.concat([
|
31
|
+
path.getValue().body[1]
|
32
|
+
? concat(["*", path.call(print, "body", 1)])
|
33
|
+
: "*"
|
34
|
+
]),
|
35
|
+
mlhs_paren: (path, opts, print) => {
|
36
|
+
if (["massign", "mlhs_paren"].includes(path.getParentNode().type)) {
|
37
|
+
// If we're nested in brackets as part of the left hand side of an
|
38
|
+
// assignment, i.e., (a, b, c) = 1, 2, 3
|
39
|
+
// ignore the current node and just go straight to the content
|
40
|
+
return path.call(print, "body", 0);
|
41
|
+
}
|
42
|
+
|
43
|
+
return group(
|
44
|
+
concat([
|
45
|
+
"(",
|
46
|
+
indent(
|
47
|
+
concat([
|
48
|
+
softline,
|
49
|
+
join(concat([",", line]), path.call(print, "body", 0))
|
50
|
+
])
|
51
|
+
),
|
52
|
+
concat([softline, ")"])
|
53
|
+
])
|
54
|
+
);
|
55
|
+
},
|
56
|
+
mrhs: makeList,
|
57
|
+
mrhs_add_star: (path, opts, print) =>
|
58
|
+
path
|
59
|
+
.call(print, "body", 0)
|
60
|
+
.concat([concat(["*", path.call(print, "body", 1)])]),
|
61
|
+
mrhs_new_from_args: (path, opts, print) => {
|
62
|
+
const parts = path.call(print, "body", 0);
|
63
|
+
|
64
|
+
if (path.getValue().body.length > 1) {
|
65
|
+
parts.push(path.call(print, "body", 1));
|
66
|
+
}
|
67
|
+
|
68
|
+
return parts;
|
69
|
+
}
|
70
|
+
};
|
data/src/nodes/methods.js
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
-
const {
|
1
|
+
const {
|
2
|
+
align,
|
3
|
+
concat,
|
4
|
+
group,
|
5
|
+
hardline,
|
6
|
+
indent,
|
7
|
+
join,
|
8
|
+
line
|
9
|
+
} = require("../prettier");
|
10
|
+
const { first, literal } = require("../utils");
|
2
11
|
|
3
12
|
const printMethod = offset => (path, opts, print) => {
|
4
13
|
const [_name, params, body] = path.getValue().body.slice(offset);
|
@@ -37,6 +46,35 @@ const printMethod = offset => (path, opts, print) => {
|
|
37
46
|
};
|
38
47
|
|
39
48
|
module.exports = {
|
49
|
+
access_ctrl: first,
|
40
50
|
def: printMethod(0),
|
41
|
-
defs: printMethod(2)
|
51
|
+
defs: printMethod(2),
|
52
|
+
methref: (path, opts, print) => join(".:", path.map(print, "body")),
|
53
|
+
super: (path, opts, print) => {
|
54
|
+
const args = path.getValue().body[0];
|
55
|
+
|
56
|
+
if (args.type === "arg_paren") {
|
57
|
+
// In case there are explicitly no arguments but they are using parens,
|
58
|
+
// we assume they are attempting to override the initializer and pass no
|
59
|
+
// arguments up.
|
60
|
+
if (args.body[0] === null) {
|
61
|
+
return "super()";
|
62
|
+
}
|
63
|
+
|
64
|
+
return concat(["super", path.call(print, "body", 0)]);
|
65
|
+
}
|
66
|
+
|
67
|
+
return concat(["super ", join(", ", path.call(print, "body", 0))]);
|
68
|
+
},
|
69
|
+
undef: (path, opts, print) =>
|
70
|
+
group(
|
71
|
+
concat([
|
72
|
+
"undef ",
|
73
|
+
align(
|
74
|
+
"undef ".length,
|
75
|
+
join(concat([",", line]), path.map(print, "body", 0))
|
76
|
+
)
|
77
|
+
])
|
78
|
+
),
|
79
|
+
zsuper: literal("super")
|
42
80
|
};
|
@@ -0,0 +1,44 @@
|
|
1
|
+
const { concat, group, indent, line, softline } = require("../prettier");
|
2
|
+
|
3
|
+
module.exports = {
|
4
|
+
binary: (path, opts, print) => {
|
5
|
+
const operator = path.getValue().body[1];
|
6
|
+
const useNoSpace = operator === "**";
|
7
|
+
|
8
|
+
return group(
|
9
|
+
concat([
|
10
|
+
concat([path.call(print, "body", 0), useNoSpace ? "" : " "]),
|
11
|
+
operator,
|
12
|
+
indent(
|
13
|
+
concat([useNoSpace ? softline : line, path.call(print, "body", 2)])
|
14
|
+
)
|
15
|
+
])
|
16
|
+
);
|
17
|
+
},
|
18
|
+
dot2: (path, opts, print) =>
|
19
|
+
concat([
|
20
|
+
path.call(print, "body", 0),
|
21
|
+
"..",
|
22
|
+
path.getValue().body[1] ? path.call(print, "body", 1) : ""
|
23
|
+
]),
|
24
|
+
dot3: (path, opts, print) =>
|
25
|
+
concat([
|
26
|
+
path.call(print, "body", 0),
|
27
|
+
"...",
|
28
|
+
path.getValue().body[1] ? path.call(print, "body", 1) : ""
|
29
|
+
]),
|
30
|
+
unary: (path, opts, print) => {
|
31
|
+
const oper = path.getValue().body[0];
|
32
|
+
const doc = path.call(print, "body", 1);
|
33
|
+
|
34
|
+
if (oper === "not") {
|
35
|
+
// For the `not` operator, we're explicitly making the space character
|
36
|
+
// another element in the `concat` because there are some circumstances
|
37
|
+
// where we need to force parentheses (e.g., ternaries). In that case the
|
38
|
+
// printer for those nodes can just take out the space and put in parens.
|
39
|
+
return concat(["not", " ", doc]);
|
40
|
+
}
|
41
|
+
|
42
|
+
return concat([oper[0], doc]);
|
43
|
+
}
|
44
|
+
};
|
data/src/nodes/params.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
const { concat, group, join, line } = require("../
|
1
|
+
const { concat, group, join, line } = require("../prettier");
|
2
2
|
|
3
3
|
const printGenericRestParam = symbol => (path, opts, print) =>
|
4
4
|
path.getValue().body[0]
|
@@ -33,7 +33,7 @@ const printParams = (path, opts, print) => {
|
|
33
33
|
);
|
34
34
|
}
|
35
35
|
|
36
|
-
if (rest) {
|
36
|
+
if (rest && rest.type !== "excessed_comma") {
|
37
37
|
parts.push(path.call(print, "body", 2));
|
38
38
|
}
|
39
39
|
|
@@ -60,7 +60,18 @@ const printParams = (path, opts, print) => {
|
|
60
60
|
parts.push(path.call(print, "body", 6));
|
61
61
|
}
|
62
62
|
|
63
|
-
|
63
|
+
// You can put an extra comma at the end of block args between pipes to
|
64
|
+
// change what it does. Below is the difference:
|
65
|
+
//
|
66
|
+
// [[1, 2], [3, 4]].each { |x| p x } # prints [1, 2] then [3, 4]
|
67
|
+
// [[1, 2], [3, 4]].each { |x,| p x } # prints 1 then 3
|
68
|
+
//
|
69
|
+
// In ruby 2.5, the excessed comma is indicated by having a 0 in the rest
|
70
|
+
// param position. In ruby 2.6+ it's indicated by having an "excessed_comma"
|
71
|
+
// node in the rest position. Seems odd, but it's true.
|
72
|
+
const comma = rest === 0 || (rest && rest.type === "excessed_comma");
|
73
|
+
|
74
|
+
return group(concat([join(concat([",", line]), parts), comma ? "," : ""]));
|
64
75
|
};
|
65
76
|
|
66
77
|
const paramError = () => {
|
data/src/nodes/regexp.js
CHANGED
data/src/nodes/rescue.js
CHANGED
data/src/nodes/scopes.js
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
const {
|
2
|
+
concat,
|
3
|
+
group,
|
4
|
+
hardline,
|
5
|
+
ifBreak,
|
6
|
+
indent,
|
7
|
+
line
|
8
|
+
} = require("../prettier");
|
9
|
+
|
10
|
+
module.exports = {
|
11
|
+
class: (path, opts, print) => {
|
12
|
+
const [_constant, superclass, statements] = path.getValue().body;
|
13
|
+
|
14
|
+
const parts = ["class ", path.call(print, "body", 0)];
|
15
|
+
if (superclass) {
|
16
|
+
parts.push(" < ", path.call(print, "body", 1));
|
17
|
+
}
|
18
|
+
|
19
|
+
// If the body is empty, we can replace with a ;
|
20
|
+
const stmts = statements.body[0].body;
|
21
|
+
if (stmts.length === 1 && stmts[0].type === "void_stmt") {
|
22
|
+
return group(concat([concat(parts), ifBreak(line, "; "), "end"]));
|
23
|
+
}
|
24
|
+
|
25
|
+
return group(
|
26
|
+
concat([
|
27
|
+
concat(parts),
|
28
|
+
indent(concat([hardline, path.call(print, "body", 2)])),
|
29
|
+
concat([hardline, "end"])
|
30
|
+
])
|
31
|
+
);
|
32
|
+
},
|
33
|
+
class_name_error: (_path, _opts, _print) => {
|
34
|
+
throw new Error("class/module name must be CONSTANT");
|
35
|
+
},
|
36
|
+
module: (path, opts, print) => {
|
37
|
+
const declaration = group(concat(["module ", path.call(print, "body", 0)]));
|
38
|
+
|
39
|
+
// If the body is empty, we can replace with a ;
|
40
|
+
const stmts = path.getValue().body[1].body[0].body;
|
41
|
+
if (stmts.length === 1 && stmts[0].type === "void_stmt") {
|
42
|
+
return group(concat([declaration, ifBreak(line, "; "), "end"]));
|
43
|
+
}
|
44
|
+
|
45
|
+
return group(
|
46
|
+
concat([
|
47
|
+
declaration,
|
48
|
+
indent(concat([hardline, path.call(print, "body", 1)])),
|
49
|
+
concat([hardline, "end"])
|
50
|
+
])
|
51
|
+
);
|
52
|
+
},
|
53
|
+
sclass: (path, opts, print) =>
|
54
|
+
group(
|
55
|
+
concat([
|
56
|
+
concat(["class << ", path.call(print, "body", 0)]),
|
57
|
+
indent(concat([hardline, path.call(print, "body", 1)])),
|
58
|
+
concat([hardline, "end"])
|
59
|
+
])
|
60
|
+
)
|
61
|
+
};
|