prettier 1.5.5 → 2.0.0.pre.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +344 -282
  3. data/CONTRIBUTING.md +8 -11
  4. data/LICENSE +1 -1
  5. data/README.md +30 -12
  6. data/dist/haml/embed.js +53 -0
  7. data/dist/haml/parser.js +31 -0
  8. data/{src → dist}/haml/parser.rb +0 -0
  9. data/dist/haml/printer.js +336 -0
  10. data/dist/parser/getInfo.js +17 -0
  11. data/{src → dist}/parser/netcat.js +1 -0
  12. data/dist/parser/parseSync.js +142 -0
  13. data/dist/parser/server.rb +140 -0
  14. data/dist/plugin.js +143 -0
  15. data/dist/prettier.js +15 -0
  16. data/dist/rbs/parser.js +34 -0
  17. data/{src → dist}/rbs/parser.rb +0 -0
  18. data/dist/rbs/printer.js +517 -0
  19. data/dist/ruby/embed.js +110 -0
  20. data/dist/ruby/nodes/alias.js +59 -0
  21. data/{src → dist}/ruby/nodes/aref.js +26 -35
  22. data/dist/ruby/nodes/args.js +165 -0
  23. data/dist/ruby/nodes/arrays.js +126 -0
  24. data/dist/ruby/nodes/assign.js +41 -0
  25. data/dist/ruby/nodes/blocks.js +68 -0
  26. data/dist/ruby/nodes/calls.js +260 -0
  27. data/dist/ruby/nodes/case.js +50 -0
  28. data/dist/ruby/nodes/class.js +54 -0
  29. data/dist/ruby/nodes/commands.js +124 -0
  30. data/dist/ruby/nodes/conditionals.js +242 -0
  31. data/dist/ruby/nodes/constants.js +38 -0
  32. data/dist/ruby/nodes/flow.js +66 -0
  33. data/dist/ruby/nodes/hashes.js +130 -0
  34. data/dist/ruby/nodes/heredocs.js +30 -0
  35. data/dist/ruby/nodes/hooks.js +35 -0
  36. data/dist/ruby/nodes/ints.js +27 -0
  37. data/dist/ruby/nodes/lambdas.js +69 -0
  38. data/dist/ruby/nodes/loops.js +73 -0
  39. data/dist/ruby/nodes/massign.js +73 -0
  40. data/dist/ruby/nodes/methods.js +70 -0
  41. data/dist/ruby/nodes/operators.js +70 -0
  42. data/dist/ruby/nodes/params.js +89 -0
  43. data/dist/ruby/nodes/patterns.js +109 -0
  44. data/dist/ruby/nodes/regexp.js +45 -0
  45. data/dist/ruby/nodes/rescue.js +82 -0
  46. data/dist/ruby/nodes/return.js +75 -0
  47. data/dist/ruby/nodes/statements.js +111 -0
  48. data/dist/ruby/nodes/strings.js +218 -0
  49. data/dist/ruby/nodes/super.js +30 -0
  50. data/dist/ruby/nodes/undef.js +26 -0
  51. data/dist/ruby/nodes.js +151 -0
  52. data/dist/ruby/parser.js +34 -0
  53. data/{src → dist}/ruby/parser.rb +1274 -288
  54. data/dist/ruby/printer.js +125 -0
  55. data/dist/ruby/toProc.js +93 -0
  56. data/dist/types/haml.js +4 -0
  57. data/dist/types/plugin.js +3 -0
  58. data/dist/types/rbs.js +4 -0
  59. data/dist/types/ruby.js +4 -0
  60. data/dist/types/utils.js +2 -0
  61. data/dist/types.js +30 -0
  62. data/dist/utils/containsAssignment.js +15 -0
  63. data/dist/utils/getTrailingComma.js +6 -0
  64. data/dist/utils/hasAncestor.js +15 -0
  65. data/{src → dist}/utils/inlineEnsureParens.js +16 -16
  66. data/dist/utils/isEmptyBodyStmt.js +10 -0
  67. data/dist/utils/isEmptyStmts.js +10 -0
  68. data/dist/utils/literal.js +8 -0
  69. data/dist/utils/literallineWithoutBreakParent.js +8 -0
  70. data/dist/utils/makeCall.js +13 -0
  71. data/dist/utils/noIndent.js +11 -0
  72. data/dist/utils/printEmptyCollection.js +44 -0
  73. data/dist/utils/skipAssignIndent.js +15 -0
  74. data/dist/utils.js +30 -0
  75. data/node_modules/prettier/bin-prettier.js +13343 -10961
  76. data/node_modules/prettier/doc.js +4829 -0
  77. data/node_modules/prettier/index.js +23988 -22229
  78. data/node_modules/prettier/package.json +23 -0
  79. data/node_modules/prettier/parser-angular.js +60 -40
  80. data/node_modules/prettier/parser-babel.js +22 -1
  81. data/node_modules/prettier/parser-espree.js +22 -1
  82. data/node_modules/prettier/parser-flow.js +22 -1
  83. data/node_modules/prettier/parser-glimmer.js +1 -1
  84. data/node_modules/prettier/parser-graphql.js +1 -1
  85. data/node_modules/prettier/parser-html.js +82 -63
  86. data/node_modules/prettier/parser-markdown.js +24 -9
  87. data/node_modules/prettier/parser-meriyah.js +22 -1
  88. data/node_modules/prettier/parser-postcss.js +22 -1
  89. data/node_modules/prettier/parser-typescript.js +22 -1
  90. data/node_modules/prettier/parser-yaml.js +2 -2
  91. data/node_modules/prettier/third-party.js +1734 -862
  92. data/package.json +27 -19
  93. data/rubocop.yml +9 -0
  94. metadata +77 -77
  95. data/src/haml/embed.js +0 -87
  96. data/src/haml/nodes/comment.js +0 -27
  97. data/src/haml/nodes/doctype.js +0 -34
  98. data/src/haml/nodes/filter.js +0 -16
  99. data/src/haml/nodes/hamlComment.js +0 -21
  100. data/src/haml/nodes/plain.js +0 -6
  101. data/src/haml/nodes/root.js +0 -8
  102. data/src/haml/nodes/script.js +0 -33
  103. data/src/haml/nodes/silentScript.js +0 -59
  104. data/src/haml/nodes/tag.js +0 -232
  105. data/src/haml/parser.js +0 -22
  106. data/src/haml/printer.js +0 -28
  107. data/src/parser/parseSync.js +0 -170
  108. data/src/parser/server.rb +0 -66
  109. data/src/plugin.js +0 -148
  110. data/src/prettier.js +0 -16
  111. data/src/rbs/parser.js +0 -39
  112. data/src/rbs/printer.js +0 -615
  113. data/src/ruby/embed.js +0 -142
  114. data/src/ruby/nodes/alias.js +0 -73
  115. data/src/ruby/nodes/args.js +0 -178
  116. data/src/ruby/nodes/arrays.js +0 -162
  117. data/src/ruby/nodes/assign.js +0 -47
  118. data/src/ruby/nodes/blocks.js +0 -90
  119. data/src/ruby/nodes/calls.js +0 -199
  120. data/src/ruby/nodes/case.js +0 -65
  121. data/src/ruby/nodes/class.js +0 -64
  122. data/src/ruby/nodes/commands.js +0 -131
  123. data/src/ruby/nodes/conditionals.js +0 -280
  124. data/src/ruby/nodes/constants.js +0 -43
  125. data/src/ruby/nodes/flow.js +0 -74
  126. data/src/ruby/nodes/hashes.js +0 -164
  127. data/src/ruby/nodes/heredocs.js +0 -36
  128. data/src/ruby/nodes/hooks.js +0 -34
  129. data/src/ruby/nodes/ints.js +0 -31
  130. data/src/ruby/nodes/lambdas.js +0 -76
  131. data/src/ruby/nodes/loops.js +0 -98
  132. data/src/ruby/nodes/massign.js +0 -98
  133. data/src/ruby/nodes/methods.js +0 -74
  134. data/src/ruby/nodes/operators.js +0 -83
  135. data/src/ruby/nodes/params.js +0 -113
  136. data/src/ruby/nodes/patterns.js +0 -157
  137. data/src/ruby/nodes/regexp.js +0 -56
  138. data/src/ruby/nodes/rescue.js +0 -101
  139. data/src/ruby/nodes/return.js +0 -94
  140. data/src/ruby/nodes/statements.js +0 -142
  141. data/src/ruby/nodes/strings.js +0 -177
  142. data/src/ruby/nodes/super.js +0 -35
  143. data/src/ruby/nodes/undef.js +0 -42
  144. data/src/ruby/nodes.js +0 -34
  145. data/src/ruby/parser.js +0 -39
  146. data/src/ruby/printer.js +0 -138
  147. data/src/ruby/toProc.js +0 -105
  148. data/src/utils/containsAssignment.js +0 -11
  149. data/src/utils/getTrailingComma.js +0 -5
  150. data/src/utils/hasAncestor.js +0 -17
  151. data/src/utils/isEmptyBodyStmt.js +0 -7
  152. data/src/utils/isEmptyStmts.js +0 -11
  153. data/src/utils/literal.js +0 -7
  154. data/src/utils/literallineWithoutBreakParent.js +0 -7
  155. data/src/utils/makeCall.js +0 -14
  156. data/src/utils/noIndent.js +0 -11
  157. data/src/utils/printEmptyCollection.js +0 -49
  158. data/src/utils/skipAssignIndent.js +0 -10
  159. data/src/utils.js +0 -13
