Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
HeikoTheissen committed Nov 8, 2024
1 parent 316840a commit 2e9e89c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
45 changes: 25 additions & 20 deletions lib/fwprettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,40 @@ const fs = require("fs");

let promise = Promise.resolve();
let out = "";
let code;
let code, prettierrc;

function appendCode(code) {
out += code.replace(/\/\*|\*\//g, "");
}

createInterface({
input: fs.createReadStream(process.argv[2]),
crlfDelay: Infinity,
})
.on("line", function (line) {
if (/^@[$o]/.test(line)) code = [];
else if (line.startsWith("@}")) {
const m = line.match(/^@[$o]@<.*?@>.*?@\{(@!(.*)$)?/);
if (m) {
code = [];
prettierrc = m[2] ? JSON.parse(m[2]) : {};
} else if (line.startsWith("@}")) {
const unformatted = code.join("");
if (code.length < 2)
promise = promise.then(function () {
out += unformatted.replace(/\/\*|\*\//g, "");
});
else
if (!prettierrc) promise = promise.then(() => appendCode(unformatted));
else {
const prettierOptions = {
filepath: ".js",
trailingComma: "none",
...prettierrc,
};
promise = promise
.then(() =>
format(unformatted, { filepath: ".js", trailingComma: "none" }),
)
.then(
function (formatted) {
out += formatted.replace(/\/\*|\*\//g, "");
},
function () {
out += unformatted.replace(/\/\*|\*\//g, "");
},
);
.then(() => format(unformatted, prettierOptions))
.then(appendCode, function (err) {
console.error(err.message, unformatted);
appendCode(unformatted);
});
}
code = undefined;
} else if (code) {
code.push(line.replace(/@<.*?@>(@\(.*@\))?/g, "/*$&*/") + "\n");
code.push(line.replace(/@<.*?@>(@\(.*?@\))?/g, "/*$&*/") + "\n");
return;
}
promise = promise.then(function () {
Expand Down
5 changes: 4 additions & 1 deletion lib/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ class Number {
lineno++;
if (this.skip(line)) return;
if (this.meta.funnelweb)
line = line.replace(/^@[$o]@\<(.*?)@\>.*?@\{/, " ##fwisec $1");
line = line
.replace(/@!.*$/, "")
.replace(/^@[$o]@\<(.*?)@\>.*?@\{/, " ##fwisec $1");
try {
for (
var m, regex = /\[.*?\]\{id=(.*?)\}/g;
Expand Down Expand Up @@ -195,6 +197,7 @@ class Number {
line = line
.replace(/^@!\{/, "::: funnelweb")
.replace(/^@!\}/, ":::")
.replace(/@!.*$/, "")
.replace(
/^@[$o]@\<(.*?)@\>.*?@\{/,
function (m, p) {
Expand Down

0 comments on commit 2e9e89c

Please sign in to comment.