ESLint HTML Parser

6.8.2 · active · verified Sun Apr 19

eslint-html-parser is a custom ESLint parser designed to lint HTML files, including embedded JavaScript within `<script>` tags. It achieves this by parsing HTML into a specific Abstract Syntax Tree (AST) structure, while delegating the parsing of JavaScript code (both in `<script>` tags and standalone `.js`/`.jsx` files) to a configurable ECMAScript parser, defaulting to `espree`. This enables developers to apply standard ESLint rules to their JavaScript within HTML, as well as define custom rules that target the HTML structure itself. The package is currently at version 6.8.2 and appears to be actively maintained, offering a crucial bridge for projects that require robust linting across both HTML and JavaScript within a single ESLint setup. It differentiates itself by providing a comprehensive HTML AST for deep linting, unlike simpler preprocessors that only extract script content.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates installation, basic ESLint configuration using eslint-html-parser for both HTML and standalone JavaScript files, and running the linter.

{
  "name": "my-linted-project",
  "version": "1.0.0",
  "description": "Example project using eslint-html-parser",
  "scripts": {
    "lint": "eslint \"./**/*.{js,htm,html}\"
  },
  "devDependencies": {
    "eslint": "^8.0.0",
    "eslint-html-parser": "^6.0.0"
  }
}

// .eslintrc.json
{
  "root": true,
  "parser": "eslint-html-parser",
  "parserOptions": {
    "ecmaVersion": 2021,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": "eslint:recommended",
  "rules": {
    "no-console": "warn"
  }
}

<!-- src/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h1>Hello, HTML Lint!</h1>
  <script>
    console.log("This JS will be linted."); // no-console will warn here
    var unusedVar = 1;
  </script>
  <my-custom-element some-attribute="value"></my-custom-element>
</body>
</html>

// src/app.js
console.log("This standalone JS will also be linted.");

# Terminal commands to set up and run:
npm init -y
npm install --save-dev eslint eslint-html-parser
# Create .eslintrc.json, src/index.html, src/app.js as shown above
npm run lint

view raw JSON →