You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-within.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // For now lets enable it on this extension
  2. // once we have made the refactoring we can add it to sonar-web and all other extension.
  3. module.exports = {
  4. meta: {
  5. type: 'problem',
  6. docs: {
  7. description: 'Prevent unsing within from testing library',
  8. category: 'Best Practices',
  9. },
  10. messages: {
  11. noWithin: "Don't use within as it may hide DOM update. Prefer using chain selector.",
  12. },
  13. },
  14. // eslint-disable-next-line object-shorthand
  15. create: function (context) {
  16. const fnNames = [];
  17. const currentFilePath = context.getFilename();
  18. const sourceCode = context.sourceCode;
  19. return {
  20. // eslint-disable-next-line object-shorthand
  21. CallExpression: function (node) {
  22. if (node.callee.name === 'within') {
  23. let scope = sourceCode.getScope(node);
  24. while (node.callee.name && scope && scope.set.get(node.callee.name) === undefined) {
  25. scope = scope.upper;
  26. }
  27. if (node.callee.name && scope) {
  28. let variable = scope.set.get(node.callee.name);
  29. if (variable.defs[0] && variable.defs[0].parent.source) {
  30. const importPath = variable.defs[0].parent.source.value;
  31. if (importPath.startsWith('@testing-library')) {
  32. context.report({
  33. node: node.callee,
  34. messageId: 'noWithin',
  35. });
  36. }
  37. }
  38. }
  39. }
  40. },
  41. };
  42. },
  43. };