├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── assign-to-getter-only.js ├── assign-to-new-prop-on-nonextensible.js ├── assign-to-non-writable.js ├── delete-raw-name.js ├── delete-undeletable.js ├── duplicate-assign.js ├── duplicate-fn-args.js ├── index.js ├── octal-literal.js └── with.js /LICENSE: -------------------------------------------------------------------------------- 1 | The ISC License 2 | 3 | Copyright (c) Isaac Z. Schlueter and Contributors 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR 15 | IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # use-strict 2 | 3 | Makes all modules in Node get loaded in strict mode. 4 | 5 | ## Usage 6 | 7 | ```javascript 8 | require('use-strict') 9 | // That's it, now everything is strict forever. 10 | // in other words: FTFY, YOU'RE WELCOME. 11 | ``` 12 | 13 | ## Downside 14 | 15 | Strict mode in JavaScript is virtually always a great thing. It 16 | prevents accidental global leakage, turns silent mistakes into errors, 17 | and removes `with` and `arguments.callee` and their sordid 18 | complexities. It's mostly a Good Thing. 19 | 20 | Unfortunately, it also removes octal literals, which is kind of a 21 | bummer. You can pass octal strings to Node's functions that deal with 22 | file modes, and they'll do the right thing, so it's not completely 23 | horrible. 24 | 25 | The implementation works by patching Node's internal `module.wrapper` 26 | array, and then freezing it, so that further modifications are not 27 | possible. 28 | 29 | This means that error printouts that occur on the first line of a node 30 | module will be off by a few characters, since Node does a bit of math 31 | to account for its wrapper script, which will now be off by 13 32 | characters. This is probably not a big problem, and not really worth 33 | working around. 34 | 35 | Also, this means that the *current* module will not be affected. You 36 | should still `"use strict"` in the module that does 37 | `require('use-strict')`. This module applies strictness to all 38 | *future* modules loaded by your program. 39 | 40 | **Note** You can also run `node --use_strict` and get the same effect 41 | without any of the caveats. 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var module = require('module') 2 | module.wrapper[0] += '"use strict";' 3 | Object.freeze(module.wrap) 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "use-strict", 3 | "version": "1.0.1", 4 | "description": "Makes all modules in Node get loaded in strict mode.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/isaacs/use-strict" 12 | }, 13 | "keywords": [ 14 | "use strict", 15 | "strict mode" 16 | ], 17 | "author": "Isaac Z. Schlueter (http://blog.izs.me/)", 18 | "license": "ISC", 19 | "readmeFilename": "README.md", 20 | "gitHead": "a7cdf43563a5ac514c96b6f781d9018e0b7a68e8", 21 | "bugs": { 22 | "url": "https://github.com/isaacs/use-strict/issues" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /test/assign-to-getter-only.js: -------------------------------------------------------------------------------- 1 | // Assignment to a getter-only property 2 | var obj2 = { get x() { return 17; } }; 3 | obj2.x = 5; // throws a TypeError 4 | -------------------------------------------------------------------------------- /test/assign-to-new-prop-on-nonextensible.js: -------------------------------------------------------------------------------- 1 | // Assignment to a new property on a non-extensible object 2 | var fixed = {}; 3 | Object.preventExtensions(fixed); 4 | fixed.newProp = "ohai"; // throws a TypeError -------------------------------------------------------------------------------- /test/assign-to-non-writable.js: -------------------------------------------------------------------------------- 1 | // Assignment to a non-writable property 2 | var obj1 = {}; 3 | Object.defineProperty(obj1, "x", { value: 42, writable: false }); 4 | obj1.x = 9; 5 | -------------------------------------------------------------------------------- /test/delete-raw-name.js: -------------------------------------------------------------------------------- 1 | var x; delete x; -------------------------------------------------------------------------------- /test/delete-undeletable.js: -------------------------------------------------------------------------------- 1 | delete Object.prototype; // throws a TypeError -------------------------------------------------------------------------------- /test/duplicate-assign.js: -------------------------------------------------------------------------------- 1 | // duplicate assignment is a syntax error, not a TypeError! 2 | var o = { p: 1, p: 2 }; // !!! syntax error 3 | -------------------------------------------------------------------------------- /test/duplicate-fn-args.js: -------------------------------------------------------------------------------- 1 | function sum(a, a, c){ // !!! syntax error 2 |   "use strict"; 3 |   return a + b + c; // wrong if this code ran 4 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | require('../index.js') 2 | var assert = require('assert') 3 | var fs = require('fs') 4 | var n = 0 5 | fs.readdirSync(__dirname).forEach(function (file) { 6 | if (file === 'index.js' || !file.match(/\.js$/)) 7 | return 8 | assert.throws(function() { 9 | require('./' + file) 10 | }) 11 | console.log('ok %d - %s throws', ++n, file) 12 | }) 13 | console.log('0..%d', n) 14 | -------------------------------------------------------------------------------- /test/octal-literal.js: -------------------------------------------------------------------------------- 1 | var sum = 015 + // !!! syntax error 2 |           197 + 3 |           142; -------------------------------------------------------------------------------- /test/with.js: -------------------------------------------------------------------------------- 1 | var x = 17; 2 | var obj = { x : 100 } 3 | with (obj) // !!! syntax error 4 | { 5 |   // If this weren't strict mode, would this be var x, or 6 |   // would it instead be obj.x?  It's impossible in general 7 |   // to say without running the code, so the name can't be 8 |   // optimized. 9 |   x; 10 | } 11 | --------------------------------------------------------------------------------