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
@@ -0,0 +1,75 @@
|
|
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.printReturn0 = exports.printReturn = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { group, ifBreak, indent, line, join, softline } = prettier_1.default;
|
10
|
+
// You can't skip the parentheses if you have comments or certain operators with
|
11
|
+
// lower precedence than the return keyword.
|
12
|
+
function canSkipParens(args) {
|
13
|
+
const stmts = args.body[0].body[0];
|
14
|
+
// return(
|
15
|
+
// # a
|
16
|
+
// b
|
17
|
+
// )
|
18
|
+
if (stmts.comments) {
|
19
|
+
return false;
|
20
|
+
}
|
21
|
+
const stmt = stmts.body[0];
|
22
|
+
// return (a or b)
|
23
|
+
if (stmt.type === "binary" && ["and", "or"].includes(stmt.body[1])) {
|
24
|
+
return false;
|
25
|
+
}
|
26
|
+
// return (not a)
|
27
|
+
if (stmt.type === "unary" && stmt.oper === "not") {
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
return true;
|
31
|
+
}
|
32
|
+
const printReturn = (path, opts, print) => {
|
33
|
+
let args = path.getValue().body[0].body[0];
|
34
|
+
let steps = ["body", 0, "body", 0];
|
35
|
+
if (args.body.length === 1) {
|
36
|
+
// If the body of the return contains parens, then just skip directly to the
|
37
|
+
// content of the parens so that we can skip printing parens if we don't
|
38
|
+
// want them.
|
39
|
+
if (args.body[0] && args.body[0].type === "paren" && canSkipParens(args)) {
|
40
|
+
args = args.body[0].body[0];
|
41
|
+
steps = steps.concat("body", 0, "body", 0);
|
42
|
+
}
|
43
|
+
// If we're returning an array literal that isn't a special array, single
|
44
|
+
// element array, or an empty array, then we want to grab the arguments so
|
45
|
+
// that we can print them out as if they were normal return arguments.
|
46
|
+
if (args.body[0] &&
|
47
|
+
args.body[0].type === "array" &&
|
48
|
+
args.body[0].body[0] &&
|
49
|
+
args.body[0].body[0].body.length > 1 &&
|
50
|
+
["args", "args_add_star"].includes(args.body[0].body[0].type)) {
|
51
|
+
steps = steps.concat("body", 0, "body", 0);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
// Now that we've established which actual node is the arguments to return,
|
55
|
+
// we grab it out of the path by diving down the steps that we've set up.
|
56
|
+
const parts = path.call(print, ...steps);
|
57
|
+
const useBrackets = Array.isArray(parts) && parts.length > 1;
|
58
|
+
// If we got the value straight out of the parens, then `parts` would only
|
59
|
+
// be a singular doc as opposed to an array.
|
60
|
+
const value = Array.isArray(parts) ? join([",", line], parts) : parts;
|
61
|
+
// We only get here if we have comments somewhere that would prevent us from
|
62
|
+
// skipping the parentheses.
|
63
|
+
if (args.body.length === 1 && args.body[0].type === "paren") {
|
64
|
+
return ["return", value];
|
65
|
+
}
|
66
|
+
return group([
|
67
|
+
"return",
|
68
|
+
ifBreak(useBrackets ? " [" : "(", " "),
|
69
|
+
indent([softline, value]),
|
70
|
+
softline,
|
71
|
+
ifBreak(useBrackets ? "]" : ")", "")
|
72
|
+
]);
|
73
|
+
};
|
74
|
+
exports.printReturn = printReturn;
|
75
|
+
exports.printReturn0 = (0, utils_1.literal)("return");
|
@@ -0,0 +1,111 @@
|
|
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.printStmts = exports.printProgram = exports.printComment = exports.printEndContent = exports.printParen = exports.printBodyStmt = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { breakParent, dedent, group, hardline, indent, join, line, literalline, softline, trim } = prettier_1.default;
|
10
|
+
const printBodyStmt = (path, opts, print) => {
|
11
|
+
const [stmts, rescue, elseClause, ensure] = path.getValue().body;
|
12
|
+
const parts = [];
|
13
|
+
if (!(0, utils_1.isEmptyStmts)(stmts)) {
|
14
|
+
parts.push(path.call(print, "body", 0));
|
15
|
+
}
|
16
|
+
if (rescue) {
|
17
|
+
parts.push(dedent([hardline, path.call(print, "body", 1)]));
|
18
|
+
}
|
19
|
+
if (elseClause) {
|
20
|
+
// Before Ruby 2.6, this piece of bodystmt was an explicit "else" node
|
21
|
+
/* istanbul ignore next */
|
22
|
+
const stmts = elseClause.type === "else"
|
23
|
+
? path.call(print, "body", 2, "body", 0)
|
24
|
+
: path.call(print, "body", 2);
|
25
|
+
parts.push([dedent([hardline, "else"]), hardline, stmts]);
|
26
|
+
}
|
27
|
+
if (ensure) {
|
28
|
+
parts.push(dedent([hardline, path.call(print, "body", 3)]));
|
29
|
+
}
|
30
|
+
return group(parts);
|
31
|
+
};
|
32
|
+
exports.printBodyStmt = printBodyStmt;
|
33
|
+
const argNodeTypes = ["args", "args_add_star", "args_add_block"];
|
34
|
+
const printParen = (path, opts, print) => {
|
35
|
+
const contentNode = path.getValue().body[0];
|
36
|
+
if (!contentNode) {
|
37
|
+
return [path.call(print, "lparen"), ")"];
|
38
|
+
}
|
39
|
+
let contentDoc = path.call(print, "body", 0);
|
40
|
+
// If the content is params, we're going to let it handle its own parentheses
|
41
|
+
// so that it breaks nicely.
|
42
|
+
if (contentNode.type === "params") {
|
43
|
+
return contentDoc;
|
44
|
+
}
|
45
|
+
// If we have an arg type node as the contents, then it's going to return an
|
46
|
+
// array, so we need to explicitly join that content here.
|
47
|
+
if (argNodeTypes.includes(contentNode.type)) {
|
48
|
+
contentDoc = join([",", line], contentDoc);
|
49
|
+
}
|
50
|
+
return group([
|
51
|
+
path.call(print, "lparen"),
|
52
|
+
indent([softline, contentDoc]),
|
53
|
+
softline,
|
54
|
+
")"
|
55
|
+
]);
|
56
|
+
};
|
57
|
+
exports.printParen = printParen;
|
58
|
+
const printEndContent = (path) => {
|
59
|
+
const { body } = path.getValue();
|
60
|
+
return [trim, "__END__", literalline, body];
|
61
|
+
};
|
62
|
+
exports.printEndContent = printEndContent;
|
63
|
+
const printComment = (path, opts) => {
|
64
|
+
return opts.printer.printComment(path, opts);
|
65
|
+
};
|
66
|
+
exports.printComment = printComment;
|
67
|
+
const printProgram = (path, opts, print) => {
|
68
|
+
return [join(hardline, path.map(print, "body")), hardline];
|
69
|
+
};
|
70
|
+
exports.printProgram = printProgram;
|
71
|
+
const printStmts = (path, opts, print) => {
|
72
|
+
const stmts = path.getValue().body;
|
73
|
+
// This is a special case where we have only comments inside a statement
|
74
|
+
// list. In this case we want to avoid doing any kind of line number
|
75
|
+
// tracking and just print out the comments.
|
76
|
+
if (stmts.length === 1 &&
|
77
|
+
stmts[0].type === "void_stmt" &&
|
78
|
+
stmts[0].comments) {
|
79
|
+
const nodePath = path;
|
80
|
+
const comments = nodePath.map((commentPath) => {
|
81
|
+
commentPath.getValue().printed = true;
|
82
|
+
return opts.printer.printComment(commentPath, opts);
|
83
|
+
}, "body", 0, "comments");
|
84
|
+
return [breakParent, join(hardline, comments)];
|
85
|
+
}
|
86
|
+
const parts = [];
|
87
|
+
let lineNo = null;
|
88
|
+
stmts.forEach((stmt, index) => {
|
89
|
+
if (stmt.type === "void_stmt") {
|
90
|
+
return;
|
91
|
+
}
|
92
|
+
const printed = path.call(print, "body", index);
|
93
|
+
if (lineNo === null) {
|
94
|
+
parts.push(printed);
|
95
|
+
}
|
96
|
+
else if (stmt.sl - lineNo > 1 ||
|
97
|
+
[stmt.type, stmts[index - 1].type].includes("access_ctrl")) {
|
98
|
+
parts.push(hardline, hardline, printed);
|
99
|
+
}
|
100
|
+
else if (stmt.sl !== lineNo ||
|
101
|
+
path.getParentNode().type !== "string_embexpr") {
|
102
|
+
parts.push(hardline, printed);
|
103
|
+
}
|
104
|
+
else {
|
105
|
+
parts.push("; ", printed);
|
106
|
+
}
|
107
|
+
lineNo = stmt.el;
|
108
|
+
});
|
109
|
+
return parts;
|
110
|
+
};
|
111
|
+
exports.printStmts = printStmts;
|
@@ -0,0 +1,218 @@
|
|
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.printXStringLiteral = exports.printSymbolLiteral = exports.printStringLiteral = exports.printStringEmbExpr = exports.printStringDVar = exports.printStringConcat = exports.printDynaSymbol = exports.printChar = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const { group, hardline, indent, literalline, removeLines, softline, join } = prettier_1.default;
|
9
|
+
// If there is some part of this string that matches an escape sequence or that
|
10
|
+
// contains the interpolation pattern ("#{"), then we are locked into whichever
|
11
|
+
// quote the user chose. (If they chose single quotes, then double quoting
|
12
|
+
// would activate the escape sequence, and if they chose double quotes, then
|
13
|
+
// single quotes would deactivate it.)
|
14
|
+
function isQuoteLocked(node) {
|
15
|
+
return node.body.some((part) => part.type === "@tstring_content" &&
|
16
|
+
(part.body.includes("#{") || part.body.includes("\\")));
|
17
|
+
}
|
18
|
+
// A string is considered to be able to use single quotes if it contains only
|
19
|
+
// plain string content and that content does not contain a single quote.
|
20
|
+
function isSingleQuotable(node) {
|
21
|
+
return node.body.every((part) => part.type === "@tstring_content" && !part.body.includes("'"));
|
22
|
+
}
|
23
|
+
const quotePattern = new RegExp("\\\\([\\s\\S])|(['\"])", "g");
|
24
|
+
function normalizeQuotes(content, enclosingQuote) {
|
25
|
+
// Escape and unescape single and double quotes as needed to be able to
|
26
|
+
// enclose `content` with `enclosingQuote`.
|
27
|
+
return content.replace(quotePattern, (match, escaped, quote) => {
|
28
|
+
if (quote === enclosingQuote) {
|
29
|
+
return `\\${quote}`;
|
30
|
+
}
|
31
|
+
if (quote) {
|
32
|
+
return quote;
|
33
|
+
}
|
34
|
+
return `\\${escaped}`;
|
35
|
+
});
|
36
|
+
}
|
37
|
+
const quotePairs = {
|
38
|
+
"(": ")",
|
39
|
+
"[": "]",
|
40
|
+
"{": "}",
|
41
|
+
"<": ">"
|
42
|
+
};
|
43
|
+
function getClosingQuote(quote) {
|
44
|
+
if (!quote.startsWith("%")) {
|
45
|
+
return quote;
|
46
|
+
}
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
48
|
+
const boundary = /%[Qq]?(.)/.exec(quote)[1];
|
49
|
+
if (boundary in quotePairs) {
|
50
|
+
return quotePairs[boundary];
|
51
|
+
}
|
52
|
+
return boundary;
|
53
|
+
}
|
54
|
+
// Prints a @CHAR node. @CHAR nodes are special character strings that usually
|
55
|
+
// are strings of length 1. If they're any longer than we'll try to apply the
|
56
|
+
// correct quotes.
|
57
|
+
const printChar = (path, opts) => {
|
58
|
+
const { body } = path.getValue();
|
59
|
+
if (body.length !== 2) {
|
60
|
+
return body;
|
61
|
+
}
|
62
|
+
const quote = opts.rubySingleQuote ? "'" : '"';
|
63
|
+
return [quote, body.slice(1), quote];
|
64
|
+
};
|
65
|
+
exports.printChar = printChar;
|
66
|
+
const printPercentSDynaSymbol = (path, opts, print) => {
|
67
|
+
const node = path.getValue();
|
68
|
+
const parts = [];
|
69
|
+
// Push on the quote, which includes the opening character.
|
70
|
+
parts.push(node.quote);
|
71
|
+
path.each((childPath) => {
|
72
|
+
const childNode = childPath.getValue();
|
73
|
+
if (childNode.type !== "@tstring_content") {
|
74
|
+
// Here we are printing an embedded variable or expression.
|
75
|
+
parts.push(print(childPath));
|
76
|
+
}
|
77
|
+
else {
|
78
|
+
// Here we are printing plain string content.
|
79
|
+
parts.push(join(literalline, childNode.body.split("\n")));
|
80
|
+
}
|
81
|
+
}, "body");
|
82
|
+
// Push on the closing character, which is the opposite of the third
|
83
|
+
// character from the opening.
|
84
|
+
parts.push(quotePairs[node.quote[2]]);
|
85
|
+
return parts;
|
86
|
+
};
|
87
|
+
// We don't actually want to print %s symbols, as they're much more rarely seen
|
88
|
+
// in the wild. But we're going to be forced into it if it's a multi-line symbol
|
89
|
+
// or if the quoting would get super complicated.
|
90
|
+
function shouldPrintPercentSDynaSymbol(node) {
|
91
|
+
// We shouldn't print a %s dyna symbol if it was not already that way in the
|
92
|
+
// original source.
|
93
|
+
if (node.quote[0] !== "%") {
|
94
|
+
return false;
|
95
|
+
}
|
96
|
+
// Here we're going to check if there is a closing character, a new line, or a
|
97
|
+
// quote in the content of the dyna symbol. If there is, then quoting could
|
98
|
+
// get weird, so just bail out and stick to the original bounds in the source.
|
99
|
+
const closing = quotePairs[node.quote[2]];
|
100
|
+
return node.body.some((child) => child.type === "@tstring_content" &&
|
101
|
+
(child.body.includes("\n") ||
|
102
|
+
child.body.includes(closing) ||
|
103
|
+
child.body.includes("'") ||
|
104
|
+
child.body.includes('"')));
|
105
|
+
}
|
106
|
+
// Prints a dynamic symbol. Assumes there's a quote property attached to the
|
107
|
+
// node that will tell us which quote to use when printing. We're just going to
|
108
|
+
// use whatever quote was provided.
|
109
|
+
//
|
110
|
+
// In the case of a plain dyna symbol, node.quote will be either :" or :'
|
111
|
+
// For %s dyna symbols, node.quote will be %s[, %s(, %s{, or %s<
|
112
|
+
const printDynaSymbol = (path, opts, print) => {
|
113
|
+
const node = path.getValue();
|
114
|
+
if (shouldPrintPercentSDynaSymbol(node)) {
|
115
|
+
return printPercentSDynaSymbol(path, opts, print);
|
116
|
+
}
|
117
|
+
const parts = [];
|
118
|
+
let quote;
|
119
|
+
if (isQuoteLocked(node)) {
|
120
|
+
if (node.quote.startsWith("%")) {
|
121
|
+
quote = opts.rubySingleQuote ? "'" : '"';
|
122
|
+
}
|
123
|
+
else if (node.quote.startsWith(":")) {
|
124
|
+
quote = node.quote.slice(1);
|
125
|
+
}
|
126
|
+
else {
|
127
|
+
quote = node.quote;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
else {
|
131
|
+
quote = opts.rubySingleQuote && isSingleQuotable(node) ? "'" : '"';
|
132
|
+
}
|
133
|
+
parts.push(quote);
|
134
|
+
path.each((childPath) => {
|
135
|
+
const child = childPath.getValue();
|
136
|
+
if (child.type !== "@tstring_content") {
|
137
|
+
parts.push(print(childPath));
|
138
|
+
}
|
139
|
+
else {
|
140
|
+
parts.push(join(literalline, normalizeQuotes(child.body, quote).split("\n")));
|
141
|
+
}
|
142
|
+
}, "body");
|
143
|
+
parts.push(quote);
|
144
|
+
// If we're inside of an assoc_new node as the key, then it will handle
|
145
|
+
// printing the : on its own since it could change sides.
|
146
|
+
const parentNode = path.getParentNode();
|
147
|
+
if (parentNode.type !== "assoc_new" || parentNode.body[0] !== node) {
|
148
|
+
parts.unshift(":");
|
149
|
+
}
|
150
|
+
return parts;
|
151
|
+
};
|
152
|
+
exports.printDynaSymbol = printDynaSymbol;
|
153
|
+
const printStringConcat = (path, opts, print) => {
|
154
|
+
const [leftDoc, rightDoc] = path.map(print, "body");
|
155
|
+
return group([leftDoc, " \\", indent([hardline, rightDoc])]);
|
156
|
+
};
|
157
|
+
exports.printStringConcat = printStringConcat;
|
158
|
+
// Prints out an interpolated variable in the string by converting it into an
|
159
|
+
// embedded expression.
|
160
|
+
const printStringDVar = (path, opts, print) => {
|
161
|
+
return ["#{", path.call(print, "body", 0), "}"];
|
162
|
+
};
|
163
|
+
exports.printStringDVar = printStringDVar;
|
164
|
+
const printStringEmbExpr = (path, opts, print) => {
|
165
|
+
const node = path.getValue();
|
166
|
+
const parts = path.call(print, "body", 0);
|
167
|
+
// If the contents of this embedded expression were originally on the same
|
168
|
+
// line in the source, then we're going to leave them in place and assume
|
169
|
+
// that's the way the developer wanted this expression represented.
|
170
|
+
if (node.sl === node.el) {
|
171
|
+
return ["#{", removeLines(parts), "}"];
|
172
|
+
}
|
173
|
+
return group(["#{", indent([softline, parts]), [softline, "}"]]);
|
174
|
+
};
|
175
|
+
exports.printStringEmbExpr = printStringEmbExpr;
|
176
|
+
// Prints out a literal string. This function does its best to respect the
|
177
|
+
// wishes of the user with regards to single versus double quotes, but if the
|
178
|
+
// string contains any escape expressions then it will just keep the original
|
179
|
+
// quotes.
|
180
|
+
const printStringLiteral = (path, { rubySingleQuote }, print) => {
|
181
|
+
const node = path.getValue();
|
182
|
+
// If the string is empty, it will not have any parts, so just print out the
|
183
|
+
// quotes corresponding to the config
|
184
|
+
if (node.body.length === 0) {
|
185
|
+
return rubySingleQuote ? "''" : '""';
|
186
|
+
}
|
187
|
+
// Determine the quote that should enclose the new string
|
188
|
+
let quote;
|
189
|
+
if (isQuoteLocked(node)) {
|
190
|
+
quote = node.quote;
|
191
|
+
}
|
192
|
+
else {
|
193
|
+
quote = rubySingleQuote && isSingleQuotable(node) ? "'" : '"';
|
194
|
+
}
|
195
|
+
const parts = path.map((partPath) => {
|
196
|
+
const part = partPath.getValue();
|
197
|
+
// In this case, the part of the string is an embedded expression
|
198
|
+
if (part.type !== "@tstring_content") {
|
199
|
+
return print(partPath);
|
200
|
+
}
|
201
|
+
// In this case, the part of the string is just regular string content
|
202
|
+
return join(literalline, normalizeQuotes(part.body, quote).split("\n"));
|
203
|
+
}, "body");
|
204
|
+
return [quote, ...parts, getClosingQuote(quote)];
|
205
|
+
};
|
206
|
+
exports.printStringLiteral = printStringLiteral;
|
207
|
+
// Prints out a symbol literal. Its child will always be the ident that
|
208
|
+
// represents the string content of the symbol.
|
209
|
+
const printSymbolLiteral = (path, opts, print) => {
|
210
|
+
return [":", path.call(print, "body", 0)];
|
211
|
+
};
|
212
|
+
exports.printSymbolLiteral = printSymbolLiteral;
|
213
|
+
// Prints out an xstring literal. Its child is an array of string parts,
|
214
|
+
// including plain string content and interpolated content.
|
215
|
+
const printXStringLiteral = (path, opts, print) => {
|
216
|
+
return ["`", ...path.map(print, "body"), "`"];
|
217
|
+
};
|
218
|
+
exports.printXStringLiteral = printXStringLiteral;
|
@@ -0,0 +1,30 @@
|
|
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.printZSuper = exports.printSuper = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const utils_1 = require("../../utils");
|
9
|
+
const { align, group, join, line } = prettier_1.default;
|
10
|
+
const printSuper = (path, opts, print) => {
|
11
|
+
const args = path.getValue().body[0];
|
12
|
+
if (args.type === "arg_paren") {
|
13
|
+
// In case there are explicitly no arguments but they are using parens,
|
14
|
+
// we assume they are attempting to override the initializer and pass no
|
15
|
+
// arguments up.
|
16
|
+
if (args.body[0] === null) {
|
17
|
+
return "super()";
|
18
|
+
}
|
19
|
+
return ["super", path.call(print, "body", 0)];
|
20
|
+
}
|
21
|
+
const keyword = "super ";
|
22
|
+
const argsDocs = path.call(print, "body", 0);
|
23
|
+
return group([
|
24
|
+
keyword,
|
25
|
+
align(keyword.length, group(join([",", line], argsDocs)))
|
26
|
+
]);
|
27
|
+
};
|
28
|
+
exports.printSuper = printSuper;
|
29
|
+
// Version of super without any parens or args.
|
30
|
+
exports.printZSuper = (0, utils_1.literal)("super");
|
@@ -0,0 +1,26 @@
|
|
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.printUndef = void 0;
|
7
|
+
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
+
const { addTrailingComment, align, group, join, line } = prettier_1.default;
|
9
|
+
const printUndefSymbol = (path, opts, print) => {
|
10
|
+
const node = path.getValue();
|
11
|
+
// Since we're going to descend into the symbol literal to grab out the ident
|
12
|
+
// node, then we need to make sure we copy over any comments as well,
|
13
|
+
// otherwise we could accidentally skip printing them.
|
14
|
+
if (node.comments) {
|
15
|
+
node.comments.forEach((comment) => {
|
16
|
+
addTrailingComment(node.body[0], comment);
|
17
|
+
});
|
18
|
+
}
|
19
|
+
return path.call(print, "body", 0);
|
20
|
+
};
|
21
|
+
const printUndef = (path, opts, print) => {
|
22
|
+
const keyword = "undef ";
|
23
|
+
const argNodes = path.map((symbolPath) => printUndefSymbol(symbolPath, opts, print), "body");
|
24
|
+
return group([keyword, align(keyword.length, join([",", line], argNodes))]);
|
25
|
+
};
|
26
|
+
exports.printUndef = printUndef;
|
data/dist/ruby/nodes.js
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const alias_1 = require("./nodes/alias");
|
4
|
+
const aref_1 = require("./nodes/aref");
|
5
|
+
const args_1 = require("./nodes/args");
|
6
|
+
const arrays_1 = require("./nodes/arrays");
|
7
|
+
const assign_1 = require("./nodes/assign");
|
8
|
+
const blocks_1 = require("./nodes/blocks");
|
9
|
+
const calls_1 = require("./nodes/calls");
|
10
|
+
const case_1 = require("./nodes/case");
|
11
|
+
const class_1 = require("./nodes/class");
|
12
|
+
const commands_1 = require("./nodes/commands");
|
13
|
+
const conditionals_1 = require("./nodes/conditionals");
|
14
|
+
const constants_1 = require("./nodes/constants");
|
15
|
+
const flow_1 = require("./nodes/flow");
|
16
|
+
const hashes_1 = require("./nodes/hashes");
|
17
|
+
const heredocs_1 = require("./nodes/heredocs");
|
18
|
+
const hooks_1 = require("./nodes/hooks");
|
19
|
+
const ints_1 = require("./nodes/ints");
|
20
|
+
const lambdas_1 = require("./nodes/lambdas");
|
21
|
+
const loops_1 = require("./nodes/loops");
|
22
|
+
const massign_1 = require("./nodes/massign");
|
23
|
+
const methods_1 = require("./nodes/methods");
|
24
|
+
const operators_1 = require("./nodes/operators");
|
25
|
+
const params_1 = require("./nodes/params");
|
26
|
+
const patterns_1 = require("./nodes/patterns");
|
27
|
+
const regexp_1 = require("./nodes/regexp");
|
28
|
+
const rescue_1 = require("./nodes/rescue");
|
29
|
+
const return_1 = require("./nodes/return");
|
30
|
+
const statements_1 = require("./nodes/statements");
|
31
|
+
const strings_1 = require("./nodes/strings");
|
32
|
+
const super_1 = require("./nodes/super");
|
33
|
+
const undef_1 = require("./nodes/undef");
|
34
|
+
const nodes = {
|
35
|
+
"@__end__": statements_1.printEndContent,
|
36
|
+
"@CHAR": strings_1.printChar,
|
37
|
+
"@comment": statements_1.printComment,
|
38
|
+
"@int": ints_1.printInt,
|
39
|
+
access_ctrl: methods_1.printAccessControl,
|
40
|
+
alias: alias_1.printAlias,
|
41
|
+
aref: aref_1.printAref,
|
42
|
+
aref_field: aref_1.printArefField,
|
43
|
+
arg_paren: args_1.printArgParen,
|
44
|
+
args: args_1.printArgs,
|
45
|
+
args_add_block: args_1.printArgsAddBlock,
|
46
|
+
args_add_star: args_1.printArgsAddStar,
|
47
|
+
args_forward: params_1.printArgsForward,
|
48
|
+
array: arrays_1.printArray,
|
49
|
+
aryptn: patterns_1.printAryPtn,
|
50
|
+
assign: assign_1.printAssign,
|
51
|
+
assoc_new: hashes_1.printAssocNew,
|
52
|
+
assoc_splat: hashes_1.printAssocSplat,
|
53
|
+
assoclist_from_args: hashes_1.printHashContents,
|
54
|
+
bare_assoc_hash: hashes_1.printHashContents,
|
55
|
+
BEGIN: hooks_1.printBEGIN,
|
56
|
+
begin: rescue_1.printBegin,
|
57
|
+
binary: operators_1.printBinary,
|
58
|
+
blockarg: args_1.printBlockArg,
|
59
|
+
block_var: blocks_1.printBlockVar,
|
60
|
+
bodystmt: statements_1.printBodyStmt,
|
61
|
+
brace_block: blocks_1.printBraceBlock,
|
62
|
+
break: flow_1.printBreak,
|
63
|
+
call: calls_1.printCall,
|
64
|
+
case: case_1.printCase,
|
65
|
+
class: class_1.printClass,
|
66
|
+
command: commands_1.printCommand,
|
67
|
+
command_call: commands_1.printCommandCall,
|
68
|
+
const_path_field: constants_1.printConstPath,
|
69
|
+
const_path_ref: constants_1.printConstPath,
|
70
|
+
const_ref: constants_1.printConstRef,
|
71
|
+
def: methods_1.printDef,
|
72
|
+
defs: methods_1.printDef,
|
73
|
+
defsl: methods_1.printSingleLineMethod,
|
74
|
+
defined: constants_1.printDefined,
|
75
|
+
do_block: blocks_1.printDoBlock,
|
76
|
+
dot2: operators_1.printDot2,
|
77
|
+
dot3: operators_1.printDot3,
|
78
|
+
dyna_symbol: strings_1.printDynaSymbol,
|
79
|
+
else: conditionals_1.printElse,
|
80
|
+
elsif: conditionals_1.printElsif,
|
81
|
+
END: hooks_1.printEND,
|
82
|
+
ensure: rescue_1.printEnsure,
|
83
|
+
fcall: calls_1.printCallContainer,
|
84
|
+
fndptn: patterns_1.printFndPtn,
|
85
|
+
field: constants_1.printField,
|
86
|
+
for: loops_1.printFor,
|
87
|
+
hash: hashes_1.printHash,
|
88
|
+
heredoc: heredocs_1.printHeredoc,
|
89
|
+
hshptn: patterns_1.printHshPtn,
|
90
|
+
if: conditionals_1.printIf,
|
91
|
+
ifop: conditionals_1.printTernary,
|
92
|
+
if_mod: conditionals_1.printIfModifier,
|
93
|
+
in: patterns_1.printIn,
|
94
|
+
kwrest_param: params_1.printKeywordRestParam,
|
95
|
+
lambda: lambdas_1.printLambda,
|
96
|
+
massign: massign_1.printMAssign,
|
97
|
+
method_add_arg: calls_1.printMethodAddArg,
|
98
|
+
method_add_block: calls_1.printMethodAddBlock,
|
99
|
+
mlhs: massign_1.printMLHS,
|
100
|
+
mlhs_add_post: massign_1.printMLHSAddPost,
|
101
|
+
mlhs_add_star: massign_1.printMLHSAddStar,
|
102
|
+
mlhs_paren: massign_1.printMLHSParen,
|
103
|
+
mrhs: massign_1.printMRHS,
|
104
|
+
mrhs_add_star: massign_1.printMRHSAddStar,
|
105
|
+
mrhs_new_from_args: massign_1.printMRHSNewFromArgs,
|
106
|
+
module: class_1.printModule,
|
107
|
+
next: flow_1.printNext,
|
108
|
+
opassign: assign_1.printOpAssign,
|
109
|
+
params: params_1.printParams,
|
110
|
+
paren: statements_1.printParen,
|
111
|
+
program: statements_1.printProgram,
|
112
|
+
rassign: patterns_1.printRAssign,
|
113
|
+
redo: rescue_1.printRedo,
|
114
|
+
regexp_literal: regexp_1.printRegexpLiteral,
|
115
|
+
rescue: rescue_1.printRescue,
|
116
|
+
rescue_ex: rescue_1.printRescueEx,
|
117
|
+
rescue_mod: rescue_1.printRescueMod,
|
118
|
+
rest_param: params_1.printRestParam,
|
119
|
+
retry: rescue_1.printRetry,
|
120
|
+
return: return_1.printReturn,
|
121
|
+
return0: return_1.printReturn0,
|
122
|
+
sclass: class_1.printSClass,
|
123
|
+
stmts: statements_1.printStmts,
|
124
|
+
string_concat: strings_1.printStringConcat,
|
125
|
+
string_dvar: strings_1.printStringDVar,
|
126
|
+
string_embexpr: strings_1.printStringEmbExpr,
|
127
|
+
string_literal: strings_1.printStringLiteral,
|
128
|
+
super: super_1.printSuper,
|
129
|
+
symbol_literal: strings_1.printSymbolLiteral,
|
130
|
+
top_const_field: constants_1.printTopConst,
|
131
|
+
top_const_ref: constants_1.printTopConst,
|
132
|
+
unary: operators_1.printUnary,
|
133
|
+
undef: undef_1.printUndef,
|
134
|
+
unless: conditionals_1.printUnless,
|
135
|
+
unless_mod: conditionals_1.printUnlessModifier,
|
136
|
+
until: loops_1.printUntil,
|
137
|
+
until_mod: loops_1.printUntilModifer,
|
138
|
+
var_alias: alias_1.printAlias,
|
139
|
+
var_field: assign_1.printVarField,
|
140
|
+
var_ref: assign_1.printVarRef,
|
141
|
+
vcall: calls_1.printCallContainer,
|
142
|
+
when: case_1.printWhen,
|
143
|
+
while: loops_1.printWhile,
|
144
|
+
while_mod: loops_1.printWhileModifier,
|
145
|
+
word: arrays_1.printWord,
|
146
|
+
xstring_literal: strings_1.printXStringLiteral,
|
147
|
+
yield: flow_1.printYield,
|
148
|
+
yield0: flow_1.printYield0,
|
149
|
+
zsuper: super_1.printZSuper
|
150
|
+
};
|
151
|
+
exports.default = nodes;
|
data/dist/ruby/parser.js
ADDED
@@ -0,0 +1,34 @@
|
|
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
|
+
const parseSync_1 = __importDefault(require("../parser/parseSync"));
|
7
|
+
const parser = {
|
8
|
+
// This function is responsible for taking an input string of text and
|
9
|
+
// returning to prettier a JavaScript object that is the equivalent AST that
|
10
|
+
// represents the code stored in that string. We accomplish this by spawning a
|
11
|
+
// new Ruby process of parser.rb and reading JSON off STDOUT.
|
12
|
+
parse(text) {
|
13
|
+
return (0, parseSync_1.default)("ruby", text);
|
14
|
+
},
|
15
|
+
astFormat: "ruby",
|
16
|
+
// This function handles checking whether or not the source string has the
|
17
|
+
// pragma for prettier. This is an optional workflow for incremental adoption.
|
18
|
+
hasPragma(text) {
|
19
|
+
return /^\s*#[^\S\n]*@(?:prettier|format)\s*?(?:\n|$)/m.test(text);
|
20
|
+
},
|
21
|
+
// This function is critical for comments and cursor support, and is
|
22
|
+
// responsible for returning the index of the character within the source
|
23
|
+
// string that is the beginning of the given node.
|
24
|
+
locStart(node) {
|
25
|
+
return node.sc;
|
26
|
+
},
|
27
|
+
// This function is critical for comments and cursor support, and is
|
28
|
+
// responsible for returning the index of the character within the source
|
29
|
+
// string that is the ending of the given node.
|
30
|
+
locEnd(node) {
|
31
|
+
return node.ec;
|
32
|
+
}
|
33
|
+
};
|
34
|
+
exports.default = parser;
|