Skip to content

Enhancement: [prefer-string-starts-ends-with] Option to ignore [0] === cases? #7140

Closed
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/play/#ts=5.0.4&sourceType=module&code=CYUwxgNghgTiAEYD2A7AzgF3gNyhAriAFzyYwCWKA5gNwBQd5AZvABS4EgDaADALrwAvMJx5CvPgDoMSAKoAHeSBgBhKGhCsAlFvgBvOvHgB6Y-EkW6AXwbM2HQpMywMaAOrkMAC3Zju-aTlFZTUNbR19QxMzC0lrOiA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WluXxUmAc374AhtHzJ6TACYyA7pXwALdFF7QA9tEjgwAXxCGgA

Description

Coming over from #7138 (comment): when checking whether a string's [0] member equals a modification of [0], the rule's preference ends up being even more code:

declare const value: string;

if (value[0] === value[0].toUpperCase()) { /* ... */ } // before

if (value.startsWith(value[0].toUpperCase())) { /* ... */ } // after

Agreeing with @bradzacher:

I actually think that the previous form is clearer for this specific case.
It's also substantially faster (not that it matters at the this speed... but it is twice as fast - ~1.2B ops/s vs ~2.4B ops/s).

I wonder if we should add an option to skip this case from the rule?

It's not yet clear to me how we would categorize and describe "this case". Maybe it's that we don't want to flag cases that are just checking whether value[0] === something? In that case, maybe the option would be named something like checkFirstElementEquality?

Fail

n/a

Pass

n/a

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulelocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: 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