sveltejs / svelte-eslint-parser

Svelte parser for ESLint
https://sveltejs.github.io/svelte-eslint-parser/
MIT License
85 stars 17 forks source link

feat: makes it optional whether to parse runes. #536

Closed ota-meshi closed 2 weeks ago

ota-meshi commented 2 weeks ago

related to #423

This PR changes the parser to allow opt-in/out of Rune parsing. (However, it requires Svelte v5.)

The main changes are:

changeset-bot[bot] commented 2 weeks ago

πŸ¦‹ Changeset detected

Latest commit: baf724add98bd1672d44d88eb4c7c500bf0f285a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | -------------------- | ----- | | svelte-eslint-parser | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

coveralls commented 2 weeks ago

Pull Request Test Coverage Report for Build 9534250043

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser/globals.ts 7 8 87.5%
src/svelte-config/parser.ts 78 80 97.5%
<!-- Total: 160 163 98.16% -->
Totals Coverage Status
Change from base Build 9529150497: 0.1%
Covered Lines: 2949
Relevant Lines: 3131

πŸ’› - Coveralls
coveralls commented 2 weeks ago

Pull Request Test Coverage Report for Build 9534267800

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser/globals.ts 7 8 87.5%
src/svelte-config/parser.ts 78 80 97.5%
<!-- Total: 160 163 98.16% -->
Totals Coverage Status
Change from base Build 9529150497: 0.1%
Covered Lines: 2949
Relevant Lines: 3131

πŸ’› - Coveralls
coveralls commented 2 weeks ago

Pull Request Test Coverage Report for Build 9534824834

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser/globals.ts 7 8 87.5%
src/svelte-config/parser.ts 78 80 97.5%
<!-- Total: 160 163 98.16% -->
Totals Coverage Status
Change from base Build 9529150497: 0.1%
Covered Lines: 2949
Relevant Lines: 3131

πŸ’› - Coveralls
coveralls commented 2 weeks ago

Pull Request Test Coverage Report for Build 9540091489

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser/globals.ts 7 8 87.5%
src/scope/index.ts 5 7 71.43%
src/svelte-config/parser.ts 116 132 87.88%
<!-- Total: 196 215 91.16% -->
Totals Coverage Status
Change from base Build 9529150497: -0.5%
Covered Lines: 2985
Relevant Lines: 3183

πŸ’› - Coveralls
ota-meshi commented 2 weeks ago

I realized that it would be better to have svelte.config.js specified in eslint.config.js rather than statically analyzing svelte.config.js. I will make that change.

Done

coveralls commented 2 weeks ago

Pull Request Test Coverage Report for Build 9541551900

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser/globals.ts 7 8 87.5%
src/scope/index.ts 5 7 71.43%
src/svelte-config/parser.ts 125 151 82.78%
<!-- Total: 207 236 87.71% -->
Totals Coverage Status
Change from base Build 9529150497: -1.0%
Covered Lines: 2996
Relevant Lines: 3204

πŸ’› - Coveralls
coveralls commented 2 weeks ago

Pull Request Test Coverage Report for Build 9541685261

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser/globals.ts 7 8 87.5%
src/scope/index.ts 5 7 71.43%
src/svelte-config/parser.ts 125 151 82.78%
<!-- Total: 207 236 87.71% -->
Totals Coverage Status
Change from base Build 9529150497: -1.0%
Covered Lines: 2996
Relevant Lines: 3204

πŸ’› - Coveralls
baseballyama commented 2 weeks ago

I have a question.

