Skip to content

[prefer-includes] TypeError: Cannot read property 'name' of undefined #44

@mjbcopland

Description

@mjbcopland

package.json

{
  "devDependencies": {
    "eslint": "7.23.0",
    "eslint-plugin-more": "1.0.2"
  }
}

.eslintrc.json

{
  "plugins": ["more"],
  "rules": { "more/prefer-includes": "error" }
}

index.js

foo() + "bar";

Output

$ eslint index.js

Oops! Something went wrong! :(

ESLint: 7.23.0

TypeError: Cannot read property 'name' of undefined
Occurred while linting index.js:1
    at BinaryExpression (node_modules/eslint-plugin-more/lib/rules/prefer-includes.js:15:42)
    ...

Resolution

Possibly just need a more thorough check of the property chain.

diff --git a/lib/rules/prefer-includes.js b/lib/rules/prefer-includes.js
index 7711a22..daecf0c 100644
--- a/lib/rules/prefer-includes.js
+++ b/lib/rules/prefer-includes.js
@@ -12,6 +12,7 @@ module.exports = function (context) {
             const moreOrEqualThanZero = operator === '>=' && right.value === 0;
             const isIndexOfEqualToMinusOne = left
                 && left.callee
+                && left.callee.property
                 && (left.callee.property.name === 'indexOf')
                 && (compareWithMinusOne || lessThanZero || moreOrEqualThanZero);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions