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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 BUG: Linting seems wrong for vanilla JS in astro components #94

Closed
robinmetral opened this issue Oct 20, 2021 · 2 comments
Closed

Comments

@robinmetral
Copy link

What package manager are you using?

npm and yarn

What operating system are you using?

Mac

Describe the Bug

Linting seems off when using vanilla JS in astro components.

Here's an example page that fetches a dog image and renders it using vanilla JS:

dog.astro

<Layout> <!-- let's say this contains the html head and body elements -->
  <div id="dog"></div>
  <script type="text/javascript">
    /* let's not worry about browser support and just use modern JS */
    async function getDog() {
      try {
        const res = await fetch("https://dog.ceo/api/breeds/image/random");
        const data = await res.json();
        return data.message;
      } catch (error) {
        console.log(error);
        return null;
      }
    }
    async function renderDog() {
      const imgSrc = await getDog();
      if (imgSrc) {
        const parentElement = document.getElementById("dog");
        const imgElement = document.createElement("img");
        imgElement.src = imgSrc;
        parentElement.appendChild(imgElement);
      }
    }
    renderDog();
  </script>
</Layout>

This JS is valid, but here would be the output of astro check (same as the VSCode linting errors):

src/pages/index.astro:9:6 - Expression expected.
6        try {
         ~~~

src/pages/index.astro:10:8 - Expression expected.
7           const res = await fetch("https://dog.ceo/api/breeds/image/random");
            ~~~~~

src/pages/index.astro:13:6 - Unexpected token. Did you mean `{'}'}` or `&rbrace;`?
10        } catch (error) {
          ~

src/pages/index.astro:16:6 - Unexpected token. Did you mean `{'}'}` or `&rbrace;`?
13        }
          ~

src/pages/index.astro:17:4 - Unexpected token. Did you mean `{'}'}` or `&rbrace;`?
14      }
        ~

src/pages/index.astro:19:6 - Expression expected.
16        const imgSrc = await getDog();
          ~~~~~

src/pages/index.astro:21:8 - Expression expected.
18          const parentElement = document.getElementById("dog");
            ~~~~~

src/pages/index.astro:25:6 - Unexpected token. Did you mean `{'}'}` or `&rbrace;`?
22        }
          ~

src/pages/index.astro:26:4 - Unexpected token. Did you mean `{'}'}` or `&rbrace;`?
23      }

Steps to Reproduce

  1. Go to the CodeSandbox reproduction
  2. Open a second terminal (you might need to fork it if you don't have permissions)
  3. Run yarn run check

Screenshot of the CodeSandbox showing astro vanilla JS linting issues

Link to Minimal Reproducible Example (Optional)

https://codesandbox.io/s/astro-js-linting-errors-1rttp

@matthewp
Copy link
Collaborator

Thank you! Going to move this to the language tools repo, where this code is. Thanks for the great example!

@matthewp matthewp transferred this issue from withastro/astro Oct 20, 2021
@robinmetral
Copy link
Author

-   <script type="text/javascript">
+   <script type="module">

Still not used to writing these without a build step 馃槄

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