Rework "strict" build option and fix typo in test-ui.js.

Former-commit-id: e06045546bea43bbe1fff78d6c948036ad98a88c
This commit is contained in:
John-David Dalton
2012-10-22 21:27:09 -07:00
parent 2f16f6dc23
commit 15cf2ad076
3 changed files with 30 additions and 35 deletions

View File

@@ -784,12 +784,12 @@
* @returns {String} Returns the modified source. * @returns {String} Returns the modified source.
*/ */
function setUseStrictOption(source, value) { function setUseStrictOption(source, value) {
// remove `isStrictFast` assignment // inject "use strict"
return removeVar(source, 'isStrictFast') if (value) {
// replace `useStrict` branch in `value` with hard-coded option source = source.replace(/^[\s\S]*?function[^{]+{/, "$&\n 'use strict';");
.replace(/(?: *\/\/.*\n)*(\s*)' *<%.+?useStrict.+/, value ? "$1'\\'use strict\\';\\n' +" : '') }
// remove `useStrict` from iterator options // replace `useStrict` branch in `value` with hard-coded option
.replace(/(?:,\s*'useStrict':[^,]+?\n| *'useStrict':[^,]+,\n)/g, ''); return source.replace(/(?: *\/\/.*\n)*(\s*)' *<%.+?useStrict.+/, value ? "$1'\\'use strict\\';\\n' +" : '');
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -940,9 +940,6 @@
// the lodash.js source // the lodash.js source
var source = fs.readFileSync(path.join(__dirname, 'lodash.js'), 'utf8'); var source = fs.readFileSync(path.join(__dirname, 'lodash.js'), 'utf8');
// flag used to specify if the build should include the "use strict" directive
var useStrict = isStrict || !(isLegacy || isMobile);
// flag used to specify replacing Lo-Dash's `_.clone` with Underscore's // flag used to specify replacing Lo-Dash's `_.clone` with Underscore's
var useUnderscoreClone = isUnderscore; var useUnderscoreClone = isUnderscore;
@@ -1037,17 +1034,10 @@
'setTimeout': setTimeout 'setTimeout': setTimeout
}); });
if (isStrict) { source = setUseStrictOption(source, isStrict);
source = setUseStrictOption(source, true);
} else {
// remove "use strict" directive
source = source.replace(/(["'])use strict\1;( *\n)?/, '');
if (!useStrict) {
source = setUseStrictOption(source, false);
}
}
if (isLegacy) { if (isLegacy) {
_.each(['getPrototypeOf', 'isBindFast', 'isKeysFast', 'isStrictFast', 'nativeBind', 'nativeIsArray', 'nativeKeys'], function(varName) { _.each(['getPrototypeOf', 'isBindFast', 'isKeysFast', 'nativeBind', 'nativeIsArray', 'nativeKeys'], function(varName) {
source = replaceVar(source, varName, 'false'); source = replaceVar(source, varName, 'false');
}); });
@@ -1607,7 +1597,6 @@
} }
if (isRemoved(source, 'createIterator', 'bind')) { if (isRemoved(source, 'createIterator', 'bind')) {
source = removeVar(source, 'isBindFast'); source = removeVar(source, 'isBindFast');
source = removeVar(source, 'isStrictFast');
source = removeVar(source, 'nativeBind'); source = removeVar(source, 'nativeBind');
} }
if (isRemoved(source, 'createIterator', 'bind', 'isArray', 'isPlainObject', 'keys')) { if (isRemoved(source, 'createIterator', 'bind', 'isArray', 'isPlainObject', 'keys')) {
@@ -1668,7 +1657,7 @@
} }
// inject "use strict" directive // inject "use strict" directive
if (isStrict) { if (isStrict) {
source = source.replace(/^(\/\*![\s\S]+?\*\/\n;\(function[^)]+\){)([^'"])/, '$1"use strict";$2'); source = source.replace(/^([\s\S]*?function[^{]+{)([^'"])/, '$1"use strict";$2');
} }
if (isStdOut) { if (isStdOut) {
stdout.write(source); stdout.write(source);

View File

@@ -574,9 +574,9 @@
QUnit.module('strict modifier'); QUnit.module('strict modifier');
(function() { (function() {
var object = Object.create(Object.prototype, { var object = Object.freeze({
'a': { 'value': _.identify }, 'a': _.identity,
'b': { 'value': null } 'b': null
}); });
['non-strict', 'strict'].forEach(function(strictMode, index) { ['non-strict', 'strict'].forEach(function(strictMode, index) {
@@ -589,20 +589,26 @@
} }
build(commands, function(source, filePath) { build(commands, function(source, filePath) {
var basename = path.basename(filePath, '.js'), var basename = path.basename(filePath, '.js'),
context = createContext(), context = createContext();
pass = !index;
vm.runInContext(source, context); vm.runInContext(source, context);
var lodash = context._; var lodash = context._;
try { var actual = _.every([
lodash.bindAll(object); function() { lodash.bindAll(object); },
lodash.extend(object, { 'a': 1 }); function() { lodash.extend(object, { 'a': 1 }); },
lodash.defaults(object, { 'b': 2 }); function() { lodash.defaults(object, { 'b': 2 }); }
} catch(e) { ], function(fn) {
pass = !!index; var pass = !index;
} try {
equal(pass, true, basename); fn();
} catch(e) {
pass = !!index;
}
return pass;
});
equal(actual, true, basename);
start(); start();
}); });
}); });

View File

@@ -21,7 +21,7 @@
case 'lodash-prod': return 'lodash.min.js'; case 'lodash-prod': return 'lodash.min.js';
case 'lodash-underscore': return 'lodash.underscore.min.js'; case 'lodash-underscore': return 'lodash.underscore.min.js';
case 'lodash-custom': return 'lodash.custom.min.js'; case 'lodash-custom': return 'lodash.custom.min.js';
case 'lodash-custom-debug': return 'lodash.custom'.js; case 'lodash-custom-debug': return 'lodash.custom.js';
} }
return 'lodash.js'; return 'lodash.js';
}()); }());