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

Skip to content

Commit 66e9c6e

Browse files
authored
fix(eslint-plugin): [prefer-string-starts-ends-with] Check negative indices in the second position for slice (typescript-eslint#2696)
1 parent 343d20d commit 66e9c6e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/eslint-plugin/src/rules/prefer-string-starts-ends-with.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,8 @@ export default createRule({
575575
const isStartsWith =
576576
!isEndsWith &&
577577
callNode.arguments.length === 2 &&
578-
isNumber(callNode.arguments[0], 0);
578+
isNumber(callNode.arguments[0], 0) &&
579+
!isNegativeIndexExpression(callNode.arguments[1], node.object);
579580
if (!isStartsWith && !isEndsWith) {
580581
return;
581582
}

packages/eslint-plugin/tests/rules/prefer-string-starts-ends-with.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ ruleTester.run('prefer-string-starts-ends-with', rule, {
230230
x.test(s)
231231
}
232232
`,
233+
`
234+
function f(s: string) {
235+
s.slice(0, -4) === "car"
236+
}
237+
`,
238+
`
239+
function f(x: string, s: string) {
240+
x.endsWith('foo') && x.slice(0, -4) === 'bar'
241+
}
242+
`,
233243
]),
234244
invalid: addOptional([
235245
// String indexing.

0 commit comments

Comments
 (0)