Git Product home page Git Product logo

node-jslint's Introduction

node-jslint

Easily use JSLint from the command line.

  jslint bin/jslint.js

What's New

Added latest jslint, 2018-01-27.

Version 0.12.0 contains the latest jslint-es6

See CHANGELOG.md for detailed change history

Use the command-line client

Install (both local and global are supported)

npm i jslint

Use the default jslint

jslint lib/color.js

Always use the latest jslint

jslint --edition=latest lib/color.js

Use a specific edition

For example, edition 2013-02-03 which shipped with node-jslint 0.1.9:

jslint --edition=2013-02-03 lib/color.js

Use node-jslint programmatically

Streams interface

As of node-jslint 0.4.0, a streams interface is exposed. You can use it in client code like this:

Install as a dependency:

$ npm install --save jslint

Pull it into your code with require:

var LintStream = require('jslint').LintStream;

Create and configure the stream linter:

var options = {
    "edition": "latest",
    "length": 100
},
    l = new LintStream(options);

Send files to the linter:

var fileName, fileContents;
l.write({file: fileName, body: fileContents});

Receive lint from the linter:

l.on('data', function (chunk, encoding, callback) {
    // chunk is an object

    // chunk.file is whatever you supplied to write (see above)
    assert.deepEqual(chunk.file, fileName);

    // chunk.linted is an object holding the result from running JSLint
    // chunk.linted.ok is the boolean return code from JSLINT()
    // chunk.linted.errors is the array of errors, etc.
    // see JSLINT for the complete contents of the object

    callback();
});

You can only pass options to the LintStream when creating it. The edition option can be used to select different editions of JSLint.

The LintStream is in object mode (objectMode: true). It expects an object with two properties: file and body. The file property can be used to pass metadata along with the file. The body property contains the file to be linted; it can be either a string or a Buffer.

The LintStream emits 'data' events containing an object with two properties. The file property is copied from the file property that is passed in. The linted property contains the results of running JSLINT.

Simple interface

The simple interface provides an edition-aware loader. This can be used as a frontend to node-jslint's collection of editions of the JSLINT code.

var node_jslint = require('jslint'),
    JSLINT = node_jslint.load(edition);

This exposes the same loading interface used in node-jslint, so it supports the special edition names default and latest as well as date-based edition names such as 2013-08-26

As of version 0.5.0, the load function also accepts filenames. To be recognized as a filename, the argument to load must contain a path-separator character (/ or \) or end with the extension .js.

Usage examples

Multiple files

jslint lib/color.js lib/reporter.js

All JSLint options supported

jslint --white --vars --regexp lib/color.js

Defaults to true, but you can specify false

jslint --bitwise false lib/color.js

Pass arrays

jslint --predef $ --predef Backbone lib/color.js

JSLint your entire project

jslint '**/*.js'

Using JSLint with a config file

Start with the included jslint.conf.example file, name it jslint.conf and customize your options per project or copy it to $HOME/.jslint.conf to apply your setting globally

License

See LICENSE file.

node-jslint's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-jslint's Issues

The "File is OK" message obscures issues found by jslint

For each file scanned with no issues found, the message "FILE is OK" is output, meaning that we have to scroll up to find which issue caused our build to fail. If this message did not exist, we would see the issue message right above the build failed line.

It would be nice to remove this message or have an option to hide it.

Stop on errors?

I would like to use jslint on all files in a directory, which this line handles well - find . -name "*.js" -print0 | xargs -0 jslint. Unfortunately I will end up with my terminal filled up of errors if I have several erroneous files in my directory.

So, is there a way I could setup jslint to bail and halt further execution/scanning instead of continuing with the next file?

Here's what I am doing now in my makefile, altough not pretty:

lint:
    @set -e; find . -path ./node_modules -prune -o -type f -name "*.js" | sort | while read file ; do \
        jslint --color --sloppy $$file; \
     done

Problems parsing command line arguments correctly?

jslint --terse --maxlen 80 -- file.js
interprets '80' as the file name and throws error when it doesn't find it

jslint --maxlen 80 --terse -- file.js
works as expected

jslint --terse true --maxlen 80 -- file.js
also works as expected

nopt

Please use git submodules to put nopt module in your library submodules.

Error: Cannot find module 'nopt'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
at require (module.js:346:19)
at Object. (/home/silent/Projects/twic/tools/node-jslint/bin/jslint.js:5:12)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at Array. (module.js:421:10)
at EventEmitter._tickCallback (node.js:126:26)

