forked from jhipster/prettier-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-node-types.ts
More file actions
186 lines (160 loc) · 4.8 KB
/
generate-node-types.ts
File metadata and controls
186 lines (160 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { writeFileSync } from "node:fs";
import nodeTypeInfo from "tree-sitter-java-orchard/src/node-types.json" with { type: "json" };
writeFileSync(
"src/node-types.ts",
`interface Point {
index: number;
row: number;
column: number;
}
interface SyntaxNodeBase {
value: string;
start: Point;
end: Point;
comments?: CommentNode[];
}
interface NamedNodeBase extends SyntaxNodeBase {
isNamed: true;
fieldName: string | null;
children: SyntaxNode[];
namedChildren: NamedNode[];
}
export interface UnnamedNode<T extends UnnamedType = UnnamedType> extends SyntaxNodeBase {
type: T;
isNamed: false;
fieldName: string | null;
}
export interface CommentNode extends SyntaxNodeBase {
type: CommentType;
leading: boolean;
trailing: boolean;
printed: boolean;
enclosingNode?: SyntaxNode;
precedingNode?: SyntaxNode;
followingNode?: SyntaxNode;
}
type PickNamedType<Node, T extends SyntaxType> = Node extends { type: T; isNamed: true } ? Node : never;
export type NamedNode<T extends NamedType = NamedType> = PickNamedType<SyntaxNode, T>;
export const enum SyntaxType {
ERROR = "ERROR",
${nodeTypeInfo
.filter(({ named, subtypes }) => named && !subtypes?.length)
.map(
({ type }) =>
` ${getSyntaxKindFromString(type)} = ${JSON.stringify(type)},`
)
.join("\n")}
};
export type CommentType = SyntaxType.BlockComment | SyntaxType.LineComment;
export type NamedType = Exclude<SyntaxType, CommentType>;
export type UnnamedType = ${nodeTypeInfo
.filter(({ named }) => !named)
.map(({ type }) => JSON.stringify(type))
.join(" | ")};
export type TypeString = NamedType | UnnamedType;
export const multiFieldsByType: Partial<Record<string, Partial<Record<string, true>>>> = ${JSON.stringify(
nodeTypeInfo.reduce(
(acc, nodeInfo) => {
if ("fields" in nodeInfo && nodeInfo.fields) {
const multiFields = Object.entries(nodeInfo.fields)
.filter(([, { multiple }]) => multiple)
.reduce(
(fieldAcc, [name]) => {
fieldAcc[name] = true;
return fieldAcc;
},
{} as Record<string, boolean>
);
if (Object.keys(multiFields).length > 0) {
acc[nodeInfo.type] = multiFields;
}
}
return acc;
},
{} as Record<string, Record<string, boolean>>
)
)};
export type SyntaxNode = ErrorNode | ${nodeTypeInfo
.filter(({ type }) => !type.endsWith("_comment"))
.map(getTypeExprFromRef)
.join(" | ")};
export interface ErrorNode extends NamedNodeBase {
type: SyntaxType.ERROR;
}
${nodeTypeInfo
.filter(({ named }) => named)
.map(({ type, subtypes, fields }) =>
subtypes?.length
? `export type ${getTypeNameFromString(type)} = ${subtypes.map(getTypeExprFromRef).join(" | ")};`
: `export interface ${getTypeNameFromString(type)} extends NamedNodeBase {
type: SyntaxType.${getSyntaxKindFromString(type)};
${
fields && Object.keys(fields).length
? `${Object.entries(fields as unknown as Record<string, NodeTypeChildren>)
.map(([field, children]) => {
let fieldName = `${field}Node`;
let type = children.types.length
? children.types.map(t => getTypeExprFromRef(t)).join(" | ")
: "UnnamedNode";
if (children.multiple) {
if (children.types.length > 1) {
type = `(${type})`;
}
type += "[]";
fieldName += "s";
}
const opt = children.required || children.multiple ? "" : "?";
return ` ${fieldName}${opt}: ${type};`;
})
.join("\n")}
}`
: "}"
}`
)
.join("\n\n")}
`
);
interface NodeTypeRef {
type: string;
named: boolean;
isError?: boolean;
}
interface NodeTypeChildren {
multiple: boolean;
required: boolean;
types: NodeTypeRef[];
}
function isIdentifier(str: string) {
return /^[a-z$_][a-z0-9$_]*$/i.test(str);
}
function mangleNameToIdentifier(str: string) {
let sb = "$";
for (let i = 0; i < str.length; ++i) {
const char = str.charAt(i);
if (/[a-z0-9_]/i.test(char)) {
sb += char;
} else {
sb += "$" + str.charCodeAt(i) + "$";
}
}
return sb;
}
function toCapitalCase(str: string) {
return str
.replace(/^[a-z]/, t => t.toUpperCase())
.replace(/_[a-zA-Z]/g, t => t.substring(1).toUpperCase());
}
function getTypePrefixFromString(str: string) {
return isIdentifier(str) ? toCapitalCase(str) : mangleNameToIdentifier(str);
}
function getTypeNameFromString(str: string) {
return getTypePrefixFromString(str) + "Node";
}
function getSyntaxKindFromString(str: string) {
return getTypePrefixFromString(str);
}
function getTypeExprFromRef({ type, named }: { type: string; named: boolean }) {
return named
? getTypeNameFromString(type)
: `UnnamedNode<${JSON.stringify(type)}>`;
}