Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: [member-ordering] false-positive error when order is set to natural or natural-case-insensitive  #6812

@IronGeek

Description

@IronGeek

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.0.1-rc&sourceType=module&code=MYGwhgzhAEBiD29oG8BQ1oGIBGYBOAXNBAC54CWAdgOYDcq601ApidLngBQCURpFNFIwx5WAVzyVoJABbkIAOhz56GAL4MMEVu3ycAbmBBjmfMlWrchGDLPlKO0ALzRDx5quga1QA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6AW0XYCNFpaA9tAAmfZgHN0YANrhsOAO4BDaE0gAaOfKzzskUQDMlCfFJ269nHnwAqJFFNkWL0yOMSn1UZB8gBdLWc-TWc9IVFoKUgmJXxYaCV4SED5AF8UsHT5AIh01KA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

class Foo {
  #bar: string;

  get bar(): string {
    return this.#bar;
  }

  set bar(value: string) {
    this.#bar = value;
  }
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/member-ordering": [
      "warn",
      {
        "default": {
          "memberTypes": [
            ["get", "set"]
          ],
          "order": "natural"
        }
      }
    ]
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

No error or warning reported

Actual Result

Error/ warningwith the following message is reported:

Member bar should be declared before member bar. 8:3 - 10:4

Additional Info

This happens when there are member with the same name, for example with getters and setters:

get bar(): string {}
set bar(value: string) {}

I've tried looking at the source code:

function naturalOutOfOrder(
name: string,
previousName: string,
order: AlphabeticalOrder,
): boolean {
switch (order) {
case 'alphabetically':
return name < previousName;
case 'alphabetically-case-insensitive':
return name.toLowerCase() < previousName.toLowerCase();
case 'natural':
return naturalCompare(name, previousName) !== 1;
case 'natural-case-insensitive':
return (
naturalCompare(name.toLowerCase(), previousName.toLowerCase()) !== 1
);
}
}

It seems the logic for natural and natural-case-insensitive ordering is slightly different than the alphabetically* ordering.
And with the case of name and previousName are the same, alphabetically* order will returns false while natural* order will returns true (0 !== 1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: 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