Git Product home page Git Product logo

jshint's Introduction

JSHint, A Static Code Analysis Tool for JavaScript

[ Use it onlineDocsFAQInstallContributeBlogTwitter ]

NPM version Linux Build Status Windows Build status Coverage Status

JSHint is a community-driven tool that detects errors and potential problems in JavaScript code. Since JSHint is so flexible, you can easily adjust it in the environment you expect your code to execute. JSHint is publicly available and will always stay this way.

Our goal

The project aims to help JavaScript developers write complex programs without worrying about typos and language gotchas.

Any code base eventually becomes huge at some point, so simple mistakes — that would not show themselves when written — can become show stoppers and add extra hours of debugging. So, static code analysis tools come into play and help developers spot such problems. JSHint scans a program written in JavaScript and reports about commonly made mistakes and potential bugs. The potential problem could be a syntax error, a bug due to an implicit type conversion, a leaking variable, or something else entirely.

Only 15% of all programs linted on jshint.com pass the JSHint checks. In all other cases, JSHint finds some red flags that could've been bugs or potential problems.

Please note, that while static code analysis tools can spot many different kind of mistakes, it can't detect if your program is correct, fast or has memory leaks. You should always combine tools like JSHint with unit and functional tests as well as with code reviews.

Reporting a bug

To report a bug simply create a new GitHub Issue and describe your problem or suggestion. We welcome all kinds of feedback regarding JSHint including but not limited to:

  • When JSHint doesn't work as expected
  • When JSHint complains about valid JavaScript code that works in all browsers
  • When you simply want a new option or feature

Before reporting a bug, please look around to see if there are any open or closed tickets that discuss your issue, and remember the wisdom: pull request > bug report > tweet.

Who uses JSHint?

Engineers from these companies and projects use JSHint:

And many more!

License

JSHint is licensed under the MIT Expat license.

Prior to version 2.12.0 (release in August 2020), JSHint was partially licensed under the non-free JSON license. The 2020 Relicensing document details the process maintainers followed to change the license.

The JSHint Team

JSHint is currently maintained by Rick Waldron, Caitlin Potter, Mike Pennisi, and Luke Page. You can reach them via [email protected].

Previous Maintainers

Originating from the JSLint project in 2010, JSHint has been maintained by a number of dedicated individuals. In chronological order, they are: Douglas Crockford, Anton Kovalyov, and Mike Sherov. We appreciate their long-term commitment!

Thank you!

We really appreciate all kinds of feedback and contributions. Thanks for using and supporting JSHint!

jshint's People

Contributors

benblank avatar bkw avatar caitp avatar douglascrockford avatar founddrama avatar goatslacker avatar guyzmo avatar jshaptic avatar jugglinmike avatar julienw avatar kevinoid avatar leamingrad avatar leebyron avatar lukeapage avatar mathiasbynens avatar mattfysh avatar mikesherov avatar nicolo-ribaudo avatar nikolayfrantsev avatar nschonni avatar paul-wade avatar rwaldron avatar thejosh avatar thomasboyt avatar tpei avatar usrbincc avatar valueof avatar wolfgangkluge avatar xhmikosr avatar xiemaisi avatar

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  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

jshint's Issues

Error

Getting this firebug error:

error is null
"');}return __;"    jquery.tmpl.js (line 358)

with the following code:

(function($){
 alert('hiya');
})(jQuery);

All options checked except "Tolerate eval" and only "browser" for environment.

Allow variable shadowing (re-declaration)

I use this pattern (a lot):

function test(foo) {
    var foo = foo || false;
}

This triggers an error. I'd like to be able to "shadow" variables, and turn this checking off in an option (JSure allows this).

jshint as an npm package + CLI

Hi all,

Seeing that nodejs support is out of the box, I had a craving to create a cli for jshint and published it as an npm package (called node-hint). It's pretty much like nodelint (https://github.com/tav/nodelint)

