Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Flat Config #241

Closed
JosefJezek opened this issue Aug 20, 2023 · 8 comments · Fixed by #324
Closed

Support for Flat Config #241

JosefJezek opened this issue Aug 20, 2023 · 8 comments · Fixed by #324
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@JosefJezek
Copy link

Description

@JosefJezek JosefJezek added the enhancement New feature or request label Aug 20, 2023
@ota-meshi
Copy link
Owner

I welcome pull requests

@ota-meshi ota-meshi added the help wanted Extra attention is needed label Aug 22, 2023
@Sergih28
Copy link

I'd be happy to see this issue move forward 😃

I'm a react dev, so I guess I could help if given some guidance, as I don't know how this project works 😬

@darvid
Copy link

darvid commented Dec 28, 2023

I managed to get it working, not sure if this is the best way to do it, though... here's the relevant portion of my .eslint.config.js:

// ... other eslint parsers/plugins ...
import ts from "@typescript-eslint/eslint-plugin";
import astroParser from "astro-eslint-parser";
import astro from "eslint-plugin-astro";

export default [
  // ...
  {
    files: ["**/*.astro"],
    plugins: {
      astro,
    },
    languageOptions: {
      globals: {
        // Enables global variables available in Astro components.
        node: true,
        "astro/astro": true,
        es2020: true,
      },
      parser: astroParser,
      parserOptions: {
        parser: "@typescript-eslint/parser",
        extraFileExtensions: [".astro"],
        // The script of Astro components uses ESM.
        sourceType: "module",
      },
    },
    rules: {
      ...astro.configs.all.rules,
    },
  },
  {
    // Define the configuration for `<script>` tag.
    // Script in `<script>` is assigned a virtual file name with the `.js` extension.
    files: ["**/*.astro/*.js", "*.astro/*.js"],
    languageOptions: {
      globals: {
        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",
    },
  },
  {
    // Define the configuration for `<script>` tag when using `client-side-ts` processor.
    // Script in `<script>` is assigned a virtual file name with the `.js` extension.
    files: ["**/*.astro/*.ts", "*.astro/*.ts"],
    languageOptions: {
      globals: {
        browser: true,
        es2020: true,
      },
    },
    parser: tsParser,
    parserOptions: {
      sourceType: "module",
      project: null,
    },
    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 individual flat configuration are mostly copied wholesale from the user guide, with small tweaks to use the imported plugin and parser(s) - I'm sure the shareable configuration is possible, but I'm going with non-shareable.

@TheElegantCoding
Copy link

TheElegantCoding commented Jan 9, 2024

in order to work with stylistic eslint, for now you have to add this

      rules: {
        // this is necessary to force a correct indentation in astro
        'style/indent': ['error', 2],
        'style/jsx-indent': 'off',
        'style/jsx-one-expression-per-line': 'off',
      }

@darvid in my case still dont work, only files in root directory are linter, the astro files inside src arent linter

@darvid
Copy link

darvid commented Jan 9, 2024

@TheElegantCoding hmm, even with files set to ["**/*.astro"]? I would perhaps run eslint --debug src/ and see if it matches files?

and yeah, I had to do something similar to use stylistic. 👍

@TheElegantCoding
Copy link

my mistake was that i dont put ["**/*.astro"] thanks

@raulfdm
Copy link

raulfdm commented Feb 24, 2024

Although the workaround works in some versions, using the new package from typescript eslint doesn't seem to work properly: https://typescript-eslint.io/packages/typescript-eslint.

It seems there's a mismatch between the TS code being evaluated vs the old typescript of astro eslint plugin while lining.


My workaround for that was to have 2 eslint configs (some duplication indeed), one for my regular .ts/.tsx files running flat config and a legacy eslint config only for astro.

{
  "scripts": {
    "lint:eslint": "ESLINT_USE_FLAT_CONFIG=true eslint src",
    "lint:eslint:astro": "ESLINT_USE_FLAT_CONFIG=false eslint src --config .eslintrc.cjs"
  }
}

@TheElegantCoding
Copy link

Really thank you @ota-meshi, I will see the flat config support when I have time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants