12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- // no-import-from-specific-folder.js
-
- module.exports = {
- meta: {
- type: 'problem',
- docs: {
- description: 'Warn against importing functions from a "api" folder',
- category: 'Best Practices',
- },
- messages: {
- noApiImports:
- 'Check if an existing react-query retrieves this data. Use it instead of importing the API function directly.',
- },
- },
- create: function (context) {
- const fnNames = [];
- const currentFilePath = context.getFilename();
-
- if (
- ['queries', 'mocks', '__tests__'].some((path) => currentFilePath.split('/').includes(path))
- ) {
- return {};
- }
-
- return {
- ImportDeclaration: function (node) {
- const importPath = node.source.value;
-
- if (importPath.split('/').includes('api')) {
- fnNames.push(...node.specifiers.map((specifier) => specifier.local.name));
- }
- },
- CallExpression: function (node) {
- if (fnNames.includes(node.callee.name)) {
- context.report({
- node: node.callee,
- messageId: 'noApiImports',
- });
- }
- },
- };
- },
- };
|