@rightcapital/php-parser
Version:
TypeScript types for PHP Parser JSON representation
88 lines (87 loc) • 3.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilePathHelpers = void 0;
const node_path_1 = require("node:path");
const _ = require("lodash");
class FilePathHelpers {
static extendNameNodePartsByUses(nodeParts, uses) {
const [firstNode, ...otherParts] = nodeParts;
if (firstNode in uses) {
return [...uses[firstNode], ...otherParts];
}
return nodeParts;
}
static getFilePathFromNameNodeParts(nodeParts, parentNodeType, filePathParts, uses) {
const fullQualifiedNodeParts = parentNodeType === 'Name'
? FilePathHelpers.extendNameNodePartsByUses(nodeParts, uses)
: nodeParts;
const phpTypeRootDirectory = FilePathHelpers.getRelativePathToTypesRootPathByPhpFileParts(filePathParts);
const nodeTypeRootDirectory = (0, node_path_1.join)(phpTypeRootDirectory, 'node');
const nodeInterfaceFilePath = (0, node_path_1.join)(phpTypeRootDirectory, 'node');
if (fullQualifiedNodeParts.length === 1) {
return `./${_.kebabCase(fullQualifiedNodeParts[0])}`;
}
if (fullQualifiedNodeParts.length > 1) {
const [firstNode, secondNode, ...otherParts] = fullQualifiedNodeParts;
if (firstNode === 'PhpParser' && secondNode === 'NodeAbstract') {
return nodeInterfaceFilePath;
}
if (firstNode === 'PhpParser' && secondNode === 'Node') {
return `${nodeTypeRootDirectory}/${otherParts
.map((part) => _.kebabCase(part))
.join('/')}`;
}
return `./${fullQualifiedNodeParts
.map((part) => _.kebabCase(part))
.join('/')}`;
}
throw new Error('Logic error, all nodes should have parent node parts');
}
static getRelativePathBasePhpParserAutoloadRoot(fileParts) {
const libPathIndex = fileParts.findIndex((filePart, index) => {
if (filePart === 'lib' && fileParts[index + 1] === 'PhpParser') {
return true;
}
return false;
});
if (libPathIndex === -1) {
throw new Error('Invalid Arguments, it should contains lib and PhpParser parts');
}
return fileParts.slice(libPathIndex + 1);
}
static getRelativePathToTypesRootPathByPhpFileParts(fullFileParts) {
const filePartsFromPhpParser = FilePathHelpers.getRelativePathBasePhpParserAutoloadRoot(fullFileParts);
if (filePartsFromPhpParser.length === 2) {
return './';
}
if (filePartsFromPhpParser.length > 2) {
return '../'.repeat(filePartsFromPhpParser.length - 2);
}
throw Error('fileParts must contains at least two items: PhpParser and Node');
}
static getTypesRootPath() {
return (0, node_path_1.resolve)(__dirname, '..', 'types');
}
static getFullyQualifiedNodeNameByFilePath(filePath) {
return `FullyQualified${filePath
.replace(/\.php$/, '')
.split('/')
.map((part) => _.camelCase(part))
.map((part) => _.upperFirst(part))
.join('')}`;
}
static getFullyQualifiedParentNodeNameByFilePath(fileRelativePath, parentNodeFilePath, parentNodeName) {
if (parentNodeName === 'NodeAbstract') {
return 'NodeAbstract';
}
let relativeParentNodeFilePath;
if (parentNodeFilePath.startsWith('..')) {
relativeParentNodeFilePath = (0, node_path_1.join)(fileRelativePath, parentNodeFilePath).replace(/^node\//, '');
}
else {
relativeParentNodeFilePath = (0, node_path_1.join)((0, node_path_1.dirname)(fileRelativePath), parentNodeFilePath);
}
return FilePathHelpers.getFullyQualifiedNodeNameByFilePath(relativeParentNodeFilePath);
}
}
exports.FilePathHelpers = FilePathHelpers;