prettier 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +13 -0
- data/package.json +1 -1
- data/rubocop.yml +26 -0
- data/src/prettier.js +1 -0
- data/src/ruby/embed.js +4 -0
- data/src/ruby/nodes/blocks.js +64 -59
- data/src/ruby/nodes/hooks.js +9 -19
- data/src/ruby/nodes/methods.js +4 -6
- data/src/ruby/nodes/params.js +22 -14
- data/src/ruby/nodes/patterns.js +9 -5
- data/src/ruby/nodes/return.js +0 -4
- data/src/ruby/nodes/statements.js +5 -8
- data/src/ruby/nodes/strings.js +26 -35
- data/src/ruby/parser.js +2 -1
- data/src/ruby/parser.rb +194 -193
- data/src/ruby/toProc.js +4 -8
- data/src/utils/makeCall.js +3 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c6028bdf8198f8cfdd43bdb8d8b577e7a3bae5aaa108af45e0ee065d0b373b3
|
4
|
+
data.tar.gz: 90847afd8a74ba465b5a1bcc3dcfdc146ce57c01df77f1e3fa8c04a8b254c870
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d99e6fefc109919be2317805e2462c0f38d2cfc7b005df3b200ea9b29ff2ac6f4438617580df94ae58c95a0df21717d87025b9ed6301e35c62fd1b84be63f321
|
7
|
+
data.tar.gz: 97353a101ef9256d63035e8838c32435816a84f735dc9aaf40a6f62b69e699249acbb2cc51ea4075802f848368712fca11ba54d8b5203392050199b1cc1f5f13
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [1.2.4] - 2021-01-03
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
- [@andyw8] - Explain usage with Rubocop, as well as shipping a rubocop.yml config.
|
14
|
+
|
15
|
+
### Changed
|
16
|
+
|
17
|
+
- [@kddeisz] - Reduce JSON size passing from Ruby process to node process by changing `char_start` -> `sc`, `char_end` -> `ec`, `start` -> `sl`, `end` -> `el`.
|
18
|
+
- [@kddeisz] - Up the max buffer size between the Ruby and node processes to 15K.
|
19
|
+
|
9
20
|
## [1.2.3] - 2021-01-02
|
10
21
|
|
11
22
|
### Changed
|
data/README.md
CHANGED
@@ -149,6 +149,19 @@ Or, they can be passed to `prettier` as arguments:
|
|
149
149
|
prettier --ruby-single-quote false --write '**/*.rb'
|
150
150
|
```
|
151
151
|
|
152
|
+
### Usage with RuboCop
|
153
|
+
|
154
|
+
RuboCop and Prettier for Ruby serve different purposes, but there is overlap
|
155
|
+
with some of RuboCop's functionality.
|
156
|
+
|
157
|
+
Prettier provides a RuboCop configuration fle to disable the rules which clash.
|
158
|
+
To enable, add the following config at the top of your project's `.rubocop.yml`:
|
159
|
+
|
160
|
+
```yaml
|
161
|
+
inherit_gem:
|
162
|
+
prettier: rubocop.yml
|
163
|
+
```
|
164
|
+
|
152
165
|
## Contributing
|
153
166
|
|
154
167
|
Check out our [contributing guide](CONTRIBUTING.md). Bug reports and pull requests are welcome on GitHub at https://github.com/prettier/plugin-ruby.
|
data/package.json
CHANGED
data/rubocop.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Disabling all Layout/* rules, as they're unnecessary when the user is using
|
2
|
+
# prettier to handle all of the formatting.
|
3
|
+
|
4
|
+
Layout:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
# Disabling all of the following options because they could conflict with a
|
8
|
+
# prettier configuration setting.
|
9
|
+
|
10
|
+
Style/MultilineIfModifier: # rubyModifier
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/SymbolArray: # rubyArrayLiteral
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Style/WordArray: # rubyArrayLiteral
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/TrailingCommaInArguments: # trailingComma
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Style/TrailingCommaInArrayLiteral: # trailingComma
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/TrailingCommaInHashLiteral: # trailingComma
|
26
|
+
Enabled: false
|
data/src/prettier.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
// If `RBPRETTIER` is set, then this is being run from the `Prettier::run` ruby
|
2
2
|
// method. In that case, we need to pull `prettier` from the node_modules
|
3
3
|
// directly, as it's been shipped with the gem.
|
4
|
+
/* istanbul ignore next */
|
4
5
|
const source = process.env.RBPRETTIER ? "../node_modules/prettier" : "prettier";
|
5
6
|
|
6
7
|
const prettier = require(source);
|
data/src/ruby/embed.js
CHANGED
@@ -20,6 +20,10 @@ const parsers = {
|
|
20
20
|
scss: "scss"
|
21
21
|
};
|
22
22
|
|
23
|
+
// This function is in here because it handles embedded parser values. I don't
|
24
|
+
// have a test that exercises it because I'm not sure for which parser it is
|
25
|
+
// necessary, but since it's in prettier core I'm keeping it here.
|
26
|
+
/* istanbul ignore next */
|
23
27
|
const replaceNewlines = (doc) =>
|
24
28
|
mapDoc(doc, (currentDoc) =>
|
25
29
|
typeof currentDoc === "string" && currentDoc.includes("\n")
|
data/src/ruby/nodes/blocks.js
CHANGED
@@ -10,76 +10,81 @@ const {
|
|
10
10
|
} = require("../../prettier");
|
11
11
|
const { hasAncestor } = require("../../utils");
|
12
12
|
|
13
|
-
|
14
|
-
const [
|
15
|
-
const stmts =
|
16
|
-
statements.type === "stmts" ? statements.body : statements.body[0].body;
|
13
|
+
function printBlockVar(path, opts, print) {
|
14
|
+
const parts = ["|", removeLines(path.call(print, "body", 0))];
|
17
15
|
|
18
|
-
|
19
|
-
if (
|
20
|
-
|
21
|
-
stmts[0].type !== "void_stmt" ||
|
22
|
-
stmts[0].comments
|
23
|
-
) {
|
24
|
-
doBlockBody = indent(concat([softline, path.call(print, "body", 1)]));
|
16
|
+
// The second part of this node is a list of optional block-local variables
|
17
|
+
if (path.getValue().body[1]) {
|
18
|
+
parts.push("; ", join(", ", path.map(print, "body", 1)));
|
25
19
|
}
|
26
20
|
|
27
|
-
|
28
|
-
|
29
|
-
|
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"]);
|
21
|
+
parts.push("| ");
|
22
|
+
return concat(parts);
|
23
|
+
}
|
33
24
|
|
34
|
-
|
35
|
-
|
36
|
-
variables
|
37
|
-
|
38
|
-
|
39
|
-
]);
|
25
|
+
function printBlock(braces) {
|
26
|
+
return function printBlockWithBraces(path, opts, print) {
|
27
|
+
const [variables, statements] = path.getValue().body;
|
28
|
+
const stmts =
|
29
|
+
statements.type === "stmts" ? statements.body : statements.body[0].body;
|
40
30
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
}
|
31
|
+
let doBlockBody = "";
|
32
|
+
if (
|
33
|
+
stmts.length !== 1 ||
|
34
|
+
stmts[0].type !== "void_stmt" ||
|
35
|
+
stmts[0].comments
|
36
|
+
) {
|
37
|
+
doBlockBody = indent(concat([softline, path.call(print, "body", 1)]));
|
38
|
+
}
|
50
39
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
40
|
+
// If this block is nested underneath a command or command_call node, then
|
41
|
+
// we can't use `do...end` because that will get associated with the parent
|
42
|
+
// node as opposed to the current node (because of the difference in
|
43
|
+
// operator precedence). Instead, we still use a multi-line format but
|
44
|
+
// switch to using braces instead.
|
45
|
+
const useBraces = braces && hasAncestor(path, ["command", "command_call"]);
|
56
46
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
hasBody ? " " : "",
|
64
|
-
"}"
|
65
|
-
]);
|
47
|
+
const doBlock = concat([
|
48
|
+
useBraces ? " {" : " do",
|
49
|
+
variables ? concat([" ", path.call(print, "body", 0)]) : "",
|
50
|
+
doBlockBody,
|
51
|
+
concat([softline, useBraces ? "}" : "end"])
|
52
|
+
]);
|
66
53
|
|
67
|
-
|
68
|
-
|
54
|
+
// We can hit this next pattern if within the block the only statement is a
|
55
|
+
// comment.
|
56
|
+
if (
|
57
|
+
stmts.length === 1 &&
|
58
|
+
stmts[0].type === "void_stmt" &&
|
59
|
+
stmts[0].comments
|
60
|
+
) {
|
61
|
+
return concat([breakParent, doBlock]);
|
62
|
+
}
|
69
63
|
|
70
|
-
|
71
|
-
block_var: (path, opts, print) => {
|
72
|
-
const parts = ["|", removeLines(path.call(print, "body", 0))];
|
64
|
+
const blockReceiver = path.getParentNode().body[0];
|
73
65
|
|
74
|
-
//
|
75
|
-
|
76
|
-
|
66
|
+
// If the parent node is a command node, then there are no parentheses
|
67
|
+
// around the arguments to that command, so we need to break the block
|
68
|
+
if (["command", "command_call"].includes(blockReceiver.type)) {
|
69
|
+
return concat([breakParent, doBlock]);
|
77
70
|
}
|
78
71
|
|
79
|
-
|
80
|
-
|
81
|
-
|
72
|
+
const hasBody = stmts.some(({ type }) => type !== "void_stmt");
|
73
|
+
const braceBlock = concat([
|
74
|
+
" {",
|
75
|
+
hasBody || variables ? " " : "",
|
76
|
+
variables ? path.call(print, "body", 0) : "",
|
77
|
+
path.call(print, "body", 1),
|
78
|
+
hasBody ? " " : "",
|
79
|
+
"}"
|
80
|
+
]);
|
81
|
+
|
82
|
+
return group(ifBreak(doBlock, braceBlock));
|
83
|
+
};
|
84
|
+
}
|
85
|
+
|
86
|
+
module.exports = {
|
87
|
+
block_var: printBlockVar,
|
82
88
|
brace_block: printBlock(true),
|
83
|
-
do_block: printBlock(false)
|
84
|
-
excessed_comma: () => ""
|
89
|
+
do_block: printBlock(false)
|
85
90
|
};
|
data/src/ruby/nodes/hooks.js
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
const { concat, group, indent, line } = require("../../prettier");
|
2
|
-
const { isEmptyStmts } = require("../../utils");
|
3
2
|
|
4
3
|
// The `BEGIN` and `END` keywords are used to hook into the Ruby process. Any
|
5
4
|
// `BEGIN` blocks are executed right when the process starts up, and the `END`
|
@@ -17,24 +16,15 @@ const { isEmptyStmts } = require("../../utils");
|
|
17
16
|
// nodes contain one child which is a `stmts` node.
|
18
17
|
function printHook(name) {
|
19
18
|
return function printHookWithName(path, opts, print) {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
];
|
30
|
-
|
31
|
-
// If there are no statements but there are comments, then we want to skip
|
32
|
-
// printing the newline so that we don't end up with multiple spaces.
|
33
|
-
if (isEmptyStmts(stmtsNode) && stmtsNode.comments) {
|
34
|
-
parts[1] = indent(printedStmts);
|
35
|
-
}
|
36
|
-
|
37
|
-
return group(concat(parts));
|
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
|
+
);
|
38
28
|
};
|
39
29
|
}
|
40
30
|
|
data/src/ruby/nodes/methods.js
CHANGED
@@ -47,15 +47,13 @@ function printMethod(offset) {
|
|
47
47
|
}
|
48
48
|
|
49
49
|
function printSingleLineMethod(path, opts, print) {
|
50
|
-
let
|
50
|
+
let parensNode = path.getValue().body[1];
|
51
51
|
let paramsDoc = "";
|
52
52
|
|
53
|
-
if (
|
54
|
-
|
55
|
-
paramsNode = paramsNode.body[0];
|
56
|
-
}
|
53
|
+
if (parensNode) {
|
54
|
+
const paramsNode = parensNode.body[0];
|
57
55
|
|
58
|
-
if (paramsNode.
|
56
|
+
if (paramsNode.body.some((type) => type)) {
|
59
57
|
paramsDoc = path.call(print, "body", 1);
|
60
58
|
}
|
61
59
|
}
|
data/src/ruby/nodes/params.js
CHANGED
@@ -29,18 +29,22 @@ function printParams(path, opts, print) {
|
|
29
29
|
let parts = [];
|
30
30
|
|
31
31
|
if (reqs) {
|
32
|
-
|
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
|
+
);
|
33
43
|
}
|
34
44
|
|
35
45
|
if (optls) {
|
36
46
|
parts = parts.concat(
|
37
|
-
|
38
|
-
concat([
|
39
|
-
path.call(print, "body", 1, index, 0),
|
40
|
-
" = ",
|
41
|
-
path.call(print, "body", 1, index, 1)
|
42
|
-
])
|
43
|
-
)
|
47
|
+
path.map((optlPath) => join(" = ", optlPath.map(print)), "body", 1)
|
44
48
|
);
|
45
49
|
}
|
46
50
|
|
@@ -54,12 +58,16 @@ function printParams(path, opts, print) {
|
|
54
58
|
|
55
59
|
if (kwargs) {
|
56
60
|
parts = parts.concat(
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
+
)
|
63
71
|
);
|
64
72
|
}
|
65
73
|
|
data/src/ruby/nodes/patterns.js
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
const {
|
2
|
+
align,
|
2
3
|
concat,
|
3
4
|
group,
|
4
5
|
hardline,
|
@@ -74,7 +75,7 @@ function printHshPtn(path, opts, print) {
|
|
74
75
|
const [constant, keyValuePairs, keyValueRest] = path.getValue().body;
|
75
76
|
let args = [];
|
76
77
|
|
77
|
-
if (keyValuePairs) {
|
78
|
+
if (keyValuePairs.length > 0) {
|
78
79
|
const printPair = (pairPath) => {
|
79
80
|
const parts = [pairPath.call(print, 0)];
|
80
81
|
|
@@ -116,10 +117,13 @@ function printHshPtn(path, opts, print) {
|
|
116
117
|
function printIn(path, opts, print) {
|
117
118
|
const parts = [
|
118
119
|
"in ",
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
120
|
+
align(
|
121
|
+
"in ".length,
|
122
|
+
path.call(
|
123
|
+
(valuePath) => printPatternArg(valuePath, opts, print),
|
124
|
+
"body",
|
125
|
+
0
|
126
|
+
)
|
123
127
|
),
|
124
128
|
indent(concat([hardline, path.call(print, "body", 1)]))
|
125
129
|
];
|
data/src/ruby/nodes/return.js
CHANGED
@@ -41,10 +41,6 @@ const printReturn = (path, opts, print) => {
|
|
41
41
|
let args = path.getValue().body[0].body[0];
|
42
42
|
let steps = ["body", 0, "body", 0];
|
43
43
|
|
44
|
-
if (!args) {
|
45
|
-
return "return";
|
46
|
-
}
|
47
|
-
|
48
44
|
if (args.body.length === 1) {
|
49
45
|
// If the body of the return contains parens, then just skip directly to the
|
50
46
|
// content of the parens so that we can skip printing parens if we don't
|
@@ -30,6 +30,7 @@ function printBodyStmt(path, opts, print) {
|
|
30
30
|
|
31
31
|
if (elseClause) {
|
32
32
|
// Before Ruby 2.6, this piece of bodystmt was an explicit "else" node
|
33
|
+
/* istanbul ignore next */
|
33
34
|
const stmts =
|
34
35
|
elseClause.type === "else"
|
35
36
|
? path.call(print, "body", 2, "body", 0)
|
@@ -69,11 +70,7 @@ function printParen(path, opts, print) {
|
|
69
70
|
}
|
70
71
|
|
71
72
|
return group(
|
72
|
-
concat([
|
73
|
-
"(",
|
74
|
-
indent(concat([softline, contentDoc])),
|
75
|
-
concat([softline, ")"])
|
76
|
-
])
|
73
|
+
concat(["(", indent(concat([softline, contentDoc])), softline, ")"])
|
77
74
|
);
|
78
75
|
}
|
79
76
|
|
@@ -123,12 +120,12 @@ module.exports = {
|
|
123
120
|
if (lineNo === null) {
|
124
121
|
parts.push(printed);
|
125
122
|
} else if (
|
126
|
-
stmt.
|
123
|
+
stmt.sl - lineNo > 1 ||
|
127
124
|
[stmt.type, stmts[index - 1].type].includes("access_ctrl")
|
128
125
|
) {
|
129
126
|
parts.push(hardline, hardline, printed);
|
130
127
|
} else if (
|
131
|
-
stmt.
|
128
|
+
stmt.sl !== lineNo ||
|
132
129
|
path.getParentNode().type !== "string_embexpr"
|
133
130
|
) {
|
134
131
|
parts.push(hardline, printed);
|
@@ -136,7 +133,7 @@ module.exports = {
|
|
136
133
|
parts.push("; ", printed);
|
137
134
|
}
|
138
135
|
|
139
|
-
lineNo = stmt.
|
136
|
+
lineNo = stmt.el;
|
140
137
|
});
|
141
138
|
|
142
139
|
return concat(parts);
|