Error installing

Hi there,

Trying to install on a fresh node + npm install I get :

~/Sites [master] $ sudo npm install -g jslint
npm WARN [email protected] package.json: bugs['web'] should probably be bugs['url']
npm ERR! Failed unpacking /tmp/npm-1320803143143/1320803143143-0.5566549822688103/tmp.tgz
npm ERR! couldn't unpack /tmp/npm-1320803143143/1320803143143-0.5566549822688103/tmp.tgz to /tmp/npm-1320803143143/1320803143143-0.5566549822688103/contents
npm ERR! error installing [email protected] Error: `tar "-zmvxpf" "/tmp/npm-1320803143143/1320803143143-0.5566549822688103/tmp.tgz" "-o"`
npm ERR! error installing [email protected] failed with 2
npm ERR! error installing [email protected]     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/tar.js:219:20)
npm ERR! error installing [email protected]     at ChildProcess.emit (events.js:67:17)
npm ERR! error installing [email protected]     at ChildProcess.onexit (child_process.js:192:12)
npm ERR! Error: `tar "-zmvxpf" "/tmp/npm-1320803143143/1320803143143-0.5566549822688103/tmp.tgz" "-o"`
npm ERR! failed with 2
npm ERR!     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/tar.js:219:20)
npm ERR!     at ChildProcess.emit (events.js:67:17)
npm ERR!     at ChildProcess.onexit (child_process.js:192:12)
npm ERR! Report this *entire* log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>
npm ERR! 
npm ERR! System Darwin 11.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "jslint"
npm ERR! cwd /Users/temp
npm ERR! node -v v0.4.12
npm ERR! npm -v 1.0.104
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/temp/npm-debug.log
npm not ok

I'd really like to find a solution.

Thanks,

Gabriel

Only one lint error per file (SublimeLinter3, Windows)

Via email:

I’m trying to track down a bug that somehow involves node-jslint v0.3.2 and above.

I’m using Sublime Text 3 with SublimeLinter 3 (https://sublime.wbond.net/packages/SublimeLinter) + a JSLint plugin (https://sublime.wbond.net/packages/SublimeLinter-contrib-jslint). The jslint plugin is using node-jslint as the actual linter.

Up and until v0.3.1 things worked as expected: linter errors are highlighted in real time as you work with your code in ST3. From v0.3.2 onwards, however, only the first linter error in each file is highlighted. When I fix that error, only then is the next error highlighted, and so on.

Exit code is not set properly if stdout is not a TTY

Hi!

First, I'm sorry if that's expected behavior (but at least one other person found it most confusing).

I'm running jslint 0.3.1; and when the output is not a terminal, the exit code is always zero. See:

echo aaa > test.js
jslint test.js ; echo $?
# displays 1 at the end, as expected
jslint test.js >/dev/null ; echo $?
# displays 0 at the end
jslint test.js | cat ; echo $?
# also displays 0

I wonder if the problem might be in main.js:199; after reading stream.writable drain event docs I don't understand exactly why jslint triggers on that event.

No equivalency for --var in jslintrc.example

I kept getting:

"Combine this with the previous 'var' statement."

Using --var along with the command got rid of it. The jslintrc.example file shows "onevar", so I assumed it would be the equivalent in the rc-file, but it's not.

lib/jslint.js line 207 shows "vars" in a comment. That seemed to work!

I think "onevar" should be replaced with "vars" in jslintrc.example.

differs from jslint

It seems node-jslint is configured quite differently from standard jslint. For example, it doesn't care about indentation or brace placement. Why is that?

"Use spaces, not tabs"

I get a "Use spaces, not tabs" error, when using node-jslint via a quick fix vim plugin.

Is this part of JSLint? I do not see anything about spaces vs tabs on jslint.com.

SyntaxError: Unexpected end of input

With the new (0.2.8) version of node-jslint I'm not able to lint any file.

$ node -v
v0.10.21

$ npm -v
1.3.11

$ npm list -g | awk -F@ '/jslint/{print $2}' # jslint version                                                                                                                                                            
0.2.8

$ cat /tmp/test.js                                                                                                                                                                                                       
var x = 0;

$ jslint /tmp/test.js                                                                                                                                                                                                    
undefined:0
^
SyntaxError: Unexpected end of input
    at Object.parse (native)
    at loadAndParseConfig (/home/pshevtsov/node/lib/node_modules/jslint/lib/linter.js:10:18)
    at mergeConfigs (/home/pshevtsov/node/lib/node_modules/jslint/lib/linter.js:38:22)
    at loadConfig (/home/pshevtsov/node/lib/node_modules/jslint/lib/linter.js:58:18)
    at Object.exports.lint (/home/pshevtsov/node/lib/node_modules/jslint/lib/linter.js:129:18)
    at /home/pshevtsov/node/lib/node_modules/jslint/lib/main.js:85:31
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

Some extra useful information

Checking the parse tree of JSLint, there are a few extra good pieces of information you can get.

To be able to exploit that, linter.js must return the parsing tree, which can be easily done by adding:

    result.tree = JSLINT.tree;

close to where other bits of information are returned.

Later in reporter.js, it is easy to list globals, which are rarely a good thing:

    if (lint.globals) {
        console.log('\nGlobals:\n' + lint.globals.join(' , ') + '\n');
    }

And also both unused or undefined variables:

    var uu = function (what, label) {
        if (what) {
            var i, d, snippets = [];
            for (var i = 0;i < what.length; i++) {
                d = what[i];
                snippets[i] = '[' + (d.line + 1) + ',1] en: ' + d['function'] + ' - ' + d.name;
            }
            console.log('\n' + label + ' variable:\n' + snippets.join('\n') + '\n');
        }
    };
    uu(lint.undefined, 'Undefined');            
    uu(lint.unused, 'Unused');      

The most complex addition is to list repeated literals, good candidates to turn into constants that the YUI Compressor might significantly reduce:

    var repeated = function (tree) {
        var strs = {},
            re = /^(first|second|third|else|block|\d+)$/,

            traverse = function (what) {
                var k,
                    v = what.id;
                if (v === '(string)' || v === '(number)') {
                    v = what.value;
                    if (v in strs) {
                        strs[v] += 1;
                    } else {
                        strs[v] = 1;
                    }
                }

                for (k in what) {
                    if (what.hasOwnProperty(k)) {
                        if (re.test(k)) {
                            v = what[k];
                            if (v) {
                                traverse(v);
                            }
                        }
                    }
                }
            };

        traverse(tree);
        console.log('Repeated Constants:\n');
        var now = 2,
            next,
            k,
            v,
            strs1 = {};

        for (k in strs) {
            if (strs.hasOwnProperty(k)) {
                v = strs[k];
                if (v !== 1) {
                    strs1[k] = v;
                }
            }
        }
        strs = strs1;

        while (now < Number.MAX_VALUE) {
            console.log(now + ':');
            next = Number.MAX_VALUE;
            for (k in strs) {
                if (strs.hasOwnProperty(k)) {
                    v = strs[k];
                    if (v === now) {
                        console.log('    ' + k);
                    } else {
                        if (v > now) {
                            next = Math.min(next, v);
                        }
                    }
                }
            }
            now = next;
        }
    };
    repeated(lint.tree);

Crockford made significant changes to JSLint recently, the tree is different now, I had to go back to an earlier version but my current one actually works with the latest JSLint. I am not sure if I picked the correct one for the version you are using. The code listing the repeated constants works but the globals, undefined and unused didn't show any in a few samples I tried, there might not be any (which is good) or I got the wrong version of my code for your version of JSLint.

Can't require linter

I want'to use jslint in my script as blow:

var jslint = require('jslint');
jslint.lint(...);

But require('jslint') only return an empty object.

node v0.6.17, npm 1.0.106, node-jslint 0.1.8

"--passfail false"

The "--passfail false" parameter does not seem to work as expected:

$ jslint --sloppy --white --vars --passfail false app.js

app.js
 #1 Move 'var' declarations to the top of the function.
    for (var i = 0; i < someVar; i++) { // Line 10, Pos 10
 #2 Stopping.  (25% scanned).
     // Line 10, Pos 10

--passfail true and --passfail false have been tried, both exit after the error.

Not picking up on unused variables

e.g.
var matches = getMatches(text), fn;
matches.eachKey(function (fn) {
...
}

Should report that the first fn is unused (as it does on the JSLint website). Since this is an error due to scope, it's consequently quite likely to occur in JS and a pain to debug.

Can't pipe output to less

I'd like to jslint app.js | less, but I'm having trouble capturing the output. Could jslint please report using stdout instead of stderr?

"Tolerate many var statements per function"

Checking the jslint.com website, there's an option named Tolerate many var statements per function which does what is discussed here: http://stackoverflow.com/questions/4646455/jslint-error-move-all-var-declarations-to-the-top-of-the-function

I couldn't find it among the arguments. Is it already there?

#jslint
No files specified.
Usage: /usr/bin/jslint [--indent] [--maxerr] [--maxlen] [--predef] [--anon] [--bitwise] [--browser] [--cap] [--continue] [--css]
[--debug] [--devel] [--eqeq] [--es5] [--evil] [--forin] [--fragment] [--newcap] [--node] [--nomen] [--on] [--passfail] [--plusplus] 
[--properties] [--regexp] [--rhino] [--undef] [--unparam] [--sloppy] [--sub] [--vars] [--white] [--widget] [--windows] [--json] [--color]
[--] <scriptfile>...

npm install jslint -g

on Ubuntu 10.04 I had to use the -g flag to get the command line jslint to work maybe worth mentioning in the readme.

Inconsistent results compared to website

Given this JS:

/*global console*/
(function () {
    'use strict';

    function boink(i) {
        console.log('I <3 you');
    }

    var xyz, abc = this;
    console.log('what about xyz and abc?');
}());

The website jslint.com says all of the variables and the function boink are never used.

The command line vesion, using "jslint test.js" reports no problems.

Man Page Not Saving Properly When Installing Modules Globally

Hey Guys:

I have the following set in my ~/.npmrc file:

prefix=/home/marshall/.local
manroot=/home/marshall/.local/man

When I perform npm install -g jslint, I would expect the jslint man page to be installed at /home/marshall/.local/man but it is not.

I tried to do some digging around but I could not find anything obviously incorrect. I am wondering if this is an issue with the package or possibly node itself.

Thoughts?

~M

error installing with npm

Hi,
i got an error with your package dependecies, npm doesn't find compatible versions.

I solved updating nodejs, maybe my npm version did'nt support a dependency definition like 'glob@^4.0.4' (maybe the ^ char?).

I report this just for information. My environment version before the update was:
node: 0.10.9
npm: 1.2.24

Thanks.

Update of jslint?

Is there any near term plan to pull in recent fixes from jslint? We need a fix Douglas Crockford made on May 10.

-Jerry

New minor version of node-jslint

@okuryu @reid

I've refactored node-jslint a bit in order to use streams (to support a feature request from gulp-jslint). This is currently committed to github as 0.4.0-pre. I'll let it sit for a week and then publish it to npm as 0.4.0.

This changes node-jslint to require engine >= 0.10, which will probably not inconvenience anyone.

Broken behavior with --goodparts as the last / single flag: No files specified.

aws2js|future⚡ ⇒ jslint --goodparts aws.js               
No files specified.
Usage: /usr/local/bin/jslint [...]
aws2js|future⚡ ⇒ jslint --var --goodparts aws.js
No files specified.
Usage: /usr/local/bin/jslint [...]
aws2js|future⚡ ⇒ jslint --node --goodparts aws.js
No files specified.
Usage: /usr/local/bin/jslint [...]
aws2js|future⚡ ⇒ jslint --goodparts --node aws.js

aws.js is OK.
aws2js|future⚡ ⇒ cat /usr/local/lib/node_modules/jslint/package.json | grep version
  "version": "0.1.9",

Aren't the flags supposed to be flags? Even the ordering of --goodparts with --var produces inconsistent behavior.

add --generate-jslintrc option

No linting: just take ~/.jslintrc, local jslintrc, and command line options and generate jslintrc file to stdout

-- see if this is in nopt?
-- maybe it belongs in nopt?

unreachable /*jslint

placing a /*jslint option after a break statement results in jslint saying the line is unreachable. While technically true, I don't think the actual execution path should matter to the placement of options in the file.

Refactor logging code (linter, nodelint)

Logging code is now duplicated between linter, nodelint.

Refactor to eliminate duplication. Better mock function for testing. Check whether it makes sense to use someone else's log. Would be nice to make node-jslint's log usage compatible with whatever grunt-jslint needs.

Option to display version number (e.g. --version)

Would make it easier to implement version checking in downstream dependencies of node-jslint (e.g. SublimeLinter3 offers a framework to implement version checking, which requires that the linter can be passed an argument to make it return its version number).

Thanks!

The Windows EOL breaks jslint on OS X

node -v
v0.10.21

npm -g install jslint
npm http GET https://registry.npmjs.org/jslint
npm http 304 https://registry.npmjs.org/jslint
npm http GET https://registry.npmjs.org/nopt
npm http 304 https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/abbrev
npm http 304 https://registry.npmjs.org/abbrev
/Users/SaltwaterC/.nvm/v0.10.21/bin/jslint -> /Users/SaltwaterC/.nvm/v0.10.21/lib/node_modules/jslint/bin/jslint.js
[email protected] /Users/SaltwaterC/.nvm/v0.10.21/lib/node_modules/jslint
└── [email protected] ([email protected])

jslint
env: node\r: No such file or directory

The issue resides with jslint.js on the shebang line. Just to make sure the CRLF is to blame, here's the hexdump of the shebang followed by the newlines:

0000000 23 21 2f 75 73 72 2f 62 69 6e 2f 65 6e 76 20 6e
0000010 6f 64 65 0d 0a 0d 0a

Error reporting on one line

The current format for errors is something like:

#1 Missing space between 'input' and '='.
   var input= "", line = ""; // Line 1, Pos 10

Which doesn't work well within editors that try and parse it. Could you add an option to report one error per line, eg:

Error:1:10: Missing space between 'input' and '='.

See http://lapin-bleu.net/riviera/?p=191 for details on how this causes issues with on-the-fly jslint validation in emacs

Exit code broken

Commit 680641e broke jslint for me, far as I can tell. The stdout drain event never fires - hence jslint always exist with a 0, as the program just stops after linting the last file. This means that jslint no longer works in commit hooks, which is somewhat of a problem

maxlen set to 0 (OSX Mavericks)

https://plus.google.com/u/0/+JulianoLazzarotto/posts/L5Gk82Adj66

Running on Mavericks:
$ node node_modules/jslint/bin/jslint.js --node --vars --devel --nomen --stupid --indent 4 src/.js src/Model/.js

evalmachine.:714
message: bundle.scanned_a_b.supplant({
^
JSLintError: Stopping. (1% scanned).

Got this error running on Mac, any clue?

6 comments

Ben QuarmbyYesterday 6:25 PM

Does it happen on http://www.jslint.com/?

Just trying to work out if it's the node module or core JSLint.

Juliano LazzarottoYesterday 6:32 PM

Hi Ben, no it doesn', i got the same code "OK" on windows using jslint for sublime, using on www.jslint it works fine, it fails on any validation on Mavericks. If the file is empty, it says OK, but if I add only one line like "var a;" it throws this error.

Juliano LazzarottoYesterday 7:35 PM

Hey Ben after dig in a little bit if I pass passfail = false, i got this:
#1 Line too long.

/*jslint devel: true, node: true, indent: 4 */ // Line 1, Pos 46

#2 Line too long.
'use strict'; // Line 2, Pos 13
#3 Line too long.
function ExceptionsController() { // Line 3, Pos 33
#4 Line too long.

Ben QuarmbyYesterday 7:46 PM

It sounds like the module is setting the JSLint "maxlen" option to zero (or maybe one) instead of undefined. Try explicitly setting it to a higher number (say 100) and see if that fixes it.

Juliano LazzarottoYesterday 7:53 PM

1
Reply

Thumbs up! You got it!
node node_modules/jslint/bin/jslint.js --node --vars --devel --nomen --stupid --indent 4 --passfail=false --maxlen=200 src/.js src/Model/.js

src/ExceptionsController.js is OK.

Ty Ben

Ben QuarmbyYesterday 9:11 PM

You're welcome!

predef option is passed to jslint as string

predef option when passed to jslint should be instance of Array or Object, but currently it is passed as it was read from command line - as string, which makes it unusable as jslint then just ignores its value.

It'll be great to have it parsed into an array before it goes to jslint.

jslint require fails

I installed jslint with npm, getting the following error:

module:242
      throw new Error("Cannot find module '" + request + "'");
            ^
Error: Cannot find module '/usr/local/lib/node/.npm/jslint/0.0.1/package/bin/jslint'
    at loadModule (module:242:13)
    at require (module:373:12)
    at Object.<anonymous> (/Users/agleyzer/2/jslint:13:18)
    at Module._compile (module:422:23)
    at Module._loadScriptSync (module:432:8)
    at Module.loadSync (module:305:10)
    at Object.runMain (module:486:22)
    at node.js:253:10

File /usr/local/lib/node/.npm/jslint/0.0.1/package/bin/jslint exists. I am using Node.js v0.1.101, npm 0.1.20.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.