runem / lit-analyzer

Monorepository for tools that analyze lit-html templates
MIT License
319 stars 38 forks source link

Error in core library #212

Open Allcharles opened 2 years ago

Allcharles commented 2 years ago

Lit analyzer returns the following error when attempting to run my code:

> @lit/lit-starter-ts@1.0.0 analyze
> cem analyze --litelement --globs "src/**/*.ts"

file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/utils/index.js:67
    throw new Error(`\n\n[${name ?? 'unnamed-plugin'}]: ${errorMessage}\n\n ${e.stack}\n`);
          ^

Error:

[CORE - CLASSES]: Looks like you've hit an error in the core library. Please try to create a minimal reproduction at https://custom-elements-manifest.netlify.com and create an issue at: https://github.com/open-wc/custom-elements-manifest/issues

 TypeError: Cannot read properties of undefined (reading 'kind')
    at Object.isCallExpression (C:\Users\allch\Desktop\Code\LitElement\broken-analyzer\node_modules\typescript\lib\typescript.js:28041:21)
    at file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/features/analyse-phase/creators/handlers.js:166:19
    at Array.forEach (<anonymous>)
    at file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/features/analyse-phase/creators/handlers.js:157:20
    at Array.forEach (<anonymous>)
    at handleHeritage (file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/features/analyse-phase/creators/handlers.js:153:26)
    at createClass (file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/features/analyse-phase/creators/createClass.js:154:19)
    at analyzePhase (file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/features/analyse-phase/classes.js:14:25)
    at file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/create.js:104:23
    at withErrorHandling (file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/utils/index.js:58:5)

    at withErrorHandling (file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/utils/index.js:67:11)
    at file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/create.js:103:7
    at Array.forEach (<anonymous>)
    at visitNode (file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/create.js:102:19)
    at visitNodes (C:\Users\allch\Desktop\Code\LitElement\broken-analyzer\node_modules\typescript\lib\typescript.js:29673:30)
    at Object.forEachChild (C:\Users\allch\Desktop\Code\LitElement\broken-analyzer\node_modules\typescript\lib\typescript.js:29916:24)
    at visitNode (file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/create.js:108:8)
    at analyze (file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/create.js:99:3)
    at file:///C:/Users/allch/Desktop/Code/LitElement/broken-analyzer/node_modules/@custom-elements-manifest/analyzer/src/create.js:50:5
    at Array.forEach (<anonymous>)

I have managed to make a reproducible example below, the important parts I have extracted: broken-analyzer.zip

type Constructor<T extends {} = {}> = new (...args: any[]) => T;

const CustomMixin = dedupeMixin(
  <T extends Constructor>(superclass: T = class {} as any) => {
    class MixinClass extends superclass {}
    return MixinClass;
  }
);

class MyClass extends CustomMixin() {}

It appears the issue stems from the default value for the superclass: class {} as any. If I do the following instead, the analyzer no longer crashes.

type Constructor<T extends {} = {}> = new (...args: any[]) => T;

const CustomMixin = dedupeMixin(
  <T extends Constructor>(superclass: T) => {
    class MixinClass extends superclass {}
    return MixinClass;
  }
);

class MyClass extends CustomMixin(class {}) {}

If you feel this is more of an issue on open-wc's side, I am happy to transfer this issue there