prettier 2.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +52 -6
  3. data/README.md +17 -16
  4. data/exe/rbprettier +2 -2
  5. data/lib/prettier/rake/task.rb +5 -5
  6. data/lib/prettier.rb +12 -11
  7. data/node_modules/prettier/bin-prettier.js +48 -18924
  8. data/node_modules/prettier/cli.js +12335 -0
  9. data/node_modules/prettier/doc.js +1306 -4755
  10. data/node_modules/prettier/index.js +37468 -57614
  11. data/node_modules/prettier/package.json +3 -2
  12. data/node_modules/prettier/parser-angular.js +2 -66
  13. data/node_modules/prettier/parser-babel.js +27 -22
  14. data/node_modules/prettier/parser-espree.js +26 -22
  15. data/node_modules/prettier/parser-flow.js +26 -22
  16. data/node_modules/prettier/parser-glimmer.js +27 -1
  17. data/node_modules/prettier/parser-graphql.js +15 -1
  18. data/node_modules/prettier/parser-html.js +21 -117
  19. data/node_modules/prettier/parser-markdown.js +61 -19
  20. data/node_modules/prettier/parser-meriyah.js +19 -22
  21. data/node_modules/prettier/parser-postcss.js +76 -22
  22. data/node_modules/prettier/parser-typescript.js +280 -22
  23. data/node_modules/prettier/parser-yaml.js +150 -15
  24. data/node_modules/prettier/third-party.js +8660 -11030
  25. data/package.json +11 -25
  26. data/rubocop.yml +6 -6
  27. data/src/getInfo.js +23 -0
  28. data/{dist/parser → src}/netcat.js +0 -1
  29. data/src/parseSync.js +216 -0
  30. data/src/plugin.js +170 -0
  31. data/{dist/parser → src}/server.rb +50 -27
  32. metadata +95 -75
  33. data/bin/console +0 -7
  34. data/dist/haml/embed.js +0 -53
  35. data/dist/haml/parser.js +0 -31
  36. data/dist/haml/parser.rb +0 -143
  37. data/dist/haml/printer.js +0 -336
  38. data/dist/parser/getInfo.js +0 -17
  39. data/dist/parser/parseSync.js +0 -179
  40. data/dist/plugin.js +0 -143
  41. data/dist/prettier.js +0 -15
  42. data/dist/rbs/parser.js +0 -34
  43. data/dist/rbs/parser.rb +0 -98
  44. data/dist/rbs/printer.js +0 -517
  45. data/dist/ruby/embed.js +0 -110
  46. data/dist/ruby/nodes/alias.js +0 -59
  47. data/dist/ruby/nodes/aref.js +0 -53
  48. data/dist/ruby/nodes/args.js +0 -165
  49. data/dist/ruby/nodes/arrays.js +0 -126
  50. data/dist/ruby/nodes/assign.js +0 -41
  51. data/dist/ruby/nodes/blocks.js +0 -87
  52. data/dist/ruby/nodes/calls.js +0 -260
  53. data/dist/ruby/nodes/case.js +0 -50
  54. data/dist/ruby/nodes/class.js +0 -54
  55. data/dist/ruby/nodes/commands.js +0 -124
  56. data/dist/ruby/nodes/conditionals.js +0 -242
  57. data/dist/ruby/nodes/constants.js +0 -38
  58. data/dist/ruby/nodes/flow.js +0 -66
  59. data/dist/ruby/nodes/hashes.js +0 -130
  60. data/dist/ruby/nodes/heredocs.js +0 -30
  61. data/dist/ruby/nodes/hooks.js +0 -35
  62. data/dist/ruby/nodes/ints.js +0 -27
  63. data/dist/ruby/nodes/lambdas.js +0 -69
  64. data/dist/ruby/nodes/loops.js +0 -73
  65. data/dist/ruby/nodes/massign.js +0 -73
  66. data/dist/ruby/nodes/methods.js +0 -70
  67. data/dist/ruby/nodes/operators.js +0 -70
  68. data/dist/ruby/nodes/params.js +0 -89
  69. data/dist/ruby/nodes/patterns.js +0 -122
  70. data/dist/ruby/nodes/regexp.js +0 -45
  71. data/dist/ruby/nodes/rescue.js +0 -85
  72. data/dist/ruby/nodes/return.js +0 -75
  73. data/dist/ruby/nodes/statements.js +0 -111
  74. data/dist/ruby/nodes/strings.js +0 -218
  75. data/dist/ruby/nodes/super.js +0 -30
  76. data/dist/ruby/nodes/undef.js +0 -26
  77. data/dist/ruby/nodes.js +0 -151
  78. data/dist/ruby/parser.js +0 -34
  79. data/dist/ruby/parser.rb +0 -3636
  80. data/dist/ruby/printer.js +0 -129
  81. data/dist/ruby/toProc.js +0 -93
  82. data/dist/types/haml.js +0 -4
  83. data/dist/types/plugin.js +0 -3
  84. data/dist/types/rbs.js +0 -4
  85. data/dist/types/ruby.js +0 -4
  86. data/dist/types/utils.js +0 -2
  87. data/dist/types.js +0 -30
  88. data/dist/utils/containsAssignment.js +0 -15
  89. data/dist/utils/getTrailingComma.js +0 -6
  90. data/dist/utils/hasAncestor.js +0 -15
  91. data/dist/utils/inlineEnsureParens.js +0 -49
  92. data/dist/utils/isEmptyBodyStmt.js +0 -10
  93. data/dist/utils/isEmptyStmts.js +0 -10
  94. data/dist/utils/literal.js +0 -8
  95. data/dist/utils/literallineWithoutBreakParent.js +0 -8
  96. data/dist/utils/makeCall.js +0 -13
  97. data/dist/utils/noIndent.js +0 -11
  98. data/dist/utils/printEmptyCollection.js +0 -44
  99. data/dist/utils/skipAssignIndent.js +0 -15
  100. data/dist/utils.js +0 -30
