eslint-plugin-almatvit-plugin
Version:
ESLint plugin for enforcing relative imports inside feature slices.
99 lines (86 loc) • 2.98 kB
JavaScript
const { isPathRelative } = require("../helpers");
const micromatch = require("micromatch");
const path = require("path");
const PUBLIC_ERROR = 'PUBLIC_ERROR';
const TESTING_PUBLIC_ERROR = 'TESTING_PUBLIC_ERROR';
module.exports = {
meta: {
type: null, // `problem`, `suggestion`, or `layout`
docs: {
description: "descr",
category: "Fill me in",
recommended: false,
url: null, // URL to the documentation page for this rule
},
fixable: 'code', // Or `code` or `whitespace`
messages: {
[PUBLIC_ERROR]: 'Абсолютный импорт разрешен только из Public API (index.ts)',
[TESTING_PUBLIC_ERROR]: 'Тестовые данные необходимо импортировать из publicApi/testing.ts',
},
schema: [
{
type: 'object',
properties: {
alias: {
type: 'string'
},
testFilesPatterns: {
type: 'array'
}
}
}
],
},
create(context) {
const { alias = '', testFilesPatterns = [] } = context.options[0] ?? {};
const checkingLayers = {
'entities': 'entities',
'features': 'features',
'pages': 'pages',
'widgets': 'widgets',
}
return {
ImportDeclaration(node) {
const value = node.source.value
const importTo = alias ? value.replace(`${alias}/`, '') : value;
if (isPathRelative(importTo)) {
return;
}
// [entities, article, model, types]
const segments = importTo.split('/')
const layer = segments[0];
const slice = segments[1];
if (!checkingLayers[layer]) {
return;
}
const isImportNotFromPublicApi = segments.length > 2;
// [entities, article, testing]
const isTestingPublicApi = segments[2] === 'testing' && segments.length < 4
if (isImportNotFromPublicApi && !isTestingPublicApi) {
// context.report(node, 'Абсолютный импорт разрешен только из Public API (index.ts)');
context.report({
node,
messageId: PUBLIC_ERROR,
fix: (fixer) => {
return fixer.replaceText(node.source, `'${alias}/${layer}/${slice}'`)
}
});
}
if (isTestingPublicApi) {
const currentFilePath = context.getFilename();
const normalizedPath = path.toNamespacedPath(currentFilePath);
const isCurrentFileTesting = testFilesPatterns.some(
pattern => micromatch.isMatch(normalizedPath, pattern)
)
if (!isCurrentFileTesting) {
// context.report(node, 'Тестовые данные необходимо импортировать из publicApi/testing.ts');
context.report({
node,
messageId: TESTING_PUBLIC_ERROR,
});
}
}
}
};
},
};