prettier 1.6.1 → 2.0.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +316 -293
- data/CONTRIBUTING.md +6 -9
- data/LICENSE +1 -1
- data/README.md +11 -12
- data/dist/haml/embed.js +53 -0
- data/dist/haml/parser.js +31 -0
- data/{src → dist}/haml/parser.rb +0 -0
- data/dist/haml/printer.js +336 -0
- data/dist/parser/getInfo.js +17 -0
- data/{src → dist}/parser/netcat.js +1 -0
- data/dist/parser/parseSync.js +128 -0
- data/dist/parser/server.rb +140 -0
- data/dist/plugin.js +143 -0
- data/dist/prettier.js +15 -0
- data/dist/rbs/parser.js +34 -0
- data/{src → dist}/rbs/parser.rb +0 -0
- data/dist/rbs/printer.js +517 -0
- data/dist/ruby/embed.js +110 -0
- data/dist/ruby/nodes/alias.js +59 -0
- data/{src → dist}/ruby/nodes/aref.js +26 -35
- data/dist/ruby/nodes/args.js +165 -0
- data/dist/ruby/nodes/arrays.js +126 -0
- data/dist/ruby/nodes/assign.js +41 -0
- data/dist/ruby/nodes/blocks.js +68 -0
- data/dist/ruby/nodes/calls.js +220 -0
- data/dist/ruby/nodes/case.js +50 -0
- data/dist/ruby/nodes/class.js +54 -0
- data/dist/ruby/nodes/commands.js +124 -0
- data/dist/ruby/nodes/conditionals.js +242 -0
- data/dist/ruby/nodes/constants.js +38 -0
- data/dist/ruby/nodes/flow.js +66 -0
- data/dist/ruby/nodes/hashes.js +130 -0
- data/dist/ruby/nodes/heredocs.js +30 -0
- data/dist/ruby/nodes/hooks.js +35 -0
- data/dist/ruby/nodes/ints.js +27 -0
- data/dist/ruby/nodes/lambdas.js +69 -0
- data/dist/ruby/nodes/loops.js +73 -0
- data/dist/ruby/nodes/massign.js +73 -0
- data/dist/ruby/nodes/methods.js +70 -0
- data/dist/ruby/nodes/operators.js +70 -0
- data/dist/ruby/nodes/params.js +89 -0
- data/dist/ruby/nodes/patterns.js +109 -0
- data/dist/ruby/nodes/regexp.js +45 -0
- data/dist/ruby/nodes/rescue.js +82 -0
- data/dist/ruby/nodes/return.js +75 -0
- data/dist/ruby/nodes/statements.js +111 -0
- data/dist/ruby/nodes/strings.js +218 -0
- data/dist/ruby/nodes/super.js +30 -0
- data/dist/ruby/nodes/undef.js +26 -0
- data/dist/ruby/nodes.js +151 -0
- data/dist/ruby/parser.js +34 -0
- data/{src → dist}/ruby/parser.rb +1215 -252
- data/dist/ruby/printer.js +125 -0
- data/dist/ruby/toProc.js +93 -0
- data/dist/types/haml.js +4 -0
- data/dist/types/plugin.js +3 -0
- data/dist/types/rbs.js +4 -0
- data/dist/types/ruby.js +4 -0
- data/dist/types/utils.js +2 -0
- data/dist/types.js +30 -0
- data/dist/utils/containsAssignment.js +15 -0
- data/dist/utils/getTrailingComma.js +6 -0
- data/dist/utils/hasAncestor.js +15 -0
- data/{src → dist}/utils/inlineEnsureParens.js +16 -17
- data/dist/utils/isEmptyBodyStmt.js +10 -0
- data/dist/utils/isEmptyStmts.js +10 -0
- data/dist/utils/literal.js +8 -0
- data/dist/utils/literallineWithoutBreakParent.js +8 -0
- data/dist/utils/makeCall.js +13 -0
- data/dist/utils/noIndent.js +11 -0
- data/dist/utils/printEmptyCollection.js +44 -0
- data/dist/utils/skipAssignIndent.js +15 -0
- data/dist/utils.js +30 -0
- data/node_modules/prettier/bin-prettier.js +313 -190
- data/node_modules/prettier/doc.js +191 -323
- data/node_modules/prettier/index.js +2753 -3677
- data/node_modules/prettier/package.json +1 -1
- data/node_modules/prettier/parser-angular.js +13 -14
- data/node_modules/prettier/parser-babel.js +7 -7
- data/node_modules/prettier/parser-espree.js +7 -7
- data/node_modules/prettier/parser-flow.js +7 -7
- data/node_modules/prettier/parser-glimmer.js +1 -1
- data/node_modules/prettier/parser-graphql.js +1 -1
- data/node_modules/prettier/parser-html.js +17 -17
- data/node_modules/prettier/parser-markdown.js +9 -9
- data/node_modules/prettier/parser-meriyah.js +7 -7
- data/node_modules/prettier/parser-postcss.js +2 -2
- data/node_modules/prettier/parser-typescript.js +7 -7
- data/node_modules/prettier/parser-yaml.js +2 -2
- data/node_modules/prettier/third-party.js +143 -78
- data/package.json +26 -18
- metadata +74 -67
- data/src/haml/embed.js +0 -87
- data/src/haml/parser.js +0 -23
- data/src/haml/printer.js +0 -438
- data/src/parser/parseSync.js +0 -172
- data/src/parser/server.rb +0 -66
- data/src/plugin.js +0 -148
- data/src/prettier.js +0 -16
- data/src/rbs/parser.js +0 -37
- data/src/rbs/printer.js +0 -643
- data/src/ruby/embed.js +0 -142
- data/src/ruby/nodes/alias.js +0 -73
- data/src/ruby/nodes/args.js +0 -222
- data/src/ruby/nodes/arrays.js +0 -162
- data/src/ruby/nodes/assign.js +0 -47
- data/src/ruby/nodes/blocks.js +0 -90
- data/src/ruby/nodes/calls.js +0 -246
- data/src/ruby/nodes/case.js +0 -65
- data/src/ruby/nodes/class.js +0 -64
- data/src/ruby/nodes/commands.js +0 -131
- data/src/ruby/nodes/conditionals.js +0 -282
- data/src/ruby/nodes/constants.js +0 -43
- data/src/ruby/nodes/flow.js +0 -74
- data/src/ruby/nodes/hashes.js +0 -155
- data/src/ruby/nodes/heredocs.js +0 -36
- data/src/ruby/nodes/hooks.js +0 -34
- data/src/ruby/nodes/ints.js +0 -31
- data/src/ruby/nodes/lambdas.js +0 -76
- data/src/ruby/nodes/loops.js +0 -98
- data/src/ruby/nodes/massign.js +0 -98
- data/src/ruby/nodes/methods.js +0 -74
- data/src/ruby/nodes/operators.js +0 -83
- data/src/ruby/nodes/params.js +0 -106
- data/src/ruby/nodes/patterns.js +0 -157
- data/src/ruby/nodes/regexp.js +0 -56
- data/src/ruby/nodes/rescue.js +0 -101
- data/src/ruby/nodes/return.js +0 -94
- data/src/ruby/nodes/statements.js +0 -142
- data/src/ruby/nodes/strings.js +0 -272
- data/src/ruby/nodes/super.js +0 -35
- data/src/ruby/nodes/undef.js +0 -42
- data/src/ruby/nodes.js +0 -34
- data/src/ruby/parser.js +0 -37
- data/src/ruby/printer.js +0 -147
- data/src/ruby/toProc.js +0 -105
- data/src/utils/containsAssignment.js +0 -11
- data/src/utils/getTrailingComma.js +0 -5
- data/src/utils/hasAncestor.js +0 -17
- data/src/utils/isEmptyBodyStmt.js +0 -7
- data/src/utils/isEmptyStmts.js +0 -11
- data/src/utils/literal.js +0 -7
- data/src/utils/literallineWithoutBreakParent.js +0 -7
- data/src/utils/makeCall.js +0 -14
- data/src/utils/noIndent.js +0 -10
- data/src/utils/printEmptyCollection.js +0 -49
- data/src/utils/skipAssignIndent.js +0 -17
- data/src/utils.js +0 -13
data/src/ruby/nodes/lambdas.js
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
const { concat, group, ifBreak, indent, line } = require("../../prettier");
|
2
|
-
const { hasAncestor } = require("../../utils");
|
3
|
-
|
4
|
-
// We can have our params coming in as the first child of the main lambda node,
|
5
|
-
// or if we have them wrapped in parens then they'll be one level deeper. Even
|
6
|
-
// though it's possible to omit the parens if you only have one argument, we're
|
7
|
-
// going to keep them in no matter what for consistency.
|
8
|
-
function printLambdaParams(path, print) {
|
9
|
-
let node = path.getValue().body[0];
|
10
|
-
|
11
|
-
// In this case we had something like -> (foo) { bar } which would mean that
|
12
|
-
// we're looking at a paren node, so we'll descend one level deeper to get at
|
13
|
-
// the actual params node.
|
14
|
-
if (node.type !== "params") {
|
15
|
-
node = node.body[0];
|
16
|
-
}
|
17
|
-
|
18
|
-
// If we don't have any params at all, then we're just going to bail out and
|
19
|
-
// print nothing. This is to avoid printing an empty set of parentheses.
|
20
|
-
if (node.body.every((type) => !type)) {
|
21
|
-
return "";
|
22
|
-
}
|
23
|
-
|
24
|
-
return path.call(print, "body", 0);
|
25
|
-
}
|
26
|
-
|
27
|
-
// Lambda nodes represent stabby lambda literals, which can come in a couple of
|
28
|
-
// flavors. They can use either braces or do...end for their block, and their
|
29
|
-
// arguments can be not present, have no parentheses for a single argument, or
|
30
|
-
// have parentheses for multiple arguments. Below are a couple of examples:
|
31
|
-
//
|
32
|
-
// -> { 1 }
|
33
|
-
// -> a { a + 1 }
|
34
|
-
// ->(a) { a + 1 }
|
35
|
-
// ->(a, b) { a + b }
|
36
|
-
// ->(a, b = 1) { a + b }
|
37
|
-
//
|
38
|
-
// -> do
|
39
|
-
// 1
|
40
|
-
// end
|
41
|
-
//
|
42
|
-
// -> a do
|
43
|
-
// a + 1
|
44
|
-
// end
|
45
|
-
//
|
46
|
-
// ->(a, b) do
|
47
|
-
// a + b
|
48
|
-
// end
|
49
|
-
//
|
50
|
-
// Generally, we're going to favor do...end for the multi-line form and braces
|
51
|
-
// for the single-line form. However, if we have an ancestor that is a command
|
52
|
-
// or command_call node, then we'll need to use braces either way because of
|
53
|
-
// operator precendence.
|
54
|
-
function printLambda(path, opts, print) {
|
55
|
-
const params = printLambdaParams(path, print);
|
56
|
-
const inCommand = hasAncestor(path, ["command", "command_call"]);
|
57
|
-
|
58
|
-
return group(
|
59
|
-
ifBreak(
|
60
|
-
concat([
|
61
|
-
"->",
|
62
|
-
params,
|
63
|
-
" ",
|
64
|
-
inCommand ? "{" : "do",
|
65
|
-
indent(concat([line, path.call(print, "body", 1)])),
|
66
|
-
line,
|
67
|
-
inCommand ? "}" : "end"
|
68
|
-
]),
|
69
|
-
concat(["->", params, " { ", path.call(print, "body", 1), " }"])
|
70
|
-
)
|
71
|
-
);
|
72
|
-
}
|
73
|
-
|
74
|
-
module.exports = {
|
75
|
-
lambda: printLambda
|
76
|
-
};
|
data/src/ruby/nodes/loops.js
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
const {
|
2
|
-
align,
|
3
|
-
breakParent,
|
4
|
-
concat,
|
5
|
-
group,
|
6
|
-
hardline,
|
7
|
-
ifBreak,
|
8
|
-
indent,
|
9
|
-
join,
|
10
|
-
softline
|
11
|
-
} = require("../../prettier");
|
12
|
-
|
13
|
-
const { containsAssignment, isEmptyStmts } = require("../../utils");
|
14
|
-
const inlineEnsureParens = require("../../utils/inlineEnsureParens");
|
15
|
-
|
16
|
-
function printLoop(keyword, modifier) {
|
17
|
-
return function printLoopWithOptions(path, { rubyModifier }, print) {
|
18
|
-
const [_predicate, stmts] = path.getValue().body;
|
19
|
-
|
20
|
-
// If the only statement inside this while loop is a void statement, then we
|
21
|
-
// can shorten to just displaying the predicate and then a semicolon.
|
22
|
-
if (isEmptyStmts(stmts)) {
|
23
|
-
return group(
|
24
|
-
concat([
|
25
|
-
group(concat([keyword, " ", path.call(print, "body", 0)])),
|
26
|
-
hardline,
|
27
|
-
"end"
|
28
|
-
])
|
29
|
-
);
|
30
|
-
}
|
31
|
-
|
32
|
-
const inlineLoop = concat(
|
33
|
-
inlineEnsureParens(path, [
|
34
|
-
path.call(print, "body", 1),
|
35
|
-
` ${keyword} `,
|
36
|
-
path.call(print, "body", 0)
|
37
|
-
])
|
38
|
-
);
|
39
|
-
|
40
|
-
// If we're in the modifier form and we're modifying a `begin`, then this is
|
41
|
-
// a special case where we need to explicitly use the modifier form because
|
42
|
-
// otherwise the semantic meaning changes. This looks like:
|
43
|
-
//
|
44
|
-
// begin
|
45
|
-
// foo
|
46
|
-
// end while bar
|
47
|
-
//
|
48
|
-
// The above is effectively a `do...while` loop (which we don't have in
|
49
|
-
// ruby).
|
50
|
-
if (modifier && path.getValue().body[1].type === "begin") {
|
51
|
-
return inlineLoop;
|
52
|
-
}
|
53
|
-
|
54
|
-
const blockLoop = concat([
|
55
|
-
concat([
|
56
|
-
`${keyword} `,
|
57
|
-
align(keyword.length + 1, path.call(print, "body", 0))
|
58
|
-
]),
|
59
|
-
indent(concat([softline, path.call(print, "body", 1)])),
|
60
|
-
concat([softline, "end"])
|
61
|
-
]);
|
62
|
-
|
63
|
-
// If we're disallowing inline loops or if the predicate of the loop
|
64
|
-
// contains an assignment (in which case we can't know for certain that that
|
65
|
-
// assignment doesn't impact the statements inside the loop) then we can't
|
66
|
-
// use the modifier form and we must use the block form.
|
67
|
-
if (!rubyModifier || containsAssignment(path.getValue().body[0])) {
|
68
|
-
return concat([breakParent, blockLoop]);
|
69
|
-
}
|
70
|
-
|
71
|
-
return group(ifBreak(blockLoop, inlineLoop));
|
72
|
-
};
|
73
|
-
}
|
74
|
-
|
75
|
-
function printFor(path, opts, print) {
|
76
|
-
const [varDoc, rangeDoc, stmtsDoc] = path.map(print, "body");
|
77
|
-
const varsDoc =
|
78
|
-
path.getValue().body[0].type === "mlhs" ? join(", ", varDoc) : varDoc;
|
79
|
-
|
80
|
-
return group(
|
81
|
-
concat([
|
82
|
-
"for ",
|
83
|
-
varsDoc,
|
84
|
-
" in ",
|
85
|
-
rangeDoc,
|
86
|
-
indent(concat([hardline, stmtsDoc])),
|
87
|
-
concat([hardline, "end"])
|
88
|
-
])
|
89
|
-
);
|
90
|
-
}
|
91
|
-
|
92
|
-
module.exports = {
|
93
|
-
while: printLoop("while", false),
|
94
|
-
while_mod: printLoop("while", true),
|
95
|
-
until: printLoop("until", false),
|
96
|
-
until_mod: printLoop("until", true),
|
97
|
-
for: printFor
|
98
|
-
};
|
data/src/ruby/nodes/massign.js
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
const {
|
2
|
-
concat,
|
3
|
-
group,
|
4
|
-
indent,
|
5
|
-
join,
|
6
|
-
line,
|
7
|
-
softline
|
8
|
-
} = require("../../prettier");
|
9
|
-
|
10
|
-
function printMAssign(path, opts, print) {
|
11
|
-
let right = path.call(print, "body", 1);
|
12
|
-
|
13
|
-
if (
|
14
|
-
["mrhs_add_star", "mrhs_new_from_args"].includes(
|
15
|
-
path.getValue().body[1].type
|
16
|
-
)
|
17
|
-
) {
|
18
|
-
right = group(join(concat([",", line]), right));
|
19
|
-
}
|
20
|
-
|
21
|
-
const parts = [join(concat([",", line]), path.call(print, "body", 0))];
|
22
|
-
if (path.getValue().body[0].comma) {
|
23
|
-
parts.push(",");
|
24
|
-
}
|
25
|
-
|
26
|
-
return group(
|
27
|
-
concat([group(concat(parts)), " =", indent(concat([line, right]))])
|
28
|
-
);
|
29
|
-
}
|
30
|
-
|
31
|
-
function printMLHS(path, opts, print) {
|
32
|
-
return path.map(print, "body");
|
33
|
-
}
|
34
|
-
|
35
|
-
function printMLHSAddPost(path, opts, print) {
|
36
|
-
return path.call(print, "body", 0).concat(path.call(print, "body", 1));
|
37
|
-
}
|
38
|
-
|
39
|
-
function printMLHSAddStar(path, opts, print) {
|
40
|
-
const rightParts = ["*"];
|
41
|
-
|
42
|
-
if (path.getValue().body[1]) {
|
43
|
-
rightParts.push(path.call(print, "body", 1));
|
44
|
-
}
|
45
|
-
|
46
|
-
return path.call(print, "body", 0).concat(concat(rightParts));
|
47
|
-
}
|
48
|
-
|
49
|
-
function printMLHSParen(path, opts, print) {
|
50
|
-
if (["massign", "mlhs_paren"].includes(path.getParentNode().type)) {
|
51
|
-
// If we're nested in brackets as part of the left hand side of an
|
52
|
-
// assignment, i.e., (a, b, c) = 1, 2, 3
|
53
|
-
// ignore the current node and just go straight to the content
|
54
|
-
return path.call(print, "body", 0);
|
55
|
-
}
|
56
|
-
|
57
|
-
const parts = [
|
58
|
-
softline,
|
59
|
-
join(concat([",", line]), path.call(print, "body", 0))
|
60
|
-
];
|
61
|
-
|
62
|
-
if (path.getValue().body[0].comma) {
|
63
|
-
parts.push(",");
|
64
|
-
}
|
65
|
-
|
66
|
-
return group(concat(["(", indent(concat(parts)), concat([softline, ")"])]));
|
67
|
-
}
|
68
|
-
|
69
|
-
function printMRHS(path, opts, print) {
|
70
|
-
return path.map(print, "body");
|
71
|
-
}
|
72
|
-
|
73
|
-
function printMRHSAddStar(path, opts, print) {
|
74
|
-
const [leftDoc, rightDoc] = path.map(print, "body");
|
75
|
-
|
76
|
-
return leftDoc.concat([concat(["*", rightDoc])]);
|
77
|
-
}
|
78
|
-
|
79
|
-
function printMRHSNewFromArgs(path, opts, print) {
|
80
|
-
const parts = path.call(print, "body", 0);
|
81
|
-
|
82
|
-
if (path.getValue().body[1]) {
|
83
|
-
parts.push(path.call(print, "body", 1));
|
84
|
-
}
|
85
|
-
|
86
|
-
return parts;
|
87
|
-
}
|
88
|
-
|
89
|
-
module.exports = {
|
90
|
-
massign: printMAssign,
|
91
|
-
mlhs: printMLHS,
|
92
|
-
mlhs_add_post: printMLHSAddPost,
|
93
|
-
mlhs_add_star: printMLHSAddStar,
|
94
|
-
mlhs_paren: printMLHSParen,
|
95
|
-
mrhs: printMRHS,
|
96
|
-
mrhs_add_star: printMRHSAddStar,
|
97
|
-
mrhs_new_from_args: printMRHSNewFromArgs
|
98
|
-
};
|
data/src/ruby/nodes/methods.js
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
const { concat, group, hardline, indent, line } = require("../../prettier");
|
2
|
-
const { isEmptyBodyStmt } = require("../../utils");
|
3
|
-
|
4
|
-
function printMethod(offset) {
|
5
|
-
return function printMethodWithOffset(path, opts, print) {
|
6
|
-
const [_name, params, bodystmt] = 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
|
-
}
|
17
|
-
|
18
|
-
// In case there are no parens but there are arguments
|
19
|
-
const parens = params.type === "params" && params.body.some((type) => type);
|
20
|
-
|
21
|
-
declaration.push(
|
22
|
-
path.call(print, "body", offset),
|
23
|
-
parens ? "(" : "",
|
24
|
-
path.call(print, "body", offset + 1),
|
25
|
-
parens ? ")" : ""
|
26
|
-
);
|
27
|
-
|
28
|
-
if (isEmptyBodyStmt(bodystmt)) {
|
29
|
-
return group(concat(declaration.concat("; end")));
|
30
|
-
}
|
31
|
-
|
32
|
-
return group(
|
33
|
-
concat([
|
34
|
-
group(concat(declaration)),
|
35
|
-
indent(concat([hardline, path.call(print, "body", offset + 2)])),
|
36
|
-
group(concat([hardline, "end"]))
|
37
|
-
])
|
38
|
-
);
|
39
|
-
};
|
40
|
-
}
|
41
|
-
|
42
|
-
function printSingleLineMethod(path, opts, print) {
|
43
|
-
let parensNode = path.getValue().body[1];
|
44
|
-
let paramsDoc = "";
|
45
|
-
|
46
|
-
if (parensNode) {
|
47
|
-
const paramsNode = parensNode.body[0];
|
48
|
-
|
49
|
-
if (paramsNode.body.some((type) => type)) {
|
50
|
-
paramsDoc = path.call(print, "body", 1);
|
51
|
-
}
|
52
|
-
}
|
53
|
-
|
54
|
-
return group(
|
55
|
-
concat([
|
56
|
-
"def ",
|
57
|
-
path.call(print, "body", 0),
|
58
|
-
paramsDoc,
|
59
|
-
" =",
|
60
|
-
indent(group(concat([line, path.call(print, "body", 2)])))
|
61
|
-
])
|
62
|
-
);
|
63
|
-
}
|
64
|
-
|
65
|
-
function printAccessControl(path, opts, print) {
|
66
|
-
return path.call(print, "body", 0);
|
67
|
-
}
|
68
|
-
|
69
|
-
module.exports = {
|
70
|
-
access_ctrl: printAccessControl,
|
71
|
-
def: printMethod(0),
|
72
|
-
defs: printMethod(2),
|
73
|
-
defsl: printSingleLineMethod
|
74
|
-
};
|
data/src/ruby/nodes/operators.js
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
const { concat, group, indent, line, softline } = require("../../prettier");
|
2
|
-
const { noIndent } = require("../../utils");
|
3
|
-
|
4
|
-
function printBinary(path, opts, print) {
|
5
|
-
const [_leftNode, operator, rightNode] = path.getValue().body;
|
6
|
-
const space = operator === "**" ? "" : " ";
|
7
|
-
|
8
|
-
if (noIndent.includes(rightNode.type)) {
|
9
|
-
return group(
|
10
|
-
concat([
|
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(
|
25
|
-
indent(
|
26
|
-
concat([
|
27
|
-
operator,
|
28
|
-
space === "" ? softline : line,
|
29
|
-
path.call(print, "body", 2)
|
30
|
-
])
|
31
|
-
)
|
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
|
-
}
|
60
|
-
|
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
|
-
}
|
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
|
83
|
-
};
|
data/src/ruby/nodes/params.js
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
const {
|
2
|
-
concat,
|
3
|
-
group,
|
4
|
-
join,
|
5
|
-
indent,
|
6
|
-
line,
|
7
|
-
softline
|
8
|
-
} = require("../../prettier");
|
9
|
-
const { literal } = require("../../utils");
|
10
|
-
|
11
|
-
function printRestParam(symbol) {
|
12
|
-
return function printRestParamWithSymbol(path, opts, print) {
|
13
|
-
return path.getValue().body[0]
|
14
|
-
? concat([symbol, path.call(print, "body", 0)])
|
15
|
-
: symbol;
|
16
|
-
};
|
17
|
-
}
|
18
|
-
|
19
|
-
function printParams(path, opts, print) {
|
20
|
-
const [reqs, optls, rest, post, kwargs, kwargRest, block] =
|
21
|
-
path.getValue().body;
|
22
|
-
let parts = [];
|
23
|
-
|
24
|
-
if (reqs) {
|
25
|
-
path.each(
|
26
|
-
(reqPath) => {
|
27
|
-
// For some very strange reason, if you have a comment attached to a
|
28
|
-
// rest_param, it shows up here in the list of required params.
|
29
|
-
if (reqPath.getValue().type !== "rest_param") {
|
30
|
-
parts.push(print(reqPath));
|
31
|
-
}
|
32
|
-
},
|
33
|
-
"body",
|
34
|
-
0
|
35
|
-
);
|
36
|
-
}
|
37
|
-
|
38
|
-
if (optls) {
|
39
|
-
parts = parts.concat(
|
40
|
-
path.map((optlPath) => join(" = ", optlPath.map(print)), "body", 1)
|
41
|
-
);
|
42
|
-
}
|
43
|
-
|
44
|
-
if (rest && rest.type !== "excessed_comma") {
|
45
|
-
parts.push(path.call(print, "body", 2));
|
46
|
-
}
|
47
|
-
|
48
|
-
if (post) {
|
49
|
-
parts = parts.concat(path.map(print, "body", 3));
|
50
|
-
}
|
51
|
-
|
52
|
-
if (kwargs) {
|
53
|
-
parts = parts.concat(
|
54
|
-
path.map(
|
55
|
-
(kwargPath) => {
|
56
|
-
if (!kwargPath.getValue()[1]) {
|
57
|
-
return kwargPath.call(print, 0);
|
58
|
-
}
|
59
|
-
return group(join(" ", kwargPath.map(print)));
|
60
|
-
},
|
61
|
-
"body",
|
62
|
-
4
|
63
|
-
)
|
64
|
-
);
|
65
|
-
}
|
66
|
-
|
67
|
-
if (kwargRest) {
|
68
|
-
parts.push(kwargRest === "nil" ? "**nil" : path.call(print, "body", 5));
|
69
|
-
}
|
70
|
-
|
71
|
-
if (block) {
|
72
|
-
parts.push(path.call(print, "body", 6));
|
73
|
-
}
|
74
|
-
|
75
|
-
const contents = [join(concat([",", line]), parts)];
|
76
|
-
|
77
|
-
// You can put an extra comma at the end of block args between pipes to
|
78
|
-
// change what it does. Below is the difference:
|
79
|
-
//
|
80
|
-
// [[1, 2], [3, 4]].each { |x| p x } # prints [1, 2] then [3, 4]
|
81
|
-
// [[1, 2], [3, 4]].each { |x,| p x } # prints 1 then 3
|
82
|
-
//
|
83
|
-
// In ruby 2.5, the excessed comma is indicated by having a 0 in the rest
|
84
|
-
// param position. In ruby 2.6+ it's indicated by having an "excessed_comma"
|
85
|
-
// node in the rest position. Seems odd, but it's true.
|
86
|
-
if (rest === 0 || (rest && rest.type === "excessed_comma")) {
|
87
|
-
contents.push(",");
|
88
|
-
}
|
89
|
-
|
90
|
-
// If the parent node is a paren then we skipped printing the parentheses so
|
91
|
-
// that we could handle them here and get nicer formatting.
|
92
|
-
if (["lambda", "paren"].includes(path.getParentNode().type)) {
|
93
|
-
return group(
|
94
|
-
concat(["(", indent(concat([softline].concat(contents))), softline, ")"])
|
95
|
-
);
|
96
|
-
}
|
97
|
-
|
98
|
-
return group(concat(contents));
|
99
|
-
}
|
100
|
-
|
101
|
-
module.exports = {
|
102
|
-
args_forward: literal("..."),
|
103
|
-
kwrest_param: printRestParam("**"),
|
104
|
-
rest_param: printRestParam("*"),
|
105
|
-
params: printParams
|
106
|
-
};
|
data/src/ruby/nodes/patterns.js
DELETED
@@ -1,157 +0,0 @@
|
|
1
|
-
const {
|
2
|
-
align,
|
3
|
-
concat,
|
4
|
-
group,
|
5
|
-
hardline,
|
6
|
-
indent,
|
7
|
-
join,
|
8
|
-
line
|
9
|
-
} = require("../../prettier");
|
10
|
-
|
11
|
-
const patterns = ["aryptn", "binary", "fndptn", "hshptn", "rassign"];
|
12
|
-
|
13
|
-
function printPatternArg(path, opts, print) {
|
14
|
-
// Pinning is a really special syntax in pattern matching that's not really
|
15
|
-
// all that well supported in ripper. Here we're just going to the original
|
16
|
-
// source to see if the variable is pinned.
|
17
|
-
if (
|
18
|
-
opts.originalText &&
|
19
|
-
opts.originalText[opts.locStart(path.getValue()) - 1] === "^"
|
20
|
-
) {
|
21
|
-
return concat(["^", path.call(print)]);
|
22
|
-
}
|
23
|
-
|
24
|
-
return path.call(print);
|
25
|
-
}
|
26
|
-
|
27
|
-
function printAryPtn(path, opts, print) {
|
28
|
-
const [constant, preargs, splatarg, postargs] = path.getValue().body;
|
29
|
-
let args = [];
|
30
|
-
|
31
|
-
if (preargs) {
|
32
|
-
args = args.concat(
|
33
|
-
path.map((argPath) => printPatternArg(argPath, opts, print), "body", 1)
|
34
|
-
);
|
35
|
-
}
|
36
|
-
|
37
|
-
if (splatarg) {
|
38
|
-
args.push(concat(["*", path.call(print, "body", 2)]));
|
39
|
-
}
|
40
|
-
|
41
|
-
if (postargs) {
|
42
|
-
args = args.concat(path.map(print, "body", 3));
|
43
|
-
}
|
44
|
-
|
45
|
-
args = group(join(concat([",", line]), args));
|
46
|
-
|
47
|
-
if (constant || patterns.includes(path.getParentNode().type)) {
|
48
|
-
args = concat(["[", args, "]"]);
|
49
|
-
}
|
50
|
-
|
51
|
-
if (constant) {
|
52
|
-
return concat([path.call(print, "body", 0), args]);
|
53
|
-
}
|
54
|
-
|
55
|
-
return args;
|
56
|
-
}
|
57
|
-
|
58
|
-
function printFndPtn(path, opts, print) {
|
59
|
-
const [constant] = path.getValue().body;
|
60
|
-
|
61
|
-
let args = [concat(["*", path.call(print, "body", 1)])]
|
62
|
-
.concat(path.map(print, "body", 2))
|
63
|
-
.concat(concat(["*", path.call(print, "body", 3)]));
|
64
|
-
|
65
|
-
args = concat(["[", group(join(concat([",", line]), args)), "]"]);
|
66
|
-
|
67
|
-
if (constant) {
|
68
|
-
return concat([path.call(print, "body", 0), args]);
|
69
|
-
}
|
70
|
-
|
71
|
-
return args;
|
72
|
-
}
|
73
|
-
|
74
|
-
function printHshPtn(path, opts, print) {
|
75
|
-
const [constant, keyValuePairs, keyValueRest] = path.getValue().body;
|
76
|
-
let args = [];
|
77
|
-
|
78
|
-
if (keyValuePairs.length > 0) {
|
79
|
-
const printPair = (pairPath) => {
|
80
|
-
const parts = [pairPath.call(print, 0)];
|
81
|
-
|
82
|
-
if (pairPath.getValue()[1]) {
|
83
|
-
parts.push(
|
84
|
-
" ",
|
85
|
-
pairPath.call(
|
86
|
-
(pairValuePath) => printPatternArg(pairValuePath, opts, print),
|
87
|
-
1
|
88
|
-
)
|
89
|
-
);
|
90
|
-
}
|
91
|
-
|
92
|
-
return concat(parts);
|
93
|
-
};
|
94
|
-
|
95
|
-
args = args.concat(path.map(printPair, "body", 1));
|
96
|
-
}
|
97
|
-
|
98
|
-
if (keyValueRest) {
|
99
|
-
args.push(concat(["**", path.call(print, "body", 2)]));
|
100
|
-
}
|
101
|
-
|
102
|
-
args = group(join(concat([",", line]), args));
|
103
|
-
|
104
|
-
if (constant) {
|
105
|
-
args = concat(["[", args, "]"]);
|
106
|
-
} else if (patterns.includes(path.getParentNode().type)) {
|
107
|
-
args = concat(["{ ", args, " }"]);
|
108
|
-
}
|
109
|
-
|
110
|
-
if (constant) {
|
111
|
-
return concat([path.call(print, "body", 0), args]);
|
112
|
-
}
|
113
|
-
|
114
|
-
return args;
|
115
|
-
}
|
116
|
-
|
117
|
-
function printIn(path, opts, print) {
|
118
|
-
const parts = [
|
119
|
-
"in ",
|
120
|
-
align(
|
121
|
-
"in ".length,
|
122
|
-
path.call(
|
123
|
-
(valuePath) => printPatternArg(valuePath, opts, print),
|
124
|
-
"body",
|
125
|
-
0
|
126
|
-
)
|
127
|
-
),
|
128
|
-
indent(concat([hardline, path.call(print, "body", 1)]))
|
129
|
-
];
|
130
|
-
|
131
|
-
if (path.getValue().body[2]) {
|
132
|
-
parts.push(hardline, path.call(print, "body", 2));
|
133
|
-
}
|
134
|
-
|
135
|
-
return group(concat(parts));
|
136
|
-
}
|
137
|
-
|
138
|
-
function printRAssign(path, opts, print) {
|
139
|
-
const { keyword } = path.getValue();
|
140
|
-
const [leftDoc, rightDoc] = path.map(print, "body");
|
141
|
-
|
142
|
-
return group(
|
143
|
-
concat([
|
144
|
-
leftDoc,
|
145
|
-
keyword ? " in" : " =>",
|
146
|
-
group(indent(concat([line, rightDoc])))
|
147
|
-
])
|
148
|
-
);
|
149
|
-
}
|
150
|
-
|
151
|
-
module.exports = {
|
152
|
-
aryptn: printAryPtn,
|
153
|
-
fndptn: printFndPtn,
|
154
|
-
hshptn: printHshPtn,
|
155
|
-
in: printIn,
|
156
|
-
rassign: printRAssign
|
157
|
-
};
|