Skip to content
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

feat(eslint-plugin): add prefer-string-starts-ends-with rule (fixes #285) #289

Merged
merged 24 commits into from Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0a10459
feat(eslint-plugin): add prefer-string-starts-ends-with rule (fixes #…
mysticatea Feb 16, 2019
fcb945a
Merge remote-tracking branch 'origin/master' into prefer-string-start…
mysticatea Feb 19, 2019
ee7c1df
fix s.match behavior
mysticatea Feb 19, 2019
34e9fe1
fix formating errors
mysticatea Feb 19, 2019
c39407a
Update packages/eslint-plugin/docs/rules/prefer-string-starts-ends-wi…
sindresorhus Feb 19, 2019
4cd6097
fix for coverage 100
mysticatea Feb 19, 2019
740fe07
Merge remote-tracking branch 'origin/master' into prefer-string-start…
mysticatea Feb 20, 2019
881a3e4
fix format as following master branch
mysticatea Feb 20, 2019
08ab0ad
Update packages/eslint-plugin/src/rules/prefer-string-starts-ends-wit…
j-f1 Feb 22, 2019
86018fd
Update packages/eslint-plugin/src/rules/prefer-string-starts-ends-wit…
j-f1 Feb 22, 2019
3c1def5
Update packages/eslint-plugin/docs/rules/prefer-string-starts-ends-wi…
existentialism Feb 22, 2019
38005cd
Update packages/eslint-plugin/docs/rules/prefer-string-starts-ends-wi…
existentialism Feb 22, 2019
c113c9c
Merge branch 'master' into prefer-string-starts-ends-with/new
JamesHenry Feb 23, 2019
ef3e7d6
Update packages/eslint-plugin/docs/rules/prefer-string-starts-ends-wi…
JamesHenry Feb 23, 2019
ab1dd85
remove comment
mysticatea Feb 23, 2019
671edd6
update description
mysticatea Feb 23, 2019
fecf943
update example
mysticatea Feb 23, 2019
e15a7ff
Merge remote-tracking branch 'origin/master' into prefer-string-start…
mysticatea Feb 23, 2019
ef0daaf
Merge branch 'master' into prefer-string-starts-ends-with/new
mysticatea Mar 6, 2019
150dd9a
Merge branch 'master' into prefer-string-starts-ends-with/new
bradzacher Mar 8, 2019
081a338
Merge branch 'master' into prefer-string-starts-ends-with/new
mysticatea Apr 9, 2019
872ed49
fix for review
mysticatea Apr 9, 2019
f955b79
Merge branch 'master' into prefer-string-starts-ends-with/new
bradzacher Apr 11, 2019
b86eba7
Merge branch 'master' into prefer-string-starts-ends-with/new
bradzacher Apr 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,54 @@
# Enforce the use of `String#startsWith` and `String#endsWith` instead of other equivalent methods of checking substrings (prefer-string-starts-ends-with)

There are multiple ways to verify if a string starts or ends with a specific string, such as `foo.indexOf('bar') === 0`.

Since ES2015 has added `String#startsWith` and `String#endsWith`, this rule reports other ways to be consistent.

## Rule Details

This rule is aimed at enforcing a consistent way to check whether a string starts or ends with a specific string.

Examples of **incorrect** code for this rule:

```ts
let foo: string;

// starts with
foo[0] === 'b';
foo.charAt(0) === 'b';
foo.indexOf('bar') === 0;
foo.slice(0, 3) === 'bar';
foo.substring(0, 3) === 'bar';
foo.match(/^bar/) != null;
/^bar/.test(foo);

// ends with
foo[foo.length - 1] === 'b';
foo.charAt(foo.length - 1) === 'b';
foo.lastIndexOf('bar') === foo.length - 3;
foo.slice(-3) === 'bar';
foo.substring(foo.length - 3) === 'bar';
foo.match(/bar$/) != null;
/bar$/.test(foo);
```

Examples of **correct** code for this rule:

```ts
foo.startsWith('bar');
foo.endsWith('bar');
```

## Options

There are no options.

```JSON
{
"@typescript-eslint/prefer-string-starts-ends-with": "error"
}
```

## When Not To Use It

If you don't mind that style, you can turn this rule off safely.