Search code examples
eslintabstract-syntax-tree

Can't get the right AST selector for history.push()


I'm migrating a codebase from react-router v5 to v6, and I'd like to add an eslint rule forbidding history.push calls with a no-restricted-syntax rule.

Using the AST explorer with history.push(); as the input, it seems to me that the history and push Identifier nodes are adjacent siblings, so I should be able to catch the call with selector: 'Identifier[name="history"] + Identifier[name="push"]', but as you can check on ESLint Playground it's not working.


Solution

  • The following should work for history.push():

    /* eslint no-restricted-syntax: ['error', 'CallExpression[callee.object.name="history"][callee.property.name="push"]'] */
    history.push();