no-force.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict'
  2. //------------------------------------------------------------------------------
  3. // Rule Definition
  4. //------------------------------------------------------------------------------
  5. module.exports = {
  6. meta: {
  7. docs: {
  8. description: 'Disallow using of \'force: true\' option for click and type calls',
  9. category: 'Possible Errors',
  10. recommended: false,
  11. },
  12. fixable: null, // or "code" or "whitespace"
  13. schema: [],
  14. messages: {
  15. unexpected: 'Do not use force on click and type calls',
  16. },
  17. },
  18. create (context) {
  19. // variables should be defined here
  20. //----------------------------------------------------------------------
  21. // Helpers
  22. //----------------------------------------------------------------------
  23. function isCallingClickOrType (node) {
  24. const allowedMethods = ['click', 'dblclick', 'type', 'trigger', 'check', 'rightclick', 'focus', 'select']
  25. return node.property && node.property.type === 'Identifier' &&
  26. allowedMethods.includes(node.property.name)
  27. }
  28. function isCypressCall (node) {
  29. return node.callee.type === 'MemberExpression' &&
  30. node.callee.object.type === 'Identifier' &&
  31. node.callee.object.name === 'cy'
  32. }
  33. function hasOptionForce (node) {
  34. return node.arguments && node.arguments.length &&
  35. node.arguments.some((arg) => {
  36. return arg.type === 'ObjectExpression' && arg.properties.some((propNode) => propNode.key && propNode.key.name === 'force')
  37. })
  38. }
  39. function deepCheck (node, checkFunc) {
  40. let currentNode = node
  41. while (currentNode.parent) {
  42. if (checkFunc(currentNode.parent)) {
  43. return true
  44. }
  45. currentNode = currentNode.parent
  46. }
  47. return false
  48. }
  49. //----------------------------------------------------------------------
  50. // Public
  51. //----------------------------------------------------------------------
  52. return {
  53. CallExpression (node) {
  54. if (isCypressCall(node) && deepCheck(node, isCallingClickOrType) && deepCheck(node, hasOptionForce)) {
  55. context.report({ node, messageId: 'unexpected' })
  56. }
  57. },
  58. }
  59. },
  60. }