prettier 1.2.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +64 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +22 -94
  5. data/node_modules/prettier/index.js +54 -54
  6. data/package.json +2 -3
  7. data/rubocop.yml +26 -0
  8. data/src/haml/embed.js +87 -0
  9. data/src/haml/nodes/comment.js +27 -0
  10. data/src/haml/nodes/doctype.js +34 -0
  11. data/src/haml/nodes/filter.js +16 -0
  12. data/src/haml/nodes/hamlComment.js +21 -0
  13. data/src/haml/nodes/plain.js +6 -0
  14. data/src/haml/nodes/root.js +8 -0
  15. data/src/haml/nodes/script.js +33 -0
  16. data/src/haml/nodes/silentScript.js +59 -0
  17. data/src/haml/nodes/tag.js +193 -0
  18. data/src/haml/parser.js +22 -0
  19. data/src/haml/parser.rb +138 -0
  20. data/src/haml/printer.js +28 -0
  21. data/src/parser/getLang.js +32 -0
  22. data/src/parser/getNetcat.js +50 -0
  23. data/src/parser/netcat.js +15 -0
  24. data/src/parser/parseSync.js +33 -0
  25. data/src/parser/requestParse.js +74 -0
  26. data/src/parser/server.rb +61 -0
  27. data/src/{ruby.js → plugin.js} +25 -4
  28. data/src/prettier.js +1 -0
  29. data/src/rbs/parser.js +39 -0
  30. data/src/rbs/parser.rb +88 -0
  31. data/src/rbs/printer.js +605 -0
  32. data/src/{embed.js → ruby/embed.js} +6 -2
  33. data/src/{nodes.js → ruby/nodes.js} +0 -0
  34. data/src/{nodes → ruby/nodes}/alias.js +1 -1
  35. data/src/{nodes → ruby/nodes}/aref.js +8 -1
  36. data/src/{nodes → ruby/nodes}/args.js +2 -2
  37. data/src/{nodes → ruby/nodes}/arrays.js +2 -3
  38. data/src/{nodes → ruby/nodes}/assign.js +7 -3
  39. data/src/ruby/nodes/blocks.js +90 -0
  40. data/src/{nodes → ruby/nodes}/calls.js +20 -47
  41. data/src/{nodes → ruby/nodes}/case.js +1 -1
  42. data/src/{nodes → ruby/nodes}/class.js +1 -1
  43. data/src/ruby/nodes/commands.js +131 -0
  44. data/src/{nodes → ruby/nodes}/conditionals.js +3 -3
  45. data/src/{nodes → ruby/nodes}/constants.js +2 -2
  46. data/src/{nodes → ruby/nodes}/flow.js +2 -2
  47. data/src/{nodes → ruby/nodes}/hashes.js +32 -10
  48. data/src/{nodes → ruby/nodes}/heredocs.js +2 -2
  49. data/src/ruby/nodes/hooks.js +34 -0
  50. data/src/{nodes → ruby/nodes}/ints.js +0 -0
  51. data/src/{nodes → ruby/nodes}/lambdas.js +2 -2
  52. data/src/{nodes → ruby/nodes}/loops.js +10 -7
  53. data/src/{nodes → ruby/nodes}/massign.js +8 -1
  54. data/src/{nodes → ruby/nodes}/methods.js +10 -9
  55. data/src/{nodes → ruby/nodes}/operators.js +2 -2
  56. data/src/{nodes → ruby/nodes}/params.js +31 -16
  57. data/src/{nodes → ruby/nodes}/patterns.js +17 -6
  58. data/src/{nodes → ruby/nodes}/regexp.js +2 -2
  59. data/src/{nodes → ruby/nodes}/rescue.js +34 -27
  60. data/src/{nodes → ruby/nodes}/return.js +21 -10
  61. data/src/{nodes → ruby/nodes}/statements.js +9 -9
  62. data/src/{nodes → ruby/nodes}/strings.js +28 -36
  63. data/src/{nodes → ruby/nodes}/super.js +2 -2
  64. data/src/{nodes → ruby/nodes}/undef.js +1 -1
  65. data/src/ruby/parser.js +39 -0
  66. data/src/{parser.rb → ruby/parser.rb} +498 -529
  67. data/src/{printer.js → ruby/printer.js} +1 -3
  68. data/src/{toProc.js → ruby/toProc.js} +4 -8
  69. data/src/utils.js +10 -93
  70. data/src/utils/containsAssignment.js +11 -0
  71. data/src/utils/getTrailingComma.js +5 -0
  72. data/src/utils/hasAncestor.js +17 -0
  73. data/src/utils/literal.js +7 -0
  74. data/src/utils/makeCall.js +14 -0
  75. data/src/utils/noIndent.js +11 -0
  76. data/src/utils/skipAssignIndent.js +10 -0
  77. metadata +71 -41
  78. data/src/nodes/blocks.js +0 -85
  79. data/src/nodes/commands.js +0 -91
  80. data/src/nodes/hooks.js +0 -44
  81. data/src/parser.js +0 -86
