Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [prefer-string-starts-ends-with] add allowSingleElementEquality option#8374

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion.eslintrc.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,7 +53,6 @@ module.exports = {

// TODO(#7130): Investigate changing these in or removing these from presets
'@typescript-eslint/no-confusing-void-expression': 'off',
'@typescript-eslint/prefer-string-starts-ends-with': 'off',

//
// our plugin :D
Expand DownExpand Up@@ -91,6 +90,12 @@ module.exports = {
allowBitwiseExpressions: true,
},
],
'@typescript-eslint/prefer-string-starts-ends-with': [
'error',
{
allowSingleElementEquality: 'always',
},
],
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/restrict-template-expressions': [
'error',
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -52,6 +52,26 @@ foo.startsWith('bar');
foo.endsWith('bar');
```

<!--/tabs-->

## Options

### `allowSingleElementEquality`

If switched to `'always'`, the rule will allow equality checks against the first or last character in a string.
This can be preferable in projects that don't deal with special character encodings and prefer a more succinct style.

The following code is considered incorrect by default, but is allowed with `allowSingleElementEquality: 'always'`:

```ts option='{ "allowSingleElementEquality": "always" }' showPlaygroundButton
declare const text: string;

text[0] === 'a';
text[0] === text[0].toUpperCase();
text[0] === text[1];
text[text.length - 1] === 'b';
```

## When Not To Use It

If you don't mind which style of string checking is used, you can turn this rule off safely.
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,9 +17,19 @@ import {
const EQ_OPERATORS = /^[=!]=/;
const regexpp = new RegExpParser();

export default createRule({
type AllowedSingleElementEquality = 'always' | 'never';

export type Options = [
{
allowSingleElementEquality?: AllowedSingleElementEquality;
},
];

type MessageIds = 'preferEndsWith' | 'preferStartsWith';

export default createRule<Options, MessageIds>({
name: 'prefer-string-starts-ends-with',
defaultOptions: [],
defaultOptions: [{ allowSingleElementEquality: 'never' }],

meta: {
type: 'suggestion',
Expand All@@ -33,11 +43,24 @@ export default createRule({
preferStartsWith: "Use 'String#startsWith' method instead.",
preferEndsWith: "Use the 'String#endsWith' method instead.",
},
schema: [],
schema: [
{
additionalProperties: false,
properties: {
allowSingleElementEquality: {
description:
'Whether to allow equality checks against the first or last element of a string.',
enum: ['always', 'never'],
type: 'string',
},
},
type: 'object',
},
],
fixable: 'code',
},

create(context) {
create(context, [{ allowSingleElementEquality }]) {
const globalScope = context.sourceCode.getScope(context.sourceCode.ast);

const services = getParserServices(context);
Expand DownExpand Up@@ -401,8 +424,15 @@ export default createRule({
}

const isEndsWith = isLastIndexExpression(indexNode, node.object);
if (allowSingleElementEquality === 'always' && isEndsWith) {
return;
}

const isStartsWith = !isEndsWith && isNumber(indexNode, 0);
if (!isStartsWith && !isEndsWith) {
if (
(allowSingleElementEquality === 'always' && isStartsWith) ||
(!isStartsWith && !isEndsWith)
) {
return;
}

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -437,7 +437,7 @@ function isExported(variable: TSESLint.Scope.Variable): boolean {
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return node.parent!.type.indexOf('Export') === 0;
return node.parent!.type.startsWith('Export');
});
}

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp