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-02-09 13:31:48 +03:00
|
|
|
/* eslint-disable */
|
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-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-02-09 13:31:48 +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-02-09 13:31:48 +03:00
|
|
|
/* eslint-enable */
|
|
|
|
|
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
|
|
|
|
|
|
|
self.onmessage = function(message) {
|
2017-08-15 14:54:10 +03:00
|
|
|
var options = message.data.options || {};
|
|
|
|
options.parser = options.parser || "babylon";
|
2017-06-13 19:20:13 +03:00
|
|
|
|
2017-06-18 13:25:27 +03:00
|
|
|
delete options.ast;
|
|
|
|
delete options.doc;
|
2017-09-14 00:39:18 +03:00
|
|
|
delete options.output2;
|
2017-06-18 13:25:27 +03:00
|
|
|
|
2017-07-01 19:14:21 +03:00
|
|
|
var formatted = formatCode(message.data.text, options);
|
2017-07-05 23:54:46 +03:00
|
|
|
var doc;
|
|
|
|
var ast;
|
2017-09-14 00:39:18 +03:00
|
|
|
var formatted2;
|
2017-06-13 19:20:13 +03:00
|
|
|
|
2017-06-18 13:25:27 +03:00
|
|
|
if (message.data.ast) {
|
2017-09-12 22:15:49 +03:00
|
|
|
var actualAst;
|
|
|
|
var errored = false;
|
2017-06-18 13:25:27 +03:00
|
|
|
try {
|
2018-01-09 16:27:26 +03:00
|
|
|
actualAst = prettier.__debug.parse(message.data.text, options).ast;
|
2017-09-12 22:15:49 +03:00
|
|
|
ast = JSON.stringify(actualAst);
|
2017-06-18 13:25:27 +03:00
|
|
|
} catch (e) {
|
2017-09-12 22:15:49 +03:00
|
|
|
errored = true;
|
|
|
|
ast = String(e);
|
|
|
|
}
|
|
|
|
if (!errored) {
|
|
|
|
try {
|
|
|
|
ast = formatCode(ast, { parser: "json" });
|
|
|
|
} catch (e) {
|
|
|
|
ast = JSON.stringify(actualAst, null, 2);
|
|
|
|
}
|
2017-06-18 13:25:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 19:20:13 +03:00
|
|
|
if (message.data.doc) {
|
|
|
|
try {
|
|
|
|
doc = prettier.__debug.formatDoc(
|
2017-06-18 13:25:27 +03:00
|
|
|
prettier.__debug.printToDoc(message.data.text, options),
|
|
|
|
{ parser: "babylon" }
|
2017-06-13 19:20:13 +03:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2017-09-12 22:15:49 +03:00
|
|
|
doc = String(e);
|
2017-06-13 19:20:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 00:39:18 +03:00
|
|
|
if (message.data.formatted2) {
|
|
|
|
formatted2 = formatCode(formatted, options);
|
|
|
|
}
|
|
|
|
|
2017-09-12 22:44:50 +03:00
|
|
|
self.postMessage({
|
|
|
|
formatted: formatted,
|
|
|
|
doc: doc,
|
|
|
|
ast: ast,
|
2017-09-14 00:39:18 +03:00
|
|
|
formatted2: formatted2,
|
2017-12-31 04:12:56 +03:00
|
|
|
version: prettier.version
|
2017-09-12 22:44:50 +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
|
|
|
}
|
|
|
|
}
|