Skip to content

Commit

Permalink
bug: prettier v3 async not working with synchronous lexical update ca…
Browse files Browse the repository at this point in the history
…lls; also required estree plugin
umaranis committed May 27, 2024
1 parent 337615f commit 81c5ff0
Showing 2 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import {getEditor} from '../../../../composerContext.js';
import {$isCodeNode as isCodeNode} from '@lexical/code';
import {CodeNode, $isCodeNode as isCodeNode} from '@lexical/code';
import {$getNearestNodeFromDOMNode as getNearestNodeFromDOMNode} from 'lexical';
import type {Options} from 'prettier';
import {
@@ -30,37 +30,37 @@
async function handleClick(): Promise<void> {
const codeDOMNode = getCodeDOMNode();
if (!codeDOMNode) {
return;
}
try {
const format = await loadPrettierFormat();
const options = getPrettierOptions(lang);
options.plugins = [await loadPrettierParserByLang(lang)];
if (!codeDOMNode) {
return;
}
editor.update(async () => {
const codeNode = getNearestNodeFromDOMNode(codeDOMNode);
options.plugins = await loadPrettierParserByLang(lang);
let codeNode: CodeNode;
let content = '';
editor.update(() => {
codeNode = getNearestNodeFromDOMNode(codeDOMNode) as CodeNode;
if (isCodeNode(codeNode)) {
const content = codeNode.getTextContent();
content = codeNode.getTextContent();
}
});
let parsed = '';
try {
parsed = await format(content, options);
} catch (error: unknown) {
setError(error);
}
if (content) {
let parsed = '';
parsed = await format(content, options);
editor.update(() => {
if (parsed !== '') {
const selection = codeNode.select(0);
selection.insertText(parsed);
syntaxError = '';
tipsVisible = false;
}
}
});
});
}
} catch (error: unknown) {
setError(error);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type {Options} from 'prettier';
import type {Options, Plugin} from 'prettier';

const PRETTIER_PARSER_MODULES = {
css: () => import('prettier/parser-postcss'),
html: () => import('prettier/parser-html'),
js: () => import('prettier/parser-babel'),
markdown: () => import('prettier/parser-markdown'),
css: async () => [await import('prettier/parser-postcss')],
html: async () => [await import('prettier/parser-html')],
js: async () => [
await import('prettier/parser-babel'),
(await import('prettier/plugins/estree')) as Plugin,
],
markdown: async () => [await import('prettier/parser-markdown')],
} as const;

type LanguagesType = keyof typeof PRETTIER_PARSER_MODULES;

0 comments on commit 81c5ff0

Please sign in to comment.