storybook/scripts/eslint-plugin-local-rules/storybook-monorepo-imports.js
2025-01-16 15:49:34 +01:00

57 lines
1.8 KiB
JavaScript

const path = require('path');
const cache = {};
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Ensure the imports-paths of packages in the monorepo is correct',
category: 'Best Practices',
recommended: true,
url: 'https://github.com/storybookjs/storybook/blob/next/code/core/README.md',
},
fixable: 'code',
},
create(context) {
return {
ImportDeclaration: (node) => {
const fileName = context.getPhysicalFilename();
const isInCLI = !!fileName.includes(path.join('code', 'lib', 'cli') + path.sep);
const isInCodemod = !!fileName.includes(path.join('code', 'lib', 'codemod'));
const isInCore = !!fileName.includes(path.join('code', 'core'));
if (
node.source.value.startsWith('@storybook/core/') &&
!isInCLI &&
!isInCore &&
!isInCodemod
) {
const newPath = node.source.value
.replace('@storybook/core', 'storybook/internal')
.replace('/src', '');
context.report({
node: node,
message: `Cannot import from @storybook/core in this package. Use storybook/internal instead.`,
fix: (fixer) => {
return fixer.replaceText(node.source, `'${newPath}'`);
},
});
}
if (node.source.value.startsWith('storybook/internal/') && isInCore) {
const newPath = node.source.value
.replace('storybook/internal', '@storybook/core')
.replace('/src', '');
context.report({
node: node,
message: `Cannot import from storybook/internal in this package. Use @storybook/core instead.`,
fix: (fixer) => {
return fixer.replaceText(node.source, `'${newPath}'`);
},
});
}
},
};
},
};