prettier/website/static/worker.js

153 lines
3.3 KiB
JavaScript

/* eslint-env worker */
/* eslint no-var: off, strict: off */
var parsersLoaded = {};
// "Polyfills" in order for all the code to run
/* eslint-disable no-undef, no-global-assign */
self.global = self;
self.util = {};
self.path = {};
self.path.resolve = self.path.join = self.path.dirname = function() {
return "";
};
self.path.parse = function() {
return { root: "" };
};
self.Buffer = {
isBuffer: function() {
return false;
}
};
self.constants = {};
module$1 = module = os = crypto = {};
self.fs = { readFile: function() {} };
os.homedir = function() {
return "/home/prettier";
};
os.EOL = "\n";
self.process = {
argv: [],
env: { PRETTIER_DEBUG: true },
version: "v8.5.0",
binding: function() {
return {};
},
cwd: function() {
return "";
}
};
self.assert = { ok: function() {}, strictEqual: function() {} };
self.require = function require(path) {
if (path === "stream") {
return { PassThrough() {} };
}
if (path === "./third-party") {
return {};
}
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];
};
/* eslint-enable */
var prettier;
importScripts("lib/index.js");
if (typeof prettier === "undefined") {
prettier = module.exports; // eslint-disable-line
}
if (typeof prettier === "undefined") {
prettier = index; // eslint-disable-line
}
self.onmessage = function(event) {
self.postMessage({
uid: event.data.uid,
message: handleMessage(event.data.message)
});
};
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),
debugAst: null,
debugDoc: null,
reformatted: null
};
if (message.debugAst) {
var ast;
var errored = false;
try {
ast = JSON.stringify(prettier.__debug.parse(message.code, options).ast);
} catch (e) {
errored = true;
ast = String(e);
}
if (!errored) {
try {
ast = formatCode(ast, { parser: "json" });
} catch (e) {
ast = JSON.stringify(ast, null, 2);
}
}
response.debugAst = ast;
}
if (message.debugDoc) {
try {
response.debugDoc = prettier.__debug.formatDoc(
prettier.__debug.printToDoc(message.code, options),
{ parser: "babylon" }
);
} catch (e) {
response.debugDoc = String(e);
}
}
if (message.secondFormat) {
response.reformatted = formatCode(response.formatted, options);
}
return response;
}
}
function formatCode(text, options) {
try {
return prettier.format(text, options);
} catch (e) {
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);
}
}