prettier 0.12.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +368 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/CONTRIBUTING.md +143 -0
- data/LICENSE +21 -0
- data/README.md +151 -0
- data/bin/console +7 -0
- data/exe/rbprettier +8 -0
- data/lib/prettier.rb +18 -0
- data/lib/prettier/rake/task.rb +57 -0
- data/node_modules/prettier/bin-prettier.js +44620 -0
- data/node_modules/prettier/index.js +42565 -0
- data/node_modules/prettier/third-party.js +5326 -0
- data/package.json +39 -0
- data/src/builders.js +9 -0
- data/src/escapePattern.js +45 -0
- data/src/nodes.js +594 -0
- data/src/nodes/alias.js +32 -0
- data/src/nodes/arrays.js +162 -0
- data/src/nodes/blocks.js +66 -0
- data/src/nodes/calls.js +28 -0
- data/src/nodes/case.js +57 -0
- data/src/nodes/commands.js +70 -0
- data/src/nodes/conditionals.js +154 -0
- data/src/nodes/hashes.js +134 -0
- data/src/nodes/hooks.js +15 -0
- data/src/nodes/lambdas.js +59 -0
- data/src/nodes/loops.js +46 -0
- data/src/nodes/methods.js +42 -0
- data/src/nodes/params.js +75 -0
- data/src/nodes/regexp.js +18 -0
- data/src/nodes/rescue.js +77 -0
- data/src/nodes/strings.js +143 -0
- data/src/parse.js +16 -0
- data/src/print.js +23 -0
- data/src/ripper.rb +542 -0
- data/src/ruby.js +127 -0
- data/src/toProc.js +82 -0
- data/src/utils.js +150 -0
- metadata +109 -0
data/src/nodes/hashes.js
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
const {
|
2
|
+
concat,
|
3
|
+
group,
|
4
|
+
ifBreak,
|
5
|
+
indent,
|
6
|
+
join,
|
7
|
+
line,
|
8
|
+
literalline
|
9
|
+
} = require("../builders");
|
10
|
+
const { skipAssignIndent } = require("../utils");
|
11
|
+
|
12
|
+
const nodeDive = (node, steps) => {
|
13
|
+
let current = node;
|
14
|
+
|
15
|
+
steps.forEach(step => {
|
16
|
+
current = current[step];
|
17
|
+
});
|
18
|
+
|
19
|
+
return current;
|
20
|
+
};
|
21
|
+
|
22
|
+
const makeLabel = (path, { preferHashLabels }, print, steps) => {
|
23
|
+
const labelNode = nodeDive(path.getValue(), steps);
|
24
|
+
const labelDoc = path.call.apply(path, [print].concat(steps));
|
25
|
+
|
26
|
+
switch (labelNode.type) {
|
27
|
+
case "@label":
|
28
|
+
if (preferHashLabels) {
|
29
|
+
return labelDoc;
|
30
|
+
}
|
31
|
+
return `:${labelDoc.slice(0, labelDoc.length - 1)} =>`;
|
32
|
+
case "symbol_literal": {
|
33
|
+
// You can have a symbol literal as a key in a hash that ends with an =
|
34
|
+
// character, which breaks when you use hash labels.
|
35
|
+
const endsInEquals = labelNode.body[0].body[0].body.endsWith("=");
|
36
|
+
|
37
|
+
if (preferHashLabels && labelNode.body.length === 1 && !endsInEquals) {
|
38
|
+
const symbolSteps = steps.concat("body", 0, "body", 0);
|
39
|
+
|
40
|
+
return concat([
|
41
|
+
path.call.apply(path, [print].concat(symbolSteps)),
|
42
|
+
":"
|
43
|
+
]);
|
44
|
+
}
|
45
|
+
return concat([labelDoc, " =>"]);
|
46
|
+
}
|
47
|
+
case "dyna_symbol":
|
48
|
+
if (preferHashLabels) {
|
49
|
+
return concat(labelDoc.parts.slice(1).concat(":"));
|
50
|
+
}
|
51
|
+
return concat([labelDoc, " =>"]);
|
52
|
+
default:
|
53
|
+
return concat([labelDoc, " =>"]);
|
54
|
+
}
|
55
|
+
};
|
56
|
+
|
57
|
+
module.exports = {
|
58
|
+
assoc_new: (path, opts, print) => {
|
59
|
+
const valueDoc = path.call(print, "body", 1);
|
60
|
+
const parts = [makeLabel(path, opts, print, ["body", 0])];
|
61
|
+
|
62
|
+
if (skipAssignIndent(path.getValue().body[1])) {
|
63
|
+
parts.push(" ", valueDoc);
|
64
|
+
} else {
|
65
|
+
parts.push(indent(concat([line, valueDoc])));
|
66
|
+
}
|
67
|
+
|
68
|
+
return group(concat(parts));
|
69
|
+
},
|
70
|
+
assoclist_from_args: (path, opts, print) => {
|
71
|
+
const { addTrailingCommas } = opts;
|
72
|
+
|
73
|
+
const assocNodes = path.getValue().body[0];
|
74
|
+
const assocDocs = [];
|
75
|
+
|
76
|
+
assocNodes.forEach((assocNode, index) => {
|
77
|
+
const isInner = index !== assocNodes.length - 1;
|
78
|
+
const valueNode = assocNode.body[1];
|
79
|
+
|
80
|
+
const isStraightHeredoc = valueNode.type === "heredoc";
|
81
|
+
const isSquigglyHeredoc =
|
82
|
+
valueNode.type === "string_literal" &&
|
83
|
+
valueNode.body[0].type === "heredoc";
|
84
|
+
|
85
|
+
if (isStraightHeredoc || isSquigglyHeredoc) {
|
86
|
+
const heredocSteps = isStraightHeredoc
|
87
|
+
? ["body", 1]
|
88
|
+
: ["body", 1, "body", 0];
|
89
|
+
const { beging, ending } = nodeDive(assocNode, heredocSteps);
|
90
|
+
|
91
|
+
assocDocs.push(
|
92
|
+
makeLabel(path, opts, print, ["body", 0, index, "body", 0]),
|
93
|
+
" ",
|
94
|
+
beging,
|
95
|
+
isInner || addTrailingCommas ? "," : "",
|
96
|
+
literalline,
|
97
|
+
concat(
|
98
|
+
path.map.apply(
|
99
|
+
path,
|
100
|
+
[print, "body", 0, index].concat(heredocSteps).concat("body")
|
101
|
+
)
|
102
|
+
),
|
103
|
+
ending,
|
104
|
+
isInner ? line : ""
|
105
|
+
);
|
106
|
+
} else {
|
107
|
+
assocDocs.push(path.call(print, "body", 0, index));
|
108
|
+
|
109
|
+
if (isInner) {
|
110
|
+
assocDocs.push(concat([",", line]));
|
111
|
+
} else if (addTrailingCommas) {
|
112
|
+
assocDocs.push(ifBreak(",", ""));
|
113
|
+
}
|
114
|
+
}
|
115
|
+
});
|
116
|
+
|
117
|
+
return group(concat(assocDocs));
|
118
|
+
},
|
119
|
+
bare_assoc_hash: (path, opts, print) =>
|
120
|
+
group(join(concat([",", line]), path.map(print, "body", 0))),
|
121
|
+
hash: (path, opts, print) => {
|
122
|
+
if (path.getValue().body[0] === null) {
|
123
|
+
return "{}";
|
124
|
+
}
|
125
|
+
|
126
|
+
return group(
|
127
|
+
concat([
|
128
|
+
"{",
|
129
|
+
indent(concat([line, concat(path.map(print, "body"))])),
|
130
|
+
concat([line, "}"])
|
131
|
+
])
|
132
|
+
);
|
133
|
+
}
|
134
|
+
};
|
data/src/nodes/hooks.js
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
const { concat, group, indent, line } = require("../builders");
|
2
|
+
|
3
|
+
const printHook = name => (path, opts, print) =>
|
4
|
+
group(
|
5
|
+
concat([
|
6
|
+
`${name} {`,
|
7
|
+
indent(concat([line, path.call(print, "body", 0)])),
|
8
|
+
concat([line, "}"])
|
9
|
+
])
|
10
|
+
);
|
11
|
+
|
12
|
+
module.exports = {
|
13
|
+
BEGIN: printHook("BEGIN"),
|
14
|
+
END: printHook("END")
|
15
|
+
};
|
@@ -0,0 +1,59 @@
|
|
1
|
+
const {
|
2
|
+
concat,
|
3
|
+
group,
|
4
|
+
ifBreak,
|
5
|
+
indent,
|
6
|
+
line,
|
7
|
+
removeLines,
|
8
|
+
softline
|
9
|
+
} = require("../builders");
|
10
|
+
const { hasAncestor } = require("../utils");
|
11
|
+
|
12
|
+
module.exports = {
|
13
|
+
lambda: (path, opts, print) => {
|
14
|
+
let params = path.getValue().body[0];
|
15
|
+
let paramsConcat = "";
|
16
|
+
|
17
|
+
if (params.type === "params") {
|
18
|
+
paramsConcat = path.call(print, "body", 0);
|
19
|
+
} else {
|
20
|
+
[params] = params.body;
|
21
|
+
paramsConcat = path.call(print, "body", 0, "body", 0);
|
22
|
+
}
|
23
|
+
|
24
|
+
const noParams = params.body.every(type => !type);
|
25
|
+
const inlineLambda = concat([
|
26
|
+
"->",
|
27
|
+
noParams ? "" : concat(["(", paramsConcat, ")"]),
|
28
|
+
" { ",
|
29
|
+
path.call(print, "body", 1),
|
30
|
+
" }"
|
31
|
+
]);
|
32
|
+
|
33
|
+
if (hasAncestor(path, ["command", "command_call"])) {
|
34
|
+
return group(
|
35
|
+
ifBreak(
|
36
|
+
concat([
|
37
|
+
"lambda {",
|
38
|
+
noParams ? "" : concat([" |", removeLines(paramsConcat), "|"]),
|
39
|
+
indent(concat([line, path.call(print, "body", 1)])),
|
40
|
+
concat([line, "}"])
|
41
|
+
]),
|
42
|
+
inlineLambda
|
43
|
+
)
|
44
|
+
);
|
45
|
+
}
|
46
|
+
|
47
|
+
return group(
|
48
|
+
ifBreak(
|
49
|
+
concat([
|
50
|
+
"lambda do",
|
51
|
+
noParams ? "" : concat([" |", removeLines(paramsConcat), "|"]),
|
52
|
+
indent(concat([softline, path.call(print, "body", 1)])),
|
53
|
+
concat([softline, "end"])
|
54
|
+
]),
|
55
|
+
inlineLambda
|
56
|
+
)
|
57
|
+
);
|
58
|
+
}
|
59
|
+
};
|
data/src/nodes/loops.js
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
const {
|
2
|
+
breakParent,
|
3
|
+
concat,
|
4
|
+
group,
|
5
|
+
hardline,
|
6
|
+
ifBreak,
|
7
|
+
indent,
|
8
|
+
softline
|
9
|
+
} = require("../builders");
|
10
|
+
|
11
|
+
const printLoop = keyword => (path, { inlineLoops }, print) =>
|
12
|
+
group(
|
13
|
+
ifBreak(
|
14
|
+
concat([
|
15
|
+
concat([`${keyword} `, path.call(print, "body", 0)]),
|
16
|
+
indent(concat([softline, path.call(print, "body", 1)])),
|
17
|
+
concat([softline, "end"])
|
18
|
+
]),
|
19
|
+
concat([
|
20
|
+
inlineLoops ? "" : breakParent,
|
21
|
+
path.call(print, "body", 1),
|
22
|
+
` ${keyword} `,
|
23
|
+
path.call(print, "body", 0)
|
24
|
+
])
|
25
|
+
)
|
26
|
+
);
|
27
|
+
|
28
|
+
const printFor = (path, opts, print) =>
|
29
|
+
group(
|
30
|
+
concat([
|
31
|
+
path.call(print, "body", 1),
|
32
|
+
".each do |",
|
33
|
+
path.call(print, "body", 0),
|
34
|
+
"|",
|
35
|
+
indent(concat([hardline, path.call(print, "body", 2)])),
|
36
|
+
concat([hardline, "end"])
|
37
|
+
])
|
38
|
+
);
|
39
|
+
|
40
|
+
module.exports = {
|
41
|
+
while: printLoop("while"),
|
42
|
+
while_mod: printLoop("while"),
|
43
|
+
until: printLoop("until"),
|
44
|
+
until_mod: printLoop("until"),
|
45
|
+
for: printFor
|
46
|
+
};
|
@@ -0,0 +1,42 @@
|
|
1
|
+
const { concat, group, hardline, indent } = require("../builders");
|
2
|
+
|
3
|
+
const printMethod = offset => (path, opts, print) => {
|
4
|
+
const [_name, params, body] = path.getValue().body.slice(offset);
|
5
|
+
const declaration = ["def "];
|
6
|
+
|
7
|
+
// In this case, we're printing a method that's defined as a singleton, so we
|
8
|
+
// need to include the target and the operator
|
9
|
+
if (offset > 0) {
|
10
|
+
declaration.push(path.call(print, "body", 0), path.call(print, "body", 1));
|
11
|
+
}
|
12
|
+
|
13
|
+
// In case there are no parens but there are arguments
|
14
|
+
const parens =
|
15
|
+
params.type === "params" && params.body.some(paramType => paramType);
|
16
|
+
|
17
|
+
declaration.push(
|
18
|
+
path.call(print, "body", offset),
|
19
|
+
parens ? "(" : "",
|
20
|
+
path.call(print, "body", offset + 1),
|
21
|
+
parens ? ")" : ""
|
22
|
+
);
|
23
|
+
|
24
|
+
// If the body is empty, we can replace with a ;
|
25
|
+
const stmts = body.body[0].body;
|
26
|
+
if (stmts.length === 1 && stmts[0].type === "void_stmt") {
|
27
|
+
return group(concat(declaration.concat(["; end"])));
|
28
|
+
}
|
29
|
+
|
30
|
+
return group(
|
31
|
+
concat([
|
32
|
+
group(concat(declaration)),
|
33
|
+
indent(concat([hardline, path.call(print, "body", offset + 2)])),
|
34
|
+
group(concat([hardline, "end"]))
|
35
|
+
])
|
36
|
+
);
|
37
|
+
};
|
38
|
+
|
39
|
+
module.exports = {
|
40
|
+
def: printMethod(0),
|
41
|
+
defs: printMethod(2)
|
42
|
+
};
|
data/src/nodes/params.js
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
const { concat, group, join, line } = require("../builders");
|
2
|
+
|
3
|
+
const printGenericRestParam = symbol => (path, opts, print) =>
|
4
|
+
path.getValue().body[0]
|
5
|
+
? concat([symbol, path.call(print, "body", 0)])
|
6
|
+
: symbol;
|
7
|
+
|
8
|
+
const printParams = (path, opts, print) => {
|
9
|
+
const [
|
10
|
+
reqs,
|
11
|
+
optls,
|
12
|
+
rest,
|
13
|
+
post,
|
14
|
+
kwargs,
|
15
|
+
kwargRest,
|
16
|
+
block
|
17
|
+
] = path.getValue().body;
|
18
|
+
let parts = [];
|
19
|
+
|
20
|
+
if (reqs) {
|
21
|
+
parts = parts.concat(path.map(print, "body", 0));
|
22
|
+
}
|
23
|
+
|
24
|
+
if (optls) {
|
25
|
+
parts = parts.concat(
|
26
|
+
optls.map((_, index) =>
|
27
|
+
concat([
|
28
|
+
path.call(print, "body", 1, index, 0),
|
29
|
+
" = ",
|
30
|
+
path.call(print, "body", 1, index, 1)
|
31
|
+
])
|
32
|
+
)
|
33
|
+
);
|
34
|
+
}
|
35
|
+
|
36
|
+
if (rest) {
|
37
|
+
parts.push(path.call(print, "body", 2));
|
38
|
+
}
|
39
|
+
|
40
|
+
if (post) {
|
41
|
+
parts = parts.concat(path.map(print, "body", 3));
|
42
|
+
}
|
43
|
+
|
44
|
+
if (kwargs) {
|
45
|
+
parts = parts.concat(
|
46
|
+
kwargs.map(([, value], index) => {
|
47
|
+
if (!value) {
|
48
|
+
return path.call(print, "body", 4, index, 0);
|
49
|
+
}
|
50
|
+
return group(join(" ", path.map(print, "body", 4, index)));
|
51
|
+
})
|
52
|
+
);
|
53
|
+
}
|
54
|
+
|
55
|
+
if (kwargRest) {
|
56
|
+
parts.push(path.call(print, "body", 5));
|
57
|
+
}
|
58
|
+
|
59
|
+
if (block) {
|
60
|
+
parts.push(path.call(print, "body", 6));
|
61
|
+
}
|
62
|
+
|
63
|
+
return group(join(concat([",", line]), parts));
|
64
|
+
};
|
65
|
+
|
66
|
+
const paramError = () => {
|
67
|
+
throw new Error("formal argument cannot be a global variable");
|
68
|
+
};
|
69
|
+
|
70
|
+
module.exports = {
|
71
|
+
kwrest_param: printGenericRestParam("**"),
|
72
|
+
rest_param: printGenericRestParam("*"),
|
73
|
+
params: printParams,
|
74
|
+
param_error: paramError
|
75
|
+
};
|
data/src/nodes/regexp.js
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
const { concat } = require("../builders");
|
2
|
+
const { makeList } = require("../utils");
|
3
|
+
|
4
|
+
module.exports = {
|
5
|
+
regexp: makeList,
|
6
|
+
regexp_literal: (path, opts, print) => {
|
7
|
+
const [contents, ending] = path.map(print, "body");
|
8
|
+
|
9
|
+
const useBraces = contents.some(
|
10
|
+
content => typeof content === "string" && content.includes("/")
|
11
|
+
);
|
12
|
+
const parts = [useBraces ? "%r{" : "/"]
|
13
|
+
.concat(contents)
|
14
|
+
.concat([useBraces ? "}" : "/", ending.slice(1)]);
|
15
|
+
|
16
|
+
return concat(parts);
|
17
|
+
}
|
18
|
+
};
|
data/src/nodes/rescue.js
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
const {
|
2
|
+
align,
|
3
|
+
concat,
|
4
|
+
group,
|
5
|
+
hardline,
|
6
|
+
indent,
|
7
|
+
join,
|
8
|
+
line
|
9
|
+
} = require("../builders");
|
10
|
+
const { literal } = require("../utils");
|
11
|
+
|
12
|
+
module.exports = {
|
13
|
+
begin: (path, opts, print) =>
|
14
|
+
concat([
|
15
|
+
"begin",
|
16
|
+
indent(concat([hardline, concat(path.map(print, "body"))])),
|
17
|
+
hardline,
|
18
|
+
"end"
|
19
|
+
]),
|
20
|
+
ensure: (path, opts, print) =>
|
21
|
+
concat([
|
22
|
+
"ensure",
|
23
|
+
indent(concat([hardline, concat(path.map(print, "body"))]))
|
24
|
+
]),
|
25
|
+
redo: literal("redo"),
|
26
|
+
rescue: (path, opts, print) => {
|
27
|
+
const [exception, variable, _stmts, addition] = path.getValue().body;
|
28
|
+
const parts = ["rescue"];
|
29
|
+
|
30
|
+
if (exception || variable) {
|
31
|
+
if (exception) {
|
32
|
+
if (Array.isArray(exception)) {
|
33
|
+
// In this case, it's actually only the one exception (it's an array
|
34
|
+
// of length 1).
|
35
|
+
parts.push(" ", path.call(print, "body", 0, 0));
|
36
|
+
} else {
|
37
|
+
// Here we have multiple exceptions from which we're rescuing, so we
|
38
|
+
// need to align them and join them together.
|
39
|
+
const joiner = concat([",", line]);
|
40
|
+
const exceptions = group(join(joiner, path.call(print, "body", 0)));
|
41
|
+
|
42
|
+
parts.push(" ", align("rescue ".length, exceptions));
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
if (variable) {
|
47
|
+
parts.push(" => ", path.call(print, "body", 1));
|
48
|
+
}
|
49
|
+
} else {
|
50
|
+
// If you don't specify an error to rescue in a `begin/rescue` block, then
|
51
|
+
// implicitly you're rescuing from `StandardError`. In this case, we're
|
52
|
+
// just going to explicitly add it.
|
53
|
+
parts.push(" StandardError");
|
54
|
+
}
|
55
|
+
|
56
|
+
parts.push(indent(concat([hardline, path.call(print, "body", 2)])));
|
57
|
+
|
58
|
+
// This is the next clause on the `begin` statement, either another
|
59
|
+
// `rescue`, and `ensure`, or an `else` clause.
|
60
|
+
if (addition) {
|
61
|
+
parts.push(concat([hardline, path.call(print, "body", 3)]));
|
62
|
+
}
|
63
|
+
|
64
|
+
return group(concat(parts));
|
65
|
+
},
|
66
|
+
rescue_mod: (path, opts, print) =>
|
67
|
+
concat([
|
68
|
+
"begin",
|
69
|
+
indent(concat([hardline, path.call(print, "body", 0)])),
|
70
|
+
hardline,
|
71
|
+
"rescue StandardError",
|
72
|
+
indent(concat([hardline, path.call(print, "body", 1)])),
|
73
|
+
hardline,
|
74
|
+
"end"
|
75
|
+
]),
|
76
|
+
retry: literal("retry")
|
77
|
+
};
|