prettier 1.2.1 → 1.3.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +21 -91
  5. data/lib/prettier.rb +2 -2
  6. data/node_modules/prettier/index.js +54 -54
  7. data/package.json +3 -4
  8. data/rubocop.yml +26 -0
  9. data/src/haml/embed.js +87 -0
  10. data/src/haml/nodes/comment.js +27 -0
  11. data/src/haml/nodes/doctype.js +34 -0
  12. data/src/haml/nodes/filter.js +16 -0
  13. data/src/haml/nodes/hamlComment.js +21 -0
  14. data/src/haml/nodes/plain.js +6 -0
  15. data/src/haml/nodes/root.js +8 -0
  16. data/src/haml/nodes/script.js +33 -0
  17. data/src/haml/nodes/silentScript.js +59 -0
  18. data/src/haml/nodes/tag.js +193 -0
  19. data/src/haml/parser.js +33 -0
  20. data/src/haml/parser.rb +141 -0
  21. data/src/haml/printer.js +28 -0
  22. data/src/{ruby.js → plugin.js} +25 -4
  23. data/src/prettier.js +1 -0
  24. data/src/rbs/parser.js +51 -0
  25. data/src/rbs/parser.rb +91 -0
  26. data/src/rbs/printer.js +605 -0
  27. data/src/{embed.js → ruby/embed.js} +6 -2
  28. data/src/{nodes.js → ruby/nodes.js} +0 -0
  29. data/src/{nodes → ruby/nodes}/alias.js +1 -1
  30. data/src/{nodes → ruby/nodes}/aref.js +8 -1
  31. data/src/{nodes → ruby/nodes}/args.js +2 -2
  32. data/src/{nodes → ruby/nodes}/arrays.js +2 -3
  33. data/src/{nodes → ruby/nodes}/assign.js +7 -3
  34. data/src/ruby/nodes/blocks.js +90 -0
  35. data/src/{nodes → ruby/nodes}/calls.js +18 -11
  36. data/src/{nodes → ruby/nodes}/case.js +1 -1
  37. data/src/{nodes → ruby/nodes}/class.js +1 -1
  38. data/src/ruby/nodes/commands.js +131 -0
  39. data/src/{nodes → ruby/nodes}/conditionals.js +3 -3
  40. data/src/{nodes → ruby/nodes}/constants.js +2 -2
  41. data/src/{nodes → ruby/nodes}/flow.js +2 -2
  42. data/src/{nodes → ruby/nodes}/hashes.js +32 -10
  43. data/src/{nodes → ruby/nodes}/heredocs.js +2 -2
  44. data/src/ruby/nodes/hooks.js +34 -0
  45. data/src/{nodes → ruby/nodes}/ints.js +0 -0
  46. data/src/{nodes → ruby/nodes}/lambdas.js +2 -2
  47. data/src/{nodes → ruby/nodes}/loops.js +10 -7
  48. data/src/{nodes → ruby/nodes}/massign.js +8 -1
  49. data/src/{nodes → ruby/nodes}/methods.js +10 -9
  50. data/src/{nodes → ruby/nodes}/operators.js +2 -2
  51. data/src/{nodes → ruby/nodes}/params.js +31 -16
  52. data/src/{nodes → ruby/nodes}/patterns.js +17 -6
  53. data/src/{nodes → ruby/nodes}/regexp.js +2 -2
  54. data/src/{nodes → ruby/nodes}/rescue.js +2 -2
  55. data/src/ruby/nodes/return.js +94 -0
  56. data/src/{nodes → ruby/nodes}/statements.js +6 -9
  57. data/src/{nodes → ruby/nodes}/strings.js +27 -36
  58. data/src/{nodes → ruby/nodes}/super.js +2 -2
  59. data/src/{nodes → ruby/nodes}/undef.js +1 -1
  60. data/src/{parser.js → ruby/parser.js} +4 -3
  61. data/src/{parser.rb → ruby/parser.rb} +450 -501
  62. data/src/{printer.js → ruby/printer.js} +33 -1
  63. data/src/{toProc.js → ruby/toProc.js} +4 -8
  64. data/src/utils.js +10 -93
  65. data/src/utils/containsAssignment.js +11 -0
  66. data/src/utils/getTrailingComma.js +5 -0
  67. data/src/utils/hasAncestor.js +17 -0
  68. data/src/utils/literal.js +7 -0
  69. data/src/utils/makeCall.js +14 -0
  70. data/src/utils/noIndent.js +11 -0
  71. data/src/utils/skipAssignIndent.js +10 -0
  72. metadata +65 -41
  73. data/src/nodes/blocks.js +0 -85
  74. data/src/nodes/commands.js +0 -91
  75. data/src/nodes/hooks.js +0 -44
  76. data/src/nodes/return.js +0 -72
