not-function.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const not = require('./negation.js');
  3. // AST Types:
  4. // https://github.com/eslint/espree/blob/master/lib/ast-node-types.js#L18
  5. // Only types possible to be `argument` are listed
  6. const impossibleNodeTypes = [
  7. 'ArrayExpression',
  8. 'BinaryExpression',
  9. 'ClassExpression',
  10. 'Literal',
  11. 'ObjectExpression',
  12. 'TemplateLiteral',
  13. 'UnaryExpression',
  14. 'UpdateExpression',
  15. ];
  16. // Technically these nodes could be a function, but most likely not
  17. const mostLikelyNotNodeTypes = [
  18. 'AssignmentExpression',
  19. 'AwaitExpression',
  20. 'LogicalExpression',
  21. 'NewExpression',
  22. 'TaggedTemplateExpression',
  23. 'ThisExpression',
  24. ];
  25. const notFunctionSelector = node => not([
  26. [...impossibleNodeTypes, ...mostLikelyNotNodeTypes].map(type => `[${node}.type="${type}"]`),
  27. `[${node}.type="Identifier"][${node}.name="undefined"]`,
  28. [
  29. `[${node}.type="CallExpression"]`,
  30. not([
  31. `[${node}.callee.type="MemberExpression"]`,
  32. `[${node}.callee.optional!=true]`,
  33. `[${node}.callee.computed!=true]`,
  34. `[${node}.callee.property.type="Identifier"]`,
  35. `[${node}.callee.property.name="bind"]`,
  36. ].join('')),
  37. ].join(''),
  38. ]);
  39. module.exports = {
  40. notFunctionSelector,
  41. };