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

Support "Type aware" linting for gts files #1995

Closed
vstefanovic97 opened this issue Nov 12, 2023 · 13 comments · Fixed by #1996
Closed

Support "Type aware" linting for gts files #1995

vstefanovic97 opened this issue Nov 12, 2023 · 13 comments · Fixed by #1996

Comments

@vstefanovic97
Copy link

vstefanovic97 commented Nov 12, 2023

I'm not really a pro at Eslint nor Typescript Compiler, but I did notice this strange behaviour
To me it seems that "type aware" linting isn't really fully working.

For example if we import something from a gts file, whatever we imported, the @typescript-eslint/parser won't be aware of it's types

Here is a link to a fork where I setup a test with reproduction:
vstefanovic97@70235ac

This is my theory on why this is happening (though I might be wrong):

Right now we rely on @typescript-eslint/parser to do everything and we just forward options to it, but if we just rely on it, it will generate a Typescript Program w/o including any of our gts files. It seems like we need to create our own typescript Program with a custom CompilerHost that will know how to read gts files.

For context, terminology like Program and CompilerHost are from https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API, and typescript-eslint supports us providing it a program in it's options so that seems like the way to go to me at least

@bmish
Copy link
Member

bmish commented Nov 12, 2023

@patricklx @NullVoxPopuli

@patricklx
Copy link
Contributor

patricklx commented Nov 12, 2023

@NullVoxPopuli i remember you tried something with glint ts program? Maybe that would work for this?

@NullVoxPopuli
Copy link
Contributor

well, to use type-are lints, you may not use @typescript-eslint/parser on gts files, you must use the parser provided by eslint-plugin-ember

@patricklx for the glint ts program thing, I don't recall if that was for linting or not

@vstefanovic97
Copy link
Author

@NullVoxPopuli in my reproduction, I am using gjs-gts-parser for the gts files, see here.

But still there is the issue, unless I am missing something

@patricklx
Copy link
Contributor

patricklx commented Nov 12, 2023

The issue is when gts is imported, not linted directly. But for typed linting the parser also needs to load the imported files. Internally that is done by typescript-eslint parser

? typescriptParser.parseForESLint(jsCode, { ...options, ranges: true })

@patricklx
Copy link
Contributor

Maybe we need a small ts plugin for this? Like for vue
https://github.com/sandersn/vue-ts-plugin/blob/master/lib/index.ts

@vstefanovic97
Copy link
Author

vstefanovic97 commented Nov 12, 2023

@patricklx, @NullVoxPopuli seems like the only reason why even direct linting of a gts file works, is because typescript-eslint internally overrides the readFile method of it's CompilerHost to use the code we provide as the first argument to its parseForEslint method, instead of the actual file on disk.

See https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/typescript-estree/src/create-program/getWatchProgramsForProjects.ts#L273

But unfortunatelly this means it will only treat that one file like that, and for the rest it will read from disk, which means it will try to import gts files which it doesn't know how to read.

Maybe asking typescript-eslint repo for a feature to provide an option as an argument where we could provide our own readFile implementation will be enough?

@vstefanovic97
Copy link
Author

@patricklx, @NullVoxPopuli seems like the only reason why even direct linting of a gts file works, is because typescript-eslint internally overrides the readFile method of it's CompilerHost to use the code we provide as the first argument to its parseForEslint method, instead of the actual file on disk.

See https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/typescript-estree/src/create-program/getWatchProgramsForProjects.ts#L273

But unfortunatelly this means it will only treat that one file like that, and for the rest it will read from disk, which means it will try to import gts files which it doesn't know how to read.

Maybe asking typescript-eslint repo for a feature to provide an option as an argument where we could provide our own readFile implementation will be enough?

Such feature might be beneficial not only to us, but seems like vue has same issue right now
vuejs/vue-eslint-parser#104

@NullVoxPopuli
Copy link
Contributor

If we could tell it to use glint instead of tsc of 'the program', i think that could help, as it also has its own read file ...

If it weren't for this

internally overrides the readFile method of it's CompilerHost

🙈

@vstefanovic97
Copy link
Author

If we could tell it to use glint instead of tsc of 'the program', i think that could help, as it also has its own read file ...

If it weren't for this

internally overrides the readFile method of it's CompilerHost

🙈

@NullVoxPopuli well if we pass it glints TS program, then it won't override anything it just uses the program as it's given

@vstefanovic97
Copy link
Author

Glint just maybe seems a bit overkill for this, if we could just override readFile that typescript-eslint is using by asking them to provide a new option for it, we can the just use transformForEslint method as the override, it would solve the problem I think and not have us depend on glint.

But it is just a theory, I can test it out tomorrow and if it works I can raise a feature request to typescript-eslint. Wdyt?
But in case they won't be willing to let us override it we probably will need to use glint or something

@patricklx
Copy link
Contributor

patricklx commented Nov 12, 2023

Maybe we can use this?
https://github.com/typescript-eslint/typescript-eslint/blob/fadc7940b456f1903a282a4b5085daf1b8367b16/packages/parser/src/index.ts#L7
Patch it and pass it as program parser option? but it would be better to transform directly with content tag,

@vstefanovic97
Copy link
Author

vstefanovic97 commented Nov 13, 2023

Glint just maybe seems a bit overkill for this, if we could just override readFile that typescript-eslint is using by asking them to provide a new option for it, we can the just use transformForEslint method as the override, it would solve the problem I think and not have us depend on glint.

But it is just a theory, I can test it out tomorrow and if it works I can raise a feature request to typescript-eslint. Wdyt? But in case they won't be willing to let us override it we probably will need to use glint or something

I tried a few things, but for now I just can't seem to get it to work :(, I'll keep trying next couple of days, but maybe someone else will have more luck with this

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

Successfully merging a pull request may close this issue.

4 participants