From 66e9c6e29f9f56bbd178ba6405f47053be591258 Mon Sep 17 00:00:00 2001 From: kangdaehoon Date: Mon, 26 Oct 2020 06:57:35 +0900 Subject: [PATCH] fix(eslint-plugin): [prefer-string-starts-ends-with] Check negative indices in the second position for slice (#2696) --- .../src/rules/prefer-string-starts-ends-with.ts | 3 ++- .../tests/rules/prefer-string-starts-ends-with.test.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/prefer-string-starts-ends-with.ts b/packages/eslint-plugin/src/rules/prefer-string-starts-ends-with.ts index 3126f616038..f4697b7c01d 100644 --- a/packages/eslint-plugin/src/rules/prefer-string-starts-ends-with.ts +++ b/packages/eslint-plugin/src/rules/prefer-string-starts-ends-with.ts @@ -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; } diff --git a/packages/eslint-plugin/tests/rules/prefer-string-starts-ends-with.test.ts b/packages/eslint-plugin/tests/rules/prefer-string-starts-ends-with.test.ts index e97fa912df5..0cef89740d5 100644 --- a/packages/eslint-plugin/tests/rules/prefer-string-starts-ends-with.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-string-starts-ends-with.test.ts @@ -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.