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

Skip to content

[prefer-string-starts-ends-with] False positive for substring with length limit #4181

@tkalliom

Description

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

Repro

{
  "rules": {
    "@typescript-eslint/prefer-string-starts-ends-with": ["error"]
  }
}
export function isThreeCharPrefixMatch(str: string, candidate: string): boolean {
  return str.substring(0, 3) === candidate;
}

tsconfig: the default as generated by tsc --init
Expected Result
No error, since a substring call other than substring(0) can not be replaced with startsWith alone.

Actual Result
The following error was printed:

2:10  error  Use 'String#startsWith' method instead  @typescript-eslint/prefer-string-starts-ends-with

Additional Info
Consider the code with the change suggested by the linter:

export function isThreeCharPrefixMatchB(str: string, candidate: string): boolean {
  return str.startsWith(candidate);
}

Now

> isThreeCharPrefixMatch("int32", "in");
false
> isThreeCharPrefixMatchB("int32", "in");
true

Versions

package version
@typescript-eslint/eslint-plugin 5.4.0
@typescript-eslint/parser 5.4.0
TypeScript 4.5.2
ESLint 8.2.0
node 12.22.5

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