Skip to content

Commit

Permalink
fix extra regex slash when going through mozilla AST I/O (#3025)
Browse files Browse the repository at this point in the history
This relates to #1929, but in the context of mozilla AST input/output.
  • Loading branch information
fabiosantoscode authored and alexlamsl committed Mar 26, 2018
1 parent b1410be commit 9a5e205
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/mozilla-ast.js
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
};
});
Expand Down
9 changes: 9 additions & 0 deletions test/mocha/spidermonkey.js
Expand Up @@ -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() {
var tests = [
{
Expand Down

0 comments on commit 9a5e205

Please sign in to comment.