Both runes mode and legacy mode can use store right? Can you tell me more details why this PR can solve below?

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE2VR227DIAz9FQtNaqJGvbymSaR9x9hDSsjElhgEptuE-PcJ0iaV9mRzjo3tcwIb1SQdq98Cw36WrGavxrCK0a9JD3eTE0lWMae9FQlpnLDKUMeRk5qNtgQBTP8hIcJo9Qy7w9GRtnJ3SSWTJBDaI0ELL456ksWpTAxyGj0KUhpBobBylkhFCSExnNKPB2-G1FBgCW33YDhZSd7i9uY09NTXgIcUYQ_nBxMvSxbLe7Kssm_hnIHIsTluB2EzqFuXa2oIOcbmmLA7lbaqIbzk7dKwjb56Io2gUUxKfLVhvSlmqTLq0rilsGMVm_WgRiUHVpP1MlarBVm_zYRP92zAKvq3VdRfp034xawn9VH-5Fqh0dFiUru2FQEW2U6Q5fm_0Hv8A7R7ehQfAgAA

baseballyama commented 2 weeks ago

Added a new option parserOptions.svelteConfig to specify svelte.config.js.

This is just my opinion but, in 99% of cases, compilerOptions.runs is defined statically in svelte.config.js. So I think it's enough to parse svelte.config.js statically by default. And we will use svelteFeatures.runes only if the value is explicitly specified in eslint config.

ota-meshi commented 2 weeks ago

Hmm... I guess I misunderstood something. This PR doesn't solve https://github.com/sveltejs/eslint-plugin-svelte/issues/652. I thought that telling the compiler the runes mode correctly would solve the problem, but it didn't :sweat:

ota-meshi commented 2 weeks ago

This is just my opinion but, in 99% of cases, compilerOptions.runs is defined statically in svelte.config.js. So I think it's enough to parse svelte.config.js statically by default. And we will use svelteFeatures.runes only if the value is explicitly specified in eslint config.

You are right about the compilerOptions.runs. However, we think that in the future we might want to access more than just compilerOptions.runs, so I thought it made sense to add this option now.

For example, the following issue in eslint-plugin-svelte can be easily resolved if we have access to onwarn.

https://github.com/sveltejs/eslint-plugin-svelte/issues/311

For example, we can get the correct routes information even without the following settings:

https://sveltejs.github.io/eslint-plugin-svelte/user-guide/#settings-svelte-kit

Currently, the parser does not use any information other than compilerOptions.runs. However, I think that in the future it may. Also we can also use it from parserServices to share svelte.config.js information with eslint-plugin rules.

baseballyama commented 2 weeks ago

However, we think that in the future we might want to access more than just compilerOptions.runs, so I thought it made sense to add this option now.

Agreed!

How about mimic the logic with vite-plugin-svelte? https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md#config-file

As default, user don't need to configure parserOptions.svelteConfig but automatically read the file. If user use special file name, then user can configure parserOptions.svelteConfig or configure svelteFeatures .xxx as inline option.

It would be convenient for users if fewer settings could be made. And svelte users may easier to understand if there are similar rules across the Svelte ecosystem.

ota-meshi commented 2 weeks ago

As default, user don't need to configure parserOptions.svelteConfig but automatically read the file.

Unfortunately, currently the eslint custom parser cannot load ESMs because it needs to run synchronously, so svelte.config.js cannot be loaded inside the parser.

See https://github.com/eslint/eslint/issues/15475

baseballyama commented 2 weeks ago

Ah I forgot thisπŸ˜….

ota-meshi commented 1 week ago

@baseballyama With this change, we might still be missing some features πŸ˜“

I have a question. Will the Svelte core compiler automatically parse runes as runes when they are used, even without compilerOptions.runes? If so, this parser needs to be handled the same way.

baseballyama commented 1 week ago

Ahh, yeah, you are right. If there is rune symbol in .svelte / .svelte.js file, compiler will automatically enable rune mode for the component. If compilerOptions.runes is true, apply rune mode compulsorily. (So we can not use export let xxx anymore.) If compilerOptions.runes is false, the compiler doesn't use runes. (So if there is $derived or some runes, $derived will be behave as normal variable.)

baseballyama commented 1 week ago

I'm fixing.