mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Frontend: Improve barrel file detection in codemod (#87389)
This commit is contained in:
parent
acf17c7fb1
commit
6380a01543
@ -1,27 +1,24 @@
|
|||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
const isBareSpecifier = (importPath) => !importPath.startsWith('app/') && /^[^./]/.test(importPath);
|
||||||
|
const barrelFileNames = ['index.ts', 'index.tsx', 'index.js', 'index.jsx'];
|
||||||
|
|
||||||
|
const resolvePath = (fileDir, importPath) =>
|
||||||
|
importPath.startsWith('app/')
|
||||||
|
? require.resolve(path.join(__dirname, '..', '..', 'public', importPath))
|
||||||
|
: require.resolve(path.resolve(fileDir, importPath));
|
||||||
|
|
||||||
module.exports = function (fileInfo, api) {
|
module.exports = function (fileInfo, api) {
|
||||||
const j = api.jscodeshift;
|
const j = api.jscodeshift;
|
||||||
const root = j.withParser('tsx')(fileInfo.source);
|
const root = j.withParser('tsx')(fileInfo.source);
|
||||||
const fileDir = path.dirname(fileInfo.path);
|
const fileDir = path.dirname(fileInfo.path);
|
||||||
|
// Update import declarations that import from barrel files
|
||||||
// Function to check if the path potentially points to a barrel file
|
|
||||||
const mightBeBarrelFileImport = (importPath) => {
|
|
||||||
const fullPath = path.join(fileDir, importPath);
|
|
||||||
if (fs.existsSync(fullPath) && fs.lstatSync(fullPath).isDirectory()) {
|
|
||||||
if (fs.existsSync(path.join(fullPath, 'index.ts')) || fs.existsSync(path.join(fullPath, 'index.js'))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Udpate import declarations that import from barrel files
|
|
||||||
root
|
root
|
||||||
.find(j.ImportDeclaration)
|
.find(j.ImportDeclaration)
|
||||||
.filter((path) => mightBeBarrelFileImport(path.node.source.value))
|
.filter((path) => !isBareSpecifier(path.node.source.value))
|
||||||
.forEach((path) => {
|
.forEach((path) => {
|
||||||
|
const resolvedPath = resolvePath(fileDir, path.node.source.value);
|
||||||
|
if (barrelFileNames.some((barrelFileName) => resolvedPath.endsWith(barrelFileName))) {
|
||||||
// Create a comment node
|
// Create a comment node
|
||||||
const comment = j.commentLine(' @todo: replace barrel import path');
|
const comment = j.commentLine(' @todo: replace barrel import path');
|
||||||
|
|
||||||
@ -33,7 +30,10 @@ module.exports = function (fileInfo, api) {
|
|||||||
|
|
||||||
// Update the import path appending '/index'
|
// Update the import path appending '/index'
|
||||||
path.node.source.value = path.node.source.value + '/index';
|
path.node.source.value = path.node.source.value + '/index';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return root.toSource();
|
return root.toSource({
|
||||||
|
quote: 'single',
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user