Improve regex printing (#1341)

- Print the raw regex for Flow, just like for Babylon.
- Sort regex flags.

Fixes #1334.
This commit is contained in:
Simon Lydell authored and Christopher Chedeau committed 2017-04-19 10:24:13 -07:00
1 parent e2fbaaf1f3
commit 938f0e3404
4 files changed
+21 -3

No files matched your search

+11 -3
View File
@@ -878,11 +878,13 @@ function genericPrintNoParens(path, options, print, args) {
case "ThisExpression":
return "this";
case "Super":
return "super"; // Babel 6 Literal split
return "super";
// Babel 6 Literal split
case "NullLiteral":
return "null"; // Babel 6 Literal split
return "null";
// Babel 6 Literal split
case "RegExpLiteral":
return n.extra.raw;
return printRegex(n);
// Babel 6 Literal split
case "NumericLiteral":
return printNumber(n.extra.raw);
@@ -892,6 +894,7 @@ function genericPrintNoParens(path, options, print, args) {
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
@@ -3335,6 +3338,11 @@ function makeString(rawContent, enclosingQuote) {
return enclosingQuote + newContent + enclosingQuote;
}
function printRegex(node) {
const flags = node.flags.split('').sort().join('');
return `/${node.pattern}/${flags}`;
}
function printNumber(rawNumber) {
return (
rawNumber
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`test.js 1`] = `
/[/]\\/\\u0aBc/mgi;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/[/]\\/\\u0aBc/gim;
`;
+1
View File
@@ -0,0 +1 @@
run_spec(__dirname, null, ["babylon"]);
+1
View File
@@ -0,0 +1 @@
/[/]\/\u0aBc/mgi;