From 9a5e2052c4860256021ede7c2c5d529fa0f320ed Mon Sep 17 00:00:00 2001 From: Fábio Santos Date: Mon, 26 Mar 2018 19:22:01 +0100 Subject: fix extra regex slash when going through mozilla AST I/O (#3025) This relates to #1929, but in the context of mozilla AST input/output. --- lib/mozilla-ast.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js index b80afbbb..8e66c916 100644 --- a/lib/mozilla-ast.js +++ b/lib/mozilla-ast.js @@ -180,6 +180,17 @@ end : my_end_token(M) }; if (val === null) return new AST_Null(args); + var rx = M.regex; + if (rx && rx.pattern) { + // RegExpLiteral as per ESTree AST spec + args.value = new RegExp(rx.pattern, rx.flags); + args.value.raw_source = rx.pattern; + return new AST_RegExp(args); + } else if (rx) { + // support legacy RegExp + args.value = M.regex && M.raw ? M.raw : val; + return new AST_RegExp(args); + } switch (typeof val) { case "string": args.value = val; @@ -189,16 +200,6 @@ return new AST_Number(args); case "boolean": return new (val ? AST_True : AST_False)(args); - default: - var rx = M.regex; - if (rx && rx.pattern) { - // RegExpLiteral as per ESTree AST spec - args.value = new RegExp(rx.pattern, rx.flags).toString(); - } else { - // support legacy RegExp - args.value = M.regex && M.raw ? M.raw : val; - } - return new AST_RegExp(args); } }, Identifier: function(M) { @@ -410,14 +411,15 @@ }); def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) { - var value = M.value; + var flags = M.value.toString().match(/[gimuy]*$/)[0]; + var value = "/" + M.value.raw_source + "/" + flags; return { type: "Literal", value: value, - raw: value.toString(), + raw: value, regex: { - pattern: value.source, - flags: value.toString().match(/[gimuy]*$/)[0] + pattern: M.value.raw_source, + flags: flags } }; }); -- cgit v1.2.3