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.
This commit is contained in:
Fábio Santos
2018-03-26 19:22:01 +01:00
committed by Alex Lam S.L
parent b1410be443
commit 9a5e2052c4
2 changed files with 25 additions and 14 deletions

View File

@@ -180,6 +180,17 @@
end : my_end_token(M) end : my_end_token(M)
}; };
if (val === null) return new AST_Null(args); 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) { switch (typeof val) {
case "string": case "string":
args.value = val; args.value = val;
@@ -189,16 +200,6 @@
return new AST_Number(args); return new AST_Number(args);
case "boolean": case "boolean":
return new (val ? AST_True : AST_False)(args); 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) { Identifier: function(M) {
@@ -410,14 +411,15 @@
}); });
def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) { 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 { return {
type: "Literal", type: "Literal",
value: value, value: value,
raw: value.toString(), raw: value,
regex: { regex: {
pattern: value.source, pattern: M.value.raw_source,
flags: value.toString().match(/[gimuy]*$/)[0] flags: flags
} }
}; };
}); });

View File

@@ -23,6 +23,15 @@ describe("spidermonkey export/import sanity test", function() {
}); });
}); });
it("should not add unnecessary escape slashes to regexps", function() {
var input = "/[\\\\/]/;";
var ast = uglify.parse(input).to_mozilla_ast();
assert.equal(
uglify.AST_Node.from_mozilla_ast(ast).print_to_string(),
input
);
});
it("Should judge between directives and strings correctly on import", function() { it("Should judge between directives and strings correctly on import", function() {
var tests = [ var tests = [
{ {