-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[prefer-string-starts-ends-with] False positives when slicing #2688
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
Comments
This just looks like a really cryptic way to do |
I mean, you could also interpret it that way, but I think it's less cryptic than To me, this version is much clearer: function isATextFileNamed(filename, name) {
return filename.endsWith('.txt') && filename.slice(0, -4) === name;
} Than if you wanted to use function isATextFileNamed(filename, name) {
return filename.endsWith('.txt') && filename.startsWith(name) && filename.length === name.length + 4;
} |
Another use-case is if you want to check a first line. This: function firstLineEquals(text, firstLine) {
const newlinePosition = text.indexOf('\n');
return text.slice(0, newlinePosition) === firstLine;
} Is much clearer than this: function firstLineEquals(text, firstLine) {
const newlinePosition = text.indexOf('\n');
return text.startsWith(firstLine) && newlinePosition === firstLine.length;
} But the former also dings |
IMO it's not because (in my experience) most devs don't have experience with negative index form of For your second example, I'd use But this is just bikeshedding. The rule doesn't currently understand negative indices in the second position for |
Repro
Expected Result
No error
Actual Result
Additional Info
Versions
@typescript-eslint/eslint-plugin
4.4.1
@typescript-eslint/parser
4.4.1
TypeScript
4.0.3
ESLint
7.11.0
node
14.14.0
The text was updated successfully, but these errors were encountered: