From 15cf2ad076a17cfb595016f5d035b3c9e4a29d5b Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 22 Oct 2012 21:27:09 -0700 Subject: [PATCH] Rework "strict" build option and fix typo in test-ui.js. Former-commit-id: e06045546bea43bbe1fff78d6c948036ad98a88c --- build.js | 31 ++++++++++--------------------- test/test-build.js | 32 +++++++++++++++++++------------- test/test-ui.js | 2 +- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/build.js b/build.js index 8c1258712..2e064cc36 100755 --- a/build.js +++ b/build.js @@ -784,12 +784,12 @@ * @returns {String} Returns the modified source. */ function setUseStrictOption(source, value) { - // remove `isStrictFast` assignment - return removeVar(source, 'isStrictFast') - // replace `useStrict` branch in `value` with hard-coded option - .replace(/(?: *\/\/.*\n)*(\s*)' *<%.+?useStrict.+/, value ? "$1'\\'use strict\\';\\n' +" : '') - // remove `useStrict` from iterator options - .replace(/(?:,\s*'useStrict':[^,]+?\n| *'useStrict':[^,]+,\n)/g, ''); + // inject "use strict" + if (value) { + source = source.replace(/^[\s\S]*?function[^{]+{/, "$&\n 'use strict';"); + } + // replace `useStrict` branch in `value` with hard-coded option + return source.replace(/(?: *\/\/.*\n)*(\s*)' *<%.+?useStrict.+/, value ? "$1'\\'use strict\\';\\n' +" : ''); } /*--------------------------------------------------------------------------*/ @@ -940,9 +940,6 @@ // the lodash.js source 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 var useUnderscoreClone = isUnderscore; @@ -1037,17 +1034,10 @@ 'setTimeout': setTimeout }); - if (isStrict) { - source = setUseStrictOption(source, true); - } else { - // remove "use strict" directive - source = source.replace(/(["'])use strict\1;( *\n)?/, ''); - if (!useStrict) { - source = setUseStrictOption(source, false); - } - } + source = setUseStrictOption(source, isStrict); + 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'); }); @@ -1607,7 +1597,6 @@ } if (isRemoved(source, 'createIterator', 'bind')) { source = removeVar(source, 'isBindFast'); - source = removeVar(source, 'isStrictFast'); source = removeVar(source, 'nativeBind'); } if (isRemoved(source, 'createIterator', 'bind', 'isArray', 'isPlainObject', 'keys')) { @@ -1668,7 +1657,7 @@ } // inject "use strict" directive 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) { stdout.write(source); diff --git a/test/test-build.js b/test/test-build.js index ae598f98e..89548e397 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -574,9 +574,9 @@ QUnit.module('strict modifier'); (function() { - var object = Object.create(Object.prototype, { - 'a': { 'value': _.identify }, - 'b': { 'value': null } + var object = Object.freeze({ + 'a': _.identity, + 'b': null }); ['non-strict', 'strict'].forEach(function(strictMode, index) { @@ -589,20 +589,26 @@ } build(commands, function(source, filePath) { var basename = path.basename(filePath, '.js'), - context = createContext(), - pass = !index; + context = createContext(); vm.runInContext(source, context); var lodash = context._; - try { - lodash.bindAll(object); - lodash.extend(object, { 'a': 1 }); - lodash.defaults(object, { 'b': 2 }); - } catch(e) { - pass = !!index; - } - equal(pass, true, basename); + var actual = _.every([ + function() { lodash.bindAll(object); }, + function() { lodash.extend(object, { 'a': 1 }); }, + function() { lodash.defaults(object, { 'b': 2 }); } + ], function(fn) { + var pass = !index; + try { + fn(); + } catch(e) { + pass = !!index; + } + return pass; + }); + + equal(actual, true, basename); start(); }); }); diff --git a/test/test-ui.js b/test/test-ui.js index 220b20a26..6f3ee4209 100644 --- a/test/test-ui.js +++ b/test/test-ui.js @@ -21,7 +21,7 @@ case 'lodash-prod': return 'lodash.min.js'; case 'lodash-underscore': return 'lodash.underscore.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'; }());