prettier 1.2.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +64 -1
- data/CONTRIBUTING.md +2 -2
- data/README.md +22 -94
- data/node_modules/prettier/index.js +54 -54
- data/package.json +2 -3
- data/rubocop.yml +26 -0
- data/src/haml/embed.js +87 -0
- data/src/haml/nodes/comment.js +27 -0
- data/src/haml/nodes/doctype.js +34 -0
- data/src/haml/nodes/filter.js +16 -0
- data/src/haml/nodes/hamlComment.js +21 -0
- data/src/haml/nodes/plain.js +6 -0
- data/src/haml/nodes/root.js +8 -0
- data/src/haml/nodes/script.js +33 -0
- data/src/haml/nodes/silentScript.js +59 -0
- data/src/haml/nodes/tag.js +193 -0
- data/src/haml/parser.js +22 -0
- data/src/haml/parser.rb +138 -0
- data/src/haml/printer.js +28 -0
- data/src/parser/getLang.js +32 -0
- data/src/parser/getNetcat.js +50 -0
- data/src/parser/netcat.js +15 -0
- data/src/parser/parseSync.js +33 -0
- data/src/parser/requestParse.js +74 -0
- data/src/parser/server.rb +61 -0
- data/src/{ruby.js → plugin.js} +25 -4
- data/src/prettier.js +1 -0
- data/src/rbs/parser.js +39 -0
- data/src/rbs/parser.rb +88 -0
- data/src/rbs/printer.js +605 -0
- data/src/{embed.js → ruby/embed.js} +6 -2
- data/src/{nodes.js → ruby/nodes.js} +0 -0
- data/src/{nodes → ruby/nodes}/alias.js +1 -1
- data/src/{nodes → ruby/nodes}/aref.js +8 -1
- data/src/{nodes → ruby/nodes}/args.js +2 -2
- data/src/{nodes → ruby/nodes}/arrays.js +2 -3
- data/src/{nodes → ruby/nodes}/assign.js +7 -3
- data/src/ruby/nodes/blocks.js +90 -0
- data/src/{nodes → ruby/nodes}/calls.js +20 -47
- data/src/{nodes → ruby/nodes}/case.js +1 -1
- data/src/{nodes → ruby/nodes}/class.js +1 -1
- data/src/ruby/nodes/commands.js +131 -0
- data/src/{nodes → ruby/nodes}/conditionals.js +3 -3
- data/src/{nodes → ruby/nodes}/constants.js +2 -2
- data/src/{nodes → ruby/nodes}/flow.js +2 -2
- data/src/{nodes → ruby/nodes}/hashes.js +32 -10
- data/src/{nodes → ruby/nodes}/heredocs.js +2 -2
- data/src/ruby/nodes/hooks.js +34 -0
- data/src/{nodes → ruby/nodes}/ints.js +0 -0
- data/src/{nodes → ruby/nodes}/lambdas.js +2 -2
- data/src/{nodes → ruby/nodes}/loops.js +10 -7
- data/src/{nodes → ruby/nodes}/massign.js +8 -1
- data/src/{nodes → ruby/nodes}/methods.js +10 -9
- data/src/{nodes → ruby/nodes}/operators.js +2 -2
- data/src/{nodes → ruby/nodes}/params.js +31 -16
- data/src/{nodes → ruby/nodes}/patterns.js +17 -6
- data/src/{nodes → ruby/nodes}/regexp.js +2 -2
- data/src/{nodes → ruby/nodes}/rescue.js +34 -27
- data/src/{nodes → ruby/nodes}/return.js +21 -10
- data/src/{nodes → ruby/nodes}/statements.js +9 -9
- data/src/{nodes → ruby/nodes}/strings.js +28 -36
- data/src/{nodes → ruby/nodes}/super.js +2 -2
- data/src/{nodes → ruby/nodes}/undef.js +1 -1
- data/src/ruby/parser.js +39 -0
- data/src/{parser.rb → ruby/parser.rb} +498 -529
- data/src/{printer.js → ruby/printer.js} +1 -3
- data/src/{toProc.js → ruby/toProc.js} +4 -8
- data/src/utils.js +10 -93
- data/src/utils/containsAssignment.js +11 -0
- data/src/utils/getTrailingComma.js +5 -0
- data/src/utils/hasAncestor.js +17 -0
- data/src/utils/literal.js +7 -0
- data/src/utils/makeCall.js +14 -0
- data/src/utils/noIndent.js +11 -0
- data/src/utils/skipAssignIndent.js +10 -0
- metadata +71 -41
- data/src/nodes/blocks.js +0 -85
- data/src/nodes/commands.js +0 -91
- data/src/nodes/hooks.js +0 -44
- data/src/parser.js +0 -86
data/src/nodes/commands.js
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
const {
|
2
|
-
align,
|
3
|
-
concat,
|
4
|
-
group,
|
5
|
-
ifBreak,
|
6
|
-
indent,
|
7
|
-
join,
|
8
|
-
line,
|
9
|
-
softline
|
10
|
-
} = require("../prettier");
|
11
|
-
const { docLength, makeCall } = require("../utils");
|
12
|
-
|
13
|
-
const hasDef = (node) =>
|
14
|
-
node.body[1].type === "args_add_block" &&
|
15
|
-
node.body[1].body[0].type === "args" &&
|
16
|
-
node.body[1].body[0].body[0] &&
|
17
|
-
["def", "defs"].includes(node.body[1].body[0].body[0].type);
|
18
|
-
|
19
|
-
// Very special handling case for rspec matchers. In general with rspec matchers
|
20
|
-
// you expect to see something like:
|
21
|
-
//
|
22
|
-
// expect(foo).to receive(:bar).with(
|
23
|
-
// 'one',
|
24
|
-
// 'two',
|
25
|
-
// 'three',
|
26
|
-
// 'four',
|
27
|
-
// 'five'
|
28
|
-
// )
|
29
|
-
//
|
30
|
-
// In this case the arguments are aligned to the left side as opposed to being
|
31
|
-
// aligned with the `receive` call.
|
32
|
-
const skipArgsAlign = (path) =>
|
33
|
-
["to", "not_to"].includes(path.getValue().body[2].body);
|
34
|
-
|
35
|
-
// If there is a ternary argument to a command and it's going to get broken
|
36
|
-
// into multiple lines, then we're going to have to use parentheses around the
|
37
|
-
// command in order to make sure operator precedence doesn't get messed up.
|
38
|
-
const hasTernaryArg = (path) =>
|
39
|
-
path.getValue().body[1].body[0].body.some((node) => node.type === "ifop");
|
40
|
-
|
41
|
-
module.exports = {
|
42
|
-
command: (path, opts, print) => {
|
43
|
-
const command = path.call(print, "body", 0);
|
44
|
-
const joinedArgs = join(concat([",", line]), path.call(print, "body", 1));
|
45
|
-
|
46
|
-
const hasTernary = hasTernaryArg(path);
|
47
|
-
let breakArgs;
|
48
|
-
|
49
|
-
if (hasTernary) {
|
50
|
-
breakArgs = indent(concat([softline, joinedArgs]));
|
51
|
-
} else if (hasDef(path.getValue())) {
|
52
|
-
breakArgs = joinedArgs;
|
53
|
-
} else {
|
54
|
-
breakArgs = align(command.length + 1, joinedArgs);
|
55
|
-
}
|
56
|
-
|
57
|
-
return group(
|
58
|
-
ifBreak(
|
59
|
-
concat([
|
60
|
-
command,
|
61
|
-
hasTernary ? "(" : " ",
|
62
|
-
breakArgs,
|
63
|
-
hasTernary ? concat([softline, ")"]) : ""
|
64
|
-
]),
|
65
|
-
concat([command, " ", joinedArgs])
|
66
|
-
)
|
67
|
-
);
|
68
|
-
},
|
69
|
-
command_call: (path, opts, print) => {
|
70
|
-
const parts = [
|
71
|
-
path.call(print, "body", 0),
|
72
|
-
makeCall(path, opts, print),
|
73
|
-
path.call(print, "body", 2)
|
74
|
-
];
|
75
|
-
|
76
|
-
if (!path.getValue().body[3]) {
|
77
|
-
return concat(parts);
|
78
|
-
}
|
79
|
-
|
80
|
-
parts.push(" ");
|
81
|
-
|
82
|
-
const joinedArgs = join(concat([",", line]), path.call(print, "body", 3));
|
83
|
-
const breakArgs = skipArgsAlign(path)
|
84
|
-
? joinedArgs
|
85
|
-
: align(docLength(concat(parts)), joinedArgs);
|
86
|
-
|
87
|
-
return group(
|
88
|
-
ifBreak(concat(parts.concat(breakArgs)), concat(parts.concat(joinedArgs)))
|
89
|
-
);
|
90
|
-
}
|
91
|
-
};
|
data/src/nodes/hooks.js
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
const { concat, group, indent, line } = require("../prettier");
|
2
|
-
const { isEmptyStmts } = require("../utils");
|
3
|
-
|
4
|
-
// The `BEGIN` and `END` keywords are used to hook into the Ruby process. Any
|
5
|
-
// `BEGIN` blocks are executed right when the process starts up, and the `END`
|
6
|
-
// blocks are executed right before exiting.
|
7
|
-
//
|
8
|
-
// BEGIN {
|
9
|
-
// # content goes here
|
10
|
-
// }
|
11
|
-
//
|
12
|
-
// END {
|
13
|
-
// # content goes here
|
14
|
-
// }
|
15
|
-
//
|
16
|
-
// Interesting side note, you don't use `do...end` blocks with these hooks. Both
|
17
|
-
// nodes contain one child which is a `stmts` node.
|
18
|
-
function printHook(name) {
|
19
|
-
return function printHookWithName(path, opts, print) {
|
20
|
-
const stmtsNode = path.getValue().body[1];
|
21
|
-
const printedStmts = path.call(print, "body", 1);
|
22
|
-
|
23
|
-
const parts = [
|
24
|
-
name,
|
25
|
-
" ",
|
26
|
-
path.call(print, "body", 0),
|
27
|
-
indent(concat([line, printedStmts])),
|
28
|
-
concat([line, "}"])
|
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));
|
38
|
-
};
|
39
|
-
}
|
40
|
-
|
41
|
-
module.exports = {
|
42
|
-
BEGIN: printHook("BEGIN"),
|
43
|
-
END: printHook("END")
|
44
|
-
};
|
data/src/parser.js
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
const { spawnSync } = require("child_process");
|
2
|
-
const path = require("path");
|
3
|
-
|
4
|
-
// In order to properly parse ruby code, we need to tell the ruby process to
|
5
|
-
// parse using UTF-8. Unfortunately, the way that you accomplish this looks
|
6
|
-
// differently depending on your platform.
|
7
|
-
const LANG = (() => {
|
8
|
-
const { env, platform } = process;
|
9
|
-
const envValue = env.LC_ALL || env.LC_CTYPE || env.LANG;
|
10
|
-
|
11
|
-
// If an env var is set for the locale that already includes UTF-8 in the
|
12
|
-
// name, then assume we can go with that.
|
13
|
-
if (envValue && envValue.includes("UTF-8")) {
|
14
|
-
return envValue;
|
15
|
-
}
|
16
|
-
|
17
|
-
// Otherwise, we're going to guess which encoding to use based on the system.
|
18
|
-
// This is probably not the best approach in the world, as you could be on
|
19
|
-
// linux and not have C.UTF-8, but in that case you're probably passing an env
|
20
|
-
// var for it. This object below represents all of the possible values of
|
21
|
-
// process.platform per:
|
22
|
-
// https://nodejs.org/api/process.html#process_process_platform
|
23
|
-
return {
|
24
|
-
aix: "C.UTF-8",
|
25
|
-
darwin: "en_US.UTF-8",
|
26
|
-
freebsd: "C.UTF-8",
|
27
|
-
linux: "C.UTF-8",
|
28
|
-
openbsd: "C.UTF-8",
|
29
|
-
sunos: "C.UTF-8",
|
30
|
-
win32: ".UTF-8"
|
31
|
-
}[platform];
|
32
|
-
})();
|
33
|
-
|
34
|
-
// This function is responsible for taking an input string of text and returning
|
35
|
-
// to prettier a JavaScript object that is the equivalent AST that represents
|
36
|
-
// the code stored in that string. We accomplish this by spawning a new Ruby
|
37
|
-
// process of parser.rb and reading JSON off STDOUT.
|
38
|
-
function parse(text, _parsers, _opts) {
|
39
|
-
const child = spawnSync(
|
40
|
-
"ruby",
|
41
|
-
["--disable-gems", path.join(__dirname, "./parser.rb")],
|
42
|
-
{
|
43
|
-
env: Object.assign({}, process.env, { LANG }),
|
44
|
-
input: text,
|
45
|
-
maxBuffer: 10 * 1024 * 1024 // 10MB
|
46
|
-
}
|
47
|
-
);
|
48
|
-
|
49
|
-
const error = child.stderr.toString();
|
50
|
-
if (error) {
|
51
|
-
throw new Error(error);
|
52
|
-
}
|
53
|
-
|
54
|
-
const response = child.stdout.toString();
|
55
|
-
return JSON.parse(response);
|
56
|
-
}
|
57
|
-
|
58
|
-
const pragmaPattern = /#\s*@(prettier|format)/;
|
59
|
-
|
60
|
-
// This function handles checking whether or not the source string has the
|
61
|
-
// pragma for prettier. This is an optional workflow for incremental adoption.
|
62
|
-
function hasPragma(text) {
|
63
|
-
return pragmaPattern.test(text);
|
64
|
-
}
|
65
|
-
|
66
|
-
// This function is critical for comments and cursor support, and is responsible
|
67
|
-
// for returning the index of the character within the source string that is the
|
68
|
-
// beginning of the given node.
|
69
|
-
function locStart(node) {
|
70
|
-
return node.char_start;
|
71
|
-
}
|
72
|
-
|
73
|
-
// This function is critical for comments and cursor support, and is responsible
|
74
|
-
// for returning the index of the character within the source string that is the
|
75
|
-
// ending of the given node.
|
76
|
-
function locEnd(node) {
|
77
|
-
return node.char_end;
|
78
|
-
}
|
79
|
-
|
80
|
-
module.exports = {
|
81
|
-
parse,
|
82
|
-
astFormat: "ruby",
|
83
|
-
hasPragma,
|
84
|
-
locStart,
|
85
|
-
locEnd
|
86
|
-
};
|