@@ -1,73 +0,0 @@
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);
@@ -1,73 +0,0 @@
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;
@@ -1,70 +0,0 @@
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;
@@ -1,70 +0,0 @@
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;
@@ -1,89 +0,0 @@
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("*");
@@ -1,122 +0,0 @@
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 argDocs = [];
23
- if (preargs) {
24
- argDocs = argDocs.concat(path.map((argPath) => printPatternArg(argPath, opts, print), "body", 1));
25
- }
26
- if (splatarg) {
27
- argDocs.push(["*", path.call(print, "body", 2)]);
28
- }
29
- if (postargs) {
30
- argDocs = argDocs.concat(path.map(print, "body", 3));
31
- }
32
- let argDoc = group(join([",", line], argDocs));
33
- // There are a couple of cases where we _must_ use brackets. They include:
34
- //
35
- // * When the number of arguments inside the array pattern is one 1, then we
36
- // have to include them, otherwise it matches the whole array. Consider the
37
- // difference between `in [elem]` and `in elem`.
38
- // * If we have a wrapping constant, then we definitely need the brackets.
39
- // Consider the difference between `in Const[elem]` and `in Const elem`
40
- // * If we're nested inside a parent pattern, then we have to have brackets.
41
- // Consider the difference between `in key: first, second` and
42
- // `in key: [first, second]`.
43
- if (argDocs.length === 1 ||
44
- constant ||
45
- patterns.includes(path.getParentNode().type)) {
46
- argDoc = ["[", argDoc, "]"];
47
- }
48
- if (constant) {
49
- return [path.call(print, "body", 0), argDoc];
50
- }
51
- return argDoc;
52
- };
53
- exports.printAryPtn = printAryPtn;
54
- const printFndPtn = (path, opts, print) => {
55
- const [constant] = path.getValue().body;
56
- const docs = [
57
- "[",
58
- group(join([",", line], [
59
- ["*", path.call(print, "body", 1)],
60
- ...path.map(print, "body", 2),
61
- ["*", path.call(print, "body", 3)]
62
- ])),
63
- "]"
64
- ];
65
- if (constant) {
66
- return [path.call(print, "body", 0), docs];
67
- }
68
- return docs;
69
- };
70
- exports.printFndPtn = printFndPtn;
71
- const printHshPtn = (path, opts, print) => {
72
- const [constant, keyValuePairs, keyValueRest] = path.getValue().body;
73
- let args = [];
74
- if (keyValuePairs.length > 0) {
75
- const printPair = (pairPath) => {
76
- const parts = [pairPath.call(print, 0)];
77
- if (pairPath.getValue()[1]) {
78
- parts.push(" ", pairPath.call((pairValuePath) => printPatternArg(pairValuePath, opts, print), 1));
79
- }
80
- return parts;
81
- };
82
- args = args.concat(path.map(printPair, "body", 1));
83
- }
84
- if (keyValueRest) {
85
- args.push(["**", path.call(print, "body", 2)]);
86
- }
87
- args = group(join([",", line], args));
88
- if (constant) {
89
- args = ["[", args, "]"];
90
- }
91
- else if (patterns.includes(path.getParentNode().type)) {
92
- args = ["{ ", args, " }"];
93
- }
94
- if (constant) {
95
- return [path.call(print, "body", 0), args];
96
- }
97
- return args;
98
- };
99
- exports.printHshPtn = printHshPtn;
100
- const printIn = (path, opts, print) => {
101
- const keyword = "in ";
102
- const parts = [
103
- keyword,
104
- align(keyword.length, path.call((valuePath) => printPatternArg(valuePath, opts, print), "body", 0)),
105
- indent([hardline, path.call(print, "body", 1)])
106
- ];
107
- if (path.getValue().body[2]) {
108
- parts.push(hardline, path.call(print, "body", 2));
109
- }
110
- return group(parts);
111
- };
112
- exports.printIn = printIn;
113
- const printRAssign = (path, opts, print) => {
114
- const { keyword } = path.getValue();
115
- const [leftDoc, rightDoc] = path.map(print, "body");
116
- return group([
117
- leftDoc,
118
- keyword ? " in" : " =>",
119
- group(indent([line, rightDoc]))
120
- ]);
121
- };
122
- exports.printRAssign = printRAssign;
@@ -1,45 +0,0 @@
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;
@@ -1,85 +0,0 @@
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
- // If there's just one exception being rescued, then it's just going to be a
56
- // single doc node.
57
- let exceptionDoc = path.call(print, "body", 0);
58
- // If there are multiple exceptions being rescued, then we're going to have
59
- // multiple doc nodes returned as an array that we need to join together.
60
- if (["mrhs_add_star", "mrhs_new_from_args"].includes(exception.type)) {
61
- exceptionDoc = group(join([",", line], exceptionDoc));
62
- }
63
- parts.push(" ", exceptionDoc);
64
- }
65
- if (variable) {
66
- parts.push(" => ", path.call(print, "body", 1));
67
- }
68
- return group(parts);
69
- };
70
- exports.printRescueEx = printRescueEx;
71
- const printRescueMod = (path, opts, print) => {
72
- const [statementDoc, valueDoc] = path.map(print, "body");
73
- return [
74
- "begin",
75
- indent([hardline, statementDoc]),
76
- hardline,
77
- "rescue StandardError",
78
- indent([hardline, valueDoc]),
79
- hardline,
80
- "end"
81
- ];
82
- };
83
- exports.printRescueMod = printRescueMod;
84
- exports.printRedo = (0, utils_1.literal)("redo");
85
- exports.printRetry = (0, utils_1.literal)("retry");