Skip to content

Commit

Permalink
feat: generate NodeType as enum and mapping to interface for better t…
Browse files Browse the repository at this point in the history
…ype hints
  • Loading branch information
rainx committed Sep 14, 2023
1 parent 7c693cd commit 504d169
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
FullyQualifiedScalarString,
FullyQualifiedStmtEcho,
NodeRetrieverHelpers,
NodeType,
NodeTypeInheritingFromNodeAbstract,
} from '@rightcapital/php-parser';

Expand All @@ -68,7 +69,7 @@ console.log(rootNodes);
const echoNode =
NodeRetrieverHelpers.findNodeByNodeType<FullyQualifiedStmtEcho>(
rootNodes,
'Stmt_Echo',
NodeType.Stmt_Echo,
);

// Get the specified node with type annotation
Expand All @@ -84,7 +85,7 @@ console.log(echoNode);
const scalarStringNode =
NodeRetrieverHelpers.findNodeByNodeType<FullyQualifiedScalarString>(
echoNode!.exprs,
'Scalar_String',
NodeType.Scalar_String,
);
console.log(scalarStringNode?.value);
// Hello
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat: generate NodeType as enum and mapping to interface for better type hints",
"packageName": "@rightcapital/php-parser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
22 changes: 14 additions & 8 deletions src/php-parser/helpers/node-retriever-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import type { Namespace_ } from '../types/node/stmt/namespace';
import type { Property } from '../types/node/stmt/property';
import type { Return_ } from '../types/node/stmt/return';
import type { Use_ } from '../types/node/stmt/use';
import type { NodeTypeInheritingFromNodeAbstract } from '../types/types';
import {
NodeType,
type NodeTypeInheritingFromNodeAbstract,
} from '../types/types';

export type IUseFullQualifiedNameParts = string[];
export type IUses = {
Expand Down Expand Up @@ -37,11 +40,14 @@ export class NodeRetrieverHelpers {
): Class_ {
const namespaceNode = NodeRetrieverHelpers.findNodeByNodeType<Namespace_>(
rootNode as INode[],
'Stmt_Namespace',
NodeType.Stmt_Namespace,
);

if (namespaceNode?.stmts) {
return this.findNodeByNodeType<Class_>(namespaceNode.stmts, 'Stmt_Class');
return this.findNodeByNodeType<Class_>(
namespaceNode.stmts,
NodeType.Stmt_Class,
);
}

return undefined;
Expand All @@ -56,7 +62,7 @@ export class NodeRetrieverHelpers {
if (classNode.stmts) {
return this.filterNodeByNodeType<Property>(
classNode.stmts,
'Stmt_Property',
NodeType.Stmt_Property,
)
.filter(
/**
Expand Down Expand Up @@ -113,12 +119,12 @@ export class NodeRetrieverHelpers {
): IUses {
const namespaceNode = NodeRetrieverHelpers.findNodeByNodeType<Namespace_>(
rootNode,
'Stmt_Namespace',
NodeType.Stmt_Namespace,
);

if (namespaceNode?.stmts) {
return Object.fromEntries(
this.filterNodeByNodeType<Use_>(namespaceNode.stmts, 'Stmt_Use')
this.filterNodeByNodeType<Use_>(namespaceNode.stmts, NodeType.Stmt_Use)
.map((useNode) => useNode.uses[0].name.parts)
.map((parts) => [parts.slice(-1), parts]),
) as IUses;
Expand All @@ -133,7 +139,7 @@ export class NodeRetrieverHelpers {
if (classNode.stmts) {
const classMethodNodes = this.filterNodeByNodeType<ClassMethod>(
classNode.stmts,
'Stmt_ClassMethod',
NodeType.Stmt_ClassMethod,
);

const getTypeMethodNode = classMethodNodes.find(
Expand All @@ -143,7 +149,7 @@ export class NodeRetrieverHelpers {
if (getTypeMethodNode) {
const returnNode = this.findNodeByNodeType<Return_>(
getTypeMethodNode.stmts,
'Stmt_Return',
NodeType.Stmt_Return,
);

if (returnNode) {
Expand Down
37 changes: 36 additions & 1 deletion src/php-parser/helpers/type-generation-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path';
import type { ICliContext } from '../generate-types';
import type { ICliContext, IContextNodeItem } from '../generate-types';

export class TypeGenerationHelpers {
public static getGroupedTypeNameForNode(nodeName: string) {
Expand Down Expand Up @@ -48,6 +48,41 @@ ${importNodeNamesPart}
${combinationTypesPart}
${exportNodeNamesPart}
${TypeGenerationHelpers.generateNodeTypeEnum(cliContext)}
`;
}

public static generateNodeTypeEnum(cliContext: ICliContext): string {
const { allNodes } = cliContext;

const nodesThatHasNodeType: [string, IContextNodeItem][] = Object.values(
allNodes,
)
.filter((node) => node.nodeType !== undefined && node.nodeType !== '')
.map((node) => [node.nodeType, node]);
const nodeEnum = `export enum NodeType {
${nodesThatHasNodeType
.map(([nodeType, _]) => {
return ` ${nodeType} = '${nodeType}',`;
})
.join('\n')}
}
`;

const nodeTypeToInterfaceMap = `
export interface NodeTypeToInterfaceMap {
${nodesThatHasNodeType
.map(([nodeType, node]) => {
return ` [NodeType.${nodeType}]: ${node.name};`;
})
.join('\n')}
}
`;

return `
${nodeEnum};
${nodeTypeToInterfaceMap}
`;
}
}
Loading

0 comments on commit 504d169

Please sign in to comment.