Skip to content

allowedNames does not seem to work on classes #67

@Avasam

Description

@Avasam

Description

Using the following settings:

import { ASTUtils } from "@angular-eslint/utils"

export default defineConfig([
  // ...
  {
    rules: {
      "prefer-arrow-functions/prefer-arrow-functions": [
        "error",
        {
          {
            classPropertiesAllowed: true,
            disallowPrototype: true,
            returnStyle: "implicit"
          },
          allowedNames: Array.from(ASTUtils.ANGULAR_LIFECYCLE_METHODS)
        }
      ]
    }
  }
]

And for reference:

> const { ASTUtils } = await import("@angular-eslint/utils");
undefined
> Array.from(ASTUtils.ANGULAR_LIFECYCLE_METHODS)
[
  'ngAfterContentChecked',
  'ngAfterContentInit',
  'ngAfterViewChecked',
  'ngAfterViewInit',
  'ngDoBootstrap',
  'ngDoCheck',
  'ngOnChanges',
  'ngOnDestroy',
  'ngOnInit'
]

still flags the following:

import type { AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoBootstrap, DoCheck, OnChanges, OnDestroy, OnInit } from '@angular/core'
import { Component } from '@angular/core'

@Component({
  selector: 'app-good-example',
  template: '',
})
export class GoodExampleComponent
  implements
    AfterContentChecked,
    AfterContentInit,
    AfterViewChecked,
    AfterViewInit,
    DoBootstrap,
    DoCheck,
    OnChanges,
    OnDestroy,
    OnInit
{
  // Testing good examples for: Angular hooks need to be functions defined on the prototype
  ngAfterContentChecked() {}
  ngAfterContentInit() {}
  ngAfterViewChecked() {}
  ngAfterViewInit() {}
  ngDoBootstrap() {}
  ngDoCheck() {}
  ngOnChanges() {}
  ngOnDestroy() {}
  ngOnInit() {}
}

Suggested Solution

I would expect allowedNames: ['ngOnInit', ...] to also allow using a function method ngOnInit set on the class prototype (which is needed for Angular)

Help Needed

In the mean time I've been reconfiguring this rule with classPropertiesAllowed: false by targeting filenames, but that is fragile and only works when a specific naming convention is followed perfectly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions