• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ESLint plugin for Astro component

Introduction

eslint-plugin-astro is ESLint plugin for Astro components.
You can check on the Online DEMO.

sponsors

NPM license NPM version NPM downloads NPM downloads NPM downloads NPM downloads NPM downloads Build Status Coverage Status

This plugin is in the experimental stages of development.

At least it works fine with a withastro/docs repository.

πŸ“› What is this plugin?

ESLint plugin for Astro components.

πŸ“– Documentation

See documents.

πŸ’Ώ Installation

npm install --save-dev eslint eslint-plugin-astro

If you write TypeScript in Astro components, you also need to install the @typescript-eslint/parser:

npm install --save-dev @typescript-eslint/parser

If you want to use the rules for checking accessibility (A11Y), you also need to install eslint-plugin-jsx-a11y additionally:
(It is used internally in the rules for A11Y.)

npm install --save-dev eslint-plugin-jsx-a11y

Requirements

  • ESLint v7.0.0 and above
  • Node.js v14.18.x, v16.x and above

πŸ“– Usage

Configuration

Use .eslintrc.* file to configure rules. See also: https://eslint.org/docs/user-guide/configuring.

Example .eslintrc.js. When using the shareable configuration provided by the plugin:

module.exports = {
  // ...
  extends: [
    // ...
    "plugin:astro/recommended",
  ],
  // ...
  overrides: [
    {
      // Define the configuration for `.astro` file.
      files: ["*.astro"],
      // Allows Astro components to be parsed.
      parser: "astro-eslint-parser",
      // Parse the script in `.astro` as TypeScript by adding the following configuration.
      // It's the setting you need when using TypeScript.
      parserOptions: {
        parser: "@typescript-eslint/parser",
        extraFileExtensions: [".astro"],
      },
      rules: {
        // override/add rules settings here, such as:
        // "astro/no-set-html-directive": "error"
      },
    },
    // ...
  ],
}

If you do not use a shareable configuration, it is the same as the following configuration:

module.exports = {
  // ...
  overrides: [
    {
      // Define the configuration for `.astro` file.
      files: ["*.astro"],
      // Enable this plugin
      plugins: ["astro"],
      env: {
        // Enables global variables available in Astro components.
        node: true,
        "astro/astro": true,
        es2020: true,
      },
      // Allows Astro components to be parsed.
      parser: "astro-eslint-parser",
      // Parse the script in `.astro` as TypeScript by adding the following configuration.
      // It's the setting you need when using TypeScript.
      parserOptions: {
        parser: "@typescript-eslint/parser",
        extraFileExtensions: [".astro"],
        // The script of Astro components uses ESM.
        sourceType: "module",
      },
      rules: {
        // Enable recommended rules
        "astro/no-conflict-set-directives": "error",
        "astro/no-unused-define-vars-in-style": "error",

        // override/add rules settings here, such as:
        // "astro/no-set-html-directive": "error"
      },
    },
    {
      // Define the configuration for `<script>` tag.
      // Script in `<script>` is assigned a virtual file name with the `.js` extension.
      files: ["**/*.astro/*.js", "*.astro/*.js"],
      env: {
        browser: true,
        es2020: true,
      },
      parserOptions: {
        sourceType: "module",
      },
      rules: {
        // override/add rules settings here, such as:
        // "no-unused-vars": "error"

        // If you are using "prettier/prettier" rule,
        // you don't need to format inside <script> as it will be formatted as a `.astro` file.
        "prettier/prettier": "off",
      },
    },
    // ...
  ],
}

The pull request diff here is an example of introducing eslint-plugin-astro to the withastro/docs repository.

This plugin provides configs:

  • plugin:astro/base ... Minimal configuration to enable correct Astro component linting.
  • plugin:astro/recommended ... Above, plus rules to prevent errors or unintended behavior.
  • plugin:astro/all ... Configuration enables all astro rules. It's meant for testing, not for production use because it changes with every minor and major version of the plugin. Use it at your own risk.
  • Extension of sharable configuration provided by eslint-plugin-jsx-a11y. You need to install eslint-plugin-jsx-a11y to use it.

See the rule list to get the rules that this plugin provides.

Parser Configuration

See https://github.com/ota-meshi/astro-eslint-parser#readme.

Running ESLint from the command line

If you want to run eslint from the command line, make sure you include the .astro extension using the --ext option or a glob pattern, because ESLint targets only .js files by default.

Examples:

eslint --ext .js,.astro src
eslint "src/**/*.{js,astro}"

πŸ’» Editor Integrations

Visual Studio Code

Use the dbaeumer.vscode-eslint extension that Microsoft provides officially.

You have to configure the eslint.validate option of the extension to check .astro files, because the extension targets only *.js or *.jsx files by default.

Example .vscode/settings.json:

{
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "astro", // Enable .astro
    "typescript", // Enable .ts
    "typescriptreact" // Enable .tsx
  ]
}

βœ… Rules

The --fix option on the command line automatically fixes problems reported by rules which have a wrench πŸ”§ below.
The rules with the following star ⭐ are included in the plugin:astro/recommended configs.

Doesn't the rule you want exist? Share your idea of that rule with us.

Possible Errors

These rules relate to possible syntax or logic errors in Astro component code:

Rule ID Description
astro/no-conflict-set-directives disallow conflicting set directives and child contents ⭐
astro/no-deprecated-astro-canonicalurl disallow using deprecated Astro.canonicalURL ⭐
astro/no-deprecated-astro-fetchcontent disallow using deprecated Astro.fetchContent() β­πŸ”§
astro/no-deprecated-astro-resolve disallow using deprecated Astro.resolve() ⭐
astro/no-unused-define-vars-in-style disallow unused define:vars={...} in style tag ⭐
astro/valid-compile disallow warnings when compiling. ⭐

Security Vulnerability

These rules relate to security vulnerabilities in Astro component code:

Rule ID Description
astro/no-set-html-directive disallow use of set:html to prevent XSS attack

Best Practices

These rules relate to better ways of doing things to help you avoid problems:

Rule ID Description
astro/no-set-text-directive disallow use of set:text πŸ”§
astro/no-unused-css-selector disallow selectors defined in style tag that don't use in HTML

Stylistic Issues

These rules relate to style guidelines, and are therefore quite subjective:

Rule ID Description
astro/prefer-class-list-directive require class:list directives instead of class with expressions πŸ”§
astro/prefer-object-class-list require use object instead of ternary expression in class:list πŸ”§
astro/prefer-split-class-list require use split array elements in class:list πŸ”§

A11Y Extension Rules

These rules extend the rules provided by eslint-plugin-jsx-a11y to work well in Astro component:
(You need to install eslint-plugin-jsx-a11y to use the rules.)

Rule ID Description
astro/jsx-a11y/alt-text apply jsx-a11y/alt-text rule to Astro components
astro/jsx-a11y/anchor-ambiguous-text apply jsx-a11y/anchor-ambiguous-text rule to Astro components
astro/jsx-a11y/anchor-has-content apply jsx-a11y/anchor-has-content rule to Astro components
astro/jsx-a11y/anchor-is-valid apply jsx-a11y/anchor-is-valid rule to Astro components
astro/jsx-a11y/aria-activedescendant-has-tabindex apply jsx-a11y/aria-activedescendant-has-tabindex rule to Astro components
astro/jsx-a11y/aria-props apply jsx-a11y/aria-props rule to Astro components
astro/jsx-a11y/aria-proptypes apply jsx-a11y/aria-proptypes rule to Astro components
astro/jsx-a11y/aria-role apply jsx-a11y/aria-role rule to Astro components
astro/jsx-a11y/aria-unsupported-elements apply jsx-a11y/aria-unsupported-elements rule to Astro components
astro/jsx-a11y/autocomplete-valid apply jsx-a11y/autocomplete-valid rule to Astro components
astro/jsx-a11y/click-events-have-key-events apply jsx-a11y/click-events-have-key-events rule to Astro components
astro/jsx-a11y/control-has-associated-label apply jsx-a11y/control-has-associated-label rule to Astro components
astro/jsx-a11y/heading-has-content apply jsx-a11y/heading-has-content rule to Astro components
astro/jsx-a11y/html-has-lang apply jsx-a11y/html-has-lang rule to Astro components
astro/jsx-a11y/iframe-has-title apply jsx-a11y/iframe-has-title rule to Astro components
astro/jsx-a11y/img-redundant-alt apply jsx-a11y/img-redundant-alt rule to Astro components
astro/jsx-a11y/interactive-supports-focus apply jsx-a11y/interactive-supports-focus rule to Astro components
astro/jsx-a11y/label-has-associated-control apply jsx-a11y/label-has-associated-control rule to Astro components
astro/jsx-a11y/lang apply jsx-a11y/lang rule to Astro components
astro/jsx-a11y/media-has-caption apply jsx-a11y/media-has-caption rule to Astro components
astro/jsx-a11y/mouse-events-have-key-events apply jsx-a11y/mouse-events-have-key-events rule to Astro components
astro/jsx-a11y/no-access-key apply jsx-a11y/no-access-key rule to Astro components
astro/jsx-a11y/no-aria-hidden-on-focusable apply jsx-a11y/no-aria-hidden-on-focusable rule to Astro components
astro/jsx-a11y/no-autofocus apply jsx-a11y/no-autofocus rule to Astro components
astro/jsx-a11y/no-distracting-elements apply jsx-a11y/no-distracting-elements rule to Astro components
astro/jsx-a11y/no-interactive-element-to-noninteractive-role apply jsx-a11y/no-interactive-element-to-noninteractive-role rule to Astro components
astro/jsx-a11y/no-noninteractive-element-interactions apply jsx-a11y/no-noninteractive-element-interactions rule to Astro components
astro/jsx-a11y/no-noninteractive-element-to-interactive-role apply jsx-a11y/no-noninteractive-element-to-interactive-role rule to Astro components
astro/jsx-a11y/no-noninteractive-tabindex apply jsx-a11y/no-noninteractive-tabindex rule to Astro components
astro/jsx-a11y/no-redundant-roles apply jsx-a11y/no-redundant-roles rule to Astro components
astro/jsx-a11y/no-static-element-interactions apply jsx-a11y/no-static-element-interactions rule to Astro components
astro/jsx-a11y/prefer-tag-over-role apply jsx-a11y/prefer-tag-over-role rule to Astro components
astro/jsx-a11y/role-has-required-aria-props apply jsx-a11y/role-has-required-aria-props rule to Astro components
astro/jsx-a11y/role-supports-aria-props apply jsx-a11y/role-supports-aria-props rule to Astro components
astro/jsx-a11y/scope apply jsx-a11y/scope rule to Astro components
astro/jsx-a11y/tabindex-no-positive apply jsx-a11y/tabindex-no-positive rule to Astro components

Extension Rules

These rules extend the rules provided by ESLint itself to work well in Astro component:

Rule ID Description
astro/semi Require or disallow semicolons instead of ASI πŸ”§

🍻 Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

Development Tools

  • npm test runs tests and measures coverage.
  • npm run update runs in order to update readme and recommended configuration.

Working With Rules

This plugin uses astro-eslint-parser for the parser. Check here to find out about AST.

❀️ Supporting

If you are willing to see that this package continues to be maintained, please consider sponsoring me.

sponsors

πŸ”’ License

See the LICENSE file for license rights and limitations (MIT).

More Repositories

1

eslint-plugin-regexp

ESLint plugin for finding regex mistakes and style guide violations.
TypeScript
223
star
2

eslint-plugin-jsonc

ESLint plugin for JSON(C|5)? files
TypeScript
173
star
3

stylelint-config-recommended-vue

The recommended shareable Vue config for Stylelint.
JavaScript
72
star
4

