43081j / eslint-plugin-lit

lit-html support for ESLint
120 stars 22 forks source link

feat: new rule attribute-names #191

Closed 43081j closed 10 months ago

43081j commented 10 months ago

This introduces a new rule: attribute-names.

The rule enforces that all attributes are lowercase if the associated property is not.

For example:

class Foo extends LitElement {
  @property()
  camelCase = 'foo';
}

This will fail since the default attribute will be camelCase, and therefore not lowercase.

This should instead be:

@property({attribute: 'camel-case'})

Though in this rule, for now, the fact it is snake-case is a preference rather than being enforced by the rule.

Fixes #187

cc @ajbrun

dlockhart commented 9 months ago

@ajbrun it doesn't look like a new package has been published since this merged, so this feature isn't currently usable -- would you be able to deploy to NPM?

43081j commented 9 months ago

i'll publish a new version today or tomorrow if i can 👍 good catch, totally forgot to!

dlockhart commented 9 months ago

Feature request for this: could internal reactive state properties be exempt from this check?

For example:

static properties = {
  publicThing: { attribute: 'public-thing', type: String },
  _someInternalState: { state: true }
};

From the Lit docs:

Internal reactive state refers to reactive properties that are not part of the component's public API. These state properties don't have corresponding attributes, and aren't intended to be used from outside the component. Internal reactive state should be set by the component itself.

So since these state properties never map to an attribute, I don't think this rule should apply to them.