fix: syntax changes
This commit is contained in:
parent
c21d3ff504
commit
b4e1a99add
|
|
@ -1,24 +1,27 @@
|
|||
const path = require('path');
|
||||
const namespaces = require('./namespaces')
|
||||
const parser = require('./parser')
|
||||
const namespaces = require('./namespaces');
|
||||
const parser = require('./parser');
|
||||
|
||||
const getDependencies = async (entryPoint, projectDir) => {
|
||||
const phpNamespaces = await namespaces.parse(projectDir)
|
||||
const phpNamespaces = await namespaces.parse(projectDir);
|
||||
|
||||
const dependencies = {
|
||||
[entryPoint]: phpNamespaces[entryPoint]
|
||||
};
|
||||
|
||||
if (phpNamespaces[entryPoint]) {
|
||||
phpNamespaces[entryPoint].importedModuleNames.forEach(moduleName => {
|
||||
Object.keys(phpNamespaces).forEach(itemPath => {
|
||||
if (phpNamespaces[itemPath].moduleName !== moduleName) {
|
||||
return;
|
||||
}
|
||||
|
||||
dependencies[itemPath] = phpNamespaces[itemPath];
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
phpNamespaces[entryPoint]?.importedModuleNames.forEach((moduleName) => {
|
||||
Object.keys(phpNamespaces).forEach(itemPath => {
|
||||
if (phpNamespaces[itemPath].moduleName !== moduleName) {
|
||||
return
|
||||
}
|
||||
|
||||
dependencies[itemPath] = phpNamespaces[itemPath]
|
||||
})
|
||||
})
|
||||
return dependencies
|
||||
return dependencies;
|
||||
};
|
||||
|
||||
const getImports = (fileCode, itemPath) => {
|
||||
|
|
@ -26,41 +29,39 @@ const getImports = (fileCode, itemPath) => {
|
|||
const parsed = parser.parseCode(fileCode, path.basename(itemPath));
|
||||
const dependencies = [];
|
||||
|
||||
const findSourceFile = (phpNamespace) => {
|
||||
const findSourceFile = phpNamespace => {
|
||||
return Object.keys(phpNamespaces).find(key => {
|
||||
return phpNamespaces[key].moduleName === phpNamespace
|
||||
}
|
||||
) ?? null
|
||||
}
|
||||
return phpNamespaces[key].moduleName === phpNamespace;
|
||||
});
|
||||
};
|
||||
|
||||
parsed.children?.forEach(parsedChild => {
|
||||
parsedChild.children?.forEach(c => {
|
||||
(parsed.children || []).forEach(parsedChild => {
|
||||
(parsedChild.children || []).forEach(c => {
|
||||
if (c.kind !== 'usegroup') {
|
||||
return
|
||||
return;
|
||||
}
|
||||
const dependence = {
|
||||
importNodeLines: [
|
||||
c.loc.start.line,
|
||||
c.loc.end.line,
|
||||
],
|
||||
importNodeLines: [c.loc.start.line, c.loc.end.line],
|
||||
sourceFile: null,
|
||||
specifiers: [],
|
||||
}
|
||||
specifiers: []
|
||||
};
|
||||
c.items.forEach(item => {
|
||||
dependence.sourceFile = findSourceFile(item.name)
|
||||
const importSpecifierName = dependence?.sourceFile?.split('/').pop()
|
||||
dependence.sourceFile = findSourceFile(item.name);
|
||||
const importSpecifierName =
|
||||
dependence && dependence.sourceFile && dependence.sourceFile.split('/').pop();
|
||||
|
||||
if (importSpecifierName) {
|
||||
dependence.specifiers.push({
|
||||
type: 'ImportSpecifier',
|
||||
name: importSpecifierName
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
dependencies.push(dependence)
|
||||
})
|
||||
})
|
||||
return dependencies.filter(({sourceFile}) => !!sourceFile)
|
||||
}
|
||||
});
|
||||
dependencies.push(dependence);
|
||||
});
|
||||
});
|
||||
return dependencies.filter(({ sourceFile }) => !!sourceFile);
|
||||
};
|
||||
|
||||
// replace with own implementation if needed
|
||||
module.exports = {
|
||||
|
|
|
|||
|
|
@ -1,55 +1,54 @@
|
|||
const readdirRecursive = require('fs-readdir-recursive')
|
||||
const file = require('../../../utils/file')
|
||||
const readdirRecursive = require('fs-readdir-recursive');
|
||||
const file = require('../../../utils/file');
|
||||
const extensions = require('./extensions');
|
||||
const path = require('path');
|
||||
const parser = require('./parser')
|
||||
const parser = require('./parser');
|
||||
|
||||
let cache = {}
|
||||
let cache = {};
|
||||
|
||||
const addNamespaces = (namespaces, itemPath, parsed) =>
|
||||
parsed.children
|
||||
?.filter(parsedChild => parsedChild.name)
|
||||
.forEach(parsedChild => {
|
||||
const namespace = {
|
||||
moduleName: parsedChild.name,
|
||||
importedModuleNames: []
|
||||
}
|
||||
parsedChild.children?.forEach(c => {
|
||||
switch (c.kind) {
|
||||
(parsed.children || []).filter(parsedChild => parsedChild.name).forEach(parsedChild => {
|
||||
const namespace = {
|
||||
moduleName: parsedChild.name,
|
||||
importedModuleNames: []
|
||||
};
|
||||
|
||||
(parsedChild.children || []).forEach(c => {
|
||||
switch (c.kind) {
|
||||
case 'usegroup':
|
||||
c.items.forEach(item => {
|
||||
namespace.importedModuleNames.push(item.name)
|
||||
})
|
||||
break
|
||||
namespace.importedModuleNames.push(item.name);
|
||||
});
|
||||
break;
|
||||
case 'class':
|
||||
case 'trait':
|
||||
case 'interface':
|
||||
namespace.moduleName = `${parsedChild.name}\\${c.name.name}`
|
||||
}
|
||||
})
|
||||
namespace.moduleName = `${parsedChild.name}\\${c.name.name}`;
|
||||
}
|
||||
});
|
||||
|
||||
namespaces[itemPath] = namespace
|
||||
})
|
||||
namespaces[itemPath] = namespace;
|
||||
});
|
||||
|
||||
const parse = async (projectDir) => {
|
||||
const namespaces = {}
|
||||
const parse = async projectDir => {
|
||||
const namespaces = {};
|
||||
const tasks = readdirRecursive(projectDir)
|
||||
.filter(f => extensions.test('.' + f.split('.').pop()))
|
||||
.map(async f => {
|
||||
const separator = path.sep;
|
||||
const itemPath = `${projectDir}${separator}${f.replace(/\//g, separator)}`
|
||||
const fileCode = await file.read(itemPath, 'utf8')
|
||||
addNamespaces(namespaces, itemPath, parser.parseCode(fileCode, path.basename(f)))
|
||||
const itemPath = `${projectDir}${separator}${f.replace(/\//g, separator)}`;
|
||||
const fileCode = await file.read(itemPath, 'utf8');
|
||||
addNamespaces(namespaces, itemPath, parser.parseCode(fileCode, path.basename(f)));
|
||||
});
|
||||
|
||||
await Promise.all(tasks)
|
||||
cache = namespaces
|
||||
await Promise.all(tasks);
|
||||
cache = namespaces;
|
||||
|
||||
return namespaces
|
||||
}
|
||||
const getCache = () => cache
|
||||
return namespaces;
|
||||
};
|
||||
const getCache = () => cache;
|
||||
|
||||
module.exports = {
|
||||
parse,
|
||||
getCache
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue