Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-includes] don't auto fix when test meth…
Browse files Browse the repository at this point in the history
…od's argument type doesn't have an 'includes' method (#2391)
  • Loading branch information
sudalqueen committed Aug 13, 2020
1 parent 8318c36 commit 71c4c72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-includes.ts
Expand Up @@ -4,7 +4,12 @@ import {
} from '@typescript-eslint/experimental-utils';
import { AST as RegExpAST, parseRegExpLiteral } from 'regexpp';
import * as ts from 'typescript';
import { createRule, getParserServices, getStaticValue } from '../util';
import {
createRule,
getParserServices,
getStaticValue,
getConstrainedTypeAtLocation,
} from '../util';

export default createRule({
name: 'prefer-includes',
Expand Down Expand Up @@ -191,6 +196,18 @@ export default createRule({
return;
}

//check the argument type of test methods
const argument = callNode.arguments[0];
const tsNode = services.esTreeNodeToTSNodeMap.get(argument);
const type = getConstrainedTypeAtLocation(types, tsNode);

const includesMethodDecl = type
.getProperty('includes')
?.getDeclarations();
if (includesMethodDecl == null) {
return;
}

context.report({
node: callNode,
messageId: 'preferStringIncludes',
Expand Down
6 changes: 6 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-includes.test.ts
Expand Up @@ -115,6 +115,12 @@ ruleTester.run('prefer-includes', rule, {
something.test(a)
}
`,
`
const pattern = new RegExp("bar")
function f(a) {
return pattern.test(a)
}
`,
]),
invalid: addOptional([
// positive
Expand Down

0 comments on commit 71c4c72

Please sign in to comment.