What is jshint?
JSHint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules. It helps developers identify potential errors and enforce coding standards.
What are jshint's main functionalities?
Basic Linting
This feature allows you to perform basic linting on a piece of JavaScript code. The code sample demonstrates how to use JSHint to check a simple JavaScript snippet for errors.
const jshint = require('jshint').JSHINT;
const code = 'var a = 1;';
const options = { esversion: 6 };
jshint(code, options);
console.log(jshint.errors);
Custom Configuration
JSHint allows you to customize the linting process with various options. This example shows how to enable the 'undef' option to check for the use of undefined variables.
const jshint = require('jshint').JSHINT;
const code = 'var a = 1;';
const options = { esversion: 6, undef: true };
jshint(code, options);
console.log(jshint.errors);
Using JSHint with Configuration File
You can use a configuration file (e.g., .jshintrc) to define your linting rules. This example demonstrates how to read a JavaScript file and a JSHint configuration file, then lint the code using the specified rules.
const fs = require('fs');
const jshint = require('jshint').JSHINT;
const code = fs.readFileSync('path/to/your/file.js', 'utf8');
const config = JSON.parse(fs.readFileSync('.jshintrc', 'utf8'));
jshint(code, config);
console.log(jshint.errors);
Other packages similar to jshint
eslint
ESLint is another popular JavaScript linting tool that is highly configurable and supports custom rules. It provides more flexibility and a larger ecosystem of plugins compared to JSHint.
jscs
JSCS (JavaScript Code Style) is a code style linter for JavaScript. It focuses more on enforcing coding style conventions rather than finding potential errors. It has been merged with ESLint, but older versions are still in use.
tslint
TSLint is a linter for TypeScript, which is a superset of JavaScript. It provides similar functionality to JSHint but is specifically designed for TypeScript code. TSLint is now deprecated in favor of ESLint with TypeScript support.
JSHint, A Static Code Analysis Tool for JavaScript
JSHint is a community-driven tool to detect errors and potential problems in
JavaScript code and to enforce your team's coding conventions. We made JSHint
very flexible so you can easily adjust it to your particular coding guidelines
and the environment you expect your code to execute in.
Our goal is to help JavaScript developers write complex programs without
worrying about typos and language gotchas.
We believe that static code analysis programs—as well as other code quality
tools—are important and beneficial to the JavaScript community and, thus,
should not alienate their users.
For general usage and hacking information, visit our website:
http://jshint.com/.
Bug Bounties
Some bugs are so important to us, we will pay you if you fix them! Go to
our page on BountySource
to see which bugs have bounties behind them.
Really want to have something fixed but don't have time? You can add your
own bounty to any JSHint bug and make it more attractive for potential
contributors!
Rule: A bug is considered fixed only after it has been merged into the
master branch of the main JSHint repository.
Reporting a bug
To report a bug simply create a
new GitHub Issue and describe
your problem or suggestion. We welcome all kind 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 look around to see if there are any open or closed tickets
that cover your issue. And remember the wisdom: pull request > bug report > tweet.
FAQ
How do I turn off "mixed tabs and spaces" warning?
If you're using so-called smart tabs
then we have an option smarttabs
for you. Otherwise, your solution is to
run JSHint with a custom reporter that discards any warnings you don't like.
For example, this example reporter
discards all warnings about mixed tabs and spaces.
Contributing
Look for a file named CONTRIBUTING.md
in this repository. It contains our
contributing guidelines. We also have
a mailing list.
License
JSHint is distributed under the MIT License. One file and one file only
(src/stable/jshint.js) is distributed under the slightly modified MIT License.
Attribution
Core Team members:
Maintainer: Anton Kovalyov
Thank you!
We really appreciate all kind of feedback and contributions. Thanks for using and supporting JSHint!
2.1.6 (2013-07-29)
UPDATE: We just published another version, 2.1.7, which contains only one
bugfix: #1199.
In this release we added two new arguments to our CLI program: exclude
which
allows you to exclude directories from linting and prereq
which allows you to
specify a file containing declarations of the global variables used throughout
your project. In addition to that, we added support for stdin. JSHint now
follows a UNIX convention where if a given file path is a dash (-
) the the
program reads from stdin.
We also extended our ES6 coverage by adding support for yield
statements and
import/export
declarations. JSHint is still the only linter that can
parse most ES6 and Mozilla-specific JavaScript code.
For more changes, see the patch summary below.
- 004dc61
Fixed #1178: Changed
'predef' to 'globals' in the example .jshintrc
- cd69f13
Fixed #1187: Explicitly
define contents of our NPM package
- c83caf3
Fixed #1166: Tweaks to
import/export support
- 537dcbd
Fixed #1164: Add codes to
errors generated by quit()
- 6aed7ed
Fixed #1155: Use shelljs
option in make.js
- 87df213
Fixed #1153: Moved E037 and
E038 to the warnings section and changed their message.
- dd060c7
Fixed #779: Add support for
!pattern in the .jshintignore files
- 5de09c4
Fixed #696: Add support for
--exclude
arg - ee3d598
Fixed #809: Added short
options to bin/jshint where it made sense
- b937895
Fixed #810: Made --reporter
description in -h more straightforward
- 1c70362
Fixed #839: Add support for
prereq files
- 28dae4b
Fixed #741: expose loadConfig
from CLI
- b39e2ac
Followup #687:
eqnull
- 90b733b
Followup #687: Use '-' as a
marker for stding
- 68db0d8
Fixed #687: Allow input via
stdin
- 5924b2a
Fixed #1157: Add support for
import/export.
- 729cfd7
Fixed #1154: Add MouseEvent
and CustomEvent browser globals
- 9782fc8
Fixed #1134: Catch reserved
words in ES3 mode.
- 87e3e6c
Fixed #1138: Count ternary
and or operators for complexity
- 66f3e4c
Fixed #1133: Make shelljs
imply node.
- 79dc812
Fixed #704: Add config file
support for the Rhino wrappers.
- 88c862d
Fixed #1109: Parse yield
expressions.
Thanks to Terry Roe, Sindre Sorhus, Thomas Boyt, Nikolay S. Frantsev,
XhmikosR, Jacob Rask, Kevin Chu, Tim Ruffles, Stephen Mathieson, Lukas Domnick,
usrbincc for sending patches!
<a name="2.1.5"></a>