-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
plugin.js
381 lines (350 loc) · 9.39 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { spawn } from "child_process";
import fs from "fs";
import net from "net";
import os from "os";
import path from "path";
import process from "process";
import url from "url";
import { resolveConfigFile } from "prettier";
// In order to properly parse ruby code, we need to tell the ruby process to
// parse using UTF-8. Unfortunately, the way that you accomplish this looks
// differently depending on your platform.
function getLang() {
const { env, platform } = process;
const envValue = env.LC_ALL || env.LC_CTYPE || env.LANG;
// If an env var is set for the locale that already includes UTF-8 in the
// name, then assume we can go with that.
if (envValue && envValue.includes("UTF-8")) {
return envValue;
}
// Otherwise, we're going to guess which encoding to use based on the system.
// This is probably not the best approach in the world, as you could be on
// linux and not have C.UTF-8, but in that case you're probably passing an env
// var for it. This object below represents all of the possible values of
// process.platform per:
// https://nodejs.org/api/process.html#process_process_platform
return {
aix: "C.UTF-8",
android: "C.UTF-8",
cygwin: "C.UTF-8",
darwin: "en_US.UTF-8",
freebsd: "C.UTF-8",
haiku: "C.UTF-8",
linux: "C.UTF-8",
netbsd: "C.UTF-8",
openbsd: "C.UTF-8",
sunos: "C.UTF-8",
win32: ".UTF-8"
}[platform];
}
// Return the list of plugins that should be passed to the server process.
function getPlugins(opts) {
const plugins = new Set();
const rubyPlugins = opts.rubyPlugins.trim();
if (rubyPlugins.length > 0) {
rubyPlugins.split(",").forEach((plugin) => plugins.add(plugin.trim()));
}
if (opts.rubySingleQuote) {
plugins.add("plugin/single_quotes");
}
if (opts.trailingComma !== "none") {
plugins.add("plugin/trailing_comma");
}
return Array.from(plugins);
}
// Create a file that will act as a communication mechanism, spawn a parser
// server with that filepath as an argument, then wait for the file to be
// created that will contain the connection information.
export async function spawnServer(opts, killOnExit = true) {
const tmpdir = os.tmpdir();
const filepath = path.join(tmpdir, `prettier-ruby-parser-${process.pid}.txt`);
const options = {
env: Object.assign({}, process.env, { LANG: getLang() }),
stdio: ["ignore", "ignore", "inherit"],
detached: true
};
if (opts.filepath) {
const prettierConfig = await resolveConfigFile(opts.filepath);
options.cwd = path.dirname(prettierConfig);
}
const server = spawn(
opts.rubyExecutablePath || "ruby",
[
url.fileURLToPath(new URL("./server.rb", import.meta.url)),
`--plugins=${getPlugins(opts).join(",")}`,
filepath
],
options
);
server.unref();
if (killOnExit) {
process.on("exit", () => {
if (fs.existsSync(filepath)) {
fs.unlinkSync(filepath);
}
try {
if (server.pid) {
// Kill the server process if it's still running. If we're on windows
// we're going to use the process ID number. If we're not, we're going
// to use the negative process ID to indicate the group.
const pid = process.platform === "win32" ? server.pid : -server.pid;
process.kill(pid);
}
} catch (error) {
// If there's an error killing the process, we're going to ignore it.
}
});
}
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
if (fs.existsSync(filepath)) {
const connectionJSON = fs.readFileSync(filepath).toString("utf-8");
resolve({
serverPID: server.pid,
connectionFilepath: filepath,
connectionOptions: JSON.parse(connectionJSON)
});
clearTimeout(timeout);
clearInterval(interval);
} else if (server.exitCode) {
reject(new Error("Failed to start parse server."));
clearTimeout(timeout);
clearInterval(interval);
}
}, 100);
const timeout = setTimeout(
() => {
const message =
"Failed to get connection options from parse server in time. If this happens repeatedly, try increasing the PRETTIER_RUBY_TIMEOUT_MS environment variable beyond 10000.";
clearInterval(interval);
reject(new Error(message));
},
parseInt(process.env.PRETTIER_RUBY_TIMEOUT_MS || "10000", 10)
);
});
}
let connectionOptions;
if (process.env.PRETTIER_RUBY_HOST) {
connectionOptions = JSON.parse(process.env.PRETTIER_RUBY_HOST);
}
// Formats and sends an asynchronous request to the parser server.
async function parse(parser, source, opts) {
if (!connectionOptions) {
const spawnedServer = await spawnServer(opts);
connectionOptions = spawnedServer.connectionOptions;
}
return new Promise((resolve, reject) => {
const socket = new net.Socket();
let chunks = "";
socket.on("error", (error) => {
reject(error);
});
socket.on("data", (data) => {
chunks += data.toString("utf-8");
});
socket.on("end", () => {
const response = JSON.parse(chunks);
if (response.error) {
const error = new Error(response.error);
if (response.loc) {
error.loc = response.loc;
}
reject(error);
}
resolve(response);
});
socket.connect(connectionOptions, () => {
socket.end(
JSON.stringify({
parser,
source,
maxwidth: opts.printWidth,
tabwidth: opts.tabWidth
})
);
});
});
}
// Metadata mostly pulled from linguist and rubocop:
// https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
// https://github.com/rubocop/rubocop/blob/master/spec/rubocop/target_finder_spec.rb
const plugin = {
languages: [
{
name: "Ruby",
parsers: ["ruby"],
extensions: [
".arb",
".axlsx",
".builder",
".eye",
".fcgi",
".gemfile",
".gemspec",
".god",
".jb",
".jbuilder",
".mspec",
".opal",
".pluginspec",
".podspec",
".rabl",
".rake",
".rb",
".rbi",
".rbuild",
".rbw",
".rbx",
".ru",
".ruby",
".thor",
".watchr"
],
filenames: [
".irbrc",
".pryrc",
".simplecov",
"Appraisals",
"Berksfile",
"Brewfile",
"Buildfile",
"Capfile",
"Cheffile",
"Dangerfile",
"Deliverfile",
"Fastfile",
"Gemfile",
"Guardfile",
"Jarfile",
"Mavenfile",
"Podfile",
"Puppetfile",
"Rakefile",
"Snapfile",
"Thorfile",
"Vagabondfile",
"Vagrantfile",
"buildfile"
],
interpreters: ["jruby", "macruby", "rake", "rbx", "ruby"],
linguistLanguageId: 326,
vscodeLanguageIds: ["ruby"]
},
{
name: "RBS",
parsers: ["rbs"],
extensions: [".rbs"]
},
{
name: "HAML",
parsers: ["haml"],
extensions: [".haml"],
vscodeLanguageIds: ["haml"]
}
],
parsers: {
ruby: {
parse(text, opts) {
return parse("ruby", text, opts);
},
astFormat: "ruby",
hasPragma(text) {
return /^\s*#[^\S\n]*@(?:prettier|format)\s*?(?:\n|$)/m.test(text);
},
locStart() {
return 0;
},
locEnd() {
return 0;
}
},
rbs: {
parse(text, opts) {
return parse("rbs", text, opts);
},
astFormat: "rbs",
hasPragma(text) {
return /^\s*#[^\S\n]*@(prettier|format)\s*(\n|$)/.test(text);
},
locStart() {
return 0;
},
locEnd() {
return 0;
}
},
haml: {
parse(text, opts) {
return parse("haml", text, opts);
},
astFormat: "haml",
hasPragma(text) {
return /^\s*-#\s*@(prettier|format)/.test(text);
},
locStart() {
return 0;
},
locEnd() {
return 0;
}
}
},
printers: {
ruby: {
print(path) {
return path.getValue();
},
insertPragma(text) {
return `# @format${text.startsWith("#") ? "\n" : "\n\n"}${text}`;
}
},
rbs: {
print(path) {
return path.getValue();
},
insertPragma(text) {
return `# @format${text.startsWith("#") ? "\n" : "\n\n"}${text}`;
}
},
haml: {
print(path) {
return path.getValue();
},
insertPragma(text) {
return `-# @format${text.startsWith("-#") ? "\n" : "\n\n"}${text}`;
}
}
},
options: {
rubyPlugins: {
type: "string",
category: "Ruby",
default: "",
description: "The comma-separated list of plugins to require.",
since: "3.1.0"
},
rubySingleQuote: {
type: "boolean",
category: "Ruby",
default: false,
description:
"When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals.",
since: "1.0.0"
},
rubyExecutablePath: {
type: "string",
category: "Ruby",
default: "ruby",
description:
"The path to the Ruby executable to use to run the formatter.",
since: "3.3.0"
}
},
defaultOptions: {
printWidth: 80,
tabWidth: 2,
trailingComma: "none",
singleQuote: false
}
};
export default plugin;