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

Type declaration in script causes eslint parsing error #298

Closed
2 tasks done
davidhouweling opened this issue Dec 26, 2023 · 2 comments
Closed
2 tasks done

Type declaration in script causes eslint parsing error #298

davidhouweling opened this issue Dec 26, 2023 · 2 comments

Comments

@davidhouweling
Copy link

davidhouweling commented Dec 26, 2023

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.

What version of ESLint are you using?

8.56.0

What version of eslint-plugin-astro are you using?

0.31.0

What did you do?

Configuration
module.exports = {
  env: {
    node: true,
    es2022: true,
    browser: true,
  },
  overrides: [
    {
      files: ["*.astro"],
      extends: ["eslint:recommended", "plugin:astro/recommended", "prettier"],
      parser: "astro-eslint-parser",
      parserOptions: {
        parser: "@typescript-eslint/parser",
        extraFileExtensions: [".astro"],
        ecmaVersion: "latest",
        sourceType: "module",
      },
      processor: "astro/client-side-ts",
    },
    {
      files: ["*.d.ts"],
      rules: {
        "@typescript-eslint/triple-slash-reference": "off",
      },
    },
    {
      // Define the configuration for `<script>` tag.
      // Script in `<script>` is assigned a virtual file name with the `.js` extension.
      files: ["**/*.astro/*.js", "*.astro/*.js"],
      parser: "@typescript-eslint/parser",
      extends: ["eslint:recommended", "prettier"],
    },
  ],
};
---

---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content={Astro.generator} />
    <title>Astro</title>
  </head>
  <body>
    <h1>Astro</h1>
    <div id="example">Example div</div>
  </body>
</html>

<script>
  // error example 1: function declaration with typing fails
  const exampleTsFn = (parameter1: boolean) => {
    console.log(`parameter1: ${parameter1}`);
  };

  // error example 2: type declaration fails
  type ExampleElement = HTMLDivElement;

  const example = document.querySelector<ExampleElement>("#example");
  console.log(example);
</script>

What did you expect to happen?

eslint CLI (eslint . --ext .js,.ts,.astro) is able to parse the astro file correctly (similar to how VSCode is able to).

What actually happened?

eslint CLI fails with Parsing error: Unexpected token :

Link to Minimal Reproducible Example

https://github.com/davidhouweling/eslint-astro-error

Additional comments

No response

@ota-meshi
Copy link
Owner

I think that you need to add the following configuration to the overrides section:

    {
      // Define the configuration for `<script>` tag when using `client-side-ts` processor.
      files: ["**/*.astro/*.ts", "*.astro/*.ts"],
      parser: "@typescript-eslint/parser",
    }

Or use "plugin:astro/recommended".

@davidhouweling
Copy link
Author

thanks, that did it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants