prettier 1.2.0 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +12 -88
  5. data/lib/prettier.rb +2 -2
  6. data/node_modules/prettier/index.js +54 -54
  7. data/package.json +2 -3
  8. data/rubocop.yml +26 -0
  9. data/src/{ruby.js → plugin.js} +2 -2
  10. data/src/prettier.js +1 -0
  11. data/src/{embed.js → ruby/embed.js} +6 -2
  12. data/src/{nodes.js → ruby/nodes.js} +0 -0
  13. data/src/{nodes → ruby/nodes}/alias.js +1 -1
  14. data/src/{nodes → ruby/nodes}/aref.js +8 -1
  15. data/src/{nodes → ruby/nodes}/args.js +2 -2
  16. data/src/{nodes → ruby/nodes}/arrays.js +2 -3
  17. data/src/{nodes → ruby/nodes}/assign.js +7 -7
  18. data/src/ruby/nodes/blocks.js +90 -0
  19. data/src/{nodes → ruby/nodes}/calls.js +18 -11
  20. data/src/ruby/nodes/case.js +65 -0
  21. data/src/{nodes → ruby/nodes}/class.js +1 -1
  22. data/src/ruby/nodes/commands.js +131 -0
  23. data/src/{nodes → ruby/nodes}/conditionals.js +3 -3
  24. data/src/{nodes → ruby/nodes}/constants.js +2 -2
  25. data/src/{nodes → ruby/nodes}/flow.js +2 -2
  26. data/src/{nodes → ruby/nodes}/hashes.js +32 -10
  27. data/src/{nodes → ruby/nodes}/heredocs.js +2 -2
  28. data/src/ruby/nodes/hooks.js +34 -0
  29. data/src/{nodes → ruby/nodes}/ints.js +0 -0
  30. data/src/{nodes → ruby/nodes}/lambdas.js +2 -2
  31. data/src/{nodes → ruby/nodes}/loops.js +10 -7
  32. data/src/{nodes → ruby/nodes}/massign.js +8 -1
  33. data/src/{nodes → ruby/nodes}/methods.js +24 -7
  34. data/src/{nodes → ruby/nodes}/operators.js +2 -2
  35. data/src/{nodes → ruby/nodes}/params.js +31 -16
  36. data/src/{nodes → ruby/nodes}/patterns.js +39 -17
  37. data/src/{nodes → ruby/nodes}/regexp.js +2 -2
  38. data/src/{nodes → ruby/nodes}/rescue.js +2 -2
  39. data/src/ruby/nodes/return.js +94 -0
  40. data/src/{nodes → ruby/nodes}/statements.js +6 -9
  41. data/src/{nodes → ruby/nodes}/strings.js +27 -36
  42. data/src/{nodes → ruby/nodes}/super.js +2 -2
  43. data/src/{nodes → ruby/nodes}/undef.js +1 -1
  44. data/src/{parser.js → ruby/parser.js} +4 -3
  45. data/src/{parser.rb → ruby/parser.rb} +462 -508
  46. data/src/{printer.js → ruby/printer.js} +33 -1
  47. data/src/{toProc.js → ruby/toProc.js} +4 -8
  48. data/src/utils.js +10 -93
  49. data/src/utils/containsAssignment.js +11 -0
  50. data/src/utils/getTrailingComma.js +5 -0
  51. data/src/utils/hasAncestor.js +17 -0
  52. data/src/utils/literal.js +7 -0
  53. data/src/utils/makeCall.js +14 -0
  54. data/src/utils/noIndent.js +11 -0
  55. data/src/utils/skipAssignIndent.js +10 -0
  56. metadata +49 -41
  57. data/src/nodes/blocks.js +0 -85
  58. data/src/nodes/case.js +0 -61
  59. data/src/nodes/commands.js +0 -91
  60. data/src/nodes/hooks.js +0 -44
  61. data/src/nodes/return.js +0 -72
