Merge branch 'main' into next
# Conflicts: # netlify.toml # src/cli/find-cache-file.js # src/cli/utils.js # src/main/parser.js # website/playground/PrettierFormat.js # website/static/worker.js
This commit is contained in:
commit
298347c9fb
31 files changed
+463
-37
No files matched your search
@@ -112,6 +112,25 @@ header h1 {
|
||||
border-bottom: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.disabled-output-panel {
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 15px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.disabled-output-panel .explanation {
|
||||
font-size: 16px;
|
||||
max-width: 400px;
|
||||
color: darkred;
|
||||
}
|
||||
|
||||
.disabled-output-panel .btn {
|
||||
font-size: 22px;
|
||||
padding: 10px 20px;
|
||||
}
|
||||
|
||||
/* display as 2x2 grid */
|
||||
@media (min-width: 800px) {
|
||||
.options-container {
|
||||
@@ -258,7 +277,7 @@ input[type="number"] {
|
||||
border-color: #c8e1ff;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
.btn:hover:enabled {
|
||||
background-color: #e6ebf1;
|
||||
background-image: linear-gradient(to bottom, #f0f3f6, #dce3ec);
|
||||
border-color: #afb1b2;
|
||||
@@ -271,6 +290,10 @@ input[type="number"] {
|
||||
box-shadow: inset 0 0.15em 0.3em rgba(27, 31, 35, 0.15);
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
color: #afb1b2;
|
||||
}
|
||||
|
||||
.tooltip {
|
||||
position: absolute;
|
||||
z-index: 6;
|
||||
|
||||
@@ -30,6 +30,25 @@ for (const file in parsersLocation) {
|
||||
}
|
||||
}
|
||||
|
||||
const docExplorerPlugin = {
|
||||
parsers: {
|
||||
"doc-explorer": {
|
||||
parse: (text) =>
|
||||
new Function(
|
||||
`{ ${Object.keys(prettier.doc.builders)} }`,
|
||||
`const result = (${text || "''"}\n); return result;`
|
||||
)(prettier.doc.builders),
|
||||
astFormat: "doc-explorer",
|
||||
},
|
||||
},
|
||||
printers: {
|
||||
"doc-explorer": {
|
||||
print: (path) => path.getValue(),
|
||||
},
|
||||
},
|
||||
languages: [{ name: "doc-explorer", parsers: ["doc-explorer"] }],
|
||||
};
|
||||
|
||||
self.onmessage = async function (event) {
|
||||
self.postMessage({
|
||||
uid: event.data.uid,
|
||||
@@ -45,6 +64,8 @@ function serializeAst(ast) {
|
||||
? { name: value.name, message: value.message, ...value }
|
||||
: typeof value === "bigint"
|
||||
? `BigInt('${String(value)}')`
|
||||
: typeof value === "symbol"
|
||||
? String(value)
|
||||
: value,
|
||||
2
|
||||
);
|
||||
@@ -52,7 +73,10 @@ function serializeAst(ast) {
|
||||
|
||||
async function handleMessage(message) {
|
||||
if (message.type === "meta") {
|
||||
const supportInfo = await prettier.getSupportInfo({ showUnreleased: true });
|
||||
const supportInfo = await prettier.getSupportInfo({
|
||||
showUnreleased: true,
|
||||
plugins: [docExplorerPlugin],
|
||||
});
|
||||
|
||||
return {
|
||||
type: "meta",
|
||||
@@ -68,13 +92,15 @@ async function handleMessage(message) {
|
||||
delete options.doc;
|
||||
delete options.output2;
|
||||
|
||||
const plugins = [{ parsers }];
|
||||
const plugins = [{ parsers }, docExplorerPlugin];
|
||||
options.plugins = plugins;
|
||||
|
||||
const formatResult = await formatCode(message.code, options);
|
||||
|
||||
const response = {
|
||||
formatted: formatResult.formatted,
|
||||
cursorOffset: formatResult.cursorOffset,
|
||||
error: formatResult.error,
|
||||
debug: {
|
||||
ast: null,
|
||||
doc: null,
|
||||
@@ -109,10 +135,10 @@ async function handleMessage(message) {
|
||||
try {
|
||||
response.debug.doc = await prettier.__debug.formatDoc(
|
||||
await prettier.__debug.printToDoc(message.code, options),
|
||||
{ parser: "babel", plugins }
|
||||
{ plugins }
|
||||
);
|
||||
} catch (e) {
|
||||
response.debug.doc = String(e);
|
||||
} catch {
|
||||
response.debug.doc = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,10 +167,23 @@ async function formatCode(text, options) {
|
||||
} catch (e) {
|
||||
if (e.constructor && e.constructor.name === "SyntaxError") {
|
||||
// Likely something wrong with the user's code
|
||||
return { formatted: String(e) };
|
||||
return { formatted: String(e), error: true };
|
||||
}
|
||||
// Likely a bug in Prettier
|
||||
// Provide the whole stack for debugging
|
||||
return { formatted: e.stack || String(e) };
|
||||
return { formatted: stringifyError(e), error: true };
|
||||
}
|
||||
}
|
||||
|
||||
function stringifyError(e) {
|
||||
const stringified = String(e);
|
||||
if (typeof e.stack !== "string") {
|
||||
return stringified;
|
||||
}
|
||||
if (e.stack.includes(stringified)) {
|
||||
// Chrome
|
||||
return e.stack;
|
||||
}
|
||||
// Firefox
|
||||
return stringified + "\n" + e.stack;
|
||||
}
|
||||
Reference in new issue
Block a user