https://github.com/brentlintner/node-hint

It still needs a bit of work/refactoring and more tests (hacked it), but it (seemingly) works overall. I was not sure if anyone else was working on the same thing, so I wanted to mention it on here, in case the community finds it useful or even forkable.

Cheers,

PS, the website rocks \m/

Allow Expression in Program

JSHint does not allow many ExpressionStatement as Program code. All literals result in error:

1;
true
"blah";
null;
/a/;

JSHint Result:
Expected an assignment or function call and instead saw an expression.

The expected result should be no error. These are all valid Programs.

A Literal is an PrimaryExpression which is an Expression. A program can have ExpressionStatements.

ExpressionStatement :See 12.4
    [lookahead ∉ {{, function}] Expression ; 

Literal ::
    NullLiteral
    BooleanLiteral
    NumericLiteral
    StringLiteral 

PrimaryExpression : See 11.1
    this
    Identifier
    Literal
    ArrayLiteral
    ObjectLiteral
    ( Expression ) 

Error in ES3 spec omitted RegularExpressionLiteral from list of Literal; that too is valid.

can accept case condition: { } break;

I had to write this because of very long case/break , for using folding functions of my editor (kde4 enviro):

switch (variable)
{
case condition1 :
{
[... 412 lines]
}
break;

case condition2 :
{
    [... 96 lines]
}
break;

}

this lead to :

Line ### : {
Expected to see a statement and instead saw a block.

Parsing Requires Two Steps

The following input is valid and should not result in error. Yet JSHint calls it a fail.

a();
function a(){}

if(!b) {
var b = 1;
}

Result: failed.
'a' is not defined.

Even when unchecked "Require variables to be declared before usage", the result is still an error.

The first is not an error; a is defined and is available to be called. The second is not an error because during lexical mode if b is not a global property, then it is created and given value undefined. Then, when Statements are evaluated, the if(!b) is evalueated as false and b is given value 1.

I think JSHint should not

Assume jQuery (or assume $)

I'd love to see an option to assume jQuery (or another library using $) is available.

Chances are good that code being run through JSHint might use the $ function that has been defined in another file somewhere, and it would be nice to have it as an option to turn off the errors from $ being undefined.

Support CouchDB related globals

Here's a quick list I've compiled:

emit, log, getRow, start, send, require, but there are a couple more. I'll try to dig them up.

Switch statement lint errors and NodeJS

Hi,

Definitely love the project idea. :-)

Some points about jslint I find annoying:

  1. "Expected a 'break' statement before 'default'."

Ex:

case 'help':
default:
    // print help
}

I realized I could remove the 'help' case and always go to default, though I kept it for readability. Ironically, if you do remove it, you get this lint error (another I ask to consider):

  1. "This 'switch' should be an 'if'."

This has been plaguing me since the dawn of time. Specially when I prefer the readability of a switch structure vs if/else (ex. cli parsing)

Cheers,

Memory Leak option

like the title says, I would love to see a Memory Leak detection option to help optimize any/all JS code

hello jshint

phase 0

  • rename branches.
  • basic readme
    • purpose
    • compatible with command-line linting, of course
    • encourage issues to be filed with biggest jslint gripes

phase 1

  • get hosting (something with a nice post-commit hook)
  • redirect domain

find trailing whitespace

there are a number of options that help you with the layout and spacing of your code but it seems that there is no option that will warn of trailing whitespace.

Enable case statement fall through

One of things I've never been able to convince Crockford of is to allow case statement fallthroughs.

Currently, this is okay:

switch(foo){
case 1:
case 2:
doSomething();
}

But this is not:

switch(foo){
case 1:
doSomethingFirst();
case 2:
doSomething();
}

In a version of JSLint that I hacked before, I made a check for a comment /falls through/ to indication that you intend to fall through:

switch(foo){
case 1:
doSomethingFirst();
/falls through/
case 2:
doSomething();
}

