prettier 1.1.0 → 1.2.4

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 (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +16 -1
  5. data/lib/prettier.rb +2 -2
  6. data/package.json +2 -2
  7. data/rubocop.yml +26 -0
  8. data/src/{ruby.js → plugin.js} +2 -2
  9. data/src/prettier.js +1 -0
  10. data/src/{embed.js → ruby/embed.js} +6 -2
  11. data/src/{nodes.js → ruby/nodes.js} +0 -0
  12. data/src/{nodes → ruby/nodes}/alias.js +1 -1
  13. data/src/{nodes → ruby/nodes}/aref.js +8 -1
  14. data/src/{nodes → ruby/nodes}/args.js +2 -2
  15. data/src/{nodes → ruby/nodes}/arrays.js +2 -3
  16. data/src/{nodes → ruby/nodes}/assign.js +12 -4
  17. data/src/ruby/nodes/blocks.js +90 -0
  18. data/src/{nodes → ruby/nodes}/calls.js +18 -9
  19. data/src/ruby/nodes/case.js +65 -0
  20. data/src/{nodes → ruby/nodes}/class.js +1 -1
  21. data/src/ruby/nodes/commands.js +126 -0
  22. data/src/{nodes → ruby/nodes}/conditionals.js +3 -3
  23. data/src/{nodes → ruby/nodes}/constants.js +2 -2
  24. data/src/{nodes → ruby/nodes}/flow.js +2 -2
  25. data/src/{nodes → ruby/nodes}/hashes.js +32 -10
  26. data/src/{nodes → ruby/nodes}/heredocs.js +2 -2
  27. data/src/ruby/nodes/hooks.js +34 -0
  28. data/src/{nodes → ruby/nodes}/ints.js +0 -0
  29. data/src/{nodes → ruby/nodes}/lambdas.js +2 -2
  30. data/src/{nodes → ruby/nodes}/loops.js +10 -7
  31. data/src/{nodes → ruby/nodes}/massign.js +8 -1
  32. data/src/{nodes → ruby/nodes}/methods.js +32 -6
  33. data/src/{nodes → ruby/nodes}/operators.js +2 -2
  34. data/src/{nodes → ruby/nodes}/params.js +31 -16
  35. data/src/{nodes → ruby/nodes}/patterns.js +54 -15
  36. data/src/{nodes → ruby/nodes}/regexp.js +2 -2
  37. data/src/{nodes → ruby/nodes}/rescue.js +2 -2
  38. data/src/ruby/nodes/return.js +94 -0
  39. data/src/{nodes → ruby/nodes}/statements.js +6 -9
  40. data/src/{nodes → ruby/nodes}/strings.js +27 -36
  41. data/src/{nodes → ruby/nodes}/super.js +2 -2
  42. data/src/{nodes → ruby/nodes}/undef.js +1 -1
  43. data/src/{parser.js → ruby/parser.js} +4 -3
  44. data/src/{parser.rb → ruby/parser.rb} +498 -492
  45. data/src/{printer.js → ruby/printer.js} +33 -1
  46. data/src/{toProc.js → ruby/toProc.js} +4 -8
  47. data/src/utils.js +10 -93
  48. data/src/utils/containsAssignment.js +11 -0
  49. data/src/utils/getTrailingComma.js +5 -0
  50. data/src/utils/hasAncestor.js +17 -0
  51. data/src/utils/literal.js +7 -0
  52. data/src/utils/makeCall.js +14 -0
  53. data/src/utils/noIndent.js +10 -0
  54. data/src/utils/skipAssignIndent.js +10 -0
  55. metadata +49 -41
  56. data/src/nodes/blocks.js +0 -85
  57. data/src/nodes/case.js +0 -61
  58. data/src/nodes/commands.js +0 -91
  59. data/src/nodes/hooks.js +0 -44
  60. data/src/nodes/return.js +0 -72
@@ -0,0 +1,65 @@
1
+ const {
2
+ align,
3
+ concat,
4
+ fill,
5
+ group,
6
+ hardline,
7
+ indent,
8
+ line
9
+ } = require("../../prettier");
10
+
11
+ function printCase(path, opts, print) {
12
+ const statement = ["case"];
13
+
14
+ // You don't need to explicitly have something to test against in a case
15
+ // statement (without it it effectively becomes an if/elsif chain).
16
+ if (path.getValue().body[0]) {
17
+ statement.push(" ", path.call(print, "body", 0));
18
+ }
19
+
20
+ return concat(
21
+ statement.concat([hardline, path.call(print, "body", 1), hardline, "end"])
22
+ );
23
+ }
24
+
25
+ function printWhen(path, opts, print) {
26
+ const [_preds, _stmts, addition] = path.getValue().body;
27
+
28
+ // The `fill` builder command expects an array of docs alternating with
29
+ // line breaks. This is so it can loop through and determine where to break.
30
+ const preds = fill(
31
+ path.call(print, "body", 0).reduce((accum, pred, index) => {
32
+ if (index === 0) {
33
+ return [pred];
34
+ }
35
+
36
+ // Pull off the last element and make it concat with a comma so that
37
+ // we can maintain alternating lines and docs.
38
+ return accum
39
+ .slice(0, -1)
40
+ .concat([concat([accum[accum.length - 1], ","]), line, pred]);
41
+ }, null)
42
+ );
43
+
44
+ const stmts = path.call(print, "body", 1);
45
+ const parts = [concat(["when ", align("when ".length, preds)])];
46
+
47
+ // It's possible in a when to just have empty void statements, in which case
48
+ // we would skip adding the body.
49
+ if (!stmts.parts.every((part) => !part)) {
50
+ parts.push(indent(concat([hardline, stmts])));
51
+ }
52
+
53
+ // This is the next clause on the case statement, either another `when` or
54
+ // an `else` clause.
55
+ if (addition) {
56
+ parts.push(hardline, path.call(print, "body", 2));
57
+ }
58
+
59
+ return group(concat(parts));
60
+ }
61
+
62
+ module.exports = {
63
+ case: printCase,
64
+ when: printWhen
65
+ };
@@ -5,7 +5,7 @@ const {
5
5
  ifBreak,
6
6
  indent,
7
7
  line
8
- } = require("../prettier");
8
+ } = require("../../prettier");
9
9
 
