prettier 1.0.1 → 1.2.3

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 (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +65 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +6 -1
  5. data/lib/prettier.rb +2 -2
  6. data/node_modules/prettier/index.js +54 -54
  7. data/package.json +4 -2
  8. data/src/{ruby.js → plugin.js} +2 -2
  9. data/src/{embed.js → ruby/embed.js} +2 -2
  10. data/src/{nodes.js → ruby/nodes.js} +0 -0
  11. data/src/{nodes → ruby/nodes}/alias.js +1 -1
  12. data/src/{nodes → ruby/nodes}/aref.js +8 -1
  13. data/src/{nodes → ruby/nodes}/args.js +2 -2
  14. data/src/{nodes → ruby/nodes}/arrays.js +2 -3
  15. data/src/{nodes → ruby/nodes}/assign.js +12 -4
  16. data/src/{nodes → ruby/nodes}/blocks.js +3 -3
  17. data/src/{nodes → ruby/nodes}/calls.js +54 -11
  18. data/src/ruby/nodes/case.js +65 -0
  19. data/src/{nodes → ruby/nodes}/class.js +1 -1
  20. data/src/ruby/nodes/commands.js +126 -0
  21. data/src/{nodes → ruby/nodes}/conditionals.js +3 -3
  22. data/src/{nodes → ruby/nodes}/constants.js +2 -2
  23. data/src/{nodes → ruby/nodes}/flow.js +2 -2
  24. data/src/{nodes → ruby/nodes}/hashes.js +32 -10
  25. data/src/{nodes → ruby/nodes}/heredocs.js +2 -2
  26. data/src/{nodes → ruby/nodes}/hooks.js +2 -2
  27. data/src/{nodes → ruby/nodes}/ints.js +0 -0
  28. data/src/{nodes → ruby/nodes}/lambdas.js +2 -2
  29. data/src/ruby/nodes/loops.js +104 -0
  30. data/src/{nodes → ruby/nodes}/massign.js +8 -1
  31. data/src/{nodes → ruby/nodes}/methods.js +34 -6
  32. data/src/{nodes → ruby/nodes}/operators.js +2 -2
  33. data/src/{nodes → ruby/nodes}/params.js +9 -2
  34. data/src/{nodes → ruby/nodes}/patterns.js +45 -10
  35. data/src/ruby/nodes/regexp.js +56 -0
  36. data/src/{nodes → ruby/nodes}/rescue.js +2 -2
  37. data/src/ruby/nodes/return.js +98 -0
  38. data/src/{nodes → ruby/nodes}/statements.js +1 -1
  39. data/src/{nodes → ruby/nodes}/strings.js +1 -1
  40. data/src/{nodes → ruby/nodes}/super.js +2 -2
  41. data/src/{nodes → ruby/nodes}/undef.js +1 -1
  42. data/src/{parser.js → ruby/parser.js} +2 -2
  43. data/src/{parser.rb → ruby/parser.rb} +323 -317
  44. data/src/{printer.js → ruby/printer.js} +46 -1
  45. data/src/{toProc.js → ruby/toProc.js} +0 -0
  46. data/src/utils.js +10 -93
  47. data/src/utils/containsAssignment.js +11 -0
  48. data/src/utils/getTrailingComma.js +5 -0
  49. data/src/utils/hasAncestor.js +17 -0
  50. data/src/utils/literal.js +7 -0
  51. data/src/utils/makeCall.js +11 -0
  52. data/src/utils/noIndent.js +10 -0
  53. data/src/utils/skipAssignIndent.js +10 -0
  54. metadata +48 -41
  55. data/src/nodes/case.js +0 -61
  56. data/src/nodes/commands.js +0 -91
  57. data/src/nodes/loops.js +0 -101
  58. data/src/nodes/regexp.js +0 -49
  59. 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");
@@ -20,6 +20,18 @@ function printNode(path, opts, print) {
20
20
  throw new Error(`Unsupported node encountered: ${type}\n${ast}`);
21
21
  }
22
22
 
23
+ // This is an escape-hatch to ignore nodes in the tree. If you have a comment
24
+ // that includes this pattern, then the entire node will be ignored and just the
25
+ // original source will be printed out.
26
+ function hasPrettierIgnore(path) {
27
+ const node = path.getValue();
28
+
29
+ return (
30
+ node.comments &&
31
+ node.comments.some((comment) => comment.value.includes("prettier-ignore"))
32
+ );
33
+ }
34
+
23
35
  const noComments = [
24
36
  "args",
25
37
  "args_add_block",
@@ -55,6 +67,38 @@ function getCommentChildNodes(node) {
55
67
 
56
68
  return [node.body[0]].concat(values).concat(node.body[2]);
57
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
+ }
58
102
  default:
59
103
  return node.body;
60
104
  }
@@ -83,6 +127,7 @@ function isBlockComment(comment) {
83
127
  module.exports = {
84
128
  embed,
85
129
  print: printNode,
130
+ hasPrettierIgnore,
86
131
  canAttachComment,
87
132
  getCommentChildNodes,
88
133
  printComment,
File without changes
@@ -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,11 @@
1
+ function makeCall(path, opts, print) {
2
+ const operation = path.getValue().body[1];
3
+
4
+ if ([".", "&."].includes(operation)) {
5
+ return operation;
6
+ }
7
+
8
+ return operation === "::" ? "." : path.call(print, "body", 1);
9
+ }
10
+
11
+ 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.0.1
4
+ version: 1.2.3
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-12 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,57 @@ 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
+ - src/plugin.js
68
34
  - src/prettier.js
69
- - src/printer.js
70
- - src/ruby.js
71
- - src/toProc.js
35
+ - src/ruby/embed.js
36
+ - src/ruby/nodes.js
37
+ - src/ruby/nodes/alias.js
38
+ - src/ruby/nodes/aref.js
39
+ - src/ruby/nodes/args.js
40
+ - src/ruby/nodes/arrays.js
41
+ - src/ruby/nodes/assign.js
42
+ - src/ruby/nodes/blocks.js
43
+ - src/ruby/nodes/calls.js
44
+ - src/ruby/nodes/case.js
45
+ - src/ruby/nodes/class.js
46
+ - src/ruby/nodes/commands.js
47
+ - src/ruby/nodes/conditionals.js
48
+ - src/ruby/nodes/constants.js
49
+ - src/ruby/nodes/flow.js
50
+ - src/ruby/nodes/hashes.js
51
+ - src/ruby/nodes/heredocs.js
52
+ - src/ruby/nodes/hooks.js
53
+ - src/ruby/nodes/ints.js
54
+ - src/ruby/nodes/lambdas.js
55
+ - src/ruby/nodes/loops.js
56
+ - src/ruby/nodes/massign.js
57
+ - src/ruby/nodes/methods.js
58
+ - src/ruby/nodes/operators.js
59
+ - src/ruby/nodes/params.js
60
+ - src/ruby/nodes/patterns.js
61
+ - src/ruby/nodes/regexp.js
62
+ - src/ruby/nodes/rescue.js
63
+ - src/ruby/nodes/return.js
64
+ - src/ruby/nodes/statements.js
65
+ - src/ruby/nodes/strings.js
66
+ - src/ruby/nodes/super.js
67
+ - src/ruby/nodes/undef.js
68
+ - src/ruby/parser.js
69
+ - src/ruby/parser.rb
70
+ - src/ruby/printer.js
71
+ - src/ruby/toProc.js
72
72
  - src/utils.js
73
+ - src/utils/containsAssignment.js
74
+ - src/utils/getTrailingComma.js
75
+ - src/utils/hasAncestor.js
73
76
  - src/utils/inlineEnsureParens.js
74
77
  - src/utils/isEmptyStmts.js
78
+ - src/utils/literal.js
75
79
  - src/utils/literalLineNoBreak.js
80
+ - src/utils/makeCall.js
81
+ - src/utils/noIndent.js
76
82
  - src/utils/printEmptyCollection.js
83
+ - src/utils/skipAssignIndent.js
77
84
  homepage: https://github.com/prettier/plugin-ruby#readme
78
85
  licenses:
79
86
  - MIT
@@ -93,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
100
  - !ruby/object:Gem::Version
94
101
  version: '0'
95
102
  requirements: []
96
- rubygems_version: 3.1.4
103
+ rubygems_version: 3.2.3
97
104
  signing_key:
98
105
  specification_version: 4
99
106
  summary: prettier plugin for the Ruby programming language
@@ -1,61 +0,0 @@
1
- const {
2
- align,
3
- concat,
4
- fill,
5
- group,
6
- hardline,
7
- indent,
8
- line
9
- } = require("../prettier");
10
-
11
- module.exports = {
12
- case: (path, opts, print) => {
13
- const statement = ["case"];
14
-
15
- // You don't need to explicitly have something to test against in a case
16
- // statement (without it it effectively becomes an if/elsif chain).
17
- if (path.getValue().body[0]) {
18
- statement.push(" ", path.call(print, "body", 0));
19
- }
20
-
21
- return concat(
22
- statement.concat([hardline, path.call(print, "body", 1), hardline, "end"])
23
- );
24
- },
25
- when: (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
- };
@@ -1,91 +0,0 @@
1
- const {
2
- align,
3
- concat,
4
- group,
5
- ifBreak,
6
- indent,
7
- join,
8
- line,
9
- softline
10
- } = require("../prettier");
11
- const { docLength, makeCall } = require("../utils");
12
-
13
- const hasDef = (node) =>
14
- node.body[1].type === "args_add_block" &&
15
- node.body[1].body[0].type === "args" &&
16
- node.body[1].body[0].body[0] &&
17
- ["def", "defs"].includes(node.body[1].body[0].body[0].type);
18
-
19
- // Very special handling case for rspec matchers. In general with rspec matchers
20
- // you expect to see something like:
21
- //
22
- // expect(foo).to receive(:bar).with(
23
- // 'one',
24
- // 'two',
25
- // 'three',
26
- // 'four',
27
- // 'five'
28
- // )
29
- //
30
- // In this case the arguments are aligned to the left side as opposed to being
31
- // aligned with the `receive` call.
32
- const skipArgsAlign = (path) =>
33
- ["to", "not_to"].includes(path.getValue().body[2].body);
34
-
35
- // If there is a ternary argument to a command and it's going to get broken
36
- // into multiple lines, then we're going to have to use parentheses around the
37
- // command in order to make sure operator precedence doesn't get messed up.
38
- const hasTernaryArg = (path) =>
39
- path.getValue().body[1].body[0].body.some((node) => node.type === "ifop");
40
-
41
- module.exports = {
42
- command: (path, opts, print) => {
43
- const command = path.call(print, "body", 0);
44
- const joinedArgs = join(concat([",", line]), path.call(print, "body", 1));
45
-
46
- const hasTernary = hasTernaryArg(path);
47
- let breakArgs;
48
-
49
- if (hasTernary) {
50
- breakArgs = indent(concat([softline, joinedArgs]));
51
- } else if (hasDef(path.getValue())) {
52
- breakArgs = joinedArgs;
53
- } else {
54
- breakArgs = align(command.length + 1, joinedArgs);
55
- }
56
-
57
- return group(
58
- ifBreak(
59
- concat([
60
- command,
61
- hasTernary ? "(" : " ",
62
- breakArgs,
63
- hasTernary ? concat([softline, ")"]) : ""
64
- ]),
65
- concat([command, " ", joinedArgs])
66
- )
67
- );
68
- },
69
- command_call: (path, opts, print) => {
70
- const parts = [
71
- path.call(print, "body", 0),
72
- makeCall(path, opts, print),
73
- path.call(print, "body", 2)
74
- ];
75
-
76
- if (!path.getValue().body[3]) {
77
- return concat(parts);
78
- }
79
-
80
- parts.push(" ");
81
-
82
- const joinedArgs = join(concat([",", line]), path.call(print, "body", 3));
83
- const breakArgs = skipArgsAlign(path)
84
- ? joinedArgs
85
- : align(docLength(concat(parts)), joinedArgs);
86
-
87
- return group(
88
- ifBreak(concat(parts.concat(breakArgs)), concat(parts.concat(joinedArgs)))
89
- );
90
- }
91
- };