I'd really like to see this included in JSHint, as it's been a pain in JSLint for a very long time for me.

demo page can't handle {a:"b"}

paste {a:"b"}, it says "Expected a string and instead saw a." at the same time paste alert({a:"b"}.a); it says "The code check passed."

Don't Restrict NewExpression to Identifier

new FunctionExpresssion ; Results in Error.

new function() {
this.name = "";
};

Result:
Weird construction. Delete 'new'.

This is not an error. The advice to "Delete 'new'" results in significantly changed program behavior. I would like to see JSHint allow this valid production, no warning, no error.

Allow different options based on function scope

The idea is to allow people to specify options unique to the current function scope. For example:

// You cannot use eval here

(function () {
    /*jshint evil: true */
    // You can use eval only inside this function scope.
}());

// You cannot use eval here

Add option to turn off missing semicolon checks

Automatic semicolon insertion is all scary until you actually understand it, then it stops being scary. This tool needs an option to turn off missing semicolon checks for people who decided to go semicolon-less.

I thought that the line breaks option affects this, but apparently it doesn't (at least not in the browser).

x is better written in dot notation

I'm using quotation marks instead of dot notation so Google Closure Compiler knows not to rename the keys when using advanced optimizations. Please let us turn this notice off.

How JSHint is different from JSLint

It has been expressed on Anton Kovalyov's blog by more than one person that the negative views towards Douglas Crockford on jshint.com and on Anton's blog post are not necessary and more importantly not a reflection of the views and opinions of the JavaScript community as a whole.

If this fork really is about listening to the community then can we not change the paragraph regarding the differences between it and jslint to be a little more positive about your new direction/ideas and not so derogatory towards another.

== null is not a crime

In fact, it is a very fast and succinct way of testing whether something is 'undefined or null'. Suggesting that I replace it with === null is bogus.

Operators typeof and delete need to accept Reference

THe operators typeof and delete accept a reference. If, during evaluation, the base object of that reference is null, the result is not a runtime error.

These operators were designed to fail safely without error. However JSHint prevents such acceptable usage in the following.

JSHint Inputs:
typeof f;
delete f;

Results: failed.
'f' is not defined.

The above inputs are perfectly valid and the program can be evaluated and run without error. JSHint should not result in error here.

Re-defining a variable is usually harmless

I have several patterns where, for looks, I'll repeat a variable definition. One is

if (compatiblilityTest)
  var someOperation = function(){"The standard way";};
else
  var someOperation = function(){"The IE way";}

The other is multiple loops in a function all defining their i counter with var. Of course, if the loops are nested, this'll trip you up, but it would also trip you up if you didn't use var on the second loop.

White space / Indentation checking configuration

Suggestion: Expose white space and indentation checking options, to allow customisation of which rules are applied when "apply strict whitespace rules" is chosen. This would allow white space checks to be more useful in places with different coding standards.

Having the white space rule set is very helpful for projects with many developers, to highlight and encourage coding standards. However, under JSLint (like many of its features), the option is a boolean black box where you either apply the JSLint way or turn it off.

For example, the Switch statement is commonly used in two forms, depending on what coding standards a company may have chosen to adopt:

// Java coding standards indent (and JSLint)

switch (foo) {
case "bar":
    break;
case "baz":
    break;
default:
    break;
}

// Alternative (and common) indentation style

switch (foo) {
    case "bar":
        break;
    case "baz":
        break;
    default:
        break;
}

If you are in an environment where the latter standard is in place, the white space check becomes unusable because it will always fail on switch indentation, and therefore the only choice is to disable it completely, even though the rest of the white space checks may be useful and desired by the team.

Other examples of "whitespace" rules I think would have value being exposed as options are the ones dealing with anonymous functions:

function() {} // common
function () {} // crockford

(function (){})() // common
(function (){}()) // crockford

And paren spacing:

