Reputation: 3312
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?
Upvotes: 5
Views: 3821
Reputation: 329
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
Upvotes: 2
Reputation: 7763
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)
Upvotes: 2
Reputation: 3467
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.
Upvotes: 6