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

fix(eslint-plugin): [prefer-string-starts-ends-with] check for negative start index in slice #1920

Conversation

sinyovercosy
Copy link
Contributor

Fixes #1690

In the prefer-string-starts-ends-with rule, the isEndsWith flag did not check that the startIndex argument of String#slice or String#substring is a negative index (-i or length-i)

const isEndsWith =
callNode.arguments.length === 1 ||
(callNode.arguments.length === 2 &&
isLengthExpression(callNode.arguments[1], node.object));

To resolve this issue, an additional constraint has been added:

function isNegativeIndexExpression(node, expectedIndexedNode) {
  return (
    (node.type === AST_NODE_TYPES.UnaryExpression &&
      node.operator === '-') ||
    (node.type === AST_NODE_TYPES.BinaryExpression &&
      node.operator === '-' &&
      isLengthExpression(node.left, expectedIndexedNode))
  );
}

In the prefer-string-starts-ends-with rule, the isEndsWith flag did not
check that the startIndex argument is a negative index (-i or length-i),
which has been fixed.

see the issue for details

closes issue typescript-eslint#1690
@typescript-eslint
Copy link
Contributor

Thanks for the PR, @sinyovercosy!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@codecov
Copy link

codecov bot commented Apr 20, 2020

Codecov Report

Merging #1920 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1920   +/-   ##
=======================================
  Coverage   94.37%   94.37%           
=======================================
  Files         168      168           
  Lines        7644     7646    +2     
  Branches     2195     2196    +1     
=======================================
+ Hits         7214     7216    +2     
  Misses        183      183           
  Partials      247      247           
Flag Coverage Δ
#unittest 94.37% <100.00%> (+<0.01%) ⬆️
Impacted Files Coverage Δ
...plugin/src/rules/prefer-string-starts-ends-with.ts 100.00% <100.00%> (ø)

@bradzacher bradzacher added the bug Something isn't working label Apr 20, 2020
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[prefer-string-starts-ends-with] Should only complain about .slice() with negative offset
2 participants