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
@@ -0,0 +1,73 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printUntilModifer = exports.printUntil = exports.printWhileModifier = exports.printWhile = exports.printFor = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { align, breakParent, group, hardline, ifBreak, indent, join, softline } = prettier_1.default;
|
10
|
+
function printLoop(keyword, modifier) {
|
11
|
+
return function printLoopWithOptions(path, { rubyModifier }, print) {
|
12
|
+
const [, stmts] = path.getValue().body;
|
13
|
+
// If the only statement inside this while loop is a void statement, then we
|
14
|
+
// can shorten to just displaying the predicate and then a semicolon.
|
15
|
+
if ((0, utils_1.isEmptyStmts)(stmts)) {
|
16
|
+
return group([
|
17
|
+
group([keyword, " ", path.call(print, "body", 0)]),
|
18
|
+
hardline,
|
19
|
+
"end"
|
20
|
+
]);
|
21
|
+
}
|
22
|
+
const inlineLoop = (0, utils_1.inlineEnsureParens)(path, [
|
23
|
+
path.call(print, "body", 1),
|
24
|
+
` ${keyword} `,
|
25
|
+
path.call(print, "body", 0)
|
26
|
+
]);
|
27
|
+
// If we're in the modifier form and we're modifying a `begin`, then this is
|
28
|
+
// a special case where we need to explicitly use the modifier form because
|
29
|
+
// otherwise the semantic meaning changes. This looks like:
|
30
|
+
//
|
31
|
+
// begin
|
32
|
+
// foo
|
33
|
+
// end while bar
|
34
|
+
//
|
35
|
+
// The above is effectively a `do...while` loop (which we don't have in
|
36
|
+
// ruby).
|
37
|
+
if (modifier && path.getValue().body[1].type === "begin") {
|
38
|
+
return inlineLoop;
|
39
|
+
}
|
40
|
+
const blockLoop = [
|
41
|
+
[`${keyword} `, align(keyword.length + 1, path.call(print, "body", 0))],
|
42
|
+
indent([softline, path.call(print, "body", 1)]),
|
43
|
+
softline,
|
44
|
+
"end"
|
45
|
+
];
|
46
|
+
// If we're disallowing inline loops or if the predicate of the loop
|
47
|
+
// contains an assignment (in which case we can't know for certain that that
|
48
|
+
// assignment doesn't impact the statements inside the loop) then we can't
|
49
|
+
// use the modifier form and we must use the block form.
|
50
|
+
if (!rubyModifier || (0, utils_1.containsAssignment)(path.getValue().body[0])) {
|
51
|
+
return [breakParent, blockLoop];
|
52
|
+
}
|
53
|
+
return group(ifBreak(blockLoop, inlineLoop));
|
54
|
+
};
|
55
|
+
}
|
56
|
+
const printFor = (path, opts, print) => {
|
57
|
+
const [varDoc, rangeDoc, stmtsDoc] = path.map(print, "body");
|
58
|
+
const varsDoc = path.getValue().body[0].type === "mlhs" ? join(", ", varDoc) : varDoc;
|
59
|
+
return group([
|
60
|
+
"for ",
|
61
|
+
varsDoc,
|
62
|
+
" in ",
|
63
|
+
rangeDoc,
|
64
|
+
indent([hardline, stmtsDoc]),
|
65
|
+
hardline,
|
66
|
+
"end"
|
67
|
+
]);
|
68
|
+
};
|
69
|
+
exports.printFor = printFor;
|
70
|
+
exports.printWhile = printLoop("while", false);
|
71
|
+
exports.printWhileModifier = printLoop("while", true);
|
72
|
+
exports.printUntil = printLoop("until", false);
|
73
|
+
exports.printUntilModifer = printLoop("until", true);
|
@@ -0,0 +1,73 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printMRHSNewFromArgs = exports.printMRHSAddStar = exports.printMRHS = exports.printMLHSParen = exports.printMLHSAddStar = exports.printMLHSAddPost = exports.printMLHS = exports.printMAssign = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const { group, indent, join, line, softline } = prettier_1.default;
|
9
|
+
const printMAssign = (path, opts, print) => {
|
10
|
+
let right = path.call(print, "body", 1);
|
11
|
+
if (["mrhs_add_star", "mrhs_new_from_args"].includes(path.getValue().body[1].type)) {
|
12
|
+
right = group(join([",", line], right));
|
13
|
+
}
|
14
|
+
const parts = [join([",", line], path.call(print, "body", 0))];
|
15
|
+
if (path.getValue().body[0].comma) {
|
16
|
+
parts.push(",");
|
17
|
+
}
|
18
|
+
return group([group(parts), " =", indent([line, right])]);
|
19
|
+
};
|
20
|
+
exports.printMAssign = printMAssign;
|
21
|
+
const printMLHS = (path, opts, print) => {
|
22
|
+
return path.map(print, "body");
|
23
|
+
};
|
24
|
+
exports.printMLHS = printMLHS;
|
25
|
+
const printMLHSAddPost = (path, opts, print) => {
|
26
|
+
return [
|
27
|
+
...path.call(print, "body", 0),
|
28
|
+
...path.call(print, "body", 1)
|
29
|
+
];
|
30
|
+
};
|
31
|
+
exports.printMLHSAddPost = printMLHSAddPost;
|
32
|
+
const printMLHSAddStar = (path, opts, print) => {
|
33
|
+
const parts = ["*"];
|
34
|
+
if (path.getValue().body[1]) {
|
35
|
+
parts.push(path.call(print, "body", 1));
|
36
|
+
}
|
37
|
+
return [...path.call(print, "body", 0), parts];
|
38
|
+
};
|
39
|
+
exports.printMLHSAddStar = printMLHSAddStar;
|
40
|
+
const printMLHSParen = (path, opts, print) => {
|
41
|
+
if (["massign", "mlhs_paren"].includes(path.getParentNode().type)) {
|
42
|
+
// If we're nested in brackets as part of the left hand side of an
|
43
|
+
// assignment, i.e., (a, b, c) = 1, 2, 3
|
44
|
+
// ignore the current node and just go straight to the content
|
45
|
+
return path.call(print, "body", 0);
|
46
|
+
}
|
47
|
+
const parts = [
|
48
|
+
softline,
|
49
|
+
join([",", line], path.call(print, "body", 0))
|
50
|
+
];
|
51
|
+
if (path.getValue().body[0].comma) {
|
52
|
+
parts.push(",");
|
53
|
+
}
|
54
|
+
return group(["(", indent(parts), [softline, ")"]]);
|
55
|
+
};
|
56
|
+
exports.printMLHSParen = printMLHSParen;
|
57
|
+
const printMRHS = (path, opts, print) => {
|
58
|
+
return path.map(print, "body");
|
59
|
+
};
|
60
|
+
exports.printMRHS = printMRHS;
|
61
|
+
const printMRHSAddStar = (path, opts, print) => {
|
62
|
+
const [leftDoc, rightDoc] = path.map(print, "body");
|
63
|
+
return [...leftDoc, ["*", rightDoc]];
|
64
|
+
};
|
65
|
+
exports.printMRHSAddStar = printMRHSAddStar;
|
66
|
+
const printMRHSNewFromArgs = (path, opts, print) => {
|
67
|
+
const parts = path.call(print, "body", 0);
|
68
|
+
if (path.getValue().body[1]) {
|
69
|
+
parts.push(path.call(print, "body", 1));
|
70
|
+
}
|
71
|
+
return parts;
|
72
|
+
};
|
73
|
+
exports.printMRHSNewFromArgs = printMRHSNewFromArgs;
|
@@ -0,0 +1,70 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printAccessControl = exports.printSingleLineMethod = exports.printDef = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { group, hardline, indent, line } = prettier_1.default;
|
10
|
+
const printDef = (path, opts, print) => {
|
11
|
+
const node = path.getValue();
|
12
|
+
const declaration = ["def "];
|
13
|
+
let paramsNode;
|
14
|
+
let bodystmtNode;
|
15
|
+
let nameDoc;
|
16
|
+
let paramsDoc;
|
17
|
+
let bodystmtDoc;
|
18
|
+
if (node.type === "def") {
|
19
|
+
paramsNode = node.body[1];
|
20
|
+
bodystmtNode = node.body[2];
|
21
|
+
nameDoc = path.call(print, "body", 0);
|
22
|
+
paramsDoc = path.call(print, "body", 1);
|
23
|
+
bodystmtDoc = path.call(print, "body", 2);
|
24
|
+
}
|
25
|
+
else {
|
26
|
+
// In this case, we're printing a method that's defined as a singleton, so
|
27
|
+
// we need to include the target and the operator
|
28
|
+
declaration.push(path.call(print, "body", 0), path.call(print, "body", 1));
|
29
|
+
paramsNode = node.body[3];
|
30
|
+
bodystmtNode = node.body[4];
|
31
|
+
nameDoc = path.call(print, "body", 2);
|
32
|
+
paramsDoc = path.call(print, "body", 3);
|
33
|
+
bodystmtDoc = path.call(print, "body", 4);
|
34
|
+
}
|
35
|
+
// In case there are no parens but there are arguments
|
36
|
+
const parens = paramsNode.type === "params" && paramsNode.body.some((type) => type);
|
37
|
+
declaration.push(nameDoc, parens ? "(" : "", paramsDoc, parens ? ")" : "");
|
38
|
+
if ((0, utils_1.isEmptyBodyStmt)(bodystmtNode)) {
|
39
|
+
return group([...declaration, "; end"]);
|
40
|
+
}
|
41
|
+
return group([
|
42
|
+
group(declaration),
|
43
|
+
indent([hardline, bodystmtDoc]),
|
44
|
+
hardline,
|
45
|
+
"end"
|
46
|
+
]);
|
47
|
+
};
|
48
|
+
exports.printDef = printDef;
|
49
|
+
const printSingleLineMethod = (path, opts, print) => {
|
50
|
+
const parensNode = path.getValue().body[1];
|
51
|
+
let paramsDoc = "";
|
52
|
+
if (parensNode) {
|
53
|
+
const paramsNode = parensNode.body[0];
|
54
|
+
if (paramsNode.body.some((type) => type)) {
|
55
|
+
paramsDoc = path.call(print, "body", 1);
|
56
|
+
}
|
57
|
+
}
|
58
|
+
return group([
|
59
|
+
"def ",
|
60
|
+
path.call(print, "body", 0),
|
61
|
+
paramsDoc,
|
62
|
+
" =",
|
63
|
+
indent(group([line, path.call(print, "body", 2)]))
|
64
|
+
]);
|
65
|
+
};
|
66
|
+
exports.printSingleLineMethod = printSingleLineMethod;
|
67
|
+
const printAccessControl = (path, opts, print) => {
|
68
|
+
return path.call(print, "body", 0);
|
69
|
+
};
|
70
|
+
exports.printAccessControl = printAccessControl;
|
@@ -0,0 +1,70 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printUnary = exports.printDot3 = exports.printDot2 = exports.printBinary = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { group, indent, line, softline } = prettier_1.default;
|
10
|
+
const printBinary = (path, opts, print) => {
|
11
|
+
const [, operator, rightNode] = path.getValue().body;
|
12
|
+
const space = operator === "**" ? "" : " ";
|
13
|
+
if (utils_1.noIndent.includes(rightNode.type)) {
|
14
|
+
return group([
|
15
|
+
group(path.call(print, "body", 0)),
|
16
|
+
space,
|
17
|
+
operator,
|
18
|
+
space,
|
19
|
+
group(path.call(print, "body", 2))
|
20
|
+
]);
|
21
|
+
}
|
22
|
+
return group([
|
23
|
+
group(path.call(print, "body", 0)),
|
24
|
+
space,
|
25
|
+
group(indent([
|
26
|
+
operator,
|
27
|
+
space === "" ? softline : line,
|
28
|
+
path.call(print, "body", 2)
|
29
|
+
]))
|
30
|
+
]);
|
31
|
+
};
|
32
|
+
exports.printBinary = printBinary;
|
33
|
+
// dot2 nodes are used with ranges (or flip-flops). They can optionally drop
|
34
|
+
// their left side for beginless ranges or their right side for endless ranges.
|
35
|
+
const printDot2 = (path, opts, print) => {
|
36
|
+
const [leftNode, rightNode] = path.getValue().body;
|
37
|
+
return [
|
38
|
+
leftNode ? path.call(print, "body", 0) : "",
|
39
|
+
"..",
|
40
|
+
rightNode ? path.call(print, "body", 1) : ""
|
41
|
+
];
|
42
|
+
};
|
43
|
+
exports.printDot2 = printDot2;
|
44
|
+
// dot3 nodes are used with ranges (or flip-flops). They can optionally drop
|
45
|
+
// their left side for beginless ranges or their right side for endless ranges.
|
46
|
+
const printDot3 = (path, opts, print) => {
|
47
|
+
const [leftNode, rightNode] = path.getValue().body;
|
48
|
+
return [
|
49
|
+
leftNode ? path.call(print, "body", 0) : "",
|
50
|
+
"...",
|
51
|
+
rightNode ? path.call(print, "body", 1) : ""
|
52
|
+
];
|
53
|
+
};
|
54
|
+
exports.printDot3 = printDot3;
|
55
|
+
const printUnary = (path, opts, print) => {
|
56
|
+
const node = path.getValue();
|
57
|
+
const contentsDoc = path.call(print, "body", 0);
|
58
|
+
if (node.oper === "not") {
|
59
|
+
// Here we defer to the original source, as it's kind of difficult to
|
60
|
+
// determine if we can actually remove the parentheses being used.
|
61
|
+
if (node.paren) {
|
62
|
+
return ["not", "(", contentsDoc, ")"];
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
return ["not", " ", contentsDoc];
|
66
|
+
}
|
67
|
+
}
|
68
|
+
return [node.oper, contentsDoc];
|
69
|
+
};
|
70
|
+
exports.printUnary = printUnary;
|
@@ -0,0 +1,89 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printRestParam = exports.printKeywordRestParam = exports.printArgsForward = exports.printParams = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { group, hardline, join, indent, line, lineSuffix, softline } = prettier_1.default;
|
10
|
+
function printRestParamSymbol(symbol) {
|
11
|
+
return function printRestParamWithSymbol(path, opts, print) {
|
12
|
+
return path.getValue().body[0]
|
13
|
+
? [symbol, path.call(print, "body", 0)]
|
14
|
+
: symbol;
|
15
|
+
};
|
16
|
+
}
|
17
|
+
const printParams = (path, opts, print) => {
|
18
|
+
const [reqs, optls, rest, post, kwargs, kwargRest, block] = path.getValue().body;
|
19
|
+
let parts = [];
|
20
|
+
if (reqs) {
|
21
|
+
path.each((reqPath) => {
|
22
|
+
// For some very strange reason, if you have a comment attached to a
|
23
|
+
// rest_param, it shows up here in the list of required params.
|
24
|
+
if (reqPath.getValue().type !== "rest_param") {
|
25
|
+
parts.push(print(reqPath));
|
26
|
+
}
|
27
|
+
}, "body", 0);
|
28
|
+
}
|
29
|
+
if (optls) {
|
30
|
+
parts = parts.concat(path.map((optlPath) => join(" = ", optlPath.map(print)), "body", 1));
|
31
|
+
}
|
32
|
+
if (rest && rest.type !== "excessed_comma") {
|
33
|
+
parts.push(path.call(print, "body", 2));
|
34
|
+
}
|
35
|
+
if (post) {
|
36
|
+
parts = parts.concat(path.map(print, "body", 3));
|
37
|
+
}
|
38
|
+
if (kwargs) {
|
39
|
+
parts = parts.concat(path.map((kwargPath) => {
|
40
|
+
if (!kwargPath.getValue()[1]) {
|
41
|
+
return kwargPath.call(print, 0);
|
42
|
+
}
|
43
|
+
return group(join(" ", kwargPath.map(print)));
|
44
|
+
}, "body", 4));
|
45
|
+
}
|
46
|
+
if (kwargRest) {
|
47
|
+
parts.push(kwargRest === "nil" ? "**nil" : path.call(print, "body", 5));
|
48
|
+
}
|
49
|
+
if (block) {
|
50
|
+
parts.push(path.call(print, "body", 6));
|
51
|
+
}
|
52
|
+
const contents = [join([",", line], parts)];
|
53
|
+
// You can put an extra comma at the end of block args between pipes to
|
54
|
+
// change what it does. Below is the difference:
|
55
|
+
//
|
56
|
+
// [[1, 2], [3, 4]].each { |x| p x } # prints [1, 2] then [3, 4]
|
57
|
+
// [[1, 2], [3, 4]].each { |x,| p x } # prints 1 then 3
|
58
|
+
//
|
59
|
+
// In ruby 2.5, the excessed comma is indicated by having a 0 in the rest
|
60
|
+
// param position. In ruby 2.6+ it's indicated by having an "excessed_comma"
|
61
|
+
// node in the rest position. Seems odd, but it's true.
|
62
|
+
if (rest === 0 || (rest && rest.type === "excessed_comma")) {
|
63
|
+
contents.push(",");
|
64
|
+
}
|
65
|
+
// If the parent node is a paren then we skipped printing the parentheses so
|
66
|
+
// that we could handle them here and get nicer formatting.
|
67
|
+
const parentNode = path.getParentNode();
|
68
|
+
if (["lambda", "paren"].includes(parentNode.type)) {
|
69
|
+
const parts = ["("];
|
70
|
+
// If the parent node is a paren and the paren has comments that are
|
71
|
+
// attached to the left paren, then we need to print those out explicitly
|
72
|
+
// here.
|
73
|
+
if (parentNode.type === "paren" && parentNode.lparen.comments) {
|
74
|
+
const comments = [];
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
76
|
+
parentNode.lparen.comments.forEach((comment, index) => {
|
77
|
+
comment.printed = true;
|
78
|
+
comments.push(lineSuffix(`${index === 0 ? " " : ""}#${comment.value}`));
|
79
|
+
});
|
80
|
+
parts.push(join(hardline, comments));
|
81
|
+
}
|
82
|
+
return group([...parts, indent([softline, ...contents]), softline, ")"]);
|
83
|
+
}
|
84
|
+
return group(contents);
|
85
|
+
};
|
86
|
+
exports.printParams = printParams;
|
87
|
+
exports.printArgsForward = (0, utils_1.literal)("...");
|
88
|
+
exports.printKeywordRestParam = printRestParamSymbol("**");
|
89
|
+
exports.printRestParam = printRestParamSymbol("*");
|
@@ -0,0 +1,109 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printRAssign = exports.printIn = exports.printHshPtn = exports.printFndPtn = exports.printAryPtn = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const { align, group, hardline, indent, join, line } = prettier_1.default;
|
9
|
+
const patterns = ["aryptn", "binary", "fndptn", "hshptn", "rassign"];
|
10
|
+
const printPatternArg = (path, opts, print) => {
|
11
|
+
// Pinning is a really special syntax in pattern matching that's not really
|
12
|
+
// all that well supported in ripper. Here we're just going to the original
|
13
|
+
// source to see if the variable is pinned.
|
14
|
+
if (opts.originalText &&
|
15
|
+
opts.originalText[opts.locStart(path.getValue()) - 1] === "^") {
|
16
|
+
return ["^", path.call(print)];
|
17
|
+
}
|
18
|
+
return path.call(print);
|
19
|
+
};
|
20
|
+
const printAryPtn = (path, opts, print) => {
|
21
|
+
const [constant, preargs, splatarg, postargs] = path.getValue().body;
|
22
|
+
let args = [];
|
23
|
+
if (preargs) {
|
24
|
+
args = args.concat(path.map((argPath) => printPatternArg(argPath, opts, print), "body", 1));
|
25
|
+
}
|
26
|
+
if (splatarg) {
|
27
|
+
args.push(["*", path.call(print, "body", 2)]);
|
28
|
+
}
|
29
|
+
if (postargs) {
|
30
|
+
args = args.concat(path.map(print, "body", 3));
|
31
|
+
}
|
32
|
+
args = group(join([",", line], args));
|
33
|
+
if (constant || patterns.includes(path.getParentNode().type)) {
|
34
|
+
args = ["[", args, "]"];
|
35
|
+
}
|
36
|
+
if (constant) {
|
37
|
+
return [path.call(print, "body", 0), args];
|
38
|
+
}
|
39
|
+
return args;
|
40
|
+
};
|
41
|
+
exports.printAryPtn = printAryPtn;
|
42
|
+
const printFndPtn = (path, opts, print) => {
|
43
|
+
const [constant] = path.getValue().body;
|
44
|
+
const docs = [
|
45
|
+
"[",
|
46
|
+
group(join([",", line], [
|
47
|
+
["*", path.call(print, "body", 1)],
|
48
|
+
...path.map(print, "body", 2),
|
49
|
+
["*", path.call(print, "body", 3)]
|
50
|
+
])),
|
51
|
+
"]"
|
52
|
+
];
|
53
|
+
if (constant) {
|
54
|
+
return [path.call(print, "body", 0), docs];
|
55
|
+
}
|
56
|
+
return docs;
|
57
|
+
};
|
58
|
+
exports.printFndPtn = printFndPtn;
|
59
|
+
const printHshPtn = (path, opts, print) => {
|
60
|
+
const [constant, keyValuePairs, keyValueRest] = path.getValue().body;
|
61
|
+
let args = [];
|
62
|
+
if (keyValuePairs.length > 0) {
|
63
|
+
const printPair = (pairPath) => {
|
64
|
+
const parts = [pairPath.call(print, 0)];
|
65
|
+
if (pairPath.getValue()[1]) {
|
66
|
+
parts.push(" ", pairPath.call((pairValuePath) => printPatternArg(pairValuePath, opts, print), 1));
|
67
|
+
}
|
68
|
+
return parts;
|
69
|
+
};
|
70
|
+
args = args.concat(path.map(printPair, "body", 1));
|
71
|
+
}
|
72
|
+
if (keyValueRest) {
|
73
|
+
args.push(["**", path.call(print, "body", 2)]);
|
74
|
+
}
|
75
|
+
args = group(join([",", line], args));
|
76
|
+
if (constant) {
|
77
|
+
args = ["[", args, "]"];
|
78
|
+
}
|
79
|
+
else if (patterns.includes(path.getParentNode().type)) {
|
80
|
+
args = ["{ ", args, " }"];
|
81
|
+
}
|
82
|
+
if (constant) {
|
83
|
+
return [path.call(print, "body", 0), args];
|
84
|
+
}
|
85
|
+
return args;
|
86
|
+
};
|
87
|
+
exports.printHshPtn = printHshPtn;
|
88
|
+
const printIn = (path, opts, print) => {
|
89
|
+
const parts = [
|
90
|
+
"in ",
|
91
|
+
align("in ".length, path.call((valuePath) => printPatternArg(valuePath, opts, print), "body", 0)),
|
92
|
+
indent([hardline, path.call(print, "body", 1)])
|
93
|
+
];
|
94
|
+
if (path.getValue().body[2]) {
|
95
|
+
parts.push(hardline, path.call(print, "body", 2));
|
96
|
+
}
|
97
|
+
return group(parts);
|
98
|
+
};
|
99
|
+
exports.printIn = printIn;
|
100
|
+
const printRAssign = (path, opts, print) => {
|
101
|
+
const { keyword } = path.getValue();
|
102
|
+
const [leftDoc, rightDoc] = path.map(print, "body");
|
103
|
+
return group([
|
104
|
+
leftDoc,
|
105
|
+
keyword ? " in" : " =>",
|
106
|
+
group(indent([line, rightDoc]))
|
107
|
+
]);
|
108
|
+
};
|
109
|
+
exports.printRAssign = printRAssign;
|
@@ -0,0 +1,45 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.printRegexpLiteral = void 0;
|
4
|
+
const utils_1 = require("../../utils");
|
5
|
+
function hasContent(node, pattern) {
|
6
|
+
return node.body.some((child) => child.type === "@tstring_content" && pattern.test(child.body));
|
7
|
+
}
|
8
|
+
// If the first part of this regex is plain string content, we have a space
|
9
|
+
// or an =, and we're contained within a command or command_call node, then we
|
10
|
+
// want to use braces because otherwise we could end up with an ambiguous
|
11
|
+
// operator, e.g. foo / bar/ or foo /=bar/
|
12
|
+
function forwardSlashIsAmbiguous(path) {
|
13
|
+
const node = path.getValue();
|
14
|
+
const firstChildNode = node.body[0];
|
15
|
+
return (firstChildNode &&
|
16
|
+
firstChildNode.type === "@tstring_content" &&
|
17
|
+
[" ", "="].includes(firstChildNode.body[0]) &&
|
18
|
+
(0, utils_1.hasAncestor)(path, ["command", "command_call"]));
|
19
|
+
}
|
20
|
+
// This function is responsible for printing out regexp_literal nodes. They can
|
21
|
+
// either use the special %r literal syntax or they can use forward slashes. At
|
22
|
+
// the end of either of those they can have modifiers like m or x that have
|
23
|
+
// special meaning for the regex engine.
|
24
|
+
//
|
25
|
+
// We favor the use of forward slashes unless the regex contains a forward slash
|
26
|
+
// itself. In that case we switch over to using %r with braces.
|
27
|
+
const printRegexpLiteral = (path, opts, print) => {
|
28
|
+
const node = path.getValue();
|
29
|
+
const docs = path.map(print, "body");
|
30
|
+
// We should use braces if using a forward slash would be ambiguous in the
|
31
|
+
// current context or if there's a forward slash in the content of the regexp.
|
32
|
+
const useBraces = forwardSlashIsAmbiguous(path) || hasContent(node, /\//);
|
33
|
+
// If we should be using braces but we have braces in the body of the regexp,
|
34
|
+
// then we're just going to resort to using whatever the original content was.
|
35
|
+
if (useBraces && hasContent(node, /[{}]/)) {
|
36
|
+
return [node.beging, ...docs, node.ending];
|
37
|
+
}
|
38
|
+
return [
|
39
|
+
useBraces ? "%r{" : "/",
|
40
|
+
...docs,
|
41
|
+
useBraces ? "}" : "/",
|
42
|
+
node.ending.slice(1)
|
43
|
+
];
|
44
|
+
};
|
45
|
+
exports.printRegexpLiteral = printRegexpLiteral;
|
@@ -0,0 +1,82 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printRetry = exports.printRedo = exports.printRescueMod = exports.printRescueEx = exports.printRescue = exports.printEnsure = exports.printBegin = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { align, group, hardline, indent, join, line } = prettier_1.default;
|
10
|
+
const printBegin = (path, opts, print) => {
|
11
|
+
return [
|
12
|
+
"begin",
|
13
|
+
indent([hardline, path.map(print, "body")]),
|
14
|
+
hardline,
|
15
|
+
"end"
|
16
|
+
];
|
17
|
+
};
|
18
|
+
exports.printBegin = printBegin;
|
19
|
+
const printEnsure = (path, opts, print) => {
|
20
|
+
return [
|
21
|
+
path.call(print, "body", 0),
|
22
|
+
indent([hardline, path.call(print, "body", 1)])
|
23
|
+
];
|
24
|
+
};
|
25
|
+
exports.printEnsure = printEnsure;
|
26
|
+
const printRescue = (path, opts, print) => {
|
27
|
+
const parts = ["rescue"];
|
28
|
+
if (path.getValue().body[0]) {
|
29
|
+
parts.push(align("rescue ".length, path.call(print, "body", 0)));
|
30
|
+
}
|
31
|
+
else {
|
32
|
+
// If you don't specify an error to rescue in a `begin/rescue` block, then
|
33
|
+
// implicitly you're rescuing from `StandardError`. In this case, we're
|
34
|
+
// just going to explicitly add it.
|
35
|
+
parts.push(" StandardError");
|
36
|
+
}
|
37
|
+
const bodystmt = path.call(print, "body", 1);
|
38
|
+
if (bodystmt.length > 0) {
|
39
|
+
parts.push(indent([hardline, bodystmt]));
|
40
|
+
}
|
41
|
+
// This is the next clause on the `begin` statement, either another
|
42
|
+
// `rescue`, and `ensure`, or an `else` clause.
|
43
|
+
if (path.getValue().body[2]) {
|
44
|
+
parts.push([hardline, path.call(print, "body", 2)]);
|
45
|
+
}
|
46
|
+
return group(parts);
|
47
|
+
};
|
48
|
+
exports.printRescue = printRescue;
|
49
|
+
// This is a container node that we're adding into the AST that isn't present in
|
50
|
+
// Ripper solely so that we have a nice place to attach inline comments.
|
51
|
+
const printRescueEx = (path, opts, print) => {
|
52
|
+
const [exception, variable] = path.getValue().body;
|
53
|
+
const parts = [];
|
54
|
+
if (exception) {
|
55
|
+
let exceptionDoc = path.call(print, "body", 0);
|
56
|
+
if (Array.isArray(exceptionDoc)) {
|
57
|
+
const joiner = [",", line];
|
58
|
+
exceptionDoc = group(join(joiner, exceptionDoc));
|
59
|
+
}
|
60
|
+
parts.push(" ", exceptionDoc);
|
61
|
+
}
|
62
|
+
if (variable) {
|
63
|
+
parts.push(" => ", path.call(print, "body", 1));
|
64
|
+
}
|
65
|
+
return group(parts);
|
66
|
+
};
|
67
|
+
exports.printRescueEx = printRescueEx;
|
68
|
+
const printRescueMod = (path, opts, print) => {
|
69
|
+
const [statementDoc, valueDoc] = path.map(print, "body");
|
70
|
+
return [
|
71
|
+
"begin",
|
72
|
+
indent([hardline, statementDoc]),
|
73
|
+
hardline,
|
74
|
+
"rescue StandardError",
|
75
|
+
indent([hardline, valueDoc]),
|
76
|
+
hardline,
|
77
|
+
"end"
|
78
|
+
];
|
79
|
+
};
|
80
|
+
exports.printRescueMod = printRescueMod;
|
81
|
+
exports.printRedo = (0, utils_1.literal)("redo");
|
82
|
+
exports.printRetry = (0, utils_1.literal)("retry");
|