@@ -1,4 +1,4 @@
1
- const { concat, trim } = require("./prettier");
1
+ const { concat, trim } = require("../prettier");
2
2
 
3
3
  const embed = require("./embed");
4
4
  const nodes = require("./nodes");
@@ -67,6 +67,38 @@ function getCommentChildNodes(node) {
67
67
 
68
68
  return [node.body[0]].concat(values).concat(node.body[2]);
69
69
  }
70
+ case "params": {
71
+ const [reqs, optls, rest, post, kwargs, kwargRest, block] = node.body;
72
+ let parts = reqs || [];
73
+
74
+ (optls || []).forEach((optl) => {
75
+ parts = parts.concat(optl);
76
+ });
77
+
78
+ if (rest) {
79
+ parts.push(rest);
80
+ }
81
+
82
+ parts = parts.concat(post || []);
83
+
84
+ (kwargs || []).forEach((kwarg) => {
85
+ if (kwarg[1]) {
86
+ parts = parts.concat(kwarg);
87
+ } else {
88
+ parts.push(kwarg[0]);
89
+ }
90
+ });
91
+
92
+ if (kwargRest && kwargRest !== "nil") {
93
+ parts.push(kwargRest);
94
+ }
95
+
96
+ if (block) {
97
+ parts.push(block);
98
+ }
99
+
100
+ return parts;
101
+ }
70
102
  default:
71
103
  return node.body;
72
104
  }
@@ -12,15 +12,11 @@ const isCall = (node) => ["::", "."].includes(node) || node.type === "@period";
12
12
  //
13
13
  // This works with `do` blocks as well.
