Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks done
SearleWork opened this issue Feb 22, 2023 · 0 comments · Fixed by #6619
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@SearleWork
Copy link

SearleWork commented Feb 22, 2023

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);
      }
@SearleWork SearleWork added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 22, 2023
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Feb 22, 2023
@SearleWork SearleWork changed the title Bug: member-ordering doesn't check anything if optionalityOrder is present but no optional member exists Bug: [member-ordering] Doesn't check anything if optionalityOrder is present but no optional member exists Feb 22, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants