prettier 2.1.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -6
  3. data/README.md +16 -16
  4. data/exe/rbprettier +2 -2
  5. data/lib/prettier/rake/task.rb +5 -5
  6. data/lib/prettier.rb +11 -11
  7. data/package.json +9 -23
  8. data/rubocop.yml +6 -6
  9. data/{dist/parser → src}/getInfo.js +0 -1
  10. data/{dist/parser → src}/netcat.js +0 -1
  11. data/src/parseSync.js +212 -0
  12. data/src/plugin.js +161 -0
  13. data/{dist/parser → src}/server.rb +45 -31
  14. metadata +94 -78
  15. data/bin/console +0 -7
  16. data/dist/haml/embed.js +0 -53
  17. data/dist/haml/parser.js +0 -31
  18. data/dist/haml/parser.rb +0 -149
  19. data/dist/haml/printer.js +0 -336
  20. data/dist/parser/parseSync.js +0 -179
  21. data/dist/plugin.js +0 -143
  22. data/dist/prettier.js +0 -15
  23. data/dist/rbs/parser.js +0 -34
  24. data/dist/rbs/parser.rb +0 -155
  25. data/dist/rbs/printer.js +0 -525
  26. data/dist/ruby/embed.js +0 -115
  27. data/dist/ruby/location.js +0 -19
  28. data/dist/ruby/nodes/alias.js +0 -60
  29. data/dist/ruby/nodes/aref.js +0 -51
  30. data/dist/ruby/nodes/args.js +0 -138
  31. data/dist/ruby/nodes/arrays.js +0 -122
  32. data/dist/ruby/nodes/assign.js +0 -37
  33. data/dist/ruby/nodes/blocks.js +0 -90
  34. data/dist/ruby/nodes/calls.js +0 -263
  35. data/dist/ruby/nodes/case.js +0 -50
  36. data/dist/ruby/nodes/class.js +0 -54
  37. data/dist/ruby/nodes/commands.js +0 -138
  38. data/dist/ruby/nodes/conditionals.js +0 -246
  39. data/dist/ruby/nodes/constants.js +0 -35
  40. data/dist/ruby/nodes/flow.js +0 -59
  41. data/dist/ruby/nodes/hashes.js +0 -126
  42. data/dist/ruby/nodes/heredocs.js +0 -30
  43. data/dist/ruby/nodes/hooks.js +0 -35
  44. data/dist/ruby/nodes/ints.js +0 -27
  45. data/dist/ruby/nodes/lambdas.js +0 -70
  46. data/dist/ruby/nodes/loops.js +0 -75
  47. data/dist/ruby/nodes/massign.js +0 -60
  48. data/dist/ruby/nodes/methods.js +0 -50
  49. data/dist/ruby/nodes/operators.js +0 -68
  50. data/dist/ruby/nodes/params.js +0 -95
  51. data/dist/ruby/nodes/patterns.js +0 -119
  52. data/dist/ruby/nodes/regexp.js +0 -45
  53. data/dist/ruby/nodes/rescue.js +0 -86
  54. data/dist/ruby/nodes/return.js +0 -100
  55. data/dist/ruby/nodes/statements.js +0 -110
  56. data/dist/ruby/nodes/strings.js +0 -220
  57. data/dist/ruby/nodes/super.js +0 -26
  58. data/dist/ruby/nodes/undef.js +0 -31
  59. data/dist/ruby/nodes.js +0 -177
  60. data/dist/ruby/parser.js +0 -35
  61. data/dist/ruby/parser.rb +0 -9134
  62. data/dist/ruby/printer.js +0 -67
  63. data/dist/ruby/toProc.js +0 -91
  64. data/dist/types/haml.js +0 -4
  65. data/dist/types/plugin.js +0 -3
  66. data/dist/types/rbs.js +0 -4
  67. data/dist/types/ruby.js +0 -4
  68. data/dist/types/utils.js +0 -2
  69. data/dist/types.js +0 -34
  70. data/dist/utils/containsAssignment.js +0 -18
  71. data/dist/utils/getChildNodes.js +0 -305
  72. data/dist/utils/getTrailingComma.js +0 -6
  73. data/dist/utils/hasAncestor.js +0 -15
  74. data/dist/utils/inlineEnsureParens.js +0 -49
  75. data/dist/utils/isEmptyBodyStmt.js +0 -10
  76. data/dist/utils/isEmptyParams.js +0 -12
  77. data/dist/utils/isEmptyStmts.js +0 -10
  78. data/dist/utils/literal.js +0 -8
  79. data/dist/utils/literallineWithoutBreakParent.js +0 -8
  80. data/dist/utils/makeCall.js +0 -14
  81. data/dist/utils/noIndent.js +0 -11
  82. data/dist/utils/printEmptyCollection.js +0 -46
  83. data/dist/utils/skipAssignIndent.js +0 -19
  84. data/dist/utils.js +0 -32
