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

Skip to content

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

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
JoshuaKGoldberg opened this issue Jun 26, 2023 · 4 comments · Fixed by #8374
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@JoshuaKGoldberg
Copy link
Member

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

@JoshuaKGoldberg JoshuaKGoldberg added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look enhancement: plugin rule option New rule option for an existing eslint-plugin rule labels Jun 26, 2023
@Josh-Cena
Copy link
Member

I would avoid against [0] because it doesn't deal with astral characters. .toUpperCase() also feels "icky" but I think it's generally fine. I would recommend using regex though. Consider the following case:

const adlam = "𞤢𞤣𞤤𞤢𞤥"; // All lowercase: "adlam"
console.log(adlam[0]); // '\uD83A'; only a surrogate pair, not a character
console.log(adlam[0] === adlam[0].toUpperCase()); // true??
console.log(/^\P{Lowercase_Letter}/u.test(adlam)); // false; looks better

@bradzacher
Copy link
Member

bradzacher commented Jun 27, 2023

TIL 𞤢𞤣𞤤𞤢𞤥 is actually a valid JS Identifier - but tools really go wonky with it cos it's RTL.

Also worth noting that the regex approach is much, much, much slower.
Testing on my m1 on chrome:

  • index -> 705M ops/s
  • .startsWith -> 544M ops/s -> 23% slower
  • regex -> 65M ops/s -> 90% slower

I think that in some codebases you're going to want to be i18n friendly and use the regex approach - but in a lot of cases the index approach is going to be just fine.

For example in our codebase I'm fine with us only supporting latin characters because that covers 99.9999% (i'd actually hazard a guess that it's 100% TBH) of usecases and is faster.

@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 Jun 27, 2023
@Josh-Cena
Copy link
Member

Josh-Cena commented Jun 27, 2023

So what's the scope here? Here's my attempt:

checkFirstElementEquality: ignore the case where an equality expression is used and both sides reference string[0].

Because I assume we still want to report string[0] === "f". Do we want to report string[0] === string[1]?

Also for symmetry we probably want to treat the last element the same as well (string[string.length - 1] === string[string.length - 1].toUpperCase()), so the option needs a better name.

@bradzacher
Copy link
Member

Breaking it down - we really have a two cases here.

  1. string[0] === character
    • maybe a separate option for codebases that only cares for single-code characters?
    • probably want to refine the error message to highlight that it's not safe for i18n contexts.
  2. string[0] === string[0].(toUpperCase|toLowerCase)()
    • maybe a separate option for codebases that only cares for single-code characters?
    • probably want to refine the error message to highlight that it's not safe for i18n contexts and provide the regex as an alternative.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2024
@bradzacher bradzacher added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Apr 14, 2025
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 enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants