useStringStartsEndsWith
此内容尚不支持你的语言。
Summary
Section titled “Summary”- Rule available since:
v2.4.12 - Diagnostic Category:
lint/nursery/useStringStartsEndsWith - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
@typescript-eslint/prefer-string-starts-ends-with
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useStringStartsEndsWith": "error" } } }}Description
Section titled “Description”Prefer String#startsWith() and String#endsWith() over verbose prefix and suffix checks.
This rule detects common string comparisons such as indexing, charAt, indexOf, lastIndexOf,
slice, substring, match, and anchored RegExp#test calls when they are being used to check
whether a string starts or ends with another string.
The rule uses type information and only reports when the receiver is known to be a string. Array indexing and other non-string receivers are ignored.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”declare const text: string;text[0] === "a";/invalid-index.ts:2:1 lint/nursery/useStringStartsEndsWith FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This index access comparison looks like you’re checking a string prefix.
1 │ declare const text: string;
> 2 │ text[0] === “a”;
│ ^^^^^^^^^^^^^^^
3 │
ℹ Using the built-in string method is clearer and easier to read.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use startsWith() instead.
1 1 │ declare const text: string;
2 │ - text[0]·===·“a”;
2 │ + text.startsWith(“a”);
3 3 │
declare const text: string;text.indexOf("foo") === 0;/invalid-search.ts:2:1 lint/nursery/useStringStartsEndsWith FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This indexOf() comparison looks like you’re checking a string prefix.
1 │ declare const text: string;
> 2 │ text.indexOf(“foo”) === 0;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ Using the built-in string method is clearer and easier to read.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use startsWith() instead.
1 1 │ declare const text: string;
2 │ - text.indexOf(“foo”)·===·0;
2 │ + text.startsWith(“foo”);
3 3 │
declare const text: string;/^foo/.test(text);/invalid-regex.ts:2:1 lint/nursery/useStringStartsEndsWith FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This anchored RegExp#test() call looks like you’re checking a string prefix.
1 │ declare const text: string;
> 2 │ /^foo/.test(text);
│ ^^^^^^^^^^^^^^^^^
3 │
ℹ Using the built-in string method is clearer and easier to read.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use startsWith() instead.
1 1 │ declare const text: string;
2 │ - /^foo/.test(text);
2 │ + text.startsWith(“foo”);
3 3 │
declare const text: string;text.startsWith("foo");text.endsWith("bar");declare const list: string[];list[0] === "a";Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.