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

Adding Scripts Affects TypeScript Intellisense on .astro file #800

Open
1 task
iamhectorsosa opened this issue Feb 18, 2024 · 8 comments
Open
1 task

Adding Scripts Affects TypeScript Intellisense on .astro file #800

iamhectorsosa opened this issue Feb 18, 2024 · 8 comments
Labels
- P2: has workaround Bug, but has workaround (priority) ecosystem: compiler Issue is caused by a bug in the Astro compiler

Comments

@iamhectorsosa
Copy link

Astro Info

Astro                    v4.4.0
Node                     v20.11.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/tailwind
                         @astrojs/react

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Adding Scripts Affects TypeScript Intellisense on .astro file. The snippet below has been included in Layout.astro or index.astro files. In both situations with or without directives I've noticed that the TypeScript intellisense is completely disabled. Missing imports are not detected, no intellisense on any props for the imported components, etc.

<script is:inline>
  const getThemePreference = () => {
    if (typeof localStorage !== "undefined" && localStorage.getItem("theme")) {
      return localStorage.getItem("theme");
    }
    return window.matchMedia("(prefers-color-scheme: dark)").matches
      ? "dark"
      : "light";
  };
  const isDark = getThemePreference() === "dark";
  document.documentElement.classList[isDark ? "add" : "remove"]("dark");

  if (typeof localStorage !== "undefined") {
    const observer = new MutationObserver(() => {
      const isDark = document.documentElement.classList.contains("dark");
      localStorage.setItem("theme", isDark ? "dark" : "light");
    });
    observer.observe(document.documentElement, {
      attributes: true,
      attributeFilter: ["class"],
    });
  }
</script>

What's the expected result?

TypeScript is not affected by the inclusion of script tags in .astro files

Link to Minimal Reproducible Example

I don't have a GitHub repo at the time with this.

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Feb 18, 2024
@Princesseuh
Copy link
Member

Please share the file, we cannot help without a reproduction. Doesn't need to be the full project.

@Princesseuh Princesseuh added needs repro Issue needs a reproduction and removed needs triage Issue needs to be triaged labels Feb 18, 2024
Copy link
Contributor

Hello @iamhectorsosa. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

@iamhectorsosa
Copy link
Author

@Princesseuh Princesseuh removed the needs repro Issue needs a reproduction label Feb 18, 2024
@iamhectorsosa
Copy link
Author

iamhectorsosa commented Feb 18, 2024

You know what's interesting, if I go ahead and follow the instructions of import local scripts, then this issue goes away.

<script is:inline src="./theme.js"></script>

Still I am not sure what might be causing this. It's such a strange behavior. I wish I could help more.

@Princesseuh
Copy link
Member

Yes, that's perfect, thank you!

@Princesseuh Princesseuh transferred this issue from withastro/astro Feb 19, 2024
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Feb 19, 2024
@Princesseuh Princesseuh added - P2: has workaround Bug, but has workaround (priority) and removed needs triage Issue needs to be triaged labels Feb 19, 2024
@Princesseuh
Copy link
Member

I'm not quite sure what the exact cause is, but you can workaround the issue by putting the script tag below the content.

@iamhectorsosa
Copy link
Author

Thanks for looking into it. Yeah, also importing works for this. Still a strange issue.

@Princesseuh Princesseuh added the ecosystem: compiler Issue is caused by a bug in the Astro compiler label Mar 22, 2024
@florian-lefebvre
Copy link
Member

I just experienced a similar case:

<script is:inline>
	// interepreted as js, no issue
	let a
</script>
---
const data = {
	foo: "bar"
}
---
<script is:inline>
	// interepreted as ts, complains
	let a
</script>
---
const data = {
	foo: "bar"
}
---
<div></div>
<script is:inline>
	// interepreted as js, no issue
	let a
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: has workaround Bug, but has workaround (priority) ecosystem: compiler Issue is caused by a bug in the Astro compiler
Projects
None yet
Development

No branches or pull requests

3 participants