Skip to content

Commit

Permalink
fix: escaped slash expression transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovRoman committed Feb 13, 2024
1 parent 6d95abb commit ee10ac0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion __fixtures/config/withExpressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ module.exports = {
baz: '4',
array: [
Boolean(100 + 500 * 1)
]
],
specials: /\n\t\r/g,
extraSlash: /\, \, \\/g
};
2 changes: 2 additions & 0 deletions src/fsUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down
10 changes: 7 additions & 3 deletions src/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <expression>' are turned into <expression>
return withRestoredQuotesConfigStr.replace(expressionRegExp, "$1$2");
return repairedConfig.replace(expressionRegExp, "$1$2");
};

const getObjectRepr = _.flow([toIndentedJson, withComments, withReplacedQuotes, withExpressions]);
Expand Down

0 comments on commit ee10ac0

Please sign in to comment.