14
14
  const toProc = (path, node) => {
15
- if (!node) {
16
- return null;
17
- }
18
-
19
15
  const [variables, blockContents] = node.body;
20
16
 
21
17
  // Ensure that there are variables being passed to this block.
22
18
  const params = variables && variables.body[0];
23
- if (!params || params.type !== "params") {
19
+ if (!params) {
24
20
  return null;
25
21
  }
26
22
 
@@ -39,7 +35,7 @@ const toProc = (path, node) => {
39
35
  if (blockContents.type === "bodystmt") {
40
36
  // We’re in a `do` block
41
37
  const blockStatements = blockContents.body[0];
42
- const rescueElseEnsure = blockStatements.body.slice(1);
38
+ const rescueElseEnsure = blockContents.body.slice(1);
43
39
 
44
40
  // You can’t use the to_proc shortcut if you’re rescuing
45
41
  if (rescueElseEnsure.some(Boolean)) {
@@ -84,7 +80,7 @@ const toProc = (path, node) => {
84
80
 
85
81
  if (path.getValue().type === "method_add_block") {
86
82
  assocNode = path.getParentNode();
87
- } else if (path.getValue().type === "args") {
83
+ } else {
88
84
  assocNode = path.getParentNode(2);
89
85
  }
90
86
 
@@ -97,7 +93,7 @@ const toProc = (path, node) => {
97
93
 
98
94
  if (
99
95
  key.type === "symbol_literal" &&
100
- ["if", "unless"].includes(key.body[0].body[0].body)
96
+ ["if", "unless"].includes(key.body[0].body)
101
97
  ) {
102
98
  return null;
103
99
  }
@@ -1,95 +1,12 @@
1
- const { concat } = require("./prettier");
2
- const isEmptyStmts = require("./utils/isEmptyStmts");
3
- const literalLineNoBreak = require("./utils/literalLineNoBreak");
4
- const printEmptyCollection = require("./utils/printEmptyCollection");
5
-
6
- // If the node is a type of assignment or if the node is a paren and nested
7
- // inside that paren is a node that is a type of assignment.
8
- const containsAssignment = (node) =>
9
- node &&
10
- (["assign", "massign", "opassign"].includes(node.type) ||
11
- (Array.isArray(node.body) && node.body.some(containsAssignment)));
12
-
13
- const 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
- const empty = () => "";
30
-
31
- const first = (path, opts, print) => path.call(print, "body", 0);
32
-
33
- const getTrailingComma = (opts) => ["all", "es5"].includes(opts.trailingComma);
34
-
35
- const hasAncestor = (path, types) => {
36
- let parent = 0;
37
- let parentNode = path.getParentNode();
38
-
39
- while (parentNode) {
40
- if (types.includes(parentNode.type)) {
41
- return true;
42
- }
43
-
44
- parent += 1;
45
- parentNode = path.getParentNode(parent);
46
- }
47
-
48
- return false;
49
- };
50
-
51
- const literal = (value) => () => value;
52
-
53
- const makeCall = (path, opts, print) => {
54
- const operation = path.getValue().body[1];
55
-
56
- if ([".", "&."].includes(operation)) {
57
- return operation;
58
- }
59
-
60
- return operation === "::" ? "." : path.call(print, "body", 1);
61
- };
62
-
63
- const noIndent = [
64
- "array",
65
- "hash",
66
- "heredoc",
67
- "if",
68
- "method_add_block",
69
- "xstring_literal"
70
- ];
71
-
72
- const prefix = (value) => (path, opts, print) =>
73
- concat([value, path.call(print, "body", 0)]);
74
-
75
- const skippable = ["array", "hash", "heredoc", "lambda", "regexp_literal"];
76
- const skipAssignIndent = (node) =>
77
- skippable.includes(node.type) ||
78
- (node.type === "call" && skipAssignIndent(node.body[0]));
79
-
80
1
  module.exports = {
81
- containsAssignment,
82
- docLength,
83
- empty,
84
- first,
85
- getTrailingComma,
86
- hasAncestor,
87
- isEmptyStmts,
88
- literal,
89
- literalLineNoBreak,
90
- makeCall,
91
- noIndent,
92
- prefix,
93
- printEmptyCollection,
94
- skipAssignIndent
2
+ containsAssignment: require("./utils/containsAssignment"),
3
+ getTrailingComma: require("./utils/getTrailingComma"),
4
+ isEmptyStmts: require("./utils/isEmptyStmts"),
5
+ hasAncestor: require("./utils/hasAncestor"),
6
+ literal: require("./utils/literal"),
7
+ literalLineNoBreak: require("./utils/literalLineNoBreak"),
8
+ makeCall: require("./utils/makeCall"),
9
+ noIndent: require("./utils/noIndent"),
10
+ printEmptyCollection: require("./utils/printEmptyCollection"),
11
+ skipAssignIndent: require("./utils/skipAssignIndent")
95
12
  };
@@ -0,0 +1,11 @@
1
+ // If the node is a type of assignment or if the node is a paren and nested
2
+ // inside that paren is a node that is a type of assignment.
3
+ function containsAssignment(node) {
4
+ return (
5
+ node &&
6
+ (["assign", "massign", "opassign"].includes(node.type) ||
7
+ (Array.isArray(node.body) && node.body.some(containsAssignment)))
8
+ );
9
+ }
10
+
11
+ module.exports = containsAssignment;
@@ -0,0 +1,5 @@
1
+ function getTrailingComma(opts) {
2
+ return ["all", "es5"].includes(opts.trailingComma);
3
+ }
4
+
5
+ module.exports = getTrailingComma;
@@ -0,0 +1,17 @@
1
+ function hasAncestor(path, types) {
2
+ let parent = 0;
3
+ let parentNode = path.getParentNode();
4
+
5
+ while (parentNode) {
6
+ if (types.includes(parentNode.type)) {
7
+ return true;
8
+ }
9
+
10
+ parent += 1;
11
+ parentNode = path.getParentNode(parent);
12
+ }
13
+
14
+ return false;
15
+ }
16
+
17
+ module.exports = hasAncestor;
@@ -0,0 +1,7 @@
1
+ function literal(value) {
2
+ return function printLiteral() {
3
+ return value;
4
+ };
5
+ }
6
+
7
+ module.exports = literal;
@@ -0,0 +1,14 @@
1
+ function makeCall(path, opts, print) {
2
+ const operation = path.getValue().body[1];
3
+
4
+ // Ignoring the next block for coverage information because it's only relevant
5
+ // in Ruby 2.5 and below.
6
+ /* istanbul ignore next */
7
+ if ([".", "&."].includes(operation)) {
8
+ return operation;
9
+ }
10
+
11
+ return operation === "::" ? "." : path.call(print, "body", 1);
12
+ }
13
+
14
+ module.exports = makeCall;
@@ -0,0 +1,11 @@
1
+ const noIndent = [
2
+ "array",
3
+ "hash",
4
+ "heredoc",
5
+ "if",
6
+ "method_add_block",
7
+ "unless",
8
+ "xstring_literal"
9
+ ];
10
+
11
+ module.exports = noIndent;
@@ -0,0 +1,10 @@
1
+ const skippable = ["array", "hash", "heredoc", "lambda", "regexp_literal"];
2
+
3
+ function skipAssignIndent(node) {
4
+ return (
5
+ skippable.includes(node.type) ||
6
+ (node.type === "call" && skipAssignIndent(node.body[0]))
7
+ );
8
+ }
9
+
10
+ module.exports = skipAssignIndent;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prettier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Deisz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-27 00:00:00.000000000 Z
11
+ date: 2021-01-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -30,50 +30,74 @@ files:
30
30
  - node_modules/prettier/index.js
31
31
  - node_modules/prettier/third-party.js
32
32
  - package.json
33
- - src/embed.js
34
- - src/nodes.js
35
- - src/nodes/alias.js
36
- - src/nodes/aref.js
37
- - src/nodes/args.js
38
- - src/nodes/arrays.js
39
- - src/nodes/assign.js
40
- - src/nodes/blocks.js
41
- - src/nodes/calls.js
42
- - src/nodes/case.js
43
- - src/nodes/class.js
44
- - src/nodes/commands.js
45
- - src/nodes/conditionals.js
46
- - src/nodes/constants.js
47
- - src/nodes/flow.js
48
- - src/nodes/hashes.js
49
- - src/nodes/heredocs.js
50
- - src/nodes/hooks.js
51
- - src/nodes/ints.js
52
- - src/nodes/lambdas.js
53
- - src/nodes/loops.js
54
- - src/nodes/massign.js
55
- - src/nodes/methods.js
56
- - src/nodes/operators.js
57
- - src/nodes/params.js
58
- - src/nodes/patterns.js
59
- - src/nodes/regexp.js
60
- - src/nodes/rescue.js
61
- - src/nodes/return.js
62
- - src/nodes/statements.js
63
- - src/nodes/strings.js
64
- - src/nodes/super.js
65
- - src/nodes/undef.js
66
- - src/parser.js
67
- - src/parser.rb
33
+ - rubocop.yml
34
+ - src/haml/embed.js
35
+ - src/haml/nodes/comment.js
36
+ - src/haml/nodes/doctype.js
37
+ - src/haml/nodes/filter.js
38
+ - src/haml/nodes/hamlComment.js
39
+ - src/haml/nodes/plain.js
40
+ - src/haml/nodes/root.js
41
+ - src/haml/nodes/script.js
42
+ - src/haml/nodes/silentScript.js
43
+ - src/haml/nodes/tag.js
44
+ - src/haml/parser.js
45
+ - src/haml/parser.rb
46
+ - src/haml/printer.js
47
+ - src/plugin.js
68
48
  - src/prettier.js
69
- - src/printer.js
70
- - src/ruby.js
71
- - src/toProc.js
49
+ - src/rbs/parser.js
50
+ - src/rbs/parser.rb
51
+ - src/rbs/printer.js
52
+ - src/ruby/embed.js
53
+ - src/ruby/nodes.js
54
+ - src/ruby/nodes/alias.js
55
+ - src/ruby/nodes/aref.js
56
+ - src/ruby/nodes/args.js
57
+ - src/ruby/nodes/arrays.js
58
+ - src/ruby/nodes/assign.js
59
+ - src/ruby/nodes/blocks.js
60
+ - src/ruby/nodes/calls.js
61
+ - src/ruby/nodes/case.js
62
+ - src/ruby/nodes/class.js
63
+ - src/ruby/nodes/commands.js
64
+ - src/ruby/nodes/conditionals.js
65
+ - src/ruby/nodes/constants.js
66
+ - src/ruby/nodes/flow.js
67
+ - src/ruby/nodes/hashes.js
68
+ - src/ruby/nodes/heredocs.js
69
+ - src/ruby/nodes/hooks.js
70
+ - src/ruby/nodes/ints.js
71
+ - src/ruby/nodes/lambdas.js
72
+ - src/ruby/nodes/loops.js
73
+ - src/ruby/nodes/massign.js
74
+ - src/ruby/nodes/methods.js
75
+ - src/ruby/nodes/operators.js
76
+ - src/ruby/nodes/params.js
77
+ - src/ruby/nodes/patterns.js
78
+ - src/ruby/nodes/regexp.js
79
+ - src/ruby/nodes/rescue.js
80
+ - src/ruby/nodes/return.js
81
+ - src/ruby/nodes/statements.js
82
+ - src/ruby/nodes/strings.js
83
+ - src/ruby/nodes/super.js
84
+ - src/ruby/nodes/undef.js
85
+ - src/ruby/parser.js
86
+ - src/ruby/parser.rb
87
+ - src/ruby/printer.js
88
+ - src/ruby/toProc.js
72
89
  - src/utils.js
90
+ - src/utils/containsAssignment.js
91
+ - src/utils/getTrailingComma.js
92
+ - src/utils/hasAncestor.js
73
93
  - src/utils/inlineEnsureParens.js
74
94
  - src/utils/isEmptyStmts.js
95
+ - src/utils/literal.js
75
96
  - src/utils/literalLineNoBreak.js
97
+ - src/utils/makeCall.js
98
+ - src/utils/noIndent.js
76
99
  - src/utils/printEmptyCollection.js
100
+ - src/utils/skipAssignIndent.js
77
101
  homepage: https://github.com/prettier/plugin-ruby#readme
78
102
  licenses:
79
103
  - MIT
@@ -93,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
117
  - !ruby/object:Gem::Version
94
118
  version: '0'
95
119
  requirements: []
96
- rubygems_version: 3.1.4
120
+ rubygems_version: 3.0.3
97
121
  signing_key:
98
122
  specification_version: 4
99
123
  summary: prettier plugin for the Ruby programming language
@@ -1,85 +0,0 @@
1
- const {
2
- breakParent,
3
- concat,
4
- group,
5
- ifBreak,
6
- indent,
7
- join,
8
- removeLines,
9
- softline
10
- } = require("../prettier");
11
- const { empty, hasAncestor } = require("../utils");
12
-
13
- const printBlock = (braces) => (path, opts, print) => {
14
- const [variables, statements] = path.getValue().body;
15
- const stmts =
16
- statements.type === "stmts" ? statements.body : statements.body[0].body;
17
-
18
- let doBlockBody = "";
19
- if (
20
- stmts.length !== 1 ||
21
- stmts[0].type !== "void_stmt" ||
22
- stmts[0].comments
23
- ) {
24
- doBlockBody = indent(concat([softline, path.call(print, "body", 1)]));
25
- }
26
-
27
- // If this block is nested underneath a command or command_call node, then we
28
- // can't use `do...end` because that will get associated with the parent node
29
- // as opposed to the current node (because of the difference in operator
30
- // precedence). Instead, we still use a multi-line format but switch to using
31
- // braces instead.
32
- const useBraces = braces && hasAncestor(path, ["command", "command_call"]);
33
-
34
- const doBlock = concat([
35
- useBraces ? " {" : " do",
36
- variables ? concat([" ", path.call(print, "body", 0)]) : "",
37
- doBlockBody,
38
- concat([softline, useBraces ? "}" : "end"])
39
- ]);
40
-
41
- // We can hit this next pattern if within the block the only statement is a
42
- // comment.
43
- if (
44
- stmts.length === 1 &&
45
- stmts[0].type === "void_stmt" &&
46
- stmts[0].comments
47
- ) {
48
- return concat([breakParent, doBlock]);
49
- }
50
-
51
- // If the parent node is a command node, then there are no parentheses around
52
- // the arguments to that command, so we need to break the block
53
- if (["command", "command_call"].includes(path.getParentNode().body[0].type)) {
54
- return concat([breakParent, doBlock]);
55
- }
56
-
57
- const hasBody = stmts.some(({ type }) => type !== "void_stmt");
58
- const braceBlock = concat([
59
- " {",
60
- hasBody || variables ? " " : "",
61
- variables ? path.call(print, "body", 0) : "",
62
- path.call(print, "body", 1),
63
- hasBody ? " " : "",
64
- "}"
65
- ]);
66
-
67
- return group(ifBreak(doBlock, braceBlock));
68
- };
69
-
70
- module.exports = {
71
- block_var: (path, opts, print) => {
72
- const parts = ["|", removeLines(path.call(print, "body", 0))];
73
-
74
- // The second part of this node is a list of optional block-local variables
75
- if (path.getValue().body[1]) {
76
- parts.push("; ", join(", ", path.map(print, "body", 1)));
77
- }
78
-
79
- parts.push("| ");
80
- return concat(parts);
81
- },
82
- brace_block: printBlock(true),
83
- do_block: printBlock(false),
84
- excessed_comma: empty
85
- };