Skip to content

Bug: [member-ordering] Doesn't check anything if optionalityOrder is present but no optional member exists #6514

Closed
@SearleWork

Description

@SearleWork

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=4.9.3&sourceType=module&code=JYOwLgpgTgZghgYwgAgEJQPYGsImQbwChlkAjALmRAFcBbU6AbmOTkpvqcIF9DDRIsRCgBioFERIUqdBlGYk2MzvJYYA-O1ldeQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6AW0XYCNFpaA9tAAmfZgHN0YANrhsOPtCGQANHPlZ52SM3x8AZgEMyKKZq3aBNSgKaHGRAPIi+UmIgCOsStETDa+j7I+KrqFlBCotBudviw0Pa0ZIbIiLTMqUzIlPiUAG6IkGHyAL7FYGXyALpyZSVAA

Repro Code

interface Broken {
  b: number;
  a: number;
}

interface Works {
  b: number;
  a: number;
  o?: number;
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/<rule-name>": ["error", {
      "interfaces": {
        "optionalityOrder": "required-first",
        "order": "natural-case-insensitive"
      }
    }
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

In interface "Broken", an error should be shown because the members are not sorted

Actual Result

No error is shown

Additional Info

I believe the problem is at packages/eslint-plugin/src/rules/member-ordering.ts:875

      const switchIndex = members.findIndex(
        (member, i) =>
          i && isMemberOptional(member) !== isMemberOptional(members[i - 1]),
      );

      if (switchIndex !== -1) {
        if (!checkRequiredOrder(members, optionalityOrder)) {
          return;
        }
        checkOrder(members.slice(0, switchIndex));
        checkOrder(members.slice(switchIndex));
      }

If no optional members are found, the rule does nothing. So the else case is missing:

        // ...
        checkOrder(members.slice(0, switchIndex));
        checkOrder(members.slice(switchIndex));
      }
      else {
           checkOrder(members);
      }

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