Skip to content
Permalink
Browse files

fix(eslint-plugin): [prefer-string-starts-ends-with] Check negative i…

…ndices in the second position for slice (#2696)
  • Loading branch information
ginger-kang committed Oct 25, 2020
1 parent 343d20d commit 66e9c6e29f9f56bbd178ba6405f47053be591258
@@ -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;
}
@@ -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.

0 comments on commit 66e9c6e

Please sign in to comment.
You can’t perform that action at this time.