LinuxCommandLibrary

eslint

Analyze JavaScript code for style and errors

TLDR

Create the ESLint configuration file

$ eslint --init
copy

Lint one or more files
$ eslint [path/to/file1.js path/to/file2.js ...]
copy

Fix lint issues
$ eslint --fix
copy

Lint using the specified configuration file
$ eslint [[-c|--config]] [path/to/config_file] [path/to/file1.js path/to/file2.js]
copy

SYNOPSIS

eslint [options] [files|directories|globs...]

PARAMETERS

-c, --config
    Specify a configuration file to use instead of the default .eslintrc.* files.

--ext
    Specify additional file extensions (e.g., .ts, .vue) to lint.

--fix
    Automatically fix problems reported by ESLint where possible. Not all issues are auto-fixable.

-f, --format
    Specify the output formatter (e.g., stylish, json, compact).

-i, --ignore-path
    Specify a file to use as an .eslintignore equivalent, defining patterns for files to ignore.

--ignore-pattern
    Specify additional patterns of files or directories to ignore from the command line.

--init
    Run the configuration initializer wizard, which helps set up a new .eslintrc.* file interactively.

--max-warnings
    Set the maximum number of warnings allowed before ESLint exits with a non-zero status code.

--no-eslintrc
    Prevent ESLint from loading any .eslintrc.* configuration files.

--no-inline-config
    Prevent the use of inline comments like /* eslint-disable */ to disable rules within files.

--output-file
    Write the linting results to a specified file instead of standard output.

--parser
    Specify an alternative parser to use (e.g., @typescript-eslint/parser for TypeScript files).

--print-config
    Print the effective configuration that ESLint would use for a given file after resolution.

-r, --rule
    Specify individual rules to apply or override directly from the command line (e.g., 'no-console: off').

--rulesdir
    Load additional rules from a specified directory, useful for custom rule development.

--stdin
    Lint code provided via standard input (e.g., piped from another command).

--stdin-filename
    Specify a filename for the code provided via --stdin, which helps with resolving configuration.

-v, --version
    Output the ESLint version number currently installed.

-h, --help
    Show help information for the command, listing available options and usage.

DESCRIPTION

ESLint is a powerful and highly configurable static code analysis tool designed for identifying problematic patterns in JavaScript and JSX code.

Its primary goal is to enforce coding standards and best practices, leading to more consistent, readable, and error-free codebases. Unlike older linters, ESLint operates by parsing your code into an Abstract Syntax Tree (AST), allowing rules to operate on specific code structures rather than just regular expressions. This architecture makes it incredibly flexible and extensible. Developers can define custom rules, use pre-built rule sets from popular style guides (e.g., Airbnb, Google), and extend its functionality with plugins for various frameworks (like React or Vue) or languages (like TypeScript).

ESLint can detect potential errors, enforce stylistic conventions, and even automatically fix many common issues using its --fix option. It integrates seamlessly into development workflows, often run as a pre-commit hook or part of CI/CD pipelines, ensuring code quality from the earliest stages of development.

CAVEATS

ESLint requires a Node.js environment to execute. Its extensive configuration options, while powerful, can introduce a learning curve, especially for newcomers or for tailoring complex project setups. Performance can be a consideration on very large codebases without proper caching or incremental linting strategies. Conflicts with code formatters like Prettier can arise if not configured carefully to avoid overlapping responsibilities.

CONFIGURATION FILES

ESLint's behavior is primarily controlled by configuration files, typically named .eslintrc.js, .eslintrc.json, .eslintrc.yml, or defined in package.json under the eslintConfig key.

These files specify rules, environments, parsers, plugins, and extendable configurations, allowing for highly customized linting setups for different project needs and directory structures. ESLint resolves configurations hierarchically, inheriting settings from parent directories.

PLUGINS AND EXTENSIBILITY

A core strength of ESLint is its extensibility through plugins. Plugins can provide new rules (e.g., for specific frameworks), custom parsers (e.g., for TypeScript or flow), or environments (e.g., for Node.js globals). This modular design allows ESLint to support linting for a wide array of JavaScript frameworks (like React, Vue, Angular), libraries, or even define rules specific to an organization's codebase. Popular examples include @typescript-eslint/eslint-plugin and eslint-plugin-react.

HISTORY

ESLint was created by Nicholas C. Zakas in 2013. The primary motivation was to provide a more extensible and pluggable linting tool for JavaScript compared to its predecessors like JSHint and JSLint, which were harder to customize. Zakas envisioned a linter where every rule was a plugin, allowing developers to create custom rules and share configurations easily. This AST-based approach, rather than relying on regular expressions, allowed for more precise and powerful static analysis. Over time, ESLint gained widespread adoption in the JavaScript community, becoming the de-facto standard for code quality and style enforcement due to its flexibility, robust feature set, and strong community support.

SEE ALSO

prettier(1), stylelint(1), npm(1), yarn(1), tsc(1)

Copied to clipboard