@@ -1,232 +0,0 @@
1
- const {
2
- align,
3
- concat,
4
- fill,
5
- group,
6
- hardline,
7
- ifBreak,
8
- indent,
9
- join,
10
- line,
11
- softline
12
- } = require("../../prettier");
13
-
14
- function getDynamicAttributes(header, attributes) {
15
- const pairs = attributes
16
- .slice(1, -2)
17
- .split(",")
18
- .map((pair) => pair.slice(1).split('" => '));
19
-
20
- const parts = [concat([pairs[0][0], "=", pairs[0][1]])];
21
- pairs.slice(1).forEach((pair) => {
22
- parts.push(line, concat([pair[0], "=", pair[1]]));
23
- });
24
-
25
- return group(concat(["(", align(header + 1, fill(parts)), ")"]));
26
- }
27
-
28
- function getHashValue(value, opts) {
29
- if (typeof value !== "string") {
30
- return value.toString();
31
- }
32
-
33
- // This is a very special syntax created by the parser to let us know that
34
- // this should be printed literally instead of as a string.
35
- if (value.startsWith("&")) {
36
- return value.slice(1);
37
- }
38
-
39
- const quote = opts.rubySingleQuote && !value.includes("#{") ? "'" : '"';
40
- return `${quote}${value}${quote}`;
41
- }
42
-
43
- function getHashKey(key, opts) {
44
- let quoted = key;
45
- const joiner = opts.rubyHashLabel ? ":" : " =>";
46
-
47
- if (key.includes(":") || key.includes("-")) {
48
- const quote = opts.rubySingleQuote ? "'" : '"';
49
- quoted = `${quote}${key}${quote}`;
50
- }
51
-
52
- return `${opts.rubyHashLabel ? "" : ":"}${quoted}${joiner}`;
53
- }
54
-
55
- function getKeyValuePair(key, value, opts) {
56
- return `${getHashKey(key, opts)} ${getHashValue(value, opts)}`;
57
- }
58
-
59
- function getStaticAttributes(header, attributes, opts) {
60
- const keys = Object.keys(attributes).filter(
61
- (name) => !["class", "id"].includes(name)
62
- );
63
-
64
- const parts = [getKeyValuePair(keys[0], attributes[keys[0]], opts)];
65
-
66
- keys.slice(1).forEach((key) => {
67
- parts.push(",", line, getKeyValuePair(key, attributes[key], opts));
68
- });
69
-
70
- return group(concat(["{", align(header + 1, fill(parts)), "}"]));
71
- }
72
-
73
- function getAttributesObject(object, opts, level = 0) {
74
- if (typeof object !== "object") {
75
- return getHashValue(object, opts);
76
- }
77
-
78
- const boundary = level === 0 ? softline : line;
79
- const parts = Object.keys(object).map((key) =>
80
- concat([
81
- getHashKey(key, opts),
82
- " ",
83
- getAttributesObject(object[key], opts, level + 1)
84
- ])
85
- );
86
-
87
- // If we have support for multi-line attributes laid out like a regular hash,
88
- // then we print them that way here.
89
- if (opts.supportsMultiline) {
90
- return group(
91
- concat([
92
- "{",
93
- indent(group(concat([boundary, join(concat([",", line]), parts)]))),
94
- boundary,
95
- "}"
96
- ])
97
- );
98
- }
99
-
100
- // Otherwise, if we only have one attribute, then just print it inline
101
- // regardless of how long it is.
102
- if (parts.length === 0) {
103
- return group(concat(["{", parts[0], "}"]));
104
- }
105
-
106
- // Otherwise, depending on how long the line is it will split the content into
107
- // multi-line attributes that old Haml understands.
108
- return group(
109
- concat([
110
- "{",
111
- parts[0],
112
- ",",
113
- align(
114
- opts.headerLength + 1,
115
- concat([line, join(concat([",", line]), parts.slice(1))])
116
- ),
117
- "}"
118
- ])
119
- );
120
- }
121
-
122
- function getHeader(value, opts, supportsMultiline) {
123
- const { attributes } = value;
124
- const parts = [];
125
-
126
- if (value.name !== "div") {
127
- parts.push(`%${value.name}`);
128
- }
129
-
130
- if (attributes.class) {
131
- parts.push(`.${attributes.class.replace(/ /g, ".")}`);
132
- }
133
-
134
- if (attributes.id) {
135
- parts.push(`#${attributes.id}`);
136
- }
137
-
138
- if (value.dynamic_attributes.new) {
139
- parts.push(
140
- getDynamicAttributes(parts.join("").length, value.dynamic_attributes.new)
141
- );
142
- }
143
-
144
- if (
145
- Object.keys(attributes).some((name) => name !== "class" && name !== "id")
146
- ) {
147
- parts.push(getStaticAttributes(parts.join("").length, attributes, opts));
148
- }
149
-
150
- if (value.dynamic_attributes.old) {
151
- if (parts.length === 0) {
152
- parts.push("%div");
153
- }
154
-
155
- if (typeof value.dynamic_attributes.old === "string") {
156
- parts.push(value.dynamic_attributes.old);
157
- } else {
158
- parts.push(
159
- getAttributesObject(
160
- value.dynamic_attributes.old,
161
- Object.assign({}, opts, {
162
- supportsMultiline,
163
- headerLength: parts.join("").length
164
- })
165
- )
166
- );
167
- }
168
- }
169
-
170
- if (value.object_ref) {
171
- if (parts.length === 0) {
172
- parts.push("%div");
173
- }
174
- parts.push(value.object_ref);
175
- }
176
-
177
- if (value.nuke_outer_whitespace) {
178
- parts.push(">");
179
- }
180
-
181
- if (value.nuke_inner_whitespace) {
182
- parts.push("<");
183
- }
184
-
185
- if (value.self_closing) {
186
- parts.push("/");
187
- }
188
-
189
- if (value.value) {
190
- const prefix = value.parse ? "= " : ifBreak("", " ");
191
-
192
- return group(
193
- concat([
194
- group(concat(parts)),
195
- indent(concat([softline, prefix, value.value]))
196
- ])
197
- );
198
- }
199
-
200
- // In case none of the other if statements have matched and we're printing a
201
- // div, we need to explicitly add it back into the array.
202
- if (parts.length === 0 && value.name === "div") {
203
- parts.push("%div");
204
- }
205
-
206
- return group(concat(parts));
207
- }
208
-
209
- // https://haml.info/docs/yardoc/file.REFERENCE.html#element-name-
210
- function tag(path, opts, print) {
211
- const { children, value } = path.getValue();
212
-
213
- // This is kind of a total hack in that I don't think you're really supposed
214
- // to directly use `path.stack`, but it's the easiest way to get the root node
215
- // without having to know how many levels deep we are.
216
- const { supports_multiline } = path.stack[0];
217
-
218
- const header = getHeader(value, opts, supports_multiline);
219
-
220
- if (children.length === 0) {
221
- return header;
222
- }
223
-
224
- return group(
225
- concat([
226
- header,
227
- indent(concat([hardline, join(hardline, path.map(print, "children"))]))
228
- ])
229
- );
230
- }
231
-
232
- module.exports = tag;
data/src/haml/parser.js DELETED
@@ -1,22 +0,0 @@
1
- const parseSync = require("../parser/parseSync");
2
-
3
- const parse = (text, _parsers, opts) => {
4
- return parseSync("haml", text, opts);
5
- };
6
-
7
- const pragmaPattern = /^\s*-#\s*@(prettier|format)/;
8
- const hasPragma = (text) => pragmaPattern.test(text);
9
-
10
- // These functions are just placeholders until we can actually perform this
11
- // properly. The functions are necessary otherwise the format with cursor
12
- // functions break.
13
- const locStart = (_node) => 0;
14
- const locEnd = (_node) => 0;
15
-
16
- module.exports = {
17
- parse,
18
- astFormat: "haml",
19
- hasPragma,
20
- locStart,
21
- locEnd
22
- };
data/src/haml/printer.js DELETED
@@ -1,28 +0,0 @@
1
- const embed = require("./embed");
2
- const nodes = {
3
- comment: require("./nodes/comment"),
4
- doctype: require("./nodes/doctype"),
5
- filter: require("./nodes/filter"),
6
- haml_comment: require("./nodes/hamlComment"),
7
- plain: require("./nodes/plain"),
8
- root: require("./nodes/root"),
9
- script: require("./nodes/script"),
10
- silent_script: require("./nodes/silentScript"),
11
- tag: require("./nodes/tag")
12
- };
13
-
14
- const genericPrint = (path, opts, print) => {
15
- const { type } = path.getValue();
16
-
17
- /* istanbul ignore next */
18
- if (!(type in nodes)) {
19
- throw new Error(`Unsupported node encountered: ${type}`);
20
- }
21
-
22
- return nodes[type](path, opts, print);
23
- };
24
-
25
- module.exports = {
26
- embed,
27
- print: genericPrint
28
- };
@@ -1,170 +0,0 @@
1
- const { spawn, spawnSync, execSync } = require("child_process");
2
- const { existsSync, mkdtempSync } = require("fs");
3
- const os = require("os");
4
- const path = require("path");
5
- const process = require("process");
6
-
7
- let sockfile = process.env.PRETTIER_RUBY_HOST;
8
- let netcat;
9
-
10
- // In order to properly parse ruby code, we need to tell the ruby process to
11
- // parse using UTF-8. Unfortunately, the way that you accomplish this looks
12
- // differently depending on your platform.
13
- /* istanbul ignore next */
14
- function getLang() {
15
- const { env, platform } = process;
16
- const envValue = env.LC_ALL || env.LC_CTYPE || env.LANG;
17
-
18
- // If an env var is set for the locale that already includes UTF-8 in the
19
- // name, then assume we can go with that.
20
- if (envValue && envValue.includes("UTF-8")) {
21
- return envValue;
22
- }
23
-
24
- // Otherwise, we're going to guess which encoding to use based on the system.
25
- // This is probably not the best approach in the world, as you could be on
26
- // linux and not have C.UTF-8, but in that case you're probably passing an env
27
- // var for it. This object below represents all of the possible values of
28
- // process.platform per:
29
- // https://nodejs.org/api/process.html#process_process_platform
30
- return {
31
- aix: "C.UTF-8",
32
- darwin: "en_US.UTF-8",
33
- freebsd: "C.UTF-8",
34
- linux: "C.UTF-8",
35
- openbsd: "C.UTF-8",
36
- sunos: "C.UTF-8",
37
- win32: ".UTF-8"
38
- }[platform];
39
- }
40
-
41
- // Spawn the parser.rb subprocess. We do this since booting Ruby is slow, and we
42
- // can re-use the parser process multiple times since it is statelesss.
43
- function spawnParseServer() {
44
- const tmpDir = mkdtempSync(path.join(os.tmpdir(), "prettier-ruby"));
45
- const tmpFile = path.join(tmpDir, `${process.pid}.sock`);
46
-
47
- const server = spawn("ruby", [path.join(__dirname, "./server.rb"), tmpFile], {
48
- env: Object.assign({}, process.env, { LANG: getLang() }),
49
- detached: true,
50
- stdio: "inherit"
51
- });
52
-
53
- process.on("exit", () => {
54
- try {
55
- process.kill(-server.pid);
56
- } catch (e) {
57
- // ignore
58
- }
59
- });
60
-
61
- server.unref();
62
- const now = new Date();
63
-
64
- // Wait for server to go live.
65
- while (!existsSync(tmpFile) && new Date() - now < 3000) {
66
- execSync("sleep 0.1");
67
- }
68
-
69
- return tmpFile;
70
- }
71
-
72
- // Checks to see if an executable is available.
73
- function hasCommand(name) {
74
- let result;
75
-
76
- if (os.type() === "Windows_NT") {
77
- result = spawnSync("where", [name]);
78
- } else {
79
- result = spawnSync("command", ["-v", name]);
80
- }
81
-
82
- return result.status === 0;
83
- }
84
-
85
- // Finds a netcat-like adapter to use for sending data to a socket. We order
86
- // these by likelihood of being found so we can avoid some shell-outs.
87
- function findNetcat(opts) {
88
- if (opts.rubyNetcatCommand) {
89
- const splits = opts.rubyNetcatCommand.split(" ");
90
- return { command: splits[0], args: splits.slice(1) };
91
- }
92
-
93
- if (hasCommand("nc")) {
94
- return { command: "nc", args: ["-U"] };
95
- }
96
-
97
- if (hasCommand("telnet")) {
98
- return { command: "telnet", args: ["-u"] };
99
- }
100
-
101
- if (hasCommand("ncat")) {
102
- return { command: "ncat", args: ["-U"] };
103
- }
104
-
105
- if (hasCommand("socat")) {
106
- return { command: "socat", args: ["-"] };
107
- }
108
-
109
- return { command: "node", args: [require.resolve("./netcat.js")] };
110
- }
111
-
112
- // Formats and sends a request to the parser server. We use netcat (or something
113
- // like it) here since Prettier requires the results of `parse` to be
114
- // synchronous and Node.js does not offer a mechanism for synchronous socket
115
- // requests.
116
- function parseSync(parser, source, opts) {
117
- if (!sockfile) {
118
- sockfile = spawnParseServer();
119
- }
120
-
121
- if (!netcat) {
122
- netcat = findNetcat(opts);
123
- }
124
-
125
- const response = spawnSync(netcat.command, netcat.args.concat(sockfile), {
126
- input: `${parser}|${source}`,
127
- maxBuffer: 15 * 1024 * 1024
128
- });
129
-
130
- const stdout = response.stdout.toString();
131
- const stderr = response.stderr.toString();
132
- const { status } = response;
133
-
134
- // We need special handling in case the user's version of nc doesn't support
135
- // using unix sockets.
136
- if (
137
- stderr.includes("invalid option -- U") ||
138
- stderr.includes("Protocol not supported")
139
- ) {
140
- throw new Error(`
141
- @prettier/plugin-ruby uses netcat to communicate over unix sockets between
142
- the node.js process running prettier and an underlying Ruby process used
143
- for parsing. Unfortunately the version of netcat that you have installed
144
- does not support unix sockets. To solve this either uninstall the version
145
- of netcat that you're using and use a different implementation, or change
146
- the value of the rubyNetcatCommand option in your prettier configuration.
147
- `);
148
- }
149
-
150
- // If we didn't receive anything over stdout or we have a bad exit status,
151
- // then throw whatever we can.
152
- if (stdout.length === 0 || (status !== null && status !== 0)) {
153
- throw new Error(stderr || "An unknown error occurred");
154
- }
155
-
156
- const parsed = JSON.parse(stdout);
157
-
158
- if (parsed.error) {
159
- const error = new Error(parsed.error);
160
- if (parsed.loc) {
161
- error.loc = parsed.loc;
162
- }
163
-
164
- throw error;
165
- }
166
-
167
- return parsed;
168
- }
169
-
170
- module.exports = parseSync;
data/src/parser/server.rb DELETED
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup' if ENV['PLUGIN_RUBY_CI']
4
- require 'socket'
5
- require 'json'
6
-
7
- require_relative '../ruby/parser'
8
- require_relative '../rbs/parser'
9
- require_relative '../haml/parser'
10
-
11
- # Set the program name so that it's easy to find if we need it
12
- $PROGRAM_NAME = 'prettier-ruby-parser'
13
-
14
- # Make sure we trap these signals to be sure we get the quit command coming from
15
- # the parent node process
16
- quit = false
17
- trap(:QUIT) { quit = true } if RUBY_PLATFORM != 'java'
18
- trap(:INT) { quit = true }
19
- trap(:TERM) { quit = true }
20
-
21
- sockfile = ARGV.first || "/tmp/#{$PROGRAM_NAME}.sock"
22
- server = UNIXServer.new(sockfile)
23
-
24
- at_exit do
25
- server.close
26
- File.unlink(sockfile)
27
- end
28
-
29
- loop do
30
- break if quit
31
-
32
- # Start up a new thread that will handle each successive connection.
33
- Thread.new(server.accept_nonblock) do |socket|
34
- parser, source = socket.read.force_encoding('UTF-8').split('|', 2)
35
-
36
- response =
37
- case parser
38
- when 'ruby'
39
- Prettier::Parser.parse(source)
40
- when 'rbs'
41
- Prettier::RBSParser.parse(source)
42
- when 'haml'
43
- Prettier::HAMLParser.parse(source)
44
- end
45
-
46
- if response
47
- socket.write(JSON.fast_generate(response))
48
- else
49
- socket.write('{ "error": true }')
50
- end
51
- rescue Prettier::Parser::ParserError => error
52
- loc = { start: { line: error.lineno, column: error.column } }
53
- socket.write(JSON.fast_generate(error: error.message, loc: loc))
54
- rescue StandardError => error
55
- socket.write(JSON.fast_generate(error: error.message))
56
- ensure
57
- socket.close
58
- end
59
- rescue IO::WaitReadable, Errno::EINTR
60
- # Wait for select(2) to give us a connection that has content for 1 second.
61
- # Otherwise timeout and continue on (so that we hit our "break if quit"
62
- # pretty often).
63
- IO.select([server], nil, nil, 1)
64
-
65
- retry unless quit
66
- end
data/src/plugin.js DELETED
@@ -1,148 +0,0 @@
1
- const rubyPrinter = require("./ruby/printer");
2
- const rubyParser = require("./ruby/parser");
3
-
4
- const rbsPrinter = require("./rbs/printer");
5
- const rbsParser = require("./rbs/parser");
6
-
7
- const hamlPrinter = require("./haml/printer");
8
- const hamlParser = require("./haml/parser");
9
-
10
- /*
11
- * metadata mostly pulled from linguist and rubocop:
12
- * https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
13
- * https://github.com/rubocop-hq/rubocop/blob/master/spec/rubocop/target_finder_spec.rb
14
- */
15
-
16
- module.exports = {
17
- languages: [
18
- {
19
- name: "Ruby",
20
- parsers: ["ruby"],
21
- extensions: [
22
- ".arb",
23
- ".axlsx",
24
- ".builder",
25
- ".eye",
26
- ".fcgi",
27
- ".gemfile",
28
- ".gemspec",
29
- ".god",
30
- ".jb",
31
- ".jbuilder",
32
- ".mspec",
33
- ".opal",
34
- ".pluginspec",
35
- ".podspec",
36
- ".rabl",
37
- ".rake",
38
- ".rb",
39
- ".rbi",
40
- ".rbuild",
41
- ".rbw",
42
- ".rbx",
43
- ".ru",
44
- ".ruby",
45
- ".thor",
46
- ".watchr"
47
- ],
48
- filenames: [
49
- ".irbrc",
50
- ".pryrc",
51
- ".simplecov",
52
- "Appraisals",
53
- "Berksfile",
54
- "Brewfile",
55
- "Buildfile",
56
- "Capfile",
57
- "Cheffile",
58
- "Dangerfile",
59
- "Deliverfile",
60
- "Fastfile",
61
- "Gemfile",
62
- "Guardfile",
63
- "Jarfile",
64
- "Mavenfile",
65
- "Podfile",
66
- "Puppetfile",
67
- "Rakefile",
68
- "Snapfile",
69
- "Thorfile",
70
- "Vagabondfile",
71
- "Vagrantfile",
72
- "buildfile"
73
- ],
74
- interpreters: ["jruby", "macruby", "rake", "rbx", "ruby"],
75
- linguistLanguageId: 326,
76
- vscodeLanguageIds: ["ruby"]
77
- },
78
- {
79
- name: "RBS",
80
- parsers: ["rbs"],
81
- extensions: [".rbs"]
82
- },
83
- {
84
- name: "HAML",
85
- parsers: ["haml"],
86
- extensions: [".haml"],
87
- vscodeLanguageIds: ["haml"]
88
- }
89
- ],
90
- parsers: {
91
- ruby: rubyParser,
92
- rbs: rbsParser,
93
- haml: hamlParser
94
- },
95
- printers: {
96
- ruby: rubyPrinter,
97
- rbs: rbsPrinter,
98
- haml: hamlPrinter
99
- },
100
- options: {
101
- rubyArrayLiteral: {
102
- type: "boolean",
103
- category: "Ruby",
104
- default: true,
105
- description:
106
- "When possible, favor the use of string and symbol array literals."
107
- },
108
- rubyHashLabel: {
109
- type: "boolean",
110
- category: "Ruby",
111
- default: true,
112
- description:
113
- "When possible, uses the shortened hash key syntax, as opposed to hash rockets."
114
- },
115
- rubyModifier: {
116
- type: "boolean",
117
- category: "Ruby",
118
- default: true,
119
- description:
120
- "When it fits on one line, allows if, unless, while, and until statements to use the modifier form."
121
- },
122
- rubyNetcatCommand: {
123
- type: "string",
124
- category: "Ruby",
125
- description:
126
- 'The prefix of the command to execute to communicate between the node.js process and the Ruby process. (For example, "nc -U" or "telnet -u") Normally you should not set this option.'
127
- },
128
- rubySingleQuote: {
129
- type: "boolean",
130
- category: "Ruby",
131
- default: true,
132
- description:
133
- "When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals."
134
- },
135
- rubyToProc: {
136
- type: "boolean",
137
- category: "Ruby",
138
- default: false,
139
- description:
140
- "When possible, convert blocks to the more concise Symbol#to_proc syntax."
141
- }
142
- },
143
- defaultOptions: {
144
- printWidth: 80,
145
- tabWidth: 2,
146
- trailingComma: "none"
147
- }
148
- };
data/src/prettier.js DELETED
@@ -1,16 +0,0 @@
1
- // If `RBPRETTIER` is set, then this is being run from the `Prettier::run` ruby
2
- // method. In that case, we need to pull `prettier` from the node_modules
3
- // directly, as it's been shipped with the gem.
4
- /* istanbul ignore next */
5
- const source = process.env.RBPRETTIER ? "../node_modules/prettier" : "prettier";
6
-
7
- const prettier = require(source);
8
-
9
- // Just combine all the things into one big object so that we can import
10
- // whatever we need from prettier without having to dive too deeply.
11
- module.exports = Object.assign(
12
- {},
13
- prettier.doc.builders,
14
- prettier.doc.utils,
15
- prettier.util
16
- );