Skip to content

Add rule to validate if getter/setter shadow a class fields with @api #25

@pmdartus

Description

@pmdartus

The following pattern is currently not flagged by the valid-api rule but has the unexpected behavior that the foo public property is not accessible from the template. (original issue)

import { LightningElement, api } from 'lwc';

export default class App extends LightningElement {
    @api foo = 1;

    set foo(value) {}
    get foo() {}
}

We should restrict the combination of public property with a getter/setter.

This can either be achieved by:

  • adding a new ESLint rule dedicated to this case, eg: api-accessor-shadow
  • or enhance the valid-api ESLint rule to handle this case. However, we need to ensure that the valid-api ESLint stays backward compatible because the rule is used to validate the component when compiled on the platform. In this case, we will add this new check behind a config.
{
  "@lwc/lwc/valid-api": [
    "error",
    { "disallowAccessorShadow": true }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    USER STORYkind/featureCategorizes issue or PR as related to a new feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions