diff --git a/__fixtures/config/withExpressions.js b/__fixtures/config/withExpressions.js index dc2d9f6..0292727 100644 --- a/__fixtures/config/withExpressions.js +++ b/__fixtures/config/withExpressions.js @@ -4,5 +4,7 @@ module.exports = { baz: '4', array: [ Boolean(100 + 500 * 1) - ] + ], + specials: /\n\t\r/g, + extraSlash: /\, \, \\/g }; diff --git a/src/fsUtils.test.ts b/src/fsUtils.test.ts index 13ce70f..06a456a 100644 --- a/src/fsUtils.test.ts +++ b/src/fsUtils.test.ts @@ -68,6 +68,8 @@ describe("fsUtils", () => { bar: 4, baz: "4", array: ["__expression: Boolean(100 + 500 * 1)"], + specials: "__expression: /\n\t\r/g", + extraSlash: "__expression: /\\, \\, \\\\/g", } as unknown as HermioneConfig; await expectConfig(withExpressionsConfig, configs["withExpressions"]); diff --git a/src/fsUtils.ts b/src/fsUtils.ts index ececde4..8f7549e 100644 --- a/src/fsUtils.ts +++ b/src/fsUtils.ts @@ -67,11 +67,15 @@ export const writeHermioneConfig = async (dirPath: string, hermioneConfig: Hermi const quote = template.quote; const expressionRegExp = new RegExp(`${quote}__expression: (.*)${quote}(,?)$`, "gm"); - // unescapes and restores double quotes in expressions - const withRestoredQuotesConfigStr = configStr.replace(expressionRegExp, match => match.replace(/\\"/g, '"')); + const repairQuotes = (match: string): string => match.replace(/\\"/g, '"'); + const repairSlash = (match: string): string => match.replace(/\\\\/g, "\\"); + + const repairedConfig = configStr + .replace(expressionRegExp, repairQuotes) // unescapes and restores double quotes in expressions + .replace(expressionRegExp, repairSlash); // restores '\\' in expressions // strings like '__expression: ' are turned into - return withRestoredQuotesConfigStr.replace(expressionRegExp, "$1$2"); + return repairedConfig.replace(expressionRegExp, "$1$2"); }; const getObjectRepr = _.flow([toIndentedJson, withComments, withReplacedQuotes, withExpressions]);