eslint-plugin-es-x

ESLint plugin about ECMAScript syntactic features.
JavaScript
64
star
5

eslint-plugin-json-schema-validator

ESLint plugin that validates data using JSON Schema Validator.
TypeScript
64
star
6

eslint-plugin-yml

This ESLint plugin provides linting rules for YAML.
TypeScript
64
star
7

jsonc-eslint-parser

JSON, JSONC and JSON5 parser for use with ESLint plugins.
TypeScript
54
star
8

astro-eslint-parser

Astro component parser for ESLint
TypeScript
43
star
9

vuepress-plugin-full-text-search2

VuePress v2 plugin that adds full-text search box.
TypeScript
31
star
10

stylelint-config-html

The shareable HTML config for Stylelint.
JavaScript
29
star
11

stylelint-plugin-stylus

stylelint plugin for Stylus
JavaScript
24
star
12

eslint-plugin-css

An ESLint plugin that provides rules to verify CSS definition objects.
TypeScript
19
star
13

eslint-plugin-lodash-template

ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.
JavaScript
16
star
14

eslint-plugin-toml

This ESLint plugin provides linting rules for TOML.
TypeScript
15
star
15

stylelint-config-standard-vue

The standard shareable Vue config for Stylelint.
JavaScript
15
star
16

postcss-styl

PostCSS parser plugin for converting Stylus syntax to PostCSS AST.
JavaScript
15
star
17

postcss-html

PostCSS syntax for parsing HTML (and HTML-like)
JavaScript
14
star
18

typescript-eslint-parser-for-extra-files

An experimental ESLint custom parser for Vue, Svelte, and Astro for use with TypeScript. It provides type information in combination with each framework's ESLint custom parser.
TypeScript
13
star
19

eslint-plugin-node-dependencies

ESLint plugin to check Node.js dependencies.
TypeScript
12
star
20

vite-plugin-eslint4b

Vite plugin for running ESLint on browser
TypeScript
9
star
21

vite-plugin-svelte-md

Markdown with Svelte for Vite
TypeScript
9
star
22

toml-eslint-parser

A TOML parser that produces output compatible with ESLint
TypeScript
9
star
23

estree-halstead

Halstead complexity measures for ESTree
TypeScript
8
star
24

eslint-plugin-vue-demo

eslint-plugin-vue Online Playground
Vue
5
star
25

eslint-plugin

ESLint configuration plugin for me.
JavaScript
4
star
26

chromeextension_selectbox_filter

JavaScript
4
star
27

eslint-online-playground

ESLint Online Playground
TypeScript
3
star
28

shine-and-bright

Dark and Light Theme Switch CSS
CSS
3
star
29

mrpm

npm package
JavaScript
3
star
30

data-table-performance-test

Grid/Data Table/Spreadsheet Components Performance Test
JavaScript
2
star
31

yaml-eslint-parser

A YAML parser that produces output compatible with ESLint
TypeScript
2
star
32

eslint-plugin-promise-playground

eslint-plugin-promise Online Playground
Svelte
2
star
33

stylelint4b

stylelint which works in browsers
JavaScript
1
star
34

spring-webflux-x-cheetah-grid-example

Vue
1
star
35

vue-fes-japan-online-2022-slide

Vue Fes Japan Online 2022 発葨スラむド
TypeScript
1
star
36

image-matcher

JavaScript
1
star
37

eslint-config-vue-preset

Provides various shareable configs for `eslint-plugin-vue`.
JavaScript
1
star
38

postcss-calc-ast-parser

Parse the `calc()` function defined in CSS and convert it to AST.
TypeScript
1
star
39

stylelint-demo-poc

TypeScript
1
star
40

vue_components_example_materialize

Vue.js v2γ‚’δ½Ώγ£γŸγ‚³γƒ³γƒγƒΌγƒγƒ³γƒˆδ½œζˆγ‚΅γƒ³γƒ—γƒ«οΌˆMaterializeεˆ©η”¨οΌ‰
JavaScript
1
star