@@ -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, line } = 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),
@@ -49,15 +47,34 @@ function printMethod(offset) {
49
47
  }
50
48
 
51
49
  function printSingleLineMethod(path, opts, print) {
52
- const [nameDoc, stmtDoc] = path.map(print, "body");
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
+ }
53
60
 
54
61
  return group(
55
- concat(["def ", nameDoc, " =", indent(group(concat([line, stmtDoc])))])
62
+ concat([
63
+ "def ",
64
+ path.call(print, "body", 0),
65
+ paramsDoc,
66
+ " =",
67
+ indent(group(concat([line, path.call(print, "body", 2)])))
68
+ ])
56
69
  );
57
70
  }
58
71
 
72
+ function printAccessControl(path, opts, print) {
73
+ return path.call(print, "body", 0);
74
+ }
75
+
59
76
  module.exports = {
60
- access_ctrl: first,
77
+ access_ctrl: printAccessControl,
61
78
  def: printMethod(0),
62
79
  defs: printMethod(2),
63
80
  defsl: printSingleLineMethod
@@ -1,5 +1,5 @@
1
- const { concat, group, indent, line, softline } = require("../prettier");
2
- const { noIndent } = require("../utils");
1
+ const { concat, group, indent, line, softline } = require("../../prettier");
2
+ const { noIndent } = require("../../utils");
3
3
 
4
4
  function printBinary(path, opts, print) {
5
5
  const [_leftNode, operator, rightNode] = path.getValue().body;
@@ -1,5 +1,12 @@
1
- const { concat, group, join, indent, line, softline } = require("../prettier");
2
- const { literal } = require("../utils");
1
+ const {
2
+ concat,
3
+ group,
4
+ join,
5
+ indent,
6
+ line,
7
+ softline
8
+ } = require("../../prettier");
9
+ const { literal } = require("../../utils");
3
10
 
4
11
  function printRestParam(symbol) {
5
12
  return function printRestParamWithSymbol(path, opts, print) {
@@ -22,18 +29,22 @@ function printParams(path, opts, print) {
22
29
  let parts = [];
23
30
 
24
31
  if (reqs) {
25
- parts = parts.concat(path.map(print, "body", 0));
32
+ path.each(
33
+ (reqPath) => {
34
+ // For some very strange reason, if you have a comment attached to a
35
+ // rest_param, it shows up here in the list of required params.
36
+ if (reqPath.getValue().type !== "rest_param") {
37
+ parts.push(print(reqPath));
38
+ }
39
+ },
40
+ "body",
41
+ 0
42
+ );
26
43
  }
27
44
 
28
45
  if (optls) {
29
46
  parts = parts.concat(
30
- optls.map((_, index) =>
31
- concat([
32
- path.call(print, "body", 1, index, 0),
33
- " = ",
34
- path.call(print, "body", 1, index, 1)
35
- ])
36
- )
47
+ path.map((optlPath) => join(" = ", optlPath.map(print)), "body", 1)
37
48
  );
38
49
  }
39
50
 
@@ -47,12 +58,16 @@ function printParams(path, opts, print) {
47
58
 
48
59
  if (kwargs) {
49
60
  parts = parts.concat(
50
- kwargs.map(([, value], index) => {
51
- if (!value) {
52
- return path.call(print, "body", 4, index, 0);
53
- }
54
- return group(join(" ", path.map(print, "body", 4, index)));
55
- })
61
+ path.map(
62
+ (kwargPath) => {
63
+ if (!kwargPath.getValue()[1]) {
64
+ return kwargPath.call(print, 0);
65
+ }
66
+ return group(join(" ", kwargPath.map(print)));
67
+ },
68
+ "body",
69
+ 4
70
+ )
56
71
  );
57
72
  }
58
73
 
@@ -1,4 +1,14 @@
1
- const { concat, group, hardline, indent, join, line } = require("../prettier");
1
+ const {
2
+ align,
3
+ concat,
4
+ group,
5
+ hardline,
6
+ indent,
7
+ join,
8
+ line
9
+ } = require("../../prettier");
10
+
11
+ const patterns = ["aryptn", "binary", "fndptn", "hshptn", "rassign"];
2
12
 
3
13
  function printPatternArg(path, opts, print) {
4
14
  // Pinning is a really special syntax in pattern matching that's not really
@@ -34,10 +44,7 @@ function printAryPtn(path, opts, print) {
34
44
 
35
45
  args = group(join(concat([",", line]), args));
36
46
 
37
- if (
38
- constant ||
39
- ["aryptn", "binary", "hshptn"].includes(path.getParentNode().type)
40
- ) {
47
+ if (constant || patterns.includes(path.getParentNode().type)) {
41
48
  args = concat(["[", args, "]"]);
42
49
  }
43
50
 
@@ -51,9 +58,9 @@ function printAryPtn(path, opts, print) {
51
58
  function printFndPtn(path, opts, print) {
52
59
  const [constant] = path.getValue().body;
53
60
 
54
- let args = [path.call(print, "body", 1)]
61
+ let args = [concat(["*", path.call(print, "body", 1)])]
55
62
  .concat(path.map(print, "body", 2))
56
- .concat(path.call(print, "body", 3));
63
+ .concat(concat(["*", path.call(print, "body", 3)]));
57
64
 
58
65
  args = concat(["[", group(join(concat([",", line]), args)), "]"]);
59
66
 
@@ -68,7 +75,7 @@ function printHshPtn(path, opts, print) {
68
75
  const [constant, keyValuePairs, keyValueRest] = path.getValue().body;
69
76
  let args = [];
70
77
 
71
- if (keyValuePairs) {
78
+ if (keyValuePairs.length > 0) {
72
79
  const printPair = (pairPath) => {
73
80
  const parts = [pairPath.call(print, 0)];
74
81
 
@@ -96,10 +103,8 @@ function printHshPtn(path, opts, print) {
96
103
 
97
104
  if (constant) {
98
105
  args = concat(["[", args, "]"]);
99
- } else if (
100
- ["aryptn", "binary", "hshptn"].includes(path.getParentNode().type)
101
- ) {
102
- args = concat(["{", args, "}"]);
106
+ } else if (patterns.includes(path.getParentNode().type)) {
107
+ args = concat(["{ ", args, " }"]);
103
108
  }
104
109
 
105
110
  if (constant) {
@@ -112,10 +117,13 @@ function printHshPtn(path, opts, print) {
112
117
  function printIn(path, opts, print) {
113
118
  const parts = [
114
119
  "in ",
115
- path.call(
116
- (valuePath) => printPatternArg(valuePath, opts, print),
117
- "body",
118
- 0
120
+ align(
121
+ "in ".length,
122
+ path.call(
123
+ (valuePath) => printPatternArg(valuePath, opts, print),
124
+ "body",
125
+ 0
126
+ )
119
127
  ),
120
128
  indent(concat([hardline, path.call(print, "body", 1)]))
121
129
  ];
@@ -127,9 +135,23 @@ function printIn(path, opts, print) {
127
135
  return group(concat(parts));
128
136
  }
129
137
 
138
+ function printRAssign(path, opts, print) {
139
+ const { keyword } = path.getValue();
140
+ const [leftDoc, rightDoc] = path.map(print, "body");
141
+
142
+ return group(
143
+ concat([
144
+ leftDoc,
145
+ keyword ? " in" : " =>",
146
+ group(indent(concat([line, rightDoc])))
147
+ ])
148
+ );
149
+ }
150
+
130
151
  module.exports = {
131
152
  aryptn: printAryPtn,
132
153
  fndptn: printFndPtn,
133
154
  hshptn: printHshPtn,
134
- in: printIn
155
+ in: printIn,
156
+ rassign: printRAssign
135
157
  };