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
|
|
|
|
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-01-07 14:59:14 +03:00
|
|
|
module$1 = module = os = crypto = {};
|
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
|
|
|
};
|
|
|
|
|
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) {
|
|
|
|
var uid = event.data.uid;
|
|
|
|
var message = event.data.message;
|
|
|
|
switch (message.type) {
|
|
|
|
case "meta":
|
|
|
|
self.postMessage({
|
|
|
|
uid: uid,
|
|
|
|
message: {
|
|
|
|
type: "meta",
|
|
|
|
supportInfo: JSON.parse(JSON.stringify(prettier.getSupportInfo())),
|
|
|
|
version: prettier.version
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2017-06-13 19:20:13 +03:00
|
|
|
|
2018-04-12 00:22:03 +03:00
|
|
|
case "format":
|
|
|
|
var options = message.options || {};
|
2017-06-18 13:25:27 +03:00
|
|
|
|
2018-04-12 00:22:03 +03:00
|
|
|
delete options.ast;
|
|
|
|
delete options.doc;
|
|
|
|
delete options.output2;
|
2017-06-13 19:20:13 +03:00
|
|
|
|
2018-04-12 00:22:03 +03:00
|
|
|
self.postMessage({
|
|
|
|
uid: uid,
|
|
|
|
message: {
|
|
|
|
formatted: formatCode(message.code, options)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2017-06-18 13:25:27 +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
|
|
|
}
|
|
|
|
}
|