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-api-imports.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // no-import-from-specific-folder.js
  2. module.exports = {
  3. meta: {
  4. type: 'problem',
  5. docs: {
  6. description: 'Warn against importing functions from a "api" folder',
  7. category: 'Best Practices',
  8. },
  9. messages: {
  10. noApiImports:
  11. 'Check if an existing react-query retrieves this data. Use it instead of importing the API function directly.',
  12. },
  13. },
  14. create: function (context) {
  15. const fnNames = [];
  16. const currentFilePath = context.getFilename();
  17. if (
  18. ['queries', 'mocks', '__tests__'].some((path) => currentFilePath.split('/').includes(path))
  19. ) {
  20. return {};
  21. }
  22. return {
  23. ImportDeclaration: function (node) {
  24. const importPath = node.source.value;
  25. if (importPath.split('/').includes('api')) {
  26. fnNames.push(...node.specifiers.map((specifier) => specifier.local.name));
  27. }
  28. },
  29. CallExpression: function (node) {
  30. if (fnNames.includes(node.callee.name)) {
  31. context.report({
  32. node: node.callee,
  33. messageId: 'noApiImports',
  34. });
  35. }
  36. },
  37. };
  38. },
  39. };