Skip to content

[member-ordering] migrating from v4.28.x to v5.1: how treat field, get and set equally #4038

Closed
@casaper

Description

@casaper
  • I have tried restarting my IDE and the issue persists.
  • I have read the FAQ and my problem is not listed.
  • I have updated to the latest version of the packages.

I've update everything but Eslint from 7.32.0 to 8.0.1 because:
@angular-eslint is currently not ready Eslint 8. See typescript-eslint issue 4013

Repro

{
  rules: {
        '@typescript-eslint/member-ordering': 'error',
  }
}

This example angular service validates using @typescript-eslint/eslint-plugin@'^4.28.2':

@Injectable({ providedIn: 'root' })
export class CaseService {
  public attributes: Session;
  public contractParameters: Record<string, any>;
  public personParameters: Record<string, any>;
  public packageSession: boolean;
  public prefixList: SbiListItem[] = [];
  public consultantViewMode = false;
  public easyOfferViewMode = false;

  public get contractLength(): SbiListItem {
    return this.contractLengths ? this.contractLengths.find(({ id }) => id === this.attributes.vertragsdauer) || this.contractLengths[0] : null;
  }

  public countries: SbiListItem[];

  public set country(value: SbiListItem) {
    this.attributes.postadresse.land = value?.id;
    this.updateGrenzgaenger();
  }

  public get zipCode(): ZipCode {
    return this.attributes.postadresse?.zipCode ? this.zipCodesService.get(this.attributes.postadresse.zipCode) : null;
  }

  public set zipCode(value: ZipCode) {
    this.attributes.postadresse.zipCode = value;
    this.selectedZipCode = value;
  }

  public selectedZipCode: ZipCode;

  public get isRegionSelectRequired(): boolean {
    return this.zipCode?.hasMultipleRegions() && !this.attributes.postadresse.gemeinde_ro;
  }
  public get region(): Region {
    return this.zipCode && this.attributes.postadresse.region  ? this.zipCode.getRegion(this.attributes.postadresse.region.gemeindeNummer)  : this.zipCode?.firstRegion();
  }

  private currentPersonSubject: Subject<void> = new ReplaySubject(1);
  private beraterAttributesSubject: Subject<void> = new ReplaySubject(1);

  constructor(private locationService: LocationService) {}

  // snip ... public methods first and private methods last ...

When using `@typescript-eslint/eslint-plugin@'^5.1.0'`` the default rule results like this:

$ yarn eslint --quiet src/app/services/case.service.ts
  146:3  error  Member countries should be declared before all public instance get definitions                 @typescript-eslint/member-ordering
  166:3  error  Member zipCode should be declared before all public instance set definitions                   @typescript-eslint/member-ordering
  184:3  error  Member selectedZipCode should be declared before all public instance get definitions           @typescript-eslint/member-ordering
  186:3  error  Member isRegionSelectRequired should be declared before all public instance set definitions    @typescript-eslint/member-ordering
  193:3  error  Member region should be declared before all public instance set definitions                    @typescript-eslint/member-ordering
  210:3  error  Member currentPersonSubject should be declared before all public instance get definitions      @typescript-eslint/member-ordering
  211:3  error  Member beraterAttributesSubject should be declared before all public instance get definitions  @typescript-eslint/member-ordering
  213:3  error  Member constructor should be declared before all public instance get definitions               @typescript-eslint/member-ordering

Fair enough. The default rule sais get and set should come after the constructor.

  "static-field", // = ["public-static-field", "protected-static-field", "private-static-field"]
  "instance-field", // = ["public-instance-field", "protected-instance-field", "private-instance-field"]
  "abstract-field", // = ["public-abstract-field", "protected-abstract-field", "private-abstract-field"]
  "constructor", // = ["public-constructor", "protected-constructor", "private-constructor"]
  "static-get", // = ["public-static-get", "protected-static-get", "private-static-get"]
  "instance-get", // = ["public-instance-get", "protected-instance-get", "private-instance-get"]
  "abstract-get" // = ["public-abstract-get", "protected-abstract-get", "private-abstract-get"]
  "static-set", // = ["public-static-set", "protected-static-set", "private-static-set"]
  "instance-set", // = ["public-instance-set", "protected-instance-set", "private-instance-set"]
  "abstract-set" // = ["public-abstract-set", "protected-abstract-set", "private-abstract-set"]
  "static-method", // = ["public-static-method", "protected-static-method", "private-static-method"]
  "instance-method", // = ["public-instance-method", "protected-instance-method", "private-instance-method"]
  "abstract-method" // = ["public-abstract-method", "protected-abstract-method", "private-abstract-method"]

But for my class this means I have dozens of triplets of field, get and set (which are mostly related between eachother) scattered to three different locations in the file. It could mean that I have to scroll and search even more in that class than I already have to anyway (it is ugly, oversized stuff, but I can't overcome that here and now).

Expected Result

So lets try to reconfigure:

'@typescript-eslint/member-ordering': ['error', { default: [
    'public-field',
    'public-get',
    'public-set',
    'protected-field',
    'protected-get',
    'protected-set',
    'private-field',
    'private-get',
    'private-set',
    'constructor',
    'public-method',
    'protected-method',
    'private-method'
]}],

But that is only slightly better, but still asks me to scatter stuff before the constructor:

  146:3  error  Member countries should be declared before all public get definitions               @typescript-eslint/member-ordering
  166:3  error  Member zipCode should be declared before all public set definitions                 @typescript-eslint/member-ordering
  184:3  error  Member selectedZipCode should be declared before all public get definitions         @typescript-eslint/member-ordering
  186:3  error  Member isRegionSelectRequired should be declared before all public set definitions  @typescript-eslint/member-ordering
  193:3  error  Member region should be declared before all public set definitions                  @typescript-eslint/member-ordering

I actually need field, get and set to be equals.

I could not figure out a config that does fill my requirements and ...
even way more importantly, doesn't ask me to change dozens of thousand lines of code manually.

Is there no way to configure this?

Currently, I'm only left with the option to completely disable the rule for v5.0+ .

TIA for any hints.

Versions

package version
@typescript-eslint/eslint-plugin 5.1.0
@typescript-eslint/parser 5.1.0
TypeScript 4.3.5
ESLint 7.32.0
node 14.18.1

PS [EDIT]:

It looks like there might be a way to get arround this problem for us:

        '@typescript-eslint/member-ordering': [
          'error',
          {
            default: [
              'public-field',
              'protected-field',
              'private-field',
              'constructor',
              'public-method',
              'protected-method',
              'private-method'
            ]
          }
        ],

So perhaps in such a manner we can move around our issue. It'd be less of an issue here if that was the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions