Ensure converted !0 and !1 have leading whitespace if needed.

Former-commit-id: 930001f35111d47a51c011c47d6c2608b0bb7e2d
This commit is contained in:
John-David Dalton
2013-05-27 13:47:01 -07:00
parent 507f2ec544
commit be52c181ea
3 changed files with 10 additions and 8 deletions

View File

@@ -38,17 +38,19 @@
);
// replace vars for `false` and `true` with boolean literals
[/(\w+)\s*=\s*!1\b/.exec(source), /(\w+)\s*=\s*!0\b/.exec(source)].forEach(function(varName, index) {
[/(\w+)\s*=\s*!1\b/, /(\w+)\s*=\s*!0\b/].forEach(function(regexp, index) {
var varName = (regexp.exec(source) || 0)[1];
if (varName) {
varName = varName[1];
source = source.replace(RegExp('([!=]==\\s*)' + varName + '|' + varName + '(\\s*[!=]==)', 'g'), '$1' + !!index + '$2');
}
});
// replace `!1` and `!0` in expressions with `false` and `true` values
source = source
.replace(/([!=]==\s*)!1|!1(\s*[!=]==)/g, '$1false$2')
.replace(/([!=]==\s*)!0|!0(\s*[!=]==)/g, '$1true$2');
[/([!=]==)\s*!1|(.)!1\s*([!=]==)/g, /([!=]==)\s*!0|(.)!0\s*([!=]==)/g].forEach(function(regexp, index) {
source = source.replace(regexp, function(match, prelude, chr, postlude) {
return (prelude || chr + (/\w/.test(chr) ? ' ' : '')) + !!index + (postlude || '');
});
});
// flip `typeof` expressions to help optimize Safari and
// correct the AMD module definition for AMD build optimizers