Lit Analyzer

2.0.3 · active · verified Wed Apr 22

Lit Analyzer is a command-line interface (CLI) tool designed to provide static analysis and type checking for Lit templates within JavaScript and TypeScript projects. It enhances the developer experience by catching common errors related to Lit element properties, events, and slots before runtime. The current stable version is 2.0.3, with regular updates to support new Lit features and address community feedback. Key differentiators include its deep integration with TypeScript for template-bound expressions, offering autocompletion suggestions, and enforcing best practices through configurable rules, such as ensuring proper property visibility (`@property`, `@internalProperty`) and correct custom element registration in `HTMLElementTagNameMap`. Its development is active, with significant improvements introduced in the transition to v2.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates how to install and run `lit-analyzer` on a basic Lit component to catch common template-related type errors and best practice violations.

{
  "name": "lit-analyzer-example",
  "version": "1.0.0",
  "scripts": {
    "analyze": "lit-analyzer --strict"
  },
  "devDependencies": {
    "lit": "^2.0.0",
    "lit-analyzer": "^2.0.0",
    "typescript": "^4.0.0"
  }
}
// my-element.ts
import { LitElement, html, property } from 'lit';
import { customElement } from 'lit/decorators.js';

interface MyData {
  value: string;
}

@customElement('my-element')
export class MyElement extends LitElement {
  @property({ type: String })
  name: string = 'World';

  @property({ type: Number })
  count: number = 0;

  @property({ attribute: false })
  data?: MyData;

  render() {
    return html`
      <h1>Hello, ${this.name}!</h1>
      <p>Count: ${this.count}</p>
      ${this.data ? html`<p>Data value: ${this.data.value}</p>` : ''}
      <button @click="${this._increment}">Increment</button>
      <!-- Lit Analyzer will warn about invalid event name 'my-event' -->
      <button @my-event="${() => console.log('This will error')}">Custom Event</button>
      <!-- Lit Analyzer will warn about undefined property 'undefinedProp' -->
      <p>${(this as any).undefinedProp}</p> <!-- Cast to any to prevent TS error in editor -->
    `;
  }

  private _increment() {
    this.count++;
  }
}

declare global {
  interface HTMLElementTagNameMap {
    'my-element': MyElement;
  }
}

// To run:
// 1. npm install
// 2. npm run analyze

view raw JSON →