diff --git a/lib/fwprettier.js b/lib/fwprettier.js index a4434f11e..897458ac4 100644 --- a/lib/fwprettier.js +++ b/lib/fwprettier.js @@ -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 () { diff --git a/lib/number.js b/lib/number.js index 3426c224e..b5e275c82 100644 --- a/lib/number.js +++ b/lib/number.js @@ -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; @@ -195,6 +197,7 @@ class Number { line = line .replace(/^@!\{/, "::: funnelweb") .replace(/^@!\}/, ":::") + .replace(/@!.*$/, "") .replace( /^@[$o]@\<(.*?)@\>.*?@\{/, function (m, p) {