Skip to content

Bug: MemberExpression.object should be Expression, not LeftHandSideExpression #6192

Closed
@chris-grabcad

Description

@chris-grabcad

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Relevant Package

typescript-estree

Playground Link

No response

Repro Code

function isExpressionShould(node: TSESTree.MemberExpression): boolean {
    const object = node.object;
    const property = node.property;

    return (
        object?.type === AST_NODE_TYPES.BinaryExpression &&
        isIdentifier(property) &&
        property.name === "should"
    );
}

ESLint Config

N/A

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

I expect my code to compile without errors

Actual Result

error TS2367: This condition will always return 'false' since the types 'AST_NODE_TYPES.ArrayExpression | AST_NODE_TYPES.ArrayPattern | AST_NODE_TYPES.ArrowFunctionExpression | AST_NODE_TYPES.CallExpression | AST_NODE_TYPES.ClassExpression | AST_NODE_TYPES.FunctionExpression | ... 15 more ... | AST_NODE_TYPES.TSTypeAssertion' and 'AST_NODE_TYPES.BinaryExpression' have no overlap.

57 object?.type === AST_NODE_TYPES.BinaryExpression &&
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Found 1 error.

Additional Info

I am working on a repo which uses node 14.16.0 and cannto upgrade to node 14.17.0 (yet) because it is used in an electron 12 project.

I'm trying to write a lint rule to catch sillinesses like should(a === b).be.true() etc, and another way it can be written is (a === b).should.be.true(), however I cannot get this check to compile which is a problem (I'll probably have to patch the types in my repo to get it working for now)

I checked this code with ast explorer

(apple === banana).should.be.true();

and I get this
image

I've also caught it in a vscode debugger with the values

image

image

Versions

package version
@typescript-eslint/eslint-plugin 4.33.0
@typescript-eslint/parser 4.31.2
@typescript-eslint/scope-manager 4.33.0
@typescript-eslint/typescript-estree 4.31.2`
@typescript-eslint/type-utils N/A ???
@typescript-eslint/visitor-keys 4.31.2
@typescript-eslint/types 4.31.2
TypeScript 4.2.3
ESLint 7.26.0
node 14.16.0

Metadata

Metadata

Labels

accepting prsGo ahead, send a pull request that resolves this issuebreaking changeThis change will require a new major version to be releasedbugSomething isn't workingpackage: ast-specIssues related to @typescript-eslint/ast-spec

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions