prettier 0.20.0 → 1.0.0.pre.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +118 -4
- data/CONTRIBUTING.md +8 -6
- data/README.md +67 -60
- data/node_modules/prettier/bin-prettier.js +12317 -50112
- data/node_modules/prettier/index.js +33352 -27419
- data/node_modules/prettier/third-party.js +5678 -7676
- data/package.json +4 -4
- data/src/embed.js +27 -8
- data/src/nodes.js +6 -2
- data/src/nodes/alias.js +65 -24
- data/src/nodes/aref.js +55 -0
- data/src/nodes/args.js +55 -47
- data/src/nodes/arrays.js +150 -137
- data/src/nodes/assign.js +32 -32
- data/src/nodes/blocks.js +8 -3
- data/src/nodes/calls.js +129 -70
- data/src/nodes/case.js +11 -7
- data/src/nodes/class.js +74 -0
- data/src/nodes/commands.js +36 -31
- data/src/nodes/conditionals.js +48 -46
- data/src/nodes/constants.js +39 -21
- data/src/nodes/flow.js +45 -17
- data/src/nodes/hashes.js +126 -112
- data/src/nodes/heredocs.js +34 -0
- data/src/nodes/hooks.js +36 -7
- data/src/nodes/ints.js +27 -20
- data/src/nodes/lambdas.js +69 -52
- data/src/nodes/loops.js +19 -29
- data/src/nodes/massign.js +87 -65
- data/src/nodes/methods.js +48 -73
- data/src/nodes/operators.js +70 -39
- data/src/nodes/params.js +26 -16
- data/src/nodes/patterns.js +108 -33
- data/src/nodes/regexp.js +38 -14
- data/src/nodes/rescue.js +72 -59
- data/src/nodes/statements.js +86 -44
- data/src/nodes/strings.js +94 -90
- data/src/nodes/super.js +35 -0
- data/src/nodes/undef.js +42 -0
- data/src/parser.js +71 -0
- data/src/parser.rb +2554 -0
- data/src/printer.js +90 -0
- data/src/ruby.js +20 -61
- data/src/toProc.js +4 -4
- data/src/utils.js +24 -88
- data/src/utils/inlineEnsureParens.js +42 -0
- data/src/utils/isEmptyStmts.js +7 -0
- data/src/utils/literalLineNoBreak.js +7 -0
- metadata +15 -20
- data/src/haml.js +0 -21
- data/src/haml/embed.js +0 -58
- data/src/haml/nodes/comment.js +0 -27
- data/src/haml/nodes/doctype.js +0 -32
- data/src/haml/nodes/filter.js +0 -16
- data/src/haml/nodes/hamlComment.js +0 -21
- data/src/haml/nodes/script.js +0 -29
- data/src/haml/nodes/silentScript.js +0 -59
- data/src/haml/nodes/tag.js +0 -157
- data/src/haml/parse.js +0 -18
- data/src/haml/parse.rb +0 -64
- data/src/haml/print.js +0 -38
- data/src/nodes/scopes.js +0 -61
- data/src/parse.js +0 -37
- data/src/print.js +0 -23
- data/src/ripper.rb +0 -811
data/src/nodes/methods.js
CHANGED
@@ -1,80 +1,55 @@
|
|
1
|
-
const {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
// need to include the target and the operator
|
18
|
-
if (offset > 0) {
|
19
|
-
declaration.push(path.call(print, "body", 0), path.call(print, "body", 1));
|
20
|
-
}
|
21
|
-
|
22
|
-
// In case there are no parens but there are arguments
|
23
|
-
const parens =
|
24
|
-
params.type === "params" && params.body.some((paramType) => paramType);
|
25
|
-
|
26
|
-
declaration.push(
|
27
|
-
path.call(print, "body", offset),
|
28
|
-
parens ? "(" : "",
|
29
|
-
path.call(print, "body", offset + 1),
|
30
|
-
parens ? ")" : ""
|
31
|
-
);
|
1
|
+
const { concat, group, hardline, indent } = require("../prettier");
|
2
|
+
const { first } = require("../utils");
|
3
|
+
|
4
|
+
function printMethod(offset) {
|
5
|
+
return function printMethodWithOffset(path, opts, print) {
|
6
|
+
const [_name, params, body] = path.getValue().body.slice(offset);
|
7
|
+
const declaration = ["def "];
|
8
|
+
|
9
|
+
// In this case, we're printing a method that's defined as a singleton, so
|
10
|
+
// we need to include the target and the operator
|
11
|
+
if (offset > 0) {
|
12
|
+
declaration.push(
|
13
|
+
path.call(print, "body", 0),
|
14
|
+
path.call(print, "body", 1)
|
15
|
+
);
|
16
|
+
}
|
32
17
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
18
|
+
// In case there are no parens but there are arguments
|
19
|
+
const parens =
|
20
|
+
params.type === "params" && params.body.some((paramType) => paramType);
|
21
|
+
|
22
|
+
declaration.push(
|
23
|
+
path.call(print, "body", offset),
|
24
|
+
parens ? "(" : "",
|
25
|
+
path.call(print, "body", offset + 1),
|
26
|
+
parens ? ")" : ""
|
27
|
+
);
|
28
|
+
|
29
|
+
// If the body is empty, we can replace with a ;
|
30
|
+
const stmts = body.body[0].body;
|
31
|
+
|
32
|
+
if (
|
33
|
+
!body.body.slice(1).some((node) => node) &&
|
34
|
+
stmts.length === 1 &&
|
35
|
+
stmts[0].type === "void_stmt" &&
|
36
|
+
!stmts[0].comments
|
37
|
+
) {
|
38
|
+
return group(concat(declaration.concat(["; end"])));
|
39
|
+
}
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
};
|
41
|
+
return group(
|
42
|
+
concat([
|
43
|
+
group(concat(declaration)),
|
44
|
+
indent(concat([hardline, path.call(print, "body", offset + 2)])),
|
45
|
+
group(concat([hardline, "end"]))
|
46
|
+
])
|
47
|
+
);
|
48
|
+
};
|
49
|
+
}
|
47
50
|
|
48
51
|
module.exports = {
|
49
52
|
access_ctrl: first,
|
50
53
|
def: printMethod(0),
|
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")
|
54
|
+
defs: printMethod(2)
|
80
55
|
};
|
data/src/nodes/operators.js
CHANGED
@@ -1,52 +1,83 @@
|
|
1
1
|
const { concat, group, indent, line, softline } = require("../prettier");
|
2
|
+
const { noIndent } = require("../utils");
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
const useNoSpace = operator === "**";
|
4
|
+
function printBinary(path, opts, print) {
|
5
|
+
const [_leftNode, operator, rightNode] = path.getValue().body;
|
6
|
+
const space = operator === "**" ? "" : " ";
|
7
7
|
|
8
|
+
if (noIndent.includes(rightNode.type)) {
|
8
9
|
return group(
|
9
10
|
concat([
|
10
11
|
group(path.call(print, "body", 0)),
|
12
|
+
space,
|
13
|
+
operator,
|
14
|
+
space,
|
15
|
+
group(path.call(print, "body", 2))
|
16
|
+
])
|
17
|
+
);
|
18
|
+
}
|
19
|
+
|
20
|
+
return group(
|
21
|
+
concat([
|
22
|
+
group(path.call(print, "body", 0)),
|
23
|
+
space,
|
24
|
+
group(
|
11
25
|
indent(
|
12
26
|
concat([
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
operator,
|
17
|
-
useNoSpace ? softline : line,
|
18
|
-
path.call(print, "body", 2)
|
19
|
-
])
|
20
|
-
)
|
27
|
+
operator,
|
28
|
+
space === "" ? softline : line,
|
29
|
+
path.call(print, "body", 2)
|
21
30
|
])
|
22
31
|
)
|
23
|
-
|
24
|
-
)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
32
|
+
)
|
33
|
+
])
|
34
|
+
);
|
35
|
+
}
|
36
|
+
|
37
|
+
// dot2 nodes are used with ranges (or flip-flops). They can optionally drop
|
38
|
+
// their left side for beginless ranges or their right side for endless ranges.
|
39
|
+
function printDot2(path, opts, print) {
|
40
|
+
const [leftNode, rightNode] = path.getValue().body;
|
41
|
+
|
42
|
+
return concat([
|
43
|
+
leftNode ? path.call(print, "body", 0) : "",
|
44
|
+
"..",
|
45
|
+
rightNode ? path.call(print, "body", 1) : ""
|
46
|
+
]);
|
47
|
+
}
|
48
|
+
|
49
|
+
// dot3 nodes are used with ranges (or flip-flops). They can optionally drop
|
50
|
+
// their left side for beginless ranges or their right side for endless ranges.
|
51
|
+
function printDot3(path, opts, print) {
|
52
|
+
const [leftNode, rightNode] = path.getValue().body;
|
53
|
+
|
54
|
+
return concat([
|
55
|
+
leftNode ? path.call(print, "body", 0) : "",
|
56
|
+
"...",
|
57
|
+
rightNode ? path.call(print, "body", 1) : ""
|
58
|
+
]);
|
59
|
+
}
|
49
60
|
|
50
|
-
|
61
|
+
function printUnary(path, opts, print) {
|
62
|
+
const node = path.getValue();
|
63
|
+
const contentsDoc = path.call(print, "body", 0);
|
64
|
+
|
65
|
+
if (node.oper === "not") {
|
66
|
+
// Here we defer to the original source, as it's kind of difficult to
|
67
|
+
// determine if we can actually remove the parentheses being used.
|
68
|
+
if (node.paren) {
|
69
|
+
return concat(["not", "(", contentsDoc, ")"]);
|
70
|
+
} else {
|
71
|
+
return concat(["not", " ", contentsDoc]);
|
72
|
+
}
|
51
73
|
}
|
74
|
+
|
75
|
+
return concat([node.oper, contentsDoc]);
|
76
|
+
}
|
77
|
+
|
78
|
+
module.exports = {
|
79
|
+
binary: printBinary,
|
80
|
+
dot2: printDot2,
|
81
|
+
dot3: printDot3,
|
82
|
+
unary: printUnary
|
52
83
|
};
|
data/src/nodes/params.js
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
-
const { concat, group, join, line } = require("../prettier");
|
1
|
+
const { concat, group, join, indent, line, softline } = require("../prettier");
|
2
2
|
const { literal } = require("../utils");
|
3
3
|
|
4
|
-
|
5
|
-
path
|
6
|
-
|
7
|
-
|
4
|
+
function printRestParam(symbol) {
|
5
|
+
return function printRestParamWithSymbol(path, opts, print) {
|
6
|
+
return path.getValue().body[0]
|
7
|
+
? concat([symbol, path.call(print, "body", 0)])
|
8
|
+
: symbol;
|
9
|
+
};
|
10
|
+
}
|
8
11
|
|
9
|
-
|
12
|
+
function printParams(path, opts, print) {
|
10
13
|
const [
|
11
14
|
reqs,
|
12
15
|
optls,
|
@@ -61,6 +64,8 @@ const printParams = (path, opts, print) => {
|
|
61
64
|
parts.push(path.call(print, "body", 6));
|
62
65
|
}
|
63
66
|
|
67
|
+
const contents = [join(concat([",", line]), parts)];
|
68
|
+
|
64
69
|
// You can put an extra comma at the end of block args between pipes to
|
65
70
|
// change what it does. Below is the difference:
|
66
71
|
//
|
@@ -70,19 +75,24 @@ const printParams = (path, opts, print) => {
|
|
70
75
|
// In ruby 2.5, the excessed comma is indicated by having a 0 in the rest
|
71
76
|
// param position. In ruby 2.6+ it's indicated by having an "excessed_comma"
|
72
77
|
// node in the rest position. Seems odd, but it's true.
|
73
|
-
|
78
|
+
if (rest === 0 || (rest && rest.type === "excessed_comma")) {
|
79
|
+
contents.push(",");
|
80
|
+
}
|
74
81
|
|
75
|
-
|
76
|
-
|
82
|
+
// If the parent node is a paren then we skipped printing the parentheses so
|
83
|
+
// that we could handle them here and get nicer formatting.
|
84
|
+
if (["lambda", "paren"].includes(path.getParentNode().type)) {
|
85
|
+
return group(
|
86
|
+
concat(["(", indent(concat([softline].concat(contents))), softline, ")"])
|
87
|
+
);
|
88
|
+
}
|
77
89
|
|
78
|
-
|
79
|
-
|
80
|
-
};
|
90
|
+
return group(concat(contents));
|
91
|
+
}
|
81
92
|
|
82
93
|
module.exports = {
|
83
94
|
args_forward: literal("..."),
|
84
|
-
kwrest_param:
|
85
|
-
rest_param:
|
86
|
-
params: printParams
|
87
|
-
param_error: paramError
|
95
|
+
kwrest_param: printRestParam("**"),
|
96
|
+
rest_param: printRestParam("*"),
|
97
|
+
params: printParams
|
88
98
|
};
|
data/src/nodes/patterns.js
CHANGED
@@ -1,43 +1,118 @@
|
|
1
1
|
const { concat, group, hardline, indent, join, line } = require("../prettier");
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
function printPatternArg(path, opts, print) {
|
4
|
+
// Pinning is a really special syntax in pattern matching that's not really
|
5
|
+
// all that well supported in ripper. Here we're just going to the original
|
6
|
+
// source to see if the variable is pinned.
|
7
|
+
if (
|
8
|
+
opts.originalText &&
|
9
|
+
opts.originalText[opts.locStart(path.getValue()) - 1] === "^"
|
10
|
+
) {
|
11
|
+
return concat(["^", path.call(print)]);
|
12
|
+
}
|
7
13
|
|
8
|
-
|
9
|
-
|
10
|
-
}
|
14
|
+
return path.call(print);
|
15
|
+
}
|
11
16
|
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
function printAryPtn(path, opts, print) {
|
18
|
+
const [constant, preargs, splatarg, postargs] = path.getValue().body;
|
19
|
+
let args = [];
|
15
20
|
|
16
|
-
|
17
|
-
|
18
|
-
|
21
|
+
if (preargs) {
|
22
|
+
args = args.concat(
|
23
|
+
path.map((argPath) => printPatternArg(argPath, opts, print), "body", 1)
|
24
|
+
);
|
25
|
+
}
|
19
26
|
|
20
|
-
|
27
|
+
if (splatarg) {
|
28
|
+
args.push(concat(["*", path.call(print, "body", 2)]));
|
29
|
+
}
|
21
30
|
|
22
|
-
|
23
|
-
|
24
|
-
|
31
|
+
if (postargs) {
|
32
|
+
args = args.concat(path.map(print, "body", 3));
|
33
|
+
}
|
25
34
|
|
26
|
-
|
27
|
-
return concat([path.call(print, "body", 0), args]);
|
28
|
-
}
|
35
|
+
args = group(join(concat([",", line]), args));
|
29
36
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
concat([
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
if (
|
38
|
+
constant ||
|
39
|
+
["aryptn", "binary", "hshptn"].includes(path.getParentNode().type)
|
40
|
+
) {
|
41
|
+
args = concat(["[", args, "]"]);
|
42
|
+
}
|
43
|
+
|
44
|
+
if (constant) {
|
45
|
+
return concat([path.call(print, "body", 0), args]);
|
46
|
+
}
|
47
|
+
|
48
|
+
return args;
|
49
|
+
}
|
50
|
+
|
51
|
+
function printHshPtn(path, opts, print) {
|
52
|
+
const [constant, keyValuePairs, keyValueRest] = path.getValue().body;
|
53
|
+
let args = [];
|
54
|
+
|
55
|
+
if (keyValuePairs) {
|
56
|
+
const printPair = (pairPath) => {
|
57
|
+
const parts = [pairPath.call(print, 0)];
|
58
|
+
|
59
|
+
if (pairPath.getValue()[1]) {
|
60
|
+
parts.push(
|
61
|
+
" ",
|
62
|
+
pairPath.call(
|
63
|
+
(pairValuePath) => printPatternArg(pairValuePath, opts, print),
|
64
|
+
1
|
65
|
+
)
|
66
|
+
);
|
67
|
+
}
|
68
|
+
|
69
|
+
return concat(parts);
|
70
|
+
};
|
71
|
+
|
72
|
+
args = args.concat(path.map(printPair, "body", 1));
|
73
|
+
}
|
74
|
+
|
75
|
+
if (keyValueRest) {
|
76
|
+
args.push(concat(["**", path.call(print, "body", 2)]));
|
77
|
+
}
|
78
|
+
|
79
|
+
args = group(join(concat([",", line]), args));
|
80
|
+
|
81
|
+
if (constant) {
|
82
|
+
args = concat(["[", args, "]"]);
|
83
|
+
} else if (
|
84
|
+
["aryptn", "binary", "hshptn"].includes(path.getParentNode().type)
|
85
|
+
) {
|
86
|
+
args = concat(["{", args, "}"]);
|
87
|
+
}
|
88
|
+
|
89
|
+
if (constant) {
|
90
|
+
return concat([path.call(print, "body", 0), args]);
|
91
|
+
}
|
92
|
+
|
93
|
+
return args;
|
94
|
+
}
|
95
|
+
|
96
|
+
function printIn(path, opts, print) {
|
97
|
+
const parts = [
|
98
|
+
"in ",
|
99
|
+
path.call(
|
100
|
+
(valuePath) => printPatternArg(valuePath, opts, print),
|
101
|
+
"body",
|
102
|
+
0
|
103
|
+
),
|
104
|
+
indent(concat([hardline, path.call(print, "body", 1)]))
|
105
|
+
];
|
106
|
+
|
107
|
+
if (path.getValue().body[2]) {
|
108
|
+
parts.push(hardline, path.call(print, "body", 2));
|
109
|
+
}
|
110
|
+
|
111
|
+
return group(concat(parts));
|
112
|
+
}
|
113
|
+
|
114
|
+
module.exports = {
|
115
|
+
aryptn: printAryPtn,
|
116
|
+
hshptn: printHshPtn,
|
117
|
+
in: printIn
|
43
118
|
};
|