@@ -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) {
@@ -48,15 +47,13 @@ function printMethod(offset) {
48
47
  }
49
48
 
50
49
  function printSingleLineMethod(path, opts, print) {
51
- let paramsNode = path.getValue().body[1];
50
+ let parensNode = path.getValue().body[1];
52
51
  let paramsDoc = "";
53
52
 
54
- if (paramsNode) {
55
- if (paramsNode.body[0].type === "params") {
56
- paramsNode = paramsNode.body[0];
57
- }
53
+ if (parensNode) {
54
+ const paramsNode = parensNode.body[0];
58
55
 
59
- if (paramsNode.type === "params" && paramsNode.body.some((type) => type)) {
56
+ if (paramsNode.body.some((type) => type)) {
60
57
  paramsDoc = path.call(print, "body", 1);
61
58
  }
62
59
  }
@@ -72,8 +69,12 @@ function printSingleLineMethod(path, opts, print) {
72
69
  );
73
70
  }
74
71
 
72
+ function printAccessControl(path, opts, print) {
73
+ return path.call(print, "body", 0);
74
+ }
75
+
75
76
  module.exports = {
76
- access_ctrl: first,
77
+ access_ctrl: printAccessControl,
77
78
  def: printMethod(0),
78
79
  defs: printMethod(2),
79
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,12 @@
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");
2
10
 
3
11
  const patterns = ["aryptn", "binary", "fndptn", "hshptn", "rassign"];
4
12
 
@@ -67,7 +75,7 @@ function printHshPtn(path, opts, print) {
67
75
  const [constant, keyValuePairs, keyValueRest] = path.getValue().body;
68
76
  let args = [];
69
77
 
70
- if (keyValuePairs) {
78
+ if (keyValuePairs.length > 0) {
71
79
  const printPair = (pairPath) => {
72
80
  const parts = [pairPath.call(print, 0)];
73
81
 
@@ -109,10 +117,13 @@ function printHshPtn(path, opts, print) {
109
117
  function printIn(path, opts, print) {
110
118
  const parts = [
111
119
  "in ",
112
- path.call(
113
- (valuePath) => printPatternArg(valuePath, opts, print),
114
- "body",
115
- 0
120
+ align(
121
+ "in ".length,
122
+ path.call(
123
+ (valuePath) => printPatternArg(valuePath, opts, print),
124
+ "body",
125
+ 0
126
+ )
116
127
  ),
117
128
  indent(concat([hardline, path.call(print, "body", 1)]))
118
129
  ];
@@ -1,5 +1,5 @@
1
- const { concat } = require("../prettier");
2
- const { hasAncestor } = require("../utils");
1
+ const { concat } = require("../../prettier");
2
+ const { hasAncestor } = require("../../utils");
3
3
 
4
4
  function hasContent(node, pattern) {
5
5
  return node.body.some(
@@ -6,8 +6,8 @@ const {
6
6
  indent,
7
7
  join,
8
8
  line
9
- } = require("../prettier");
10
- const { literal } = require("../utils");
9
+ } = require("../../prettier");
10
+ const { literal } = require("../../utils");
11
11
 
12
12
  function printBegin(path, opts, print) {
13
13
  return concat([
@@ -26,28 +26,10 @@ function printEnsure(path, opts, print) {
26
26
  }
27
27
 
28
28
  function printRescue(path, opts, print) {
29
- const [exception, variable, _stmts, addition] = path.getValue().body;
30
29
  const parts = ["rescue"];
31
30
 
32
- if (exception || variable) {
33
- if (exception) {
34
- if (Array.isArray(exception)) {
35
- // In this case, it's actually only the one exception (it's an array
36
- // of length 1).
37
- parts.push(" ", path.call(print, "body", 0, 0));
38
- } else {
39
- // Here we have multiple exceptions from which we're rescuing, so we
40
- // need to align them and join them together.
41
- const joiner = concat([",", line]);
42
- const exceptions = group(join(joiner, path.call(print, "body", 0)));
43
-
44
- parts.push(" ", align("rescue ".length, exceptions));
45
- }
46
- }
47
-
48
- if (variable) {
49
- parts.push(" => ", path.call(print, "body", 1));
50
- }
31
+ if (path.getValue().body[0]) {
32
+ parts.push(align("rescue ".length, path.call(print, "body", 0)));
51
33
  } else {
52
34
  // If you don't specify an error to rescue in a `begin/rescue` block, then
53
35
  // implicitly you're rescuing from `StandardError`. In this case, we're
@@ -55,16 +37,40 @@ function printRescue(path, opts, print) {
55
37
  parts.push(" StandardError");
56
38
  }
57
39
 
58
- const rescueBody = path.call(print, "body", 2);
40
+ const bodystmt = path.call(print, "body", 1);
59
41
 
60
- if (rescueBody.parts.length > 0) {
61
- parts.push(indent(concat([hardline, rescueBody])));
42
+ if (bodystmt.parts.length > 0) {
43
+ parts.push(indent(concat([hardline, bodystmt])));
62
44
  }
63
45
 
64
46
  // This is the next clause on the `begin` statement, either another
65
47
  // `rescue`, and `ensure`, or an `else` clause.
66
- if (addition) {
67
- parts.push(concat([hardline, path.call(print, "body", 3)]));
48
+ if (path.getValue().body[2]) {
49
+ parts.push(concat([hardline, path.call(print, "body", 2)]));
50
+ }
51
+
52
+ return group(concat(parts));
53
+ }
54
+
55
+ // This is a container node that we're adding into the AST that isn't present in
56
+ // Ripper solely so that we have a nice place to attach inline comments.
57
+ function printRescueEx(path, opts, print) {
58
+ const [exception, variable] = path.getValue().body;
59
+ const parts = [];
60
+
61
+ if (exception) {
62
+ let exceptionDoc = path.call(print, "body", 0);
63
+
64
+ if (Array.isArray(exceptionDoc)) {
65
+ const joiner = concat([",", line]);
66
+ exceptionDoc = group(join(joiner, exceptionDoc));
67
+ }
68
+
69
+ parts.push(" ", exceptionDoc);
70
+ }
71
+
72
+ if (variable) {
73
+ parts.push(" => ", path.call(print, "body", 1));
68
74
  }
69
75
 
70
76
  return group(concat(parts));
@@ -89,6 +95,7 @@ module.exports = {
89
95
  ensure: printEnsure,
90
96
  redo: literal("redo"),
91
97
  rescue: printRescue,
98
+ rescue_ex: printRescueEx,
92
99
  rescue_mod: printRescueMod,
93
100
  retry: literal("retry")
94
101
  };