@@ -1,75 +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.printUntil = 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 isModifier(node) {
11
- return node.type === "while_mod" || node.type === "until_mod";
12
- }
13
- function printLoop(keyword) {
14
- return function printLoopWithOptions(path, { rubyModifier }, print) {
15
- const node = path.getValue();
16
- const predicateDoc = path.call(print, "pred");
17
- // If the only statement inside this while loop is a void statement, then we
18
- // can shorten to just displaying the predicate and then a semicolon.
19
- if (!isModifier(node) && (0, utils_1.isEmptyStmts)(node.stmts)) {
20
- return group([group([keyword, " ", predicateDoc]), hardline, "end"]);
21
- }
22
- const statementDoc = path.call(print, isModifier(node) ? "stmt" : "stmts");
23
- const inlineLoop = (0, utils_1.inlineEnsureParens)(path, [
24
- statementDoc,
25
- ` ${keyword} `,
26
- predicateDoc
27
- ]);
28
- // If we're in the modifier form and we're modifying a `begin`, then this is
29
- // a special case where we need to explicitly use the modifier form because
30
- // otherwise the semantic meaning changes. This looks like:
31
- //
32
- // begin
33
- // foo
34
- // end while bar
35
- //
36
- // The above is effectively a `do...while` loop (which we don't have in
37
- // ruby).
38
- if (isModifier(node) && node.stmt.type === "begin") {
39
- return inlineLoop;
40
- }
41
- const blockLoop = [
42
- [`${keyword} `, align(keyword.length + 1, predicateDoc)],
43
- indent([softline, statementDoc]),
44
- softline,
45
- "end"
46
- ];
47
- // If we're disallowing inline loops or if the predicate of the loop
48
- // contains an assignment (in which case we can't know for certain that that
49
- // assignment doesn't impact the statements inside the loop) then we can't
50
- // use the modifier form and we must use the block form.
51
- if (!rubyModifier || (0, utils_1.containsAssignment)(node.pred)) {
52
- return [breakParent, blockLoop];
53
- }
54
- return group(ifBreak(blockLoop, inlineLoop));
55
- };
56
- }
57
- const printFor = (path, opts, print) => {
58
- const node = path.getValue();
59
- let indexDoc = path.call(print, "index");
60
- if (node.index.type === "mlhs") {
61
- indexDoc = join(", ", indexDoc);
62
- }
63
- return group([
64
- "for ",
65
- indexDoc,
66
- " in ",
67
- path.call(print, "collection"),
68
- indent([hardline, path.call(print, "stmts")]),
69
- hardline,
70
- "end"
71
- ]);
72
- };
73
- exports.printFor = printFor;
74
- exports.printWhile = printLoop("while");
75
- exports.printUntil = printLoop("until");
@@ -1,60 +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.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
- const node = path.getValue();
11
- let valueDoc = path.call(print, "value");
12
- if (["mrhs", "mrhs_add_star", "mrhs_new_from_args"].includes(node.value.type)) {
13
- valueDoc = group(join([",", line], valueDoc));
14
- }
15
- const targetDoc = [
16
- join([",", line], path.call(print, "target"))
17
- ];
18
- if (node.target.type === "mlhs" && node.target.comma) {
19
- targetDoc.push(",");
20
- }
21
- return group([group(targetDoc), " =", indent([line, valueDoc])]);
22
- };
23
- exports.printMAssign = printMAssign;
24
- const printMLHS = (path, opts, print) => {
25
- return path.map(print, "parts");
26
- };
27
- exports.printMLHS = printMLHS;
28
- const printMLHSParen = (path, opts, print) => {
29
- if (["massign", "mlhs_paren"].includes(path.getParentNode().type)) {
30
- // If we're nested in brackets as part of the left hand side of an
31
- // assignment, i.e., (a, b, c) = 1, 2, 3
32
- // ignore the current node and just go straight to the content
33
- return path.call(print, "cnts");
34
- }
35
- const node = path.getValue();
36
- const parts = [
37
- softline,
38
- join([",", line], path.call(print, "cnts"))
39
- ];
40
- if (node.cnts.comma) {
41
- parts.push(",");
42
- }
43
- return group(["(", indent(parts), [softline, ")"]]);
44
- };
45
- exports.printMLHSParen = printMLHSParen;
46
- const printMRHS = (path, opts, print) => {
47
- return path.map(print, "parts");
48
- };
49
- exports.printMRHS = printMRHS;
50
- const printMRHSAddStar = (path, opts, print) => {
51
- return [
52
- ...path.call(print, "mrhs"),
53
- ["*", path.call(print, "star")]
54
- ];
55
- };
56
- exports.printMRHSAddStar = printMRHSAddStar;
57
- const printMRHSNewFromArgs = (path, opts, print) => {
58
- return path.call(print, "args");
59
- };
60
- exports.printMRHSNewFromArgs = printMRHSNewFromArgs;
@@ -1,50 +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.printDefEndless = 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
- // In this case, we're printing a method that's defined as a singleton, so
14
- // we need to include the target and the operator before the name.
15
- if (node.type === "defs") {
16
- declaration.push(path.call(print, "target"), path.call(print, "op"));
17
- }
18
- // In case there are no parens but there are parameters
19
- const useParens = node.params.type === "params" && !(0, utils_1.isEmptyParams)(node.params);
20
- declaration.push(path.call(print, "name"), useParens ? "(" : "", path.call(print, "params"), useParens ? ")" : "");
21
- if ((0, utils_1.isEmptyBodyStmt)(node.bodystmt)) {
22
- return group([...declaration, "; end"]);
23
- }
24
- return group([
25
- group(declaration),
26
- indent([hardline, path.call(print, "bodystmt")]),
27
- hardline,
28
- "end"
29
- ]);
30
- };
31
- exports.printDef = printDef;
32
- const printDefEndless = (path, opts, print) => {
33
- const node = path.getValue();
34
- let paramsDoc = "";
35
- // If we have any kind of parameters, we're going to print the whole
36
- // parentheses. If we don't, then we're just going to skip them entirely.
37
- if (node.paren && !(0, utils_1.isEmptyParams)(node.paren.cnts)) {
38
- paramsDoc = path.call(print, "paren");
39
- }
40
- return group([
41
- "def ",
42
- path.call(print, "name"),
43
- paramsDoc,
44
- " =",
45
- indent(group([line, path.call(print, "stmt")]))
46
- ]);
47
- };
48
- exports.printDefEndless = printDefEndless;
49
- const printAccessControl = (path, opts, print) => path.call(print, "value");
50
- exports.printAccessControl = printAccessControl;
@@ -1,68 +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.printNot = 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 node = path.getValue();
12
- const space = node.op === "**" ? "" : " ";
13
- if (utils_1.noIndent.includes(node.right.type)) {
14
- return group([
15
- group(path.call(print, "left")),
16
- space,
17
- node.op,
18
- space,
19
- group(path.call(print, "right"))
20
- ]);
21
- }
22
- return group([
23
- group(path.call(print, "left")),
24
- space,
25
- group(indent([
26
- node.op,
27
- space === "" ? softline : line,
28
- path.call(print, "right")
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 node = path.getValue();
37
- return [
38
- node.left ? path.call(print, "left") : "",
39
- "..",
40
- node.right ? path.call(print, "right") : ""
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 node = path.getValue();
48
- return [
49
- node.left ? path.call(print, "left") : "",
50
- "...",
51
- node.right ? path.call(print, "right") : ""
52
- ];
53
- };
54
- exports.printDot3 = printDot3;
55
- const printUnary = (path, opts, print) => {
56
- const node = path.getValue();
57
- return [node.op, path.call(print, "value")];
58
- };
59
- exports.printUnary = printUnary;
60
- const printNot = (path, opts, print) => {
61
- const node = path.getValue();
62
- const valueDoc = path.call(print, "value");
63
- if (node.paren) {
64
- return ["not", "(", valueDoc, ")"];
65
- }
66
- return ["not", " ", valueDoc];
67
- };
68
- exports.printNot = printNot;
@@ -1,95 +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.printExcessedComma = exports.printRestParam = exports.printKeywordRestParam = exports.printArgsForward = exports.printParams = void 0;
7
- const prettier_1 = __importDefault(require("../../prettier"));
8
- const { group, hardline, join, indent, line, lineSuffix, softline } = prettier_1.default;
9
- function printRestParamSymbol(symbol) {
10
- return function printRestParamWithSymbol(path, opts, print) {
11
- const node = path.getValue();
12
- return node.name ? [symbol, path.call(print, "name")] : symbol;
13
- };
14
- }
15
- const printParams = (path, opts, print) => {
16
- const node = path.getValue();
17
- let parts = [];
18
- if (node.reqs) {
19
- path.each((reqPath) => {
20
- // For some very strange reason, if you have a comment attached to a
21
- // rest_param, it shows up here in the list of required params.
22
- if (reqPath.getValue().type !== "rest_param") {
23
- parts.push(print(reqPath));
24
- }
25
- }, "reqs");
26
- }
27
- if (node.opts) {
28
- parts = parts.concat(path.map((optlPath) => join(" = ", optlPath.map(print)), "opts"));
29
- }
30
- if (node.rest && node.rest.type !== "excessed_comma") {
31
- parts.push(path.call(print, "rest"));
32
- }
33
- if (node.posts) {
34
- parts = parts.concat(path.map(print, "posts"));
35
- }
36
- if (node.keywords) {
37
- parts = parts.concat(path.map((kwargPath) => {
38
- const kwarg = kwargPath.getValue();
39
- const keyDoc = kwargPath.call(print, 0);
40
- if (kwarg[1]) {
41
- return group([keyDoc, " ", kwargPath.call(print, 1)]);
42
- }
43
- return keyDoc;
44
- }, "keywords"));
45
- }
46
- if (node.kwrest) {
47
- parts.push(node.kwrest === "nil" ? "**nil" : path.call(print, "kwrest"));
48
- }
49
- if (node.block) {
50
- parts.push(path.call(print, "block"));
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 (node.rest === 0 ||
63
- (node.rest && node.rest.type === "excessed_comma")) {
64
- contents.push(",");
65
- }
66
- // If the parent node is a paren then we skipped printing the parentheses so
67
- // that we could handle them here and get nicer formatting.
68
- const parentNode = path.getParentNode();
69
- if (["lambda", "paren"].includes(parentNode.type)) {
70
- const parts = ["("];
71
- // If the parent node is a paren and the paren has comments that are
72
- // attached to the left paren, then we need to print those out explicitly
73
- // here.
74
- if (parentNode.type === "paren" && parentNode.lparen.comments) {
75
- const comments = [];
76
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
77
- parentNode.lparen.comments.forEach((comment, index) => {
78
- comment.printed = true;
79
- comments.push(lineSuffix(`${index === 0 ? " " : ""}#${comment.value}`));
80
- });
81
- parts.push(join(hardline, comments));
82
- }
83
- return group([...parts, indent([softline, ...contents]), softline, ")"]);
84
- }
85
- return group(contents);
86
- };
87
- exports.printParams = printParams;
88
- const printArgsForward = (path) => path.getValue().value;
89
- exports.printArgsForward = printArgsForward;
90
- exports.printKeywordRestParam = printRestParamSymbol("**");
91
- exports.printRestParam = printRestParamSymbol("*");
92
- const printExcessedComma = (path, opts, print) => {
93
- return path.call(print, "value");
94
- };
95
- exports.printExcessedComma = printExcessedComma;
@@ -1,119 +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 node = path.getValue();
22
- let argDocs = [];
23
- if (node.reqs.length > 0) {
24
- argDocs = argDocs.concat(path.map((argPath) => printPatternArg(argPath, opts, print), "reqs"));
25
- }
26
- if (node.rest) {
27
- argDocs.push(["*", path.call(print, "rest")]);
28
- }
29
- if (node.posts.length > 0) {
30
- argDocs = argDocs.concat(path.map(print, "posts"));
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
- node.constant ||
45
- patterns.includes(path.getParentNode().type)) {
46
- argDoc = ["[", argDoc, "]"];
47
- }
48
- if (node.constant) {
49
- return [path.call(print, "constant"), argDoc];
50
- }
51
- return argDoc;
52
- };
53
- exports.printAryPtn = printAryPtn;
54
- const printFndPtn = (path, opts, print) => {
55
- const node = path.getValue();
56
- const docs = [
57
- "[",
58
- group(join([",", line], [
59
- ["*", path.call(print, "left")],
60
- ...path.map(print, "values"),
61
- ["*", path.call(print, "right")]
62
- ])),
63
- "]"
64
- ];
65
- if (node.constant) {
66
- return [path.call(print, "constant"), docs];
67
- }
68
- return docs;
69
- };
70
- exports.printFndPtn = printFndPtn;
71
- const printHshPtn = (path, opts, print) => {
72
- const node = path.getValue();
73
- let args = [];
74
- if (node.keywords.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, "keywords"));
83
- }
84
- if (node.kwrest) {
85
- args.push(["**", path.call(print, "kwrest")]);
86
- }
87
- args = group(join([",", line], args));
88
- if (node.constant) {
89
- args = ["[", args, "]"];
90
- }
91
- else if (patterns.includes(path.getParentNode().type)) {
92
- args = ["{ ", args, " }"];
93
- }
94
- if (node.constant) {
95
- return [path.call(print, "constant"), 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), "pattern")),
105
- indent([hardline, path.call(print, "stmts")])
106
- ];
107
- if (path.getValue().cons) {
108
- parts.push(hardline, path.call(print, "cons"));
109
- }
110
- return group(parts);
111
- };
112
- exports.printIn = printIn;
113
- const printRAssign = (path, opts, print) => group([
114
- path.call(print, "value"),
115
- " ",
116
- path.call(print, "op"),
117
- group(indent([line, path.call(print, "pattern")]))
118
- ]);
119
- 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.parts.some((part) => part.type === "tstring_content" && pattern.test(part.value));
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 firstPart = node.parts[0];
15
- return (firstPart &&
16
- firstPart.type === "tstring_content" &&
17
- [" ", "="].includes(firstPart.value[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, "parts");
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,86 +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 { align, group, hardline, indent, join, line } = prettier_1.default;
9
- const printBegin = (path, opts, print) => {
10
- return [
11
- "begin",
12
- indent([hardline, path.call(print, "bodystmt")]),
13
- hardline,
14
- "end"
15
- ];
16
- };
17
- exports.printBegin = printBegin;
18
- const printEnsure = (path, opts, print) => {
19
- return [
20
- path.call(print, "keyword"),
21
- indent([hardline, path.call(print, "stmts")])
22
- ];
23
- };
24
- exports.printEnsure = printEnsure;
25
- const printRescue = (path, opts, print) => {
26
- const node = path.getValue();
27
- const parts = ["rescue"];
28
- if (node.extn) {
29
- parts.push(align("rescue ".length, path.call(print, "extn")));
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 stmtsDoc = path.call(print, "stmts");
38
- if (stmtsDoc.length > 0) {
39
- parts.push(indent([hardline, stmtsDoc]));
40
- }
41
- // This is the next clause on the `begin` statement, either another
42
- // `rescue`, and `ensure`, or an `else` clause.
43
- if (node.cons) {
44
- parts.push([hardline, path.call(print, "cons")]);
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 node = path.getValue();
53
- const parts = [];
54
- if (node.extns) {
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, "extns");
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", "mrhs_add_star", "mrhs_new_from_args"].includes(node.extns.type)) {
61
- exceptionDoc = group(join([",", line], exceptionDoc));
62
- }
63
- parts.push(" ", exceptionDoc);
64
- }
65
- if (node.var) {
66
- parts.push(" => ", path.call(print, "var"));
67
- }
68
- return group(parts);
69
- };
70
- exports.printRescueEx = printRescueEx;
71
- const printRescueMod = (path, opts, print) => {
72
- return [
73
- "begin",
74
- indent([hardline, path.call(print, "stmt")]),
75
- hardline,
76
- "rescue StandardError",
77
- indent([hardline, path.call(print, "value")]),
78
- hardline,
79
- "end"
80
- ];
81
- };
82
- exports.printRescueMod = printRescueMod;
83
- const printRedo = (path) => path.getValue().value;
84
- exports.printRedo = printRedo;
85
- const printRetry = (path) => path.getValue().value;
86
- exports.printRetry = printRetry;