diff --git a/src/printer.js b/src/printer.js index 538ec61b..23daf3e4 100644 --- a/src/printer.js +++ b/src/printer.js @@ -726,7 +726,7 @@ function genericPrintNoParens(path, options, print) { case "NullLiteral": return "null"; // Babel 6 Literal split case "RegExpLiteral": - return printRegex(n); + return n.extra.raw; // Babel 6 Literal split case "NumericLiteral": return printNumber(n.extra.raw); @@ -736,7 +736,6 @@ function genericPrintNoParens(path, options, print) { case "StringLiteral": case "Literal": if (typeof n.value === "number") return printNumber(n.raw); - if (n.regex) return printRegex(n.regex); if (typeof n.value !== "string") return "" + n.value; return nodeStr(n, options); // Babel 6 @@ -1231,10 +1230,7 @@ function genericPrintNoParens(path, options, print) { case "ClassExpression": return concat(printClass(path, options, print)); case "TemplateElement": - return join( - literalline, - n.value.raw.split("\n").map(line => normalizeEscapes(line)) - ); + return join(literalline, n.value.raw.split("\n")); case "TemplateLiteral": var expressions = path.map(print, "expressions"); @@ -2591,37 +2587,7 @@ function makeString(rawContent, enclosingQuote) { return match; }); - return enclosingQuote + normalizeEscapes(newContent) + enclosingQuote; -} - -function printRegex(regexData) { - const pattern = regexData.pattern; - const flags = regexData.flags; - const skipES2015 = !flags.includes("u"); - - return "/" + normalizeEscapes(pattern, skipES2015) + "/" + flags; -} - -function normalizeEscapes(rawContent, skipES2015) { - // Matches \x escapes and \u escapes (including the ES2015 variant) and other - // escapes. - const regex = /(\\x[\da-f]{2}|\\u[\da-f]{4}|\\u\{[\da-f]+\})|(\\[\s\S])/gi; - - return rawContent.replace(regex, (match, escape, otherEscape) => { - // Return other escapes as-is. - if (otherEscape) { - return otherEscape; - } - - // Regexes without the /u flag do not support ES2015 unicode escapes, so - // return the match as-is. - if (skipES2015 && escape[2] === "{") { - return escape; - } - - // Print all \x and \u escapes lowercase. - return escape.toLowerCase(); - }); + return enclosingQuote + newContent + enclosingQuote; } function printNumber(rawNumber) { diff --git a/tests/literal/__snapshots__/jsfmt.spec.js.snap b/tests/literal/__snapshots__/jsfmt.spec.js.snap index 458f8170..9e26c22a 100644 --- a/tests/literal/__snapshots__/jsfmt.spec.js.snap +++ b/tests/literal/__snapshots__/jsfmt.spec.js.snap @@ -116,48 +116,3 @@ function test5(): string { 0.1e-10; " `; - -exports[`test regex.js 1`] = ` -"// Normalization of \\x and \\u escapes: - -// Basic case. -/a\\xAaAb\\uB1cDE/gim; - -// ES2015 unicode escapes. -/\\u{1Fa3}/u; -/\\u{00000000A0}/u; - -// Leaves what looks like a ES2015 unicode escape alone if not using the /u flag. -/\\u{1Fa3}/; - -// Leaves what looks like escapes but aren\'t alone. -/\\xA\\u00BG/; - -// Leaves other escapes alone. -/\\B\\S/; - -// Handles escaped backslashes. -/\\\\xAB\\\\\\xAB\\\\\\\\xAB\\B\\\\\\B\\uAbCd/; -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -// Normalization of \\x and \\u escapes: - -// Basic case. -/a\\xaaAb\\ub1cdE/gim; - -// ES2015 unicode escapes. -/\\u{1fa3}/u; -/\\u{00000000a0}/u; - -// Leaves what looks like a ES2015 unicode escape alone if not using the /u flag. -/\\u{1Fa3}/; - -// Leaves what looks like escapes but aren\'t alone. -/\\xA\\u00BG/; - -// Leaves other escapes alone. -/\\B\\S/; - -// Handles escaped backslashes. -/\\\\xAB\\\\\\xab\\\\\\\\xAB\\B\\\\\\B\\uabcd/; -" -`; diff --git a/tests/literal/regex.js b/tests/literal/regex.js deleted file mode 100644 index 30a7b047..00000000 --- a/tests/literal/regex.js +++ /dev/null @@ -1,20 +0,0 @@ -// Normalization of \x and \u escapes: - -// Basic case. -/a\xAaAb\uB1cDE/gim; - -// ES2015 unicode escapes. -/\u{1Fa3}/u; -/\u{00000000A0}/u; - -// Leaves what looks like a ES2015 unicode escape alone if not using the /u flag. -/\u{1Fa3}/; - -// Leaves what looks like escapes but aren't alone. -/\xA\u00BG/; - -// Leaves other escapes alone. -/\B\S/; - -// Handles escaped backslashes. -/\\xAB\\\xAB\\\\xAB\B\\\B\uAbCd/; diff --git a/tests/strings/__snapshots__/jsfmt.spec.js.snap b/tests/strings/__snapshots__/jsfmt.spec.js.snap index 7cff0058..b75363d4 100644 --- a/tests/strings/__snapshots__/jsfmt.spec.js.snap +++ b/tests/strings/__snapshots__/jsfmt.spec.js.snap @@ -24,29 +24,6 @@ exports[`test strings.js 1`] = ` \'\\uD801\\uDC28\', ]; - -// Normalization of \\x and \\u escapes: - -// Basic case. -\"a\\xAaAb\\uB1cDE\"; -\`a\\xAaAb\\uB1cDE\`; - -// ES2015 unicode escapes. -\"\\u{1Fa3}\"; -\`\\u{1Fa3}\`; -\"\\u{00000000A0}\"; -\`\\u{00000000A0}\`; - -// Leaves other escapes alone. -\"\\B\\S\"; -\`\\B\\S\`; - -// Handles escaped backslashes. -\"\\\\xAB\\\\\\xAB\\\\\\\\xAB\\B\\\\\\B\\u1234\"; -\`\\\\xAB\\\\\\xAB\\\\\\\\xAB\\B\\\\\\B\\u1234\`; - -// Mix of everything. -\"\\xF0\"\`a\\uaBcD\${\'\\xFdE\'}\\\\\\u{00AbCdE}\`; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [ \"abc\", @@ -65,18 +42,6 @@ exports[`test strings.js 1`] = ` \"\\0\", \"🐶\", \'\\uD801\\uDC28\' -]; // Normalization of \\x and \\u escapes: // Basic case. - -\"a\\xaaAb\\ub1cdE\"; -\`a\\xaaAb\\ub1cdE\`; // ES2015 unicode escapes. -\"\\u{1fa3}\"; -\`\\u{1fa3}\`; -\"\\u{00000000a0}\"; -\`\\u{00000000a0}\`; // Leaves other escapes alone. -\"\\B\\S\"; -\`\\B\\S\`; // Handles escaped backslashes. -\"\\\\xAB\\\\\\xab\\\\\\\\xAB\\B\\\\\\B\\u1234\"; -\`\\\\xAB\\\\\\xab\\\\\\\\xAB\\B\\\\\\B\\u1234\`; // Mix of everything. -\"\\xf0\"\`a\\uabcd\${\"\\xfdE\"}\\\\\\u{00abcde}\`; +]; " `; diff --git a/tests/strings/strings.js b/tests/strings/strings.js index c2722e2b..d31dceb2 100644 --- a/tests/strings/strings.js +++ b/tests/strings/strings.js @@ -23,26 +23,3 @@ '\uD801\uDC28', ]; - -// Normalization of \x and \u escapes: - -// Basic case. -"a\xAaAb\uB1cDE"; -`a\xAaAb\uB1cDE`; - -// ES2015 unicode escapes. -"\u{1Fa3}"; -`\u{1Fa3}`; -"\u{00000000A0}"; -`\u{00000000A0}`; - -// Leaves other escapes alone. -"\B\S"; -`\B\S`; - -// Handles escaped backslashes. -"\\xAB\\\xAB\\\\xAB\B\\\B\u1234"; -`\\xAB\\\xAB\\\\xAB\B\\\B\u1234`; - -// Mix of everything. -"\xF0"`a\uaBcD${'\xFdE'}\\\u{00AbCdE}`;