runem / lit-analyzer

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

Feature request: require custom elements be registered on HTMLElementTagNameMap #73

Closed rictic closed 4 years ago

rictic commented 4 years ago

A pass that would diagnose when a custom element is recognized in source code but it hasn't be declared on the HTMLElementTagNameMap, or if it is declared with the wrong tag name. For example:

Warns:

import {customElement, LitElement} from 'lit-element';
@customElement('foo-bar')
class FooBar extends LitElement {}
//    ~~~~~~  warning!

Warns:

import {customElement, LitElement} from 'lit-element';
@customElement('foo-bar')
class FooBar extends LitElement {}

declare global {
  interface HTMLElementTagNameMap {
    'fizz-buzz': FooBar;
//  ~~~~~~~~~~~ warning!
  }
}

Does not warn:

import {customElement, LitElement} from 'lit-element';
@customElement('foo-bar')
class FooBar extends LitElement {}

declare global {
  interface HTMLElementTagNameMap {
    'foo-bar': FooBar;
  }
}
runem commented 4 years ago

Great idea, I love this rule! This would be possible to implement after the performance branch has been finished which includes the new version of web-component-analyzer. The new version of WCA emits all definitions of the same element and not just the last one found.

e111077 commented 4 years ago

Edit: Ignore this comment

~Alternative, what's wrong with adding types to @fires?~

// my-element.ts
/**
 * @fires my-event {MyEventDetail}
 */
export class MyEvent extends LitElement { ... }
// main.ts
html`
  <!-- type checking happens here -->
  <my-element @my-event=${this.onMyEvent}></my-element>
...
`;

onMyEvent(e: MyEventDetail) { ... }
rictic commented 4 years ago

+1 to checking events (I believe there's an event map as well, though it's not element-specific), but that should be a different issue IMO

e111077 commented 4 years ago

will file

runem commented 4 years ago

I'm currently working on this branch: https://github.com/runem/lit-analyzer/tree/1.2.0 which should be able to support building this rule :-) Lately I have been caught up in web-component-analyzer-specific work, but next I'll take a look at implementing this rule.

e111077 commented 4 years ago

oh wow I'm dumb; I totally read this as Custom Events not Custom Elements haha >.< issue filed on #85

runem commented 4 years ago

I just implemented the rule on 1.2.0 :-)

The rule is called no-missing-element-type-definition and is disabled by default both when running strict and non-strict. Please let me know if you have any comments or any additions that you want me to add to this feature.

demo

justinfagnani commented 4 years ago

That quickfix is awesome!!

rictic commented 4 years ago

Awesome!

WickyNilliams commented 2 years ago

I've not been able to get this working - is it broken with lit@2. See the attached screenshot. No error reported

image

WickyNilliams commented 2 years ago

Sorry I take it back, it is working now. Not sure what happened there!