admin管理员组文章数量:1289876
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?
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?
Share Improve this question asked Mar 21, 2014 at 9:42 JazcashJazcash 3,3122 gold badges34 silver badges52 bronze badges3 Answers
Reset to default 6You 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./a/11987281/85010 and https://stackoverflow./a/10559790/85010.
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-mit 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)
use-strict-cli is a Node.js mand line tool for adding/removing "use strict"
statements within files found in given directories.
Instructions can be found here:
https://github./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
本文标签: javascriptNode JS avoid laboriously adding quotuse strictquot to all my filesStack Overflow
版权声明:本文标题:javascript - Node JS avoid laboriously adding "use strict" to all my files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741427294a2378149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论