npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-eslint-parser

v1.1.0

Published

Astro component parser for ESLint

Downloads

491,234

Readme

astro-eslint-parser

Astro component parser for ESLint.
You can check it on Online DEMO.

sponsors

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

This parser is in the experimental stages of development.

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

:checkered_flag: Motivation

This parser allows us to lint the script of .astro files.

Note that this parser alone will not lint the scripts inside the <script> tag. Use eslint-plugin-astro to lint the script inside the <script> tag as well.

ESLint Plugins Using astro-eslint-parser

eslint-plugin-astro

ESLint plugin for Astro component.

💿 Installation

npm install --save-dev eslint astro-eslint-parser

📖 Usage

First, we recommend using eslint-plugin-astro rather than just the parser.
The following usage it are for introducing only the parser. This is not useful for most people. It can be useful if you create your own plugin.

  1. Write overrides[*].parser option into your .eslintrc.* file.

    {
        "extends": "eslint:recommended",
        "overrides": [
            {
                "files": ["*.astro"],
                "parser": "astro-eslint-parser"
            }
        ]
    }
  2. If you have specified the extension in the CLI, add .astro as well.

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

The commit diff here is an example of introducing this parser to the astro.build repository.

🔧 Options

parserOptions has the same properties as what espree, the default parser of ESLint, is supporting. For example:

{
    "parser": "astro-eslint-parser",
    "parserOptions": {
        "sourceType": "module",
        "ecmaVersion": 2021,
        "ecmaFeatures": {
            "globalReturn": false,
            "impliedStrict": false,
            "jsx": false
        }
    }
}

parserOptions.parser

You can use parserOptions.parser property to specify a custom parser to parse scripts. Other properties than parser would be given to the specified parser. For example:

{
    "parser": "astro-eslint-parser",
    "parserOptions": {
        "parser": "@typescript-eslint/parser"
    }
}

For example, if you are using the "@typescript-eslint/parser", and if you want to use TypeScript in .astro, you need to add more parserOptions configuration.

module.exports = {
  // ...
  parser: "@typescript-eslint/parser",
  parserOptions: {
    // ...
    project: "path/to/your/tsconfig.json",
    extraFileExtensions: [".astro"], // This is a required setting in `@typescript-eslint/parser` v5.
  },
  overrides: [
    {
      files: ["*.astro"],
      parser: "astro-eslint-parser",
      // Parse the script in `.astro` as TypeScript by adding the following configuration.
      parserOptions: {
        parser: "@typescript-eslint/parser",
      },
    },
    // ...
  ],
  // ...
}

When using JavaScript configuration (.eslintrc.js), you can also give the parser object directly.

const tsParser = require("@typescript-eslint/parser")

module.exports = {
    parser: "astro-eslint-parser",
    parserOptions: {
        parser: tsParser,
    },
}

:computer: 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"
    ]
}

:handshake: Compatibility With Existing ESLint Rules

Most of the rules in the ESLint core work for the script part, but some rules are incompatible.
This parser will generate a JSX compatible AST for most of the HTML part of the Astro component. Therefore, some rules of eslint-plugin-react may work. For example, the react/jsx-no-target-blank rule works fine.

:ghost: Limitations

If this parser is used with @typescript-eslint/parser and parserOptions.project is set, it will temporarily create a .tsx file to parse the .astro file.
This parser works by converting the .astro file to JSX and letting the JavaScript parser parse it. Since @typescript-eslint/parser can only parse files with the extension .tsx as JSX, it is necessary to temporarily create a .tsx file. Temporarily created files will try to be deleted after parses, but if the parsing takes a long time, the files may be visible to you.

See also @typescript-eslint/parser readme.

:hammer_and_wrench: Usage for Custom Rules / Plugins

:beers: Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

:heart: Supporting

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

sponsors

:lock: License

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