MCPcopy Index your code
hub / github.com/messageformat/messageformat / fixEscapes

Function fixEscapes

packages/parser/codemod-fix-backslash-escapes.js:26–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24let doubleSingleQuotes = false;
25
26const fixEscapes = node => {
27 if (node.type !== 'Literal' || typeof node.value !== 'string') return;
28 if (doubleSingleQuotes) node.value = node.value.replace(/''+/g, '$&$&');
29 node.value = node.value.replace(
30 /('*)\\([#{}\\]|u[0-9a-f]{4})('*)/g,
31 (_, start, char, end) => {
32 switch (char[0]) {
33 case 'u': {
34 const code = parseInt(char.slice(1), 16);
35 return start + String.fromCharCode(code) + end;
36 }
37 case '\\':
38 return `${start}\\${end}`;
39 default:
40 // Assume multiple ' are already escaped
41 if (start === "'") start = "''";
42 if (end === "'") end = "''";
43 return `'${start}${char}${end}'`;
44 }
45 }
46 );
47};
48
49module.exports = ({ source }, { jscodeshift: j }, options) => {
50 if (options.doubleSingleQuotes) doubleSingleQuotes = true;

Calls

no outgoing calls

Tested by

no test coverage detected