JSLint Documentation

repository·beta·Indexed 25 days ago

https://github.com/jslint-org/jslint

JSLint is a JavaScript code quality and coverage tool designed to enforce coding standards and identify potential issues in source code. It provides a core jslint() API for analysis, supports V8 coverage reports, and offers integrations for CodeMirror, Vim, and VSCode. The tool can be run via Node.js in the shell or imported as an ES Module or CommonJS module. It supports configuration through an options object or in-code directives such as /*global*/ and /*jslint-disable*/.

Tokens
5.9K
Snippets
12
Records
45
Agent score
37%

What's inside JSLint

  1. Integrate JSLint with Vim

    beta

    To use JSLint in Vim:

    1. Download jslint.mjs and jslint_wrapper_vim.vim to your ~/.vim/ directory.
    2. Add source ~/.vim/jslint_wrapper_vim.vim to your ~/.vimrc.
    3. Use the :SaveAndJslint command or the key combination <Ctrl-S> <Ctrl-J> to lint the current file.
  2. Integrate JSLint with CodeMirror

    beta

    To use JSLint within a CodeMirror editor, follow these steps:

    1. Download jslint.mjs and jslint_wrapper_codemirror.js.
    2. Include jslint.mjs as a module in your HTML with the query parameter ?window_jslint=1.
    3. Include jslint_wrapper_codemirror.js as a deferred script.
    4. Configure the CodeMirror instance with lint: { lintOnChange: true, options: { ... } }.
    5. Use the lintJslintBefore event to set configuration (like globals or node mode).
    6. Use the lintJslintAfter event to access options.result and render the report using window.jslint.jslint_report(options.result).
  3. Integrate JSLint with VSCode

    beta

    To use JSLint in VSCode:

    1. Install the vscode-jslint extension from the VSCode Marketplace.
    2. Right-click in a JavaScript file and select [JSLint - Lint File].
    3. Alternatively, use the keyboard shortcuts:
      • Windows/Linux: Ctrl + Shift + J, then L
      • macOS: Cmd + Shift + J, then L
  4. Create a V8 coverage report via Shell

    beta
    You can generate a V8 coverage report for a Node.js or Npm program directly from the shell using jslint.mjs. Pass the v8_coverage_report option followed by the target directory, then provide any necessary --exclude or --include flags, and finally the command you wish to run (e.g., npm run test).
  5. Analyze JavaScript code quality and coverage with JSLint

    beta
    JSLint is a tool for checking JavaScript code quality and coverage. It parses JavaScript code to identify potential issues, such as undeclared variables, improper use of language features, and stylistic inconsistencies. It can be used via a CLI, imported into ES Module or CommonJS environments, or run as a web demo.
  6. Use JSLint ignore and disable directives

    beta

    You can suppress specific JSLint warnings using directives in your source code:

    • /*jslint-disable*/: Disables JSLint for the following block of code.
    • /*jslint-enable*/: Re-enables JSLint after a /*jslint-disable*/ directive.
    • //jslint-ignore-line: Ignores all warnings for the current line.
    • //jslint-quiet: Similar to ignore-line, used to suppress warnings on a specific line.
  7. Create a V8 coverage report via JavaScript API

    beta

    Use the jslint.v8CoverageReportCreate() method to generate a V8 coverage report programmatically. This method accepts an options object containing:

    • coverageDir: The directory where the report will be saved.
    • processArgv: An array of strings representing the command-line arguments, including JSLint flags (like --exclude or --include) and the command to execute (e.g., ['npm', 'run', 'test']).
    import jslint from "../jslint.mjs";
    
    await jslint.v8CoverageReportCreate({
        coverageDir: "../.artifact/coverage_sqlite3_js/",
        processArgv: [
            "--exclude=test/**/*.js",
            "--include=lib/**/*.js",
            "npm", "run", "test"
        ]
    });
  8. Create a JSLint report in JavaScript

    beta

    To generate an HTML report programmatically, pass the result of jslint.jslint() into jslint.jslint_report(). The returned string can then be wrapped in HTML tags and written to a file.

    /*jslint devel*/
    import jslint from "./jslint.mjs";
    import fs from "fs";
    
    (async function () {
        let result;
        let source = "function foo() {console.log('hello world');}\n";
    
        // Create JSLint report from <source> in javascript.
        result = jslint.jslint(source);
        result = jslint.jslint_report(result);
        result = `<body class="JSLINT_ JSLINT_REPORT_">
    ${result}</body>
    `;
    
        await fs.promises.mkdir(".artifact/", {recursive: true});
        await fs.promises.writeFile(".artifact/jslint_report_hello.html", result);
        console.error("wrote file .artifact/jslint_report_hello.html");
    }());
  9. Use the jslint function

    beta
    The jslint function is the primary entry point for analyzing JavaScript code. It parses a source file and returns an object containing analysis results. You can optionally pass an options object to configure the linting behavior.