Skip to content

Commit

Permalink
Fix prefer-includes fixer omitting parentheses (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot authored and sindresorhus committed May 22, 2019
1 parent a7b1bdb commit e36d25c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 5 additions & 6 deletions rules/prefer-includes.js
Expand Up @@ -12,13 +12,12 @@ const isIndexOf = node => {

const isNegativeOne = (operator, value) => operator === '-' && value === 1;

const getSourceCode = (context, node) => (
context.getSourceCode().getText(node)
);

const report = (context, node, target, pattern) => {
const targetSource = getSourceCode(context, target);
const patternSource = getSourceCode(context, pattern);
const sourceCode = context.getSourceCode();
const memberExpressionNode = target.parent;
const dotToken = sourceCode.getTokenBefore(memberExpressionNode.property);
const targetSource = sourceCode.getText().slice(memberExpressionNode.range[0], dotToken.range[0]);
const patternSource = sourceCode.getText(pattern);
context.report({
node,
message: 'Use `.includes()`, rather than `.indexOf()`, when checking for existence.',
Expand Down
5 changes: 5 additions & 0 deletions test/prefer-includes.js
Expand Up @@ -60,6 +60,11 @@ ruleTester.run('prefer-includes', rule, {
code: '\'\'.indexOf(\'foo\') < 0',
output: '!\'\'.includes(\'foo\')',
errors
},
{
code: '(a || b).indexOf(\'foo\') === -1',
output: '!(a || b).includes(\'foo\')',
errors
}
]
});

0 comments on commit e36d25c

Please sign in to comment.