prettier 1.1.0 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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,10 @@
1
+ const noIndent = [
2
+ "array",
3
+ "hash",
4
+ "heredoc",
5
+ "if",
6
+ "method_add_block",
7
+ "xstring_literal"
8
+ ];
9
+
10
+ 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.1.0
4
+ version: 1.2.4
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-20 00:00:00.000000000 Z
11
+ date: 2021-01-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -30,50 +30,58 @@ 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/plugin.js
68
35
  - src/prettier.js
69
- - src/printer.js
70
- - src/ruby.js
71
- - src/toProc.js
36
+ - src/ruby/embed.js
37
+ - src/ruby/nodes.js
38
+ - src/ruby/nodes/alias.js
39
+ - src/ruby/nodes/aref.js
40
+ - src/ruby/nodes/args.js
41
+ - src/ruby/nodes/arrays.js
42
+ - src/ruby/nodes/assign.js
43
+ - src/ruby/nodes/blocks.js
44
+ - src/ruby/nodes/calls.js
45
+ - src/ruby/nodes/case.js
46
+ - src/ruby/nodes/class.js
47
+ - src/ruby/nodes/commands.js
48
+ - src/ruby/nodes/conditionals.js
49
+ - src/ruby/nodes/constants.js
50
+ - src/ruby/nodes/flow.js
51
+ - src/ruby/nodes/hashes.js
52
+ - src/ruby/nodes/heredocs.js
53
+ - src/ruby/nodes/hooks.js
54
+ - src/ruby/nodes/ints.js
55
+ - src/ruby/nodes/lambdas.js
56
+ - src/ruby/nodes/loops.js
57
+ - src/ruby/nodes/massign.js
58
+ - src/ruby/nodes/methods.js
59
+ - src/ruby/nodes/operators.js
60
+ - src/ruby/nodes/params.js
61
+ - src/ruby/nodes/patterns.js
62
+ - src/ruby/nodes/regexp.js
63
+ - src/ruby/nodes/rescue.js
64
+ - src/ruby/nodes/return.js
65
+ - src/ruby/nodes/statements.js
66
+ - src/ruby/nodes/strings.js
67
+ - src/ruby/nodes/super.js
68
+ - src/ruby/nodes/undef.js
69
+ - src/ruby/parser.js
70
+ - src/ruby/parser.rb
71
+ - src/ruby/printer.js
72
+ - src/ruby/toProc.js
72
73
  - src/utils.js
74
+ - src/utils/containsAssignment.js
75
+ - src/utils/getTrailingComma.js
76
+ - src/utils/hasAncestor.js
73
77
  - src/utils/inlineEnsureParens.js
74
78
  - src/utils/isEmptyStmts.js
79
+ - src/utils/literal.js
75
80
  - src/utils/literalLineNoBreak.js
81
+ - src/utils/makeCall.js
82
+ - src/utils/noIndent.js
76
83
  - src/utils/printEmptyCollection.js
84
+ - src/utils/skipAssignIndent.js
77
85
  homepage: https://github.com/prettier/plugin-ruby#readme
78
86
  licenses:
79
87
  - MIT
@@ -93,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
101
  - !ruby/object:Gem::Version
94
102
  version: '0'
95
103
  requirements: []
96
- rubygems_version: 3.1.4
104
+ rubygems_version: 3.2.3
97
105
  signing_key:
98
106
  specification_version: 4
99
107
  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
- };