Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-string-starts-ends-with] Check negative i…
Browse files Browse the repository at this point in the history
…ndices in the second position for slice (#2696)
  • Loading branch information
ginger-kang committed Oct 25, 2020
1 parent 343d20d commit 66e9c6e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Expand Up @@ -575,7 +575,8 @@ export default createRule({
const isStartsWith =
!isEndsWith &&
callNode.arguments.length === 2 &&
isNumber(callNode.arguments[0], 0);
isNumber(callNode.arguments[0], 0) &&
!isNegativeIndexExpression(callNode.arguments[1], node.object);
if (!isStartsWith && !isEndsWith) {
return;
}
Expand Down
Expand Up @@ -230,6 +230,16 @@ ruleTester.run('prefer-string-starts-ends-with', rule, {
x.test(s)
}
`,
`
function f(s: string) {
s.slice(0, -4) === "car"
}
`,
`
function f(x: string, s: string) {
x.endsWith('foo') && x.slice(0, -4) === 'bar'
}
`,
]),
invalid: addOptional([
// String indexing.
Expand Down

0 comments on commit 66e9c6e

Please sign in to comment.