5

So I want to enable strict mode for my project, but I have a lot of files and adding "use strict"; to the top of all of them would be a pain. I discovered the --use_strict CLI option for node which is awesome, but enables it for every single file inside my project directory, including all the third-party modules and their modules and I really don't want to go through correcting everybody's files, just mine.

So my question is, is there a way I could automate adding "use strict" to only my files?

Jazcash
  • 3,145
  • 2
  • 31
  • 46

3 Answers3

6

You could write a script that opens each file and adds "use strict"\n to the beginning of it and write it back to disk.

Very untested example:

var fs = require("fs"),
   files = fs.readDirSync('./'),
   i;

for (i = 0; i < files.length; i += 1) {
  var fileContent = fs.readFileSync(files[i]).toString();
  fileContent = "\"use strict\"\n" + fileContent;
  fs.appendFileSync(files[i], fileContent);
}

Test for file[i] being a directory and move to a function to make it recursive if you need.

Inspired by https://stackoverflow.com/a/11987281/85010 and https://stackoverflow.com/a/10559790/85010.

Community
  • 1
  • 1
fiskeben
  • 3,395
  • 4
  • 31
  • 35
  • 2
    Thanks. I enhanced your example with a check if `use strict` already exists in the file, and glob pattern matching: https://gist.github.com/blaise-io/b011f63a3f6624816fad. I hope someone may find it useful. – Blaise Jul 27 '15 at 12:07
  • @maxheiber According to [the documentation of fs](https://nodejs.org/api/fs.html), there is no function called `readDirStrict`. Where did you get that from? – fiskeben Jan 20 '18 at 08:31
2

use-strict-cli is a Node.js command line tool for adding/removing "use strict" statements within files found in given directories.

Instructions can be found here:

https://github.com/philidem/use-strict-cli

Example usage to add missing "use strict" statements:

use-strict ./src

Example usage to remove missing "use strict" statements:

use-strict ./src --remove

philidem
  • 331
  • 2
  • 7
1

I suggest you adopt jshint in your work cycle and let it do this for you, amongst a bunch of other useful checks that will guarantee good-quality code.

I maintain a .jshintrc at the root of my web project with options:

{
  "immed": true,
  "latedef": true,
  "newcap": true,
  "nonew": true,
  "trailing": true,
  "multistr": true,
  "devel": true
}

You see that strict: true is in fact missing. This is because jshint has it set to true by default :)

Then you can have jshint setup to run as a git pre-commit hook or even better install it as a plugin for your code editor and fix the errors in real time as you code (e.g. SublimeLinter package for Sublime text 3)

Thalis K.
  • 7,363
  • 6
  • 39
  • 54
  • I beg to differ. On the github page of jshint, there is a sample `.jshintrc` with the default values for all options and `strict` is shown as `true`. It can be found here: https://github.com/jshint/jshint/blob/master/examples/.jshintrc – Thalis K. Jun 19 '14 at 08:15
  • i saw that - but it is not correct. try it out: create a global `x=10;` in your code and run jshint with and without `strict: true` (and nothing else in .jshintrc). it will not report the global (ie, does not apply the strict rules). maybe this is a bug, but the docs certainly aren't clear on what the default behaviour is. – Tom Carchrae Jun 19 '14 at 20:06
  • Hmm, very interesting. You are right. It would be nice if you would report it to the [maintainer of jshint](http://stackoverflow.com/users/71701/anton-kovalyov). I have been misled by the docs :( – Thalis K. Jun 20 '14 at 07:34
  • no worries - it is a shame that the docs aren't more clear. in some regards, it would not be correct to enforce the same rules; the addition of `"use strict";` in your code changes the rules of allowable javascript. anyway, i hope this knowledge means you can find more bugs before they bite you. – Tom Carchrae Jun 20 '14 at 14:17