if ( something ) { 
if (something) {

Being able to turn "use whitespace checking" on, but have the ability to configure what whitespace checking "means" for your particular use case, dev standards and work environment would be very helpful in JSHint being a usable style checking tool for improved code quality.

Dummy configuration example:

var whiteSpaceSettings = {
    switchIndent: false,
    anonFnSpace: false,
    anonFnWrap: false,
    parenSpace: true
}

Good luck with the project, I think there's a lot of value in a community driven version of JSLint.

Ian

Wildcards in global list (regex or simple *)

I've been experimenting with C-style "namespacing" in a couple of my frameworks, in particular, Jo. For example: joView, joContainer, joControl are all global widget classes. Since they are global, I do not need to specify var = joView, and so I prefer to save a good chunk of unzipped file size (it's a small framework).

I can solve this with the global flag, and that's cool, but I'd like to be able to specify something like:

/*jshint */
/*global jo*:true */

Permit some user-defined global variables/objects

like "HTML5" technos as fileReader, localStorage ,
or any predefined libs like fck, tinymce, jquery, mootools, ..

It will permit to keep an easy way to test something not previously hardcoded in jshint

Parse error on "return x = 12;"

JShint fails to parse

function foo() {
var x;
return x = 12;
}

if breaks on the return statement with the error:

Missing semicolon. Expected an identifier but instead saw '='

Accept escaped EOL into a string declaration

This code is perfectly sane, but not accepted :

var intwolines = 'Here we have a very long string, and for lisibility,
i use a break inside';

Incorrectly return three errors :

  • Bad escapement.
  • Unclosed string.
  • Stopping, unable to continue.

I asked to have it in support in JSLint, Crawford responded me : « This is bad practice ».
I'm not sure having a very long line of 4000 chars is a better way, neither having
var =''
+''
+''
.....

undefined as the name of a formal parameter should NOT be an error

(function(window, undefined) {
    // ...
})(window);

Errors with:

Line 1 (function(window, undefined) {
Expected an identifier and instead saw 'undefined' (a reserved word).

This is a widely used and good pattern, not only does it protect against overridden undefined it also allows for better minification since undefined can be reduced to a single letter name.

THere is no function statement

JSHint error messages make repeated incorrect usage of the term "function statement" in lieu of FunctionDeclaration. While extensions exist in Mozillas' implementation, there is no standard function statement. ECMAScript has FunctionDeclaration and FunctionExpression. The wording of error messages should be changed. For example:

Input:
if(1) {
function a(){}
}

Result: Failed.
Function statements should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.

The error message is misleading or wrong. There is no "outer function" in the example and ECMAScript has no "function statement." Instead, the error message should read:

Function declarations are not Statements; they may never appear in blocks. See also: FAQ: What is a function statement? and Named Function Expressions.

[feedback needed] Behavior with globals

Right now, JSHint prohibits re-definition of globals:

/*global JSHINT: false */
JSHINT = {}; // error: read only

If you try to add var to it then the error is about variable being redefined. Should we remove the read only error or make it that there is no redefined error when variable is in /*global */?

Functions within closure do not need to be declared before use -- not an error

Consider this contrived snippet from a method in an object literal:

draw: function(flag) {
    var flag = flag || false;

    inner();

    function inner() {
        console.log("Ellipse.draw()");
    }
}

This throws a jshint error, namely, using inner before its declaration. Technically, there is no error in JavaScript in this case, and it is very often convenient to put all of the "flow" for the parent function up top, with the inner functions declared blow (instead of the other way round). This is a feature, not a bug. :) Would remove this error case entirely since it's not an error, or make it an option you can turn on.

Create Maven Plugin

One other area that JSLint still lacks is better integration with build systems like Maven. The common current solution is to wrap the Rhino call as a giant XML blob. A more elegant solution would be to load via a native Maven plugin.

I may look into this soon as it is a current need of mine, but if anyone else feels ambitious this would be a huge win for JSHint build-time integration.

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.