variable name for require'ing a module and jslint
I've just started to play around with nodejs and came across jslint, so I decided to test my code using this: jslint module for nodejs. I have 2 questions:
1.- Take for example this snippet:
var util = require('util');
when running jslint like:
jslint --indent 4 --white --regexp --goodparts file.js
it will produce:
/*jslint indent: 4, white: true, regexp: true, goodparts: true, node: true, es5: true, onevar: true, undef: true, nomen: true, plusplus: true, bitwise: true, newcap: true */
1 1,5: Redefinition of 'util开发者_开发知识库'.
var util = require('util');
2 1,10: Read only.
var util = require('util');
If I change the snippet to:
var utilButADifferentName = require('util');
The output is completely clean.
The code seems to work ok either way. So who can enlighten me about this? :)
2.- Take the following snippet:
function Some() {
}
exports.Some = Some;
Running jslint with the same options yields:
1 4,1: 'exports' has not been fully defined yet.
exports.Some = Some;
Is this because of the options I'm using with JSLint or am I missing something else? Any good tips on the best (generically speaking) options to use with JSLint when "linting" a nodejs code?
Thanks in advance!
1) It looks like node-jslint comes with a slightly out of date version of node.js. The issue was with util appears to have been fixed.
You might want to take a look at JSHint and node-jshint. I believe these are known to be much more configurable than JSLint.
2) Is it not simply because you have not initialised exports before you used it?
function Some() {
}
var exports = {
Some: Some
};
jsLint is built for client-side JavaScript only. In my experience, making it play nice with server-side code, isn't worth the trouble - at least compared to the relatively low overhead of switching to something like jshint.
Wrt. your errors, util
is a reserved variable name in the eyes of jslint (why it complains about you overwriting it), and as exports
is a purely server-side thing, jslint barfs over that as well (it believes your'e writing to undefined
).
By comparison, jshint has a Node.js-mode, where all the global Node.js stuff is pre-defined and passes without incident (if you write otherwise linting code).
JSLint seems to be out of date on node's native objects. Just remove the node:true
option and ignore warnings, or create your own (updated) definition of globals:
/*global Buffer: false, clearInterval: false, clearTimeout: false, console: false, global: false, module: false, exports: false, process: false, require: false, setInterval: false, setTimeout: false, __filename: false, __dirname: false */
See the defaults being used: http://www.jslint.com/lint.html
精彩评论