plantain-00 / type-coverage

A CLI tool to check type coverage for typescript code
MIT License
1.21k stars 43 forks source link

Question: does it support checking .vue file ? #66

Open fleksin opened 3 years ago

fleksin commented 3 years ago

As the title suggests. It seems like it doesn't support as I tested it. Do we have plan adding this feature? if not, I'm considering making a PR

plantain-00 commented 3 years ago

PR is welcome.

darrenmothersele commented 3 years ago

I could have a go at this. Any clues on where to start adding support for *.vue files?

plantain-00 commented 3 years ago

@darrenmothersele Some ideas:

  1. Design a way to find .vue files.
  2. Read the .vue files and parse typescript code from them(maybe by @vue/compiler-dom).
  3. Add the typescript source code to the program or create a new Program instance: https://github.com/plantain-00/type-coverage/blob/master/packages/core/src/core.ts#L27
  4. Check them and sum them up: https://github.com/plantain-00/type-coverage/blob/master/packages/core/src/core.ts#L133
JounQin commented 3 years ago

vue-tsc may help.

tomardern commented 2 years ago

I've done a bit of digging this evening, here is what I've found.

Route 1 - using vue-tsc

vue-tsc has a method named createProgramProxy (https://github.com/johnsoncodehk/volar/blob/master/packages/vue-tsc/src/proxy.ts#L7), which could be used instead of ts.createProgram (https://github.com/plantain-00/type-coverage/blob/master/packages/core/src/core.ts#L27)

Something along the lines of:

import { createProgramProxy } from "vue-tsc/out/proxy.js";
//...
export async function lint(project: string, options?: Partial<LintOptions>) {
//...
// If Vue project use createProgramProxy, otherwise, use ts.createProgram
const program = createProgramProxy(
      {
        rootNames: rootNames,
        options: compilerOptions,
        host: undefined, // Language host??
        oldProgram: lintOptions.oldProgram,
      },
      compilerOptions,
      undefined,  // Language host??
      lintOptions.oldProgram
    );
// ...
 }

However, the createProgramProxy from vue-tsc requires passing through a compiler (https://github.com/johnsoncodehk/volar/blob/master/packages/vue-tsc/src/proxy.ts#L28), in which this package is passing undefined. a "compilerHost" is created deep within the typescript core repo, which seems like quite a big undertaking.... (https://github.com/microsoft/TypeScript/blob/d1fa945261a30616c5e3557d1a9fc18f3482af30/src/services/services.ts#L1397)

The volar package is currently undergoing a big refactor - https://github.com/johnsoncodehk/volar/pull/1050/files - so it might be worth looking at this once the refractor is out.

Route 2 - Learnings from vue-type-audit Taking a look at a similar npm package (https://github.com/andoshin11/vue-type-audit), it looks like the "compilerHost" could be something similar to this: https://github.com/andoshin11/vue-type-audit/blob/master/src/languageService/createHost.ts#L24