typed-ember / glint

TypeScript powered tooling for Glimmer templates
https://typed-ember.gitbook.io/glint
MIT License
109 stars 51 forks source link

Expose "Program" so that @typescript-eslint/* can use it for type-aware lints #608

Closed NullVoxPopuli closed 1 year ago

NullVoxPopuli commented 1 year ago

Currently, using @typescript-eslint/* with type-aware lints cannot be done with gjs/gts files. However, we can pass Program to the lint config: https://typescript-eslint.io/packages/parser/#configuration

interface ParserOptions {
  cacheLifetime?: {
    glob?: number | 'Infinity';
  };
  ecmaFeatures?: {
    jsx?: boolean;
    globalReturn?: boolean;
  };
  ecmaVersion?: number | 'latest';
  emitDecoratorMetadata?: boolean;
  extraFileExtensions?: string[];
  jsxFragmentName?: string | null;
  jsxPragma?: string | null;
  lib?: string[];
  program?: import('typescript').Program; // <----- here
  project?: string | string[] | true;
  projectFolderIgnoreList?: string[];
  tsconfigRootDir?: string;
  warnOnUnsupportedTypeScriptVersion?: boolean;
}

If Glint exposed its Program then we could set that in the parser options and have type-aware linting.

@wagenet identified that this is pretty close: https://github.com/typed-ember/glint/blob/3d4e0685523853702c4b4e54f76dcb726fe89a32/packages/core/src/cli/utils/patch-program.ts#L15

But I think we'd want to reduce the number of arguments needed for an end-user API as this would specifically be for linting, and we can make assumptions

dfreeman commented 1 year ago

But I think we'd want to reduce the number of arguments needed for an end-user API as this would specifically be for linting, and we can make assumptions

That's a pretty big assumption 😉 — we can't meaningfully do anything to a ts.Program without a Glint TransformManager that's appropriately instantiated for the environment we're in, so there's no way to eliminate or default that argument here.

If you want to get your hands on a ts.Program that's suitably set up for the salient Glint environment, that's already available via analyzeProject() (analysis.languageServer.service.getProgram()).

If Glint exposed its Program then we could set that in the parser options and have type-aware linting.

I'd be a bit surprised if dropping in Glint's program were all that needed to happen there, but I'm happy to be wrong if that's all there is to it. Regardless, analyzeProject() should be a reasonable starting point for your investigation.