10
10
  function printClass(path, opts, print) {
11
11
  const [_constant, superclass, bodystmt] = path.getValue().body;
@@ -0,0 +1,126 @@
1
+ const {
2
+ align,
3
+ concat,
4
+ group,
5
+ ifBreak,
6
+ indent,
7
+ join,
8
+ line,
9
+ softline
10
+ } = require("../../prettier");
11
+ const { makeCall } = require("../../utils");
12
+
13
+ function docLength(doc) {
14
+ if (doc.length) {
15
+ return doc.length;
16
+ }
17
+
18
+ if (doc.parts) {
19
+ return doc.parts.reduce((sum, child) => sum + docLength(child), 0);
20
+ }
21
+
22
+ if (doc.contents) {
23
+ return docLength(doc.contents);
24
+ }
25
+
26
+ return 0;
27
+ }
28
+
29
+ function hasDef(node) {
30
+ return (
31
+ node.body[1].type === "args_add_block" &&
32
+ node.body[1].body[0].type === "args" &&
33
+ node.body[1].body[0].body[0] &&
34
+ ["def", "defs"].includes(node.body[1].body[0].body[0].type)
35
+ );
36
+ }
37
+
38
+ // Very special handling case for rspec matchers. In general with rspec matchers
39
+ // you expect to see something like:
40
+ //
41
+ // expect(foo).to receive(:bar).with(
42
+ // 'one',
43
+ // 'two',
44
+ // 'three',
45
+ // 'four',
46
+ // 'five'
47
+ // )
48
+ //
49
+ // In this case the arguments are aligned to the left side as opposed to being
50
+ // aligned with the `receive` call.
51
+ function skipArgsAlign(path) {
52
+ return ["to", "not_to"].includes(path.getValue().body[2].body);
53
+ }
54
+
55
+ // If there is a ternary argument to a command and it's going to get broken
56
+ // into multiple lines, then we're going to have to use parentheses around the
57
+ // command in order to make sure operator precedence doesn't get messed up.
58
+ function hasTernaryArg(node) {
59
+ return node.body[0].body.some((child) => child.type === "ifop");
60
+ }
61
+
62
+ function printCommand(path, opts, print) {
63
+ const node = path.getValue();
64
+
65
+ const command = path.call(print, "body", 0);
66
+ const joinedArgs = join(concat([",", line]), path.call(print, "body", 1));
67
+
68
+ const hasTernary = hasTernaryArg(node.body[1]);
69
+ let breakArgs;
70
+
71
+ if (hasTernary) {
72
+ breakArgs = indent(concat([softline, joinedArgs]));
73
+ } else if (hasDef(path.getValue())) {
74
+ breakArgs = joinedArgs;
75
+ } else {
76
+ breakArgs = align(command.length + 1, joinedArgs);
77
+ }
78
+
79
+ return group(
80
+ ifBreak(
81
+ concat([
82
+ command,
83
+ hasTernary ? "(" : " ",
84
+ breakArgs,
85
+ hasTernary ? concat([softline, ")"]) : ""
86
+ ]),
87
+ concat([command, " ", joinedArgs])
88
+ )
89
+ );
90
+ }
91
+
92
+ function printCommandCall(path, opts, print) {
93
+ const node = path.getValue();
94
+ const parts = [
95
+ path.call(print, "body", 0),
96
+ makeCall(path, opts, print),
97
+ path.call(print, "body", 2)
98
+ ];
99
+
100
+ if (!node.body[3]) {
101
+ return concat(parts);
102
+ }
103
+
104
+ const argDocs = join(concat([",", line]), path.call(print, "body", 3));
105
+ let breakDoc;
106
+
107
+ if (hasTernaryArg(node.body[3])) {
108
+ parts.push("(");
109
+ breakDoc = parts.concat(indent(concat([softline, argDocs])), softline, ")");
110
+ } else if (skipArgsAlign(path)) {
111
+ parts.push(" ");
112
+ breakDoc = parts.concat(argDocs);
113
+ } else {
114
+ parts.push(" ");
115
+ breakDoc = parts.concat(align(docLength(concat(parts)), argDocs));
116
+ }
117
+
118
+ const joinedDoc = parts.concat(argDocs);
119
+
120
+ return group(ifBreak(concat(breakDoc), concat(joinedDoc)));
121
+ }
122
+
123
+ module.exports = {
124
+ command: printCommand,
125
+ command_call: printCommandCall
126
+ };
@@ -7,10 +7,10 @@ const {
7
7
  ifBreak,
8
8
  indent,
9
9
  softline
10
- } = require("../prettier");
10
+ } = require("../../prettier");
11
11
 
12
- const { containsAssignment, isEmptyStmts } = require("../utils");
13
- const inlineEnsureParens = require("../utils/inlineEnsureParens");
12
+ const { containsAssignment, isEmptyStmts } = require("../../utils");
13
+ const inlineEnsureParens = require("../../utils/inlineEnsureParens");
14
14
 
15
15
  const printWithAddition = (keyword, path, print, { breaking = false } = {}) =>
16
16
  concat([
@@ -1,5 +1,5 @@
1
- const { concat, group, indent, join, softline } = require("../prettier");
2
- const { makeCall } = require("../utils");
1
+ const { concat, group, indent, join, softline } = require("../../prettier");
2
+ const { makeCall } = require("../../utils");
3
3
 
4
4
  function printConstPath(path, opts, print) {
5
5
  return join("::", path.map(print, "body"));
@@ -1,5 +1,5 @@
1
- const { concat, join } = require("../prettier");
2
- const { literal } = require("../utils");
1
+ const { concat, join } = require("../../prettier");
2
+ const { literal } = require("../../utils");
3
3
 
4
4
  const nodeDive = (node, steps) => {
5
5
  let current = node;
@@ -1,11 +1,16 @@
1
- const { concat, group, ifBreak, indent, join, line } = require("../prettier");
2
-
1
+ const {
2
+ concat,
3
+ group,
4
+ ifBreak,
5
+ indent,
6
+ join,
7
+ line
8
+ } = require("../../prettier");
3
9
  const {
4
10
  getTrailingComma,
5
- prefix,
6
11
  printEmptyCollection,
7
12
  skipAssignIndent
8
- } = require("../utils");
13
+ } = require("../../utils");
9
14
 
10
15
  // When attempting to convert a hash rocket into a hash label, you need to take
11
16
  // care because only certain patterns are allowed. Ruby source says that they
@@ -49,8 +54,20 @@ function printHashKeyLabel(path, print) {
49
54
  return print(path);
50
55
  case "symbol_literal":
51
56
  return concat([path.call(print, "body", 0), ":"]);
52
- case "dyna_symbol":
53
- return concat(print(path).parts.slice(1).concat(":"));
57
+ case "dyna_symbol": {
58
+ const { parts } = print(path);
59
+
60
+ // We're going to slice off the starting colon character so that we can
61
+ // move it to the end. If there are comments, then we're going to go
62
+ // further into the printed doc nodes.
63
+ if (parts[0] === ":") {
64
+ parts.splice(0, 1);
65
+ } else {
66
+ parts[1].parts.splice(0, 1);
67
+ }
68
+
69
+ return concat(parts.concat(":"));
70
+ }
54
71
  }
55
72
  }
56
73
 
@@ -66,20 +83,25 @@ function printHashKeyRocket(path, print) {
66
83
  }
67
84
 
68
85
  function printAssocNew(path, opts, print) {
86
+ const [keyNode, valueNode] = path.getValue().body;
69
87
  const { keyPrinter } = path.getParentNode();
70
88
 
71
89
  const parts = [path.call((keyPath) => keyPrinter(keyPath, print), "body", 0)];
72
90
  const valueDoc = path.call(print, "body", 1);
73
91
 
74
- if (skipAssignIndent(path.getValue().body[1])) {
75
- parts.push(" ", valueDoc);
76
- } else {
92
+ if (!skipAssignIndent(valueNode) || keyNode.comments) {
77
93
  parts.push(indent(concat([line, valueDoc])));
94
+ } else {
95
+ parts.push(" ", valueDoc);
78
96
  }
79
97
 
80
98
  return group(concat(parts));
81
99
  }
82
100
 
101
+ function printAssocSplat(path, opts, print) {
102
+ return concat(["**", path.call(print, "body", 0)]);
103
+ }
104
+
83
105
  function printHashContents(path, opts, print) {
84
106
  const node = path.getValue();
85
107
 
@@ -121,7 +143,7 @@ function printHash(path, opts, print) {
121
143
 
122
144
  module.exports = {
123
145
  assoc_new: printAssocNew,
124
- assoc_splat: prefix("**"),
146
+ assoc_splat: printAssocSplat,
125
147
  assoclist_from_args: printHashContents,
126
148
  bare_assoc_hash: printHashContents,
127
149
  hash: printHash
@@ -1,5 +1,5 @@
1
- const { concat, group, lineSuffix, join } = require("../prettier");
2
- const { literalLineNoBreak } = require("../utils");
1
+ const { concat, group, lineSuffix, join } = require("../../prettier");
2
+ const { literalLineNoBreak } = require("../../utils");
3
3
 
4
4
  function printHeredoc(path, opts, print) {
5
5
  const { body, ending } = path.getValue();
@@ -0,0 +1,34 @@
1
+ const { concat, group, indent, line } = require("../../prettier");
2
+
3
+ // The `BEGIN` and `END` keywords are used to hook into the Ruby process. Any
4
+ // `BEGIN` blocks are executed right when the process starts up, and the `END`
5
+ // blocks are executed right before exiting.
6
+ //
7
+ // BEGIN {
8
+ // # content goes here
9
+ // }
10
+ //
11
+ // END {
12
+ // # content goes here
13
+ // }
14
+ //
15
+ // Interesting side note, you don't use `do...end` blocks with these hooks. Both
16
+ // nodes contain one child which is a `stmts` node.
17
+ function printHook(name) {
18
+ return function printHookWithName(path, opts, print) {
19
+ return group(
20
+ concat([
21
+ name,
22
+ " ",
23
+ path.call(print, "body", 0),
24
+ indent(concat([line, path.call(print, "body", 1)])),
25
+ concat([line, "}"])
26
+ ])
27
+ );
28
+ };
29
+ }
30
+
31
+ module.exports = {
32
+ BEGIN: printHook("BEGIN"),
33
+ END: printHook("END")
34
+ };
File without changes
@@ -1,5 +1,5 @@
1
- const { concat, group, ifBreak, indent, line } = require("../prettier");
2
- const { hasAncestor } = require("../utils");
1
+ const { concat, group, ifBreak, indent, line } = require("../../prettier");
2
+ const { hasAncestor } = require("../../utils");
3
3
 
4
4
  // We can have our params coming in as the first child of the main lambda node,
5
5
  // or if we have them wrapped in parens then they'll be one level deeper. Even
@@ -6,11 +6,12 @@ const {
6
6
  hardline,
7
7
  ifBreak,
8
8
  indent,
9
+ join,
9
10
  softline
10
- } = require("../prettier");
11
+ } = require("../../prettier");
11
12
 
12
- const { containsAssignment } = require("../utils");
13
- const inlineEnsureParens = require("../utils/inlineEnsureParens");
13
+ const { containsAssignment } = require("../../utils");
14
+ const inlineEnsureParens = require("../../utils/inlineEnsureParens");
14
15
 
15
16
  function printLoop(keyword, modifier) {
16
17
  return function printLoopWithOptions(path, { rubyModifier }, print) {
@@ -78,15 +79,17 @@ function printLoop(keyword, modifier) {
78
79
  }
79
80
 
80
81
  function printFor(path, opts, print) {
81
- const [variable, range, stmts] = path.map(print, "body");
82
+ const [varDoc, rangeDoc, stmtsDoc] = path.map(print, "body");
83
+ const varsDoc =
84
+ path.getValue().body[0].type === "mlhs" ? join(", ", varDoc) : varDoc;
82
85
 
83
86
  return group(
84
87
  concat([
85
88
  "for ",
86
- variable,
89
+ varsDoc,
87
90
  " in ",
88
- range,
89
- indent(concat([hardline, stmts])),
91
+ rangeDoc,
92
+ indent(concat([hardline, stmtsDoc])),
90
93
  concat([hardline, "end"])
91
94
  ])
92
95
  );
@@ -1,4 +1,11 @@
1
- const { concat, group, indent, join, line, softline } = require("../prettier");
1
+ const {
2
+ concat,
3
+ group,
4
+ indent,
5
+ join,
6
+ line,
7
+ softline
8
+ } = require("../../prettier");
2
9
 
3
10
  function printMAssign(path, opts, print) {
4
11
  let right = path.call(print, "body", 1);
@@ -1,5 +1,4 @@
1
- const { concat, group, hardline, indent } = require("../prettier");
2
- const { first } = require("../utils");
1
+ const { concat, group, hardline, indent, line } = require("../../prettier");
3
2
 
4
3
  function printMethod(offset) {
5
4
  return function printMethodWithOffset(path, opts, print) {
@@ -16,8 +15,7 @@ function printMethod(offset) {
16
15
  }
17
16
 
18
17
  // In case there are no parens but there are arguments
19
- const parens =
20
- params.type === "params" && params.body.some((paramType) => paramType);
18
+ const parens = params.type === "params" && params.body.some((type) => type);
21
19
 
22
20
  declaration.push(
23
21
  path.call(print, "body", offset),
@@ -48,8 +46,36 @@ function printMethod(offset) {
48
46
  };
49
47
  }
50
48
 
49
+ function printSingleLineMethod(path, opts, print) {
50
+ let parensNode = path.getValue().body[1];
51
+ let paramsDoc = "";
52
+
53
+ if (parensNode) {
54
+ const paramsNode = parensNode.body[0];
55
+
56
+ if (paramsNode.body.some((type) => type)) {
57
+ paramsDoc = path.call(print, "body", 1);
58
+ }
59
+ }
60
+
61
+ return group(
62
+ concat([
63
+ "def ",
64
+ path.call(print, "body", 0),
65
+ paramsDoc,
66
+ " =",
67
+ indent(group(concat([line, path.call(print, "body", 2)])))
68
+ ])
69
+ );
70
+ }
71
+
72
+ function printAccessControl(path, opts, print) {
73
+ return path.call(print, "body", 0);
74
+ }
75
+
51
76
  module.exports = {
52
- access_ctrl: first,
77
+ access_ctrl: printAccessControl,
53
78
  def: printMethod(0),
54
- defs: printMethod(2)
79
+ defs: printMethod(2),
80
+ defsl: printSingleLineMethod
55
81
  };