Back to Qwik

eslint-plugin-qwik

packages/eslint-plugin-qwik/README.md

1.7.15.5 KB
Original Source

eslint-plugin-qwik

Qwik comes with its own set of ESLint rules to help developers write better code.

Usage

Install the plugin:

bash
npm add -D eslint-plugin-qwik
pnpm add -D eslint-plugin-qwik
yarn add -D eslint-plugin-qwik

eslint-plugin-qwik uses the tsc typechecker to type information. You must include the tsconfigRootDir in the parserOptions.

Configurations

js
// eslint.config.js
import js from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import { globalIgnores } from 'eslint/config';
import { qwikEslint9Plugin } from 'eslint-plugin-qwik';

export const qwikConfig = tseslint.config(
  globalIgnores(['node_modules/*', 'dist/*', 'server/*', 'tmp/*']),
  js.configs.recommended,
  tseslint.configs.recommended,
  qwikEslint9Plugin.configs.recommended,
  {
    files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
    languageOptions: {
      globals: {
        ...globals.serviceworker,
        ...globals.browser,
        ...globals.node,
      },
      parserOptions: {
        projectService: true,
        tsconfigRootDir: import.meta.dirname,
      },
    },
  }
);

Legacy config (eslint < 9)

js
// .eslintrc.js
module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:qwik/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.json'],
    ecmaVersion: 2021,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  plugins: ['@typescript-eslint'],
};

To ignore files, you must use the .eslintignore file.

List of supported rules

  • Warn in 'recommended' ruleset — ✔️
  • Error in 'recommended' ruleset — ✅
  • Warn in 'strict' ruleset — 🔒
  • Error in 'strict' ruleset — 🔐
  • Typecheck — 💭
RuleDescriptionRuleset
qwik/valid-lexical-scopeUsed the tsc typechecker to detect the capture of unserializable data in dollar ($) scopes.✅ 🔐 💭
qwik/use-method-usageDetect invalid use of use hook.✅ 🔐
qwik/no-react-propsDisallow usage of React-specific className/htmlFor props.✅ 🔐
qwik/loader-locationDetect declaration location of loader$.✔️ 🔐
qwik/prefer-classlistEnforce using the classlist prop over importing a classnames helper. The classlist prop accepts an object { [class: string]: boolean } just like classnames.✔️ 🔐
qwik/jsx-no-script-urlDisallow javascript: URLs.✔️ 🔐
qwik/jsx-keyDisallow missing key props in iterators/collection literals.✔️ 🔐
qwik/unused-serverDetect unused server$() functions.✅ 🔐
qwik/jsx-imgFor performance reasons, always provide width and height attributes for `` elements, it will help to prevent layout shifts.✔️ 🔐
qwik/jsx-aFor a perfect SEO score, always provide href attribute for <a> elements.✔️ 🔐
qwik/no-use-visible-taskDetect useVisibleTask$() functions.✔️ 🔒