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

Incorrect parsing of Astro component script tags #2017

Closed
minht11 opened this issue Jan 13, 2024 · 8 comments
Closed

Incorrect parsing of Astro component script tags #2017

minht11 opened this issue Jan 13, 2024 · 8 comments
Labels
A-linter Area - Linter C-bug Category - Bug good first issue Experience Level - Good for newcomers

Comments

@minht11
Copy link

minht11 commented Jan 13, 2024

Have astro component like this

---
const { props } = Astro

const attrs = {}
---

<script is:inline {...attrs} set:html={props.src ? '' : props.code} />

<script>
	interface Attrs {
		'data-attrs'?: string
	}
</script>

running OXC 0.2 produces

 x Expected a semicolon or an implicit semicolon after a statement, but found none
         site:lint |    ,-[src/components/Script.astro:3:1]
         site:lint |  3 | <script>
         site:lint |  4 |     interface Attrs {
         site:lint |    :              ^
         site:lint |  5 |         'data-attrs'?: string
         site:lint |    `----
         site:lint |   help: Try insert a semicolon here

My guess is that oxc does not recognize script as typescript

@minht11
Copy link
Author

minht11 commented Jan 13, 2024

Commenting out code

---
const { props } = Astro

const attrs = {}
---

<script is:inline {...attrs} set:html={props.src ? '' : props.code} />

<!-- <script>
	interface Attrs {
		'data-attrs'?: string
	}
</script> -->

Produces

x Unexpected token
         site:lint |    ,-[src/components/Script.astro:2:1]
         site:lint |  2 | 
         site:lint |  3 | <!-- <script>
         site:lint |    :  ^
         site:lint |  4 |     interface Attrs {
         site:lint |    `----

@Boshen Boshen added C-bug Category - Bug A-linter Area - Linter labels Jan 13, 2024
@Boshen
Copy link
Member

Boshen commented Jan 15, 2024

This is harder than I thought 😅

@IWANABETHATGUY
Copy link
Collaborator

Maybe we can give a tree-sitter-vue try?

image
https://ikatyang.github.io/tree-sitter-vue/

@IWANABETHATGUY
Copy link
Collaborator

Since it has error recovery, we don't need to struggle with the wired edge case, the only drawback is it may increase binary size.

@Boshen
Copy link
Member

Boshen commented Jan 15, 2024

Since it has error recovery, we don't need to struggle with the wired edge case, the only drawback is it may increase binary size.

Binary size + performance is not something I'd trade off for, I'll take a look at their compiler test suite.

@IWANABETHATGUY
Copy link
Collaborator

Since it has error recovery, we don't need to struggle with the wired edge case, the only drawback is it may increase binary size.

Binary size + performance is not something I'd trade off for, I'll take a look at their compiler test suite.

They don't have too many test suites, https://github.com/ikatyang/tree-sitter-vue/blob/master/corpus/spec.txt, the most attractive feature of tree-sitter is the error recovery, they can always find the valid script tag, what every the source code is, this could solve our problem perfectly.

@Boshen Boshen added the good first issue Experience Level - Good for newcomers label Mar 31, 2024
@Boshen
Copy link
Member

Boshen commented Mar 31, 2024

we need to improve the parser to balance out the angle brackets.

The parsing code is here https://github.com/oxc-project/oxc/tree/main/crates/oxc_linter/src/partial_loader

PRs are welcome.

Boshen pushed a commit that referenced this issue Apr 7, 2024
…2017) (#2907)

Added tests to figure out what the problem from the issue was and
realize the self closing tag was not expected. Added code to handle this
case.

I am not that familiar with astro to know how common self closing script
tags are, their docs seem to use an opening and closing one for inline
scripts
https://docs.astro.build/en/guides/client-side-scripts/#load-external-scripts
@kalvenschraut
Copy link
Contributor

@Boshen this issue can be closed, not sure why it didn't auto close on my PR being merged

eryue0220 added a commit to eryue0220/oxc that referenced this issue Apr 7, 2024
…20/oxc into feat/jest-prefer-lowercase-title

* 'feat/jest-prefer-lowercase-title' of github.com:eryue0220/oxc:
  feat(linter/import): Add `ignoreTypes` option for the `import/no-cycle` rule (oxc-project#2905)
  fix(linter): handle self closing script tags in astro partial loader (oxc-project#2017) (oxc-project#2907)
  fix(linter): svelte partial loader handle generics (oxc-project#2875) (oxc-project#2906)
  fix(tasks/rulegen): read quasi in TaggedTemplateExpression (oxc-project#2903)
@Boshen Boshen closed this as completed Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - Linter C-bug Category - Bug good first issue Experience Level - Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants