2017-06-13 19:20:13 +03:00
|
|
|
/* eslint-env worker */
|
|
|
|
/* eslint no-var: off, strict: off */
|
|
|
|
|
2018-03-16 17:37:08 +03:00
|
|
|
var parsersLoaded = {};
|
|
|
|
|
2017-06-13 19:20:13 +03:00
|
|
|
// "Polyfills" in order for all the code to run
|
2018-04-18 00:09:36 +03:00
|
|
|
/* eslint-disable no-undef, no-global-assign */
|
2017-06-13 19:20:13 +03:00
|
|
|
self.global = self;
|
2017-11-07 09:11:31 +03:00
|
|
|
self.util = {};
|
|
|
|
self.path = {};
|
2018-01-07 14:59:14 +03:00
|
|
|
self.path.resolve = self.path.join = self.path.dirname = function() {
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
self.path.parse = function() {
|
|
|
|
return { root: "" };
|
|
|
|
};
|
2017-06-13 19:20:13 +03:00
|
|
|
self.Buffer = {
|
|
|
|
isBuffer: function() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2017-12-06 03:13:38 +03:00
|
|
|
self.constants = {};
|
2018-04-24 16:45:06 +03:00
|
|
|
module$1 = module = os = crypto = buffer = {};
|
2017-12-05 13:14:03 +03:00
|
|
|
self.fs = { readFile: function() {} };
|
2017-09-16 16:35:32 +03:00
|
|
|
os.homedir = function() {
|
|
|
|
return "/home/prettier";
|
|
|
|
};
|
2018-04-12 00:22:03 +03:00
|
|
|
os.EOL = "\n";
|
2018-01-07 14:59:14 +03:00
|
|
|
self.process = {
|
|
|
|
argv: [],
|
|
|
|
env: { PRETTIER_DEBUG: true },
|
|
|
|
version: "v8.5.0",
|
|
|
|
binding: function() {
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
cwd: function() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
2017-06-13 19:20:13 +03:00
|
|
|
self.assert = { ok: function() {}, strictEqual: function() {} };
|
|
|
|
self.require = function require(path) {
|
2017-10-03 16:03:52 +03:00
|
|
|
if (path === "stream") {
|
|
|
|
return { PassThrough() {} };
|
|
|
|
}
|
2017-11-28 01:27:25 +03:00
|
|
|
if (path === "./third-party") {
|
|
|
|
return {};
|
|
|
|
}
|
2017-12-06 01:14:02 +03:00
|
|
|
|
|
|
|
if (~path.indexOf("parser-")) {
|
|
|
|
var parser = path.replace(/.+-/, "");
|
|
|
|
if (!parsersLoaded[parser]) {
|
|
|
|
importScripts("lib/parser-" + parser + ".js");
|
|
|
|
parsersLoaded[parser] = true;
|
|
|
|
}
|
|
|
|
return self[parser];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self[path];
|
2017-06-13 19:20:13 +03:00
|
|
|
};
|
2018-04-18 00:09:36 +03:00
|
|
|
/* eslint-enable */
|
2017-06-13 19:20:13 +03:00
|
|
|
|
2017-12-28 14:37:47 +03:00
|
|
|
var prettier;
|
2017-06-13 19:20:13 +03:00
|
|
|
importScripts("lib/index.js");
|
2017-12-28 14:37:47 +03:00
|
|
|
if (typeof prettier === "undefined") {
|
|
|
|
prettier = module.exports; // eslint-disable-line
|
|
|
|
}
|
|
|
|
if (typeof prettier === "undefined") {
|
|
|
|
prettier = index; // eslint-disable-line
|
|
|
|
}
|
2017-06-13 19:20:13 +03:00
|
|
|
|
2018-04-12 00:22:03 +03:00
|
|
|
self.onmessage = function(event) {
|
2018-04-18 00:09:36 +03:00
|
|
|
self.postMessage({
|
|
|
|
uid: event.data.uid,
|
|
|
|
message: handleMessage(event.data.message)
|
|
|
|
});
|
|
|
|
};
|
2018-04-12 04:28:50 +03:00
|
|
|
|
2018-04-18 00:09:36 +03:00
|
|
|
function handleMessage(message) {
|
|
|
|
if (message.type === "meta") {
|
|
|
|
return {
|
|
|
|
type: "meta",
|
|
|
|
supportInfo: JSON.parse(JSON.stringify(prettier.getSupportInfo())),
|
|
|
|
version: prettier.version
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.type === "format") {
|
|
|
|
var options = message.options || {};
|
|
|
|
|
|
|
|
delete options.ast;
|
|
|
|
delete options.doc;
|
|
|
|
delete options.output2;
|
|
|
|
|
|
|
|
var response = {
|
|
|
|
formatted: formatCode(message.code, options),
|
2018-04-19 20:46:29 +03:00
|
|
|
debug: {
|
|
|
|
ast: null,
|
|
|
|
doc: null,
|
|
|
|
reformatted: null
|
|
|
|
}
|
2018-04-18 00:09:36 +03:00
|
|
|
};
|
|
|
|
|
2018-04-19 20:46:29 +03:00
|
|
|
if (message.debug.ast) {
|
2018-04-18 00:09:36 +03:00
|
|
|
var ast;
|
|
|
|
var errored = false;
|
|
|
|
try {
|
|
|
|
ast = JSON.stringify(prettier.__debug.parse(message.code, options).ast);
|
|
|
|
} catch (e) {
|
|
|
|
errored = true;
|
|
|
|
ast = String(e);
|
2018-04-12 17:51:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-18 00:09:36 +03:00
|
|
|
if (!errored) {
|
2018-04-12 17:51:49 +03:00
|
|
|
try {
|
2018-04-18 00:09:36 +03:00
|
|
|
ast = formatCode(ast, { parser: "json" });
|
2018-04-12 17:51:49 +03:00
|
|
|
} catch (e) {
|
2018-04-18 00:09:36 +03:00
|
|
|
ast = JSON.stringify(ast, null, 2);
|
2018-04-12 17:51:49 +03:00
|
|
|
}
|
2018-04-12 04:28:50 +03:00
|
|
|
}
|
2018-04-19 20:46:29 +03:00
|
|
|
response.debug.ast = ast;
|
2018-04-18 00:09:36 +03:00
|
|
|
}
|
2018-04-12 04:28:50 +03:00
|
|
|
|
2018-04-19 20:46:29 +03:00
|
|
|
if (message.debug.doc) {
|
2018-04-18 00:09:36 +03:00
|
|
|
try {
|
2018-05-03 16:12:06 +03:00
|
|
|
response.debug.doc = prettier.__debug.formatDoc(
|
2018-04-18 00:09:36 +03:00
|
|
|
prettier.__debug.printToDoc(message.code, options),
|
|
|
|
{ parser: "babylon" }
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2018-04-19 20:46:29 +03:00
|
|
|
response.debug.doc = String(e);
|
2018-04-17 19:40:55 +03:00
|
|
|
}
|
2018-04-18 00:09:36 +03:00
|
|
|
}
|
2018-04-17 19:40:55 +03:00
|
|
|
|
2018-04-19 20:46:29 +03:00
|
|
|
if (message.debug.reformat) {
|
|
|
|
response.debug.reformatted = formatCode(response.formatted, options);
|
2018-04-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
2017-06-18 13:25:27 +03:00
|
|
|
}
|
2018-04-18 00:09:36 +03:00
|
|
|
}
|
2017-06-13 19:20:13 +03:00
|
|
|
|
2017-07-01 19:14:21 +03:00
|
|
|
function formatCode(text, options) {
|
|
|
|
try {
|
|
|
|
return prettier.format(text, options);
|
|
|
|
} catch (e) {
|
2018-01-07 14:59:14 +03:00
|
|
|
if (e.constructor && e.constructor.name === "SyntaxError") {
|
|
|
|
// Likely something wrong with the user's code
|
|
|
|
return String(e);
|
|
|
|
}
|
|
|
|
// Likely a bug in Prettier
|
|
|
|
// Provide the whole stack for debugging
|
|
|
|
return e.stack || String(e);
|
2017-07-01 19:14:21 +03:00
|
|
|
}
|
|
|
|
}
|