Add 'debug' option to mangle_properties

Add a 'debug' option to mangle_properties and add a test that verifies
it works as expected both with and without a cache passed as well.
This commit is contained in:
Ashley (Scirra) 2016-10-05 13:39:33 +01:00
parent 88f6ff38d1
commit abb6aa544f
2 changed files with 64 additions and 1 deletions

View File

@ -83,13 +83,20 @@ function mangle_properties(ast, options) {
cache : null, cache : null,
only_cache : false, only_cache : false,
regex : null, regex : null,
ignore_quoted : false ignore_quoted : false,
debug : false
}); });
var reserved = options.reserved; var reserved = options.reserved;
if (reserved == null) if (reserved == null)
reserved = find_builtins(); reserved = find_builtins();
// To properly simulate different mangling per different invocations, default a random number
// to be appended to each mangled name for this invocation. If a cache is passed, use an empty
// string to correctly simulate using consistent names when caches are passed around.
// (Of course this assumes the same cache is being passed - it's good enough for a debug check though.)
var debugCacheName = Math.floor(Math.random() * 1000).toString();
var cache = options.cache; var cache = options.cache;
if (cache == null) { if (cache == null) {
cache = { cache = {
@ -97,9 +104,14 @@ function mangle_properties(ast, options) {
props: new Dictionary() props: new Dictionary()
}; };
} }
else
{
debugCacheName = "";
}
var regex = options.regex; var regex = options.regex;
var ignore_quoted = options.ignore_quoted; var ignore_quoted = options.ignore_quoted;
var debug = options.debug;
var names_to_mangle = []; var names_to_mangle = [];
var unmangleable = []; var unmangleable = [];
@ -199,6 +211,15 @@ function mangle_properties(ast, options) {
if (!should_mangle(name)) { if (!should_mangle(name)) {
return name; return name;
} }
// If debug mode is enabled, return a predictably-mangled name with a prefix and suffix,
// so the name is different but code can still be read for diagnostic purposes.
// Also add the debug cache name which is empty if a (assumedly) consistent cache is
// passed around, otherwise is a random number to ensure separate invocations mangle
// differently as they would in practice.
// e.g. "foo" -> "_$foo$123$_" (or "_$foo$_" with consistent cache)
if (debug)
return "_$" + name + (debugCacheName ? "$" + debugCacheName : "") + "$_";
var mangled = cache.props.get(name); var mangled = cache.props.get(name);
if (!mangled) { if (!mangled) {

View File

@ -0,0 +1,42 @@
var Uglify = require('../../');
var assert = require("assert");
describe("mangle_properties 'debug' option ", function() {
it("Should test the 'debug' option of mangle_properties works with no cache passed", function() {
var js = 'var o = {}; o.foo = "bar";';
var ast = Uglify.parse(js);
ast.figure_out_scope();
ast = Uglify.mangle_properties(ast, {
debug: true
});
let stream = Uglify.OutputStream();
ast.print(stream);
var result = stream.toString();
// Should match: var o={};o._$foo$NNN$="bar";
// where NNN is a number.
assert(/var o=\{\};o\._\$foo\$\d+\$_="bar";/.test(result));
});
it("Should test the 'debug' option of mangle_properties works with a cache passed", function() {
var js = 'var o = {}; o.foo = "bar";';
var ast = Uglify.parse(js);
ast.figure_out_scope();
ast = Uglify.mangle_properties(ast, {
cache: {
cname: -1,
props: new Uglify.Dictionary()
},
debug: true
});
let stream = Uglify.OutputStream();
ast.print(stream);
var result = stream.toString();
assert.strictEqual(result, 'var o={};o._$foo$_="bar";');
});
});