Heiko Theißen
Heiko Theißen

Reputation: 17477

Use prettier to format Javascript code blocks in a FunnelWeb file

I have a FunnelWeb file whose @{...@} code blocks are tangled into a Javascript program. I want to use prettier to format these code blocks while leaving the non-code FunnelWeb source unchanged.

Here is my approach, my question is: are there better alternatives?

I use a Node.js program that processes the file line by line. Unless it is in "code block" state, it simply copies each line to the output. But:

The problem is that the contents of the @{...@} code block in the code buffer typically do not contain a Javascript program considered error-free by prettier. This is because FunnelWeb works by pasting code blocks together and into other code blocks without any regard to the programming language that they contain. In particular

To circumvent these two problems, my Node.js program

The after-prettier processing thus performs

code = code
  .replaceAll("function /*s*/", "static")
  .replaceAll("function /*m*/ ", "")
  .replace(/\/\*|\*\//g, "");

And if a code block still cannot be formatted by prettier, the unformatted code is output.

With this approach my code blocks that cannot be formatted are mostly one-liners like

@$@<Exports@>+=@{
ClassName,
@}

Again my question: Has anyone else tried this and found a better approach?

Upvotes: 0

Views: 39

Answers (0)

Related Questions