prettier 1.6.1 → 2.0.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +316 -293
- data/CONTRIBUTING.md +6 -9
- data/LICENSE +1 -1
- data/README.md +11 -12
- data/dist/haml/embed.js +53 -0
- data/dist/haml/parser.js +31 -0
- data/{src → dist}/haml/parser.rb +0 -0
- data/dist/haml/printer.js +336 -0
- data/dist/parser/getInfo.js +17 -0
- data/{src → dist}/parser/netcat.js +1 -0
- data/dist/parser/parseSync.js +128 -0
- data/dist/parser/server.rb +140 -0
- data/dist/plugin.js +143 -0
- data/dist/prettier.js +15 -0
- data/dist/rbs/parser.js +34 -0
- data/{src → dist}/rbs/parser.rb +0 -0
- data/dist/rbs/printer.js +517 -0
- data/dist/ruby/embed.js +110 -0
- data/dist/ruby/nodes/alias.js +59 -0
- data/{src → dist}/ruby/nodes/aref.js +26 -35
- data/dist/ruby/nodes/args.js +165 -0
- data/dist/ruby/nodes/arrays.js +126 -0
- data/dist/ruby/nodes/assign.js +41 -0
- data/dist/ruby/nodes/blocks.js +68 -0
- data/dist/ruby/nodes/calls.js +220 -0
- data/dist/ruby/nodes/case.js +50 -0
- data/dist/ruby/nodes/class.js +54 -0
- data/dist/ruby/nodes/commands.js +124 -0
- data/dist/ruby/nodes/conditionals.js +242 -0
- data/dist/ruby/nodes/constants.js +38 -0
- data/dist/ruby/nodes/flow.js +66 -0
- data/dist/ruby/nodes/hashes.js +130 -0
- data/dist/ruby/nodes/heredocs.js +30 -0
- data/dist/ruby/nodes/hooks.js +35 -0
- data/dist/ruby/nodes/ints.js +27 -0
- data/dist/ruby/nodes/lambdas.js +69 -0
- data/dist/ruby/nodes/loops.js +73 -0
- data/dist/ruby/nodes/massign.js +73 -0
- data/dist/ruby/nodes/methods.js +70 -0
- data/dist/ruby/nodes/operators.js +70 -0
- data/dist/ruby/nodes/params.js +89 -0
- data/dist/ruby/nodes/patterns.js +109 -0
- data/dist/ruby/nodes/regexp.js +45 -0
- data/dist/ruby/nodes/rescue.js +82 -0
- data/dist/ruby/nodes/return.js +75 -0
- data/dist/ruby/nodes/statements.js +111 -0
- data/dist/ruby/nodes/strings.js +218 -0
- data/dist/ruby/nodes/super.js +30 -0
- data/dist/ruby/nodes/undef.js +26 -0
- data/dist/ruby/nodes.js +151 -0
- data/dist/ruby/parser.js +34 -0
- data/{src → dist}/ruby/parser.rb +1215 -252
- data/dist/ruby/printer.js +125 -0
- data/dist/ruby/toProc.js +93 -0
- data/dist/types/haml.js +4 -0
- data/dist/types/plugin.js +3 -0
- data/dist/types/rbs.js +4 -0
- data/dist/types/ruby.js +4 -0
- data/dist/types/utils.js +2 -0
- data/dist/types.js +30 -0
- data/dist/utils/containsAssignment.js +15 -0
- data/dist/utils/getTrailingComma.js +6 -0
- data/dist/utils/hasAncestor.js +15 -0
- data/{src → dist}/utils/inlineEnsureParens.js +16 -17
- data/dist/utils/isEmptyBodyStmt.js +10 -0
- data/dist/utils/isEmptyStmts.js +10 -0
- data/dist/utils/literal.js +8 -0
- data/dist/utils/literallineWithoutBreakParent.js +8 -0
- data/dist/utils/makeCall.js +13 -0
- data/dist/utils/noIndent.js +11 -0
- data/dist/utils/printEmptyCollection.js +44 -0
- data/dist/utils/skipAssignIndent.js +15 -0
- data/dist/utils.js +30 -0
- data/node_modules/prettier/bin-prettier.js +313 -190
- data/node_modules/prettier/doc.js +191 -323
- data/node_modules/prettier/index.js +2753 -3677
- data/node_modules/prettier/package.json +1 -1
- data/node_modules/prettier/parser-angular.js +13 -14
- data/node_modules/prettier/parser-babel.js +7 -7
- data/node_modules/prettier/parser-espree.js +7 -7
- data/node_modules/prettier/parser-flow.js +7 -7
- data/node_modules/prettier/parser-glimmer.js +1 -1
- data/node_modules/prettier/parser-graphql.js +1 -1
- data/node_modules/prettier/parser-html.js +17 -17
- data/node_modules/prettier/parser-markdown.js +9 -9
- data/node_modules/prettier/parser-meriyah.js +7 -7
- data/node_modules/prettier/parser-postcss.js +2 -2
- data/node_modules/prettier/parser-typescript.js +7 -7
- data/node_modules/prettier/parser-yaml.js +2 -2
- data/node_modules/prettier/third-party.js +143 -78
- data/package.json +26 -18
- metadata +74 -67
- data/src/haml/embed.js +0 -87
- data/src/haml/parser.js +0 -23
- data/src/haml/printer.js +0 -438
- data/src/parser/parseSync.js +0 -172
- data/src/parser/server.rb +0 -66
- data/src/plugin.js +0 -148
- data/src/prettier.js +0 -16
- data/src/rbs/parser.js +0 -37
- data/src/rbs/printer.js +0 -643
- data/src/ruby/embed.js +0 -142
- data/src/ruby/nodes/alias.js +0 -73
- data/src/ruby/nodes/args.js +0 -222
- data/src/ruby/nodes/arrays.js +0 -162
- data/src/ruby/nodes/assign.js +0 -47
- data/src/ruby/nodes/blocks.js +0 -90
- data/src/ruby/nodes/calls.js +0 -246
- data/src/ruby/nodes/case.js +0 -65
- data/src/ruby/nodes/class.js +0 -64
- data/src/ruby/nodes/commands.js +0 -131
- data/src/ruby/nodes/conditionals.js +0 -282
- data/src/ruby/nodes/constants.js +0 -43
- data/src/ruby/nodes/flow.js +0 -74
- data/src/ruby/nodes/hashes.js +0 -155
- data/src/ruby/nodes/heredocs.js +0 -36
- data/src/ruby/nodes/hooks.js +0 -34
- data/src/ruby/nodes/ints.js +0 -31
- data/src/ruby/nodes/lambdas.js +0 -76
- data/src/ruby/nodes/loops.js +0 -98
- data/src/ruby/nodes/massign.js +0 -98
- data/src/ruby/nodes/methods.js +0 -74
- data/src/ruby/nodes/operators.js +0 -83
- data/src/ruby/nodes/params.js +0 -106
- data/src/ruby/nodes/patterns.js +0 -157
- data/src/ruby/nodes/regexp.js +0 -56
- data/src/ruby/nodes/rescue.js +0 -101
- data/src/ruby/nodes/return.js +0 -94
- data/src/ruby/nodes/statements.js +0 -142
- data/src/ruby/nodes/strings.js +0 -272
- data/src/ruby/nodes/super.js +0 -35
- data/src/ruby/nodes/undef.js +0 -42
- data/src/ruby/nodes.js +0 -34
- data/src/ruby/parser.js +0 -37
- data/src/ruby/printer.js +0 -147
- data/src/ruby/toProc.js +0 -105
- data/src/utils/containsAssignment.js +0 -11
- data/src/utils/getTrailingComma.js +0 -5
- data/src/utils/hasAncestor.js +0 -17
- data/src/utils/isEmptyBodyStmt.js +0 -7
- data/src/utils/isEmptyStmts.js +0 -11
- data/src/utils/literal.js +0 -7
- data/src/utils/literallineWithoutBreakParent.js +0 -7
- data/src/utils/makeCall.js +0 -14
- data/src/utils/noIndent.js +0 -10
- data/src/utils/printEmptyCollection.js +0 -49
- data/src/utils/skipAssignIndent.js +0 -17
- data/src/utils.js +0 -13
@@ -1,12 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
} =
|
9
|
-
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printArefField = exports.printAref = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const { group, indent, join, line, softline } = prettier_1.default;
|
10
9
|
// `aref` nodes are when you're pulling a value out of a collection at a
|
11
10
|
// specific index. Put another way, it's any time you're calling the method
|
12
11
|
// `#[]`.
|
@@ -19,16 +18,14 @@ const {
|
|
19
18
|
//
|
20
19
|
// foo[]
|
21
20
|
//
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
}
|
31
|
-
|
21
|
+
const printAref = (path, opts, print) => {
|
22
|
+
const indexNode = path.getValue().body[1];
|
23
|
+
if (!indexNode) {
|
24
|
+
return [path.call(print, "body", 0), "[]"];
|
25
|
+
}
|
26
|
+
return (0, exports.printArefField)(path, opts, print);
|
27
|
+
};
|
28
|
+
exports.printAref = printAref;
|
32
29
|
// `aref_field` nodes are for assigning values into collections at specific
|
33
30
|
// indices. Put another way, it's any time you're calling the method `#[]=`.
|
34
31
|
// The `aref_field` node itself is just the left side of the assignment, and
|
@@ -43,20 +40,14 @@ function printAref(path, opts, print) {
|
|
43
40
|
//
|
44
41
|
// foo[bar] = baz
|
45
42
|
//
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
])
|
56
|
-
);
|
57
|
-
}
|
58
|
-
|
59
|
-
module.exports = {
|
60
|
-
aref: printAref,
|
61
|
-
aref_field: printArefField
|
43
|
+
const printArefField = (path, opts, print) => {
|
44
|
+
const [printedArray, printedIndex] = path.map(print, "body");
|
45
|
+
return group([
|
46
|
+
printedArray,
|
47
|
+
"[",
|
48
|
+
indent([softline, join([",", line], printedIndex)]),
|
49
|
+
softline,
|
50
|
+
"]"
|
51
|
+
]);
|
62
52
|
};
|
53
|
+
exports.printArefField = printArefField;
|
@@ -0,0 +1,165 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printBlockArg = exports.printArgsAddStar = exports.printArgsAddBlock = exports.printArgs = exports.printArgParen = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const toProc_1 = __importDefault(require("../toProc"));
|
10
|
+
const { group, ifBreak, indent, join, line, softline } = prettier_1.default;
|
11
|
+
const noTrailingComma = ["command", "command_call"];
|
12
|
+
function getArgParenTrailingComma(node) {
|
13
|
+
// If we have a block, then we don't want to add a trailing comma.
|
14
|
+
if (node.type === "args_add_block" && node.body[1]) {
|
15
|
+
return "";
|
16
|
+
}
|
17
|
+
// If we only have one argument and that first argument necessitates that we
|
18
|
+
// skip putting a comma (because it would interfere with parsing the argument)
|
19
|
+
// then we don't want to add a trailing comma.
|
20
|
+
if (node.body.length === 1 && noTrailingComma.includes(node.body[0].type)) {
|
21
|
+
return "";
|
22
|
+
}
|
23
|
+
return ifBreak(",", "");
|
24
|
+
}
|
25
|
+
const printArgParen = (path, opts, print) => {
|
26
|
+
const argsNode = path.getValue().body[0];
|
27
|
+
if (argsNode === null) {
|
28
|
+
return "";
|
29
|
+
}
|
30
|
+
// Here we can skip the entire rest of the method by just checking if it's
|
31
|
+
// an args_forward node, as we're guaranteed that there are no other arg
|
32
|
+
// nodes.
|
33
|
+
if (argsNode.type === "args_forward") {
|
34
|
+
return group([
|
35
|
+
"(",
|
36
|
+
indent([softline, path.call(print, "body", 0)]),
|
37
|
+
softline,
|
38
|
+
")"
|
39
|
+
]);
|
40
|
+
}
|
41
|
+
// Now here we return a doc that represents the whole grouped expression,
|
42
|
+
// including the surrouding parentheses.
|
43
|
+
return group([
|
44
|
+
"(",
|
45
|
+
indent([
|
46
|
+
softline,
|
47
|
+
join([",", line], path.call(print, "body", 0)),
|
48
|
+
(0, utils_1.getTrailingComma)(opts) ? getArgParenTrailingComma(argsNode) : ""
|
49
|
+
]),
|
50
|
+
softline,
|
51
|
+
")"
|
52
|
+
]);
|
53
|
+
};
|
54
|
+
exports.printArgParen = printArgParen;
|
55
|
+
const printArgs = (path, { rubyToProc }, print) => {
|
56
|
+
const args = path.map(print, "body");
|
57
|
+
// Don't bother trying to do any kind of fancy toProc transform if the
|
58
|
+
// option is disabled.
|
59
|
+
if (rubyToProc) {
|
60
|
+
let blockNode = null;
|
61
|
+
// Look up the chain to see if these arguments are contained within a
|
62
|
+
// method_add_block node, and if they are that that node has a block
|
63
|
+
// associated with it. If it does, we're going to attempt to transform it
|
64
|
+
// into the to_proc shorthand and add it to the list of arguments.
|
65
|
+
[1, 2, 3].find((parent) => {
|
66
|
+
const parentNode = path.getParentNode(parent);
|
67
|
+
blockNode =
|
68
|
+
parentNode &&
|
69
|
+
parentNode.type === "method_add_block" &&
|
70
|
+
parentNode.body[1];
|
71
|
+
return blockNode;
|
72
|
+
});
|
73
|
+
const proc = blockNode && (0, toProc_1.default)(path, blockNode);
|
74
|
+
// If we have a successful to_proc transformation, but we're part of an
|
75
|
+
// aref node, that means it's something to the effect of
|
76
|
+
//
|
77
|
+
// foo[:bar].each(&:to_s)
|
78
|
+
//
|
79
|
+
// In this case we need to just return regular arguments, otherwise we
|
80
|
+
// would end up putting &:to_s inside the brackets accidentally.
|
81
|
+
if (proc && path.getParentNode(1).type !== "aref") {
|
82
|
+
args.push(proc);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
return args;
|
86
|
+
};
|
87
|
+
exports.printArgs = printArgs;
|
88
|
+
const printArgsAddBlock = (path, opts, print) => {
|
89
|
+
const node = path.getValue();
|
90
|
+
const blockNode = node.body[1];
|
91
|
+
const parts = path.call(print, "body", 0);
|
92
|
+
if (blockNode) {
|
93
|
+
let blockDoc = path.call(print, "body", 1);
|
94
|
+
if (!(blockNode.comments || []).some(({ leading }) => leading)) {
|
95
|
+
// If we don't have any leading comments, we can just prepend the
|
96
|
+
// operator.
|
97
|
+
blockDoc = ["&", blockDoc];
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
// If we have a method call like:
|
101
|
+
//
|
102
|
+
// foo(
|
103
|
+
// # comment
|
104
|
+
// &block
|
105
|
+
// )
|
106
|
+
//
|
107
|
+
// then we need to make sure we don't accidentally prepend the operator
|
108
|
+
// before the comment.
|
109
|
+
//
|
110
|
+
// In prettier >= 2.3.0, the comments are printed as an array before the
|
111
|
+
// content. I don't love this kind of reflection, but it's the simplest
|
112
|
+
// way at the moment to get this right.
|
113
|
+
blockDoc = blockDoc[0].concat(["&", blockDoc[1]], blockDoc.slice(2));
|
114
|
+
}
|
115
|
+
parts.push(blockDoc);
|
116
|
+
}
|
117
|
+
return parts;
|
118
|
+
};
|
119
|
+
exports.printArgsAddBlock = printArgsAddBlock;
|
120
|
+
const printArgsAddStar = (path, opts, print) => {
|
121
|
+
let docs = [];
|
122
|
+
path.each((argPath, argIndex) => {
|
123
|
+
const doc = print(argPath);
|
124
|
+
// If it's the first child, then it's an array of args, so we're going to
|
125
|
+
// concat that onto the existing docs if there are any.
|
126
|
+
if (argIndex === 0) {
|
127
|
+
if (doc.length > 0) {
|
128
|
+
docs = docs.concat(doc);
|
129
|
+
}
|
130
|
+
return;
|
131
|
+
}
|
132
|
+
// If it's after the splat, then it's an individual arg, so we're just going
|
133
|
+
// to push it onto the array.
|
134
|
+
if (argIndex !== 1) {
|
135
|
+
docs.push(doc);
|
136
|
+
return;
|
137
|
+
}
|
138
|
+
// If we don't have any leading comments, we can just prepend the operator.
|
139
|
+
const argsNode = argPath.getValue();
|
140
|
+
if (!(argsNode.comments || []).some(({ leading }) => leading)) {
|
141
|
+
docs.push(["*", doc]);
|
142
|
+
return;
|
143
|
+
}
|
144
|
+
// If we have an array like:
|
145
|
+
//
|
146
|
+
// [
|
147
|
+
// # comment
|
148
|
+
// *values
|
149
|
+
// ]
|
150
|
+
//
|
151
|
+
// then we need to make sure we don't accidentally prepend the operator
|
152
|
+
// before the comment(s).
|
153
|
+
//
|
154
|
+
// In prettier >= 2.3.0, the comments are printed as an array before the
|
155
|
+
// content. I don't love this kind of reflection, but it's the simplest way
|
156
|
+
// at the moment to get this right.
|
157
|
+
docs.push(doc[0].concat(["*", doc[1]], doc.slice(2)));
|
158
|
+
}, "body");
|
159
|
+
return docs;
|
160
|
+
};
|
161
|
+
exports.printArgsAddStar = printArgsAddStar;
|
162
|
+
const printBlockArg = (path, opts, print) => {
|
163
|
+
return ["&", path.call(print, "body", 0)];
|
164
|
+
};
|
165
|
+
exports.printBlockArg = printBlockArg;
|
@@ -0,0 +1,126 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printArray = exports.printWord = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { group, ifBreak, indent, join, line, softline } = prettier_1.default;
|
10
|
+
// Checks that every argument within this args node is a string_literal node
|
11
|
+
// that has no spaces or interpolations. This means we're dealing with an array
|
12
|
+
// that looks something like:
|
13
|
+
//
|
14
|
+
// ['a', 'b', 'c']
|
15
|
+
//
|
16
|
+
function isStringArray(args) {
|
17
|
+
return (args.body.length > 1 &&
|
18
|
+
args.body.every((arg) => {
|
19
|
+
// We want to verify that every node inside of this array is a string
|
20
|
+
// literal. We also want to make sure none of them have comments attached.
|
21
|
+
if (arg.type !== "string_literal" || arg.comments) {
|
22
|
+
return false;
|
23
|
+
}
|
24
|
+
// If the string has multiple parts (meaning plain string content but also
|
25
|
+
// interpolated content) then we know it's not a simple string.
|
26
|
+
if (arg.body.length !== 1) {
|
27
|
+
return false;
|
28
|
+
}
|
29
|
+
const part = arg.body[0];
|
30
|
+
// If the only part of this string is not @tstring_content then it's
|
31
|
+
// interpolated, so again we can return false.
|
32
|
+
if (part.type !== "@tstring_content") {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
// Finally, verify that the string doesn't contain a space, an escape
|
36
|
+
// character, or brackets so that we know it can be put into a string
|
37
|
+
// literal array.
|
38
|
+
return !/[\s\\[\]]/.test(part.body);
|
39
|
+
}));
|
40
|
+
}
|
41
|
+
// Checks that every argument within this args node is a symbol_literal node (as
|
42
|
+
// opposed to a dyna_symbol) so it has no interpolation. This means we're
|
43
|
+
// dealing with an array that looks something like:
|
44
|
+
//
|
45
|
+
// [:a, :b, :c]
|
46
|
+
//
|
47
|
+
function isSymbolArray(args) {
|
48
|
+
return (args.body.length > 1 &&
|
49
|
+
args.body.every((arg) => arg.type === "symbol_literal" && !arg.comments));
|
50
|
+
}
|
51
|
+
// Prints out a word that is a part of a special array literal that accepts
|
52
|
+
// interpolation. The body is an array of either plain strings or interpolated
|
53
|
+
// expressions.
|
54
|
+
const printWord = (path, opts, print) => {
|
55
|
+
return path.map(print, "body");
|
56
|
+
};
|
57
|
+
exports.printWord = printWord;
|
58
|
+
// Prints out a special array literal. Accepts the parts of the array literal as
|
59
|
+
// an argument, where the first element of the parts array is a string that
|
60
|
+
// contains the special start.
|
61
|
+
function printArrayLiteral(start, parts) {
|
62
|
+
return group([
|
63
|
+
start,
|
64
|
+
"[",
|
65
|
+
indent([softline, join(line, parts)]),
|
66
|
+
softline,
|
67
|
+
"]"
|
68
|
+
]);
|
69
|
+
}
|
70
|
+
const arrayLiteralStarts = {
|
71
|
+
qsymbols: "%i",
|
72
|
+
qwords: "%w",
|
73
|
+
symbols: "%I",
|
74
|
+
words: "%W"
|
75
|
+
};
|
76
|
+
// An array node is any literal array in Ruby. This includes all of the special
|
77
|
+
// array literals as well as regular arrays. If it is a special array literal
|
78
|
+
// then it will have one child that represents the special array, otherwise it
|
79
|
+
// will have one child that contains all of the elements of the array.
|
80
|
+
const printArray = (path, opts, print) => {
|
81
|
+
const array = path.getValue();
|
82
|
+
const args = array.body[0];
|
83
|
+
// If there is no inner arguments node, then we're dealing with an empty
|
84
|
+
// array, so we can go ahead and return.
|
85
|
+
if (args === null) {
|
86
|
+
return (0, utils_1.printEmptyCollection)(path, opts, "[", "]");
|
87
|
+
}
|
88
|
+
// If we don't have a regular args node at this point then we have a special
|
89
|
+
// array literal. In that case we're going to print out the body (which will
|
90
|
+
// return to us an array with the first one being the start of the array) and
|
91
|
+
// send that over to the printArrayLiteral function.
|
92
|
+
if (args.type !== "args" && args.type !== "args_add_star") {
|
93
|
+
return path.call((arrayPath) => printArrayLiteral(arrayLiteralStarts[args.type], arrayPath.map(print, "body")), "body", 0);
|
94
|
+
}
|
95
|
+
if (opts.rubyArrayLiteral) {
|
96
|
+
// If we have an array that contains only simple string literals with no
|
97
|
+
// spaces or interpolation, then we're going to print a %w array.
|
98
|
+
if (isStringArray(args)) {
|
99
|
+
const printString = (stringPath) => stringPath.call(print, "body", 0);
|
100
|
+
const nodePath = path;
|
101
|
+
const parts = nodePath.map(printString, "body", 0, "body");
|
102
|
+
return printArrayLiteral("%w", parts);
|
103
|
+
}
|
104
|
+
// If we have an array that contains only simple symbol literals with no
|
105
|
+
// interpolation, then we're going to print a %i array.
|
106
|
+
if (isSymbolArray(args)) {
|
107
|
+
const printSymbol = (symbolPath) => symbolPath.call(print, "body", 0);
|
108
|
+
const nodePath = path;
|
109
|
+
const parts = nodePath.map(printSymbol, "body", 0, "body");
|
110
|
+
return printArrayLiteral("%i", parts);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
// Here we have a normal array of any type of object with no special literal
|
114
|
+
// types or anything.
|
115
|
+
return group([
|
116
|
+
"[",
|
117
|
+
indent([
|
118
|
+
softline,
|
119
|
+
join([",", line], path.call(print, "body", 0)),
|
120
|
+
(0, utils_1.getTrailingComma)(opts) ? ifBreak(",", "") : ""
|
121
|
+
]),
|
122
|
+
softline,
|
123
|
+
"]"
|
124
|
+
]);
|
125
|
+
};
|
126
|
+
exports.printArray = printArray;
|
@@ -0,0 +1,41 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printVarRef = exports.printVarField = exports.printOpAssign = exports.printAssign = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { group, indent, join, line } = prettier_1.default;
|
10
|
+
const printAssign = (path, opts, print) => {
|
11
|
+
const [, valueNode] = path.getValue().body;
|
12
|
+
const [targetDoc, valueDoc] = path.map(print, "body");
|
13
|
+
let rightSideDoc = valueDoc;
|
14
|
+
// If the right side of this assignment is a multiple assignment, then we need
|
15
|
+
// to join it together with commas.
|
16
|
+
if (["mrhs_add_star", "mrhs_new_from_args"].includes(valueNode.type)) {
|
17
|
+
rightSideDoc = group(join([",", line], valueDoc));
|
18
|
+
}
|
19
|
+
if ((0, utils_1.skipAssignIndent)(valueNode)) {
|
20
|
+
return group([targetDoc, " = ", rightSideDoc]);
|
21
|
+
}
|
22
|
+
return group([targetDoc, " =", indent([line, rightSideDoc])]);
|
23
|
+
};
|
24
|
+
exports.printAssign = printAssign;
|
25
|
+
const printOpAssign = (path, opts, print) => {
|
26
|
+
return group([
|
27
|
+
path.call(print, "body", 0),
|
28
|
+
" ",
|
29
|
+
path.call(print, "body", 1),
|
30
|
+
indent([line, path.call(print, "body", 2)])
|
31
|
+
]);
|
32
|
+
};
|
33
|
+
exports.printOpAssign = printOpAssign;
|
34
|
+
const printVarField = (path, opts, print) => {
|
35
|
+
return path.getValue().body ? path.call(print, "body", 0) : "";
|
36
|
+
};
|
37
|
+
exports.printVarField = printVarField;
|
38
|
+
const printVarRef = (path, opts, print) => {
|
39
|
+
return path.call(print, "body", 0);
|
40
|
+
};
|
41
|
+
exports.printVarRef = printVarRef;
|
@@ -0,0 +1,68 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.printDoBlock = exports.printBraceBlock = exports.printBlockVar = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { breakParent, group, ifBreak, indent, join, removeLines, softline } = prettier_1.default;
|
10
|
+
const printBlockVar = (path, opts, print) => {
|
11
|
+
const parts = ["|", removeLines(path.call(print, "body", 0))];
|
12
|
+
// The second part of this node is a list of optional block-local variables
|
13
|
+
if (path.getValue().body[1]) {
|
14
|
+
parts.push("; ", join(", ", path.map(print, "body", 1)));
|
15
|
+
}
|
16
|
+
parts.push("| ");
|
17
|
+
return parts;
|
18
|
+
};
|
19
|
+
exports.printBlockVar = printBlockVar;
|
20
|
+
function printBlock(braces) {
|
21
|
+
return function printBlockWithBraces(path, opts, print) {
|
22
|
+
const [variables, statements] = path.getValue().body;
|
23
|
+
const stmts = statements.type === "stmts" ? statements.body : statements.body[0].body;
|
24
|
+
let doBlockBody = "";
|
25
|
+
if (stmts.length !== 1 ||
|
26
|
+
stmts[0].type !== "void_stmt" ||
|
27
|
+
stmts[0].comments) {
|
28
|
+
doBlockBody = indent([softline, path.call(print, "body", 1)]);
|
29
|
+
}
|
30
|
+
// If this block is nested underneath a command or command_call node, then
|
31
|
+
// we can't use `do...end` because that will get associated with the parent
|
32
|
+
// node as opposed to the current node (because of the difference in
|
33
|
+
// operator precedence). Instead, we still use a multi-line format but
|
34
|
+
// switch to using braces instead.
|
35
|
+
const useBraces = braces && (0, utils_1.hasAncestor)(path, ["command", "command_call"]);
|
36
|
+
const doBlock = [
|
37
|
+
useBraces ? " {" : " do",
|
38
|
+
variables ? [" ", path.call(print, "body", 0)] : "",
|
39
|
+
doBlockBody,
|
40
|
+
[softline, useBraces ? "}" : "end"]
|
41
|
+
];
|
42
|
+
// We can hit this next pattern if within the block the only statement is a
|
43
|
+
// comment.
|
44
|
+
if (stmts.length === 1 &&
|
45
|
+
stmts[0].type === "void_stmt" &&
|
46
|
+
stmts[0].comments) {
|
47
|
+
return [breakParent, doBlock];
|
48
|
+
}
|
49
|
+
const blockReceiver = path.getParentNode().body[0];
|
50
|
+
// If the parent node is a command node, then there are no parentheses
|
51
|
+
// around the arguments to that command, so we need to break the block
|
52
|
+
if (["command", "command_call"].includes(blockReceiver.type)) {
|
53
|
+
return [breakParent, doBlock];
|
54
|
+
}
|
55
|
+
const hasBody = stmts.some(({ type }) => type !== "void_stmt");
|
56
|
+
const braceBlock = [
|
57
|
+
" {",
|
58
|
+
hasBody || variables ? " " : "",
|
59
|
+
variables ? path.call(print, "body", 0) : "",
|
60
|
+
path.call(print, "body", 1),
|
61
|
+
hasBody ? " " : "",
|
62
|
+
"}"
|
63
|
+
];
|
64
|
+
return group(ifBreak(doBlock, braceBlock));
|
65
|
+
};
|
66
|
+
}
|
67
|
+
exports.printBraceBlock = printBlock(true);
|
68
|
+
exports.printDoBlock = printBlock(false);
|