Skip to content

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

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
3 tasks done
tkalliom opened this issue Nov 18, 2021 · 0 comments · Fixed by #7712
Closed
3 tasks done

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

tkalliom opened this issue Nov 18, 2021 · 0 comments · Fixed by #7712
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

@tkalliom
Copy link

  • 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
@tkalliom tkalliom added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Nov 18, 2021
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working and removed triage Waiting for team members to take a look labels Nov 23, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 16, 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