forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-buffer-to-ast.js
242 lines (227 loc) · 8.42 KB
/
generate-buffer-to-ast.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { writeFile } from 'node:fs/promises';
import { AST_NODES, astNodeNamesWithFieldOrder } from './ast-types.js';
import { firstLetterLowercase, lintFile } from './helpers.js';
const bufferToJsAstFile = new URL('../src/utils/bufferToAst.ts', import.meta.url);
const jsConverters = astNodeNamesWithFieldOrder.map(
({ name, inlinedVariableField, reservedFields, allFields }) => {
const node = getNode(name);
const readStringArgument = allFields.some(([, fieldType]) =>
['Node', 'OptionalNode', 'NodeList', 'String', 'FixedString', 'OptionalString'].includes(
fieldType
)
)
? ', readString'
: '';
/** @type {string[]} */
const definitions = [];
if (node.flags) {
definitions.push(
'const flags = buffer[position++];\n',
...node.flags.map(
(name, index) =>
`const ${node.variableNames?.[name] || name} = (flags & ${1 << index}) === ${
1 << index
};`
)
);
}
for (const [index, field] of reservedFields.entries()) {
definitions.push(
`${getFieldDefinition(field, name, false, index === allFields.length - 1)}\n`
);
}
if (inlinedVariableField) {
definitions.push(`${getFieldDefinition(inlinedVariableField, name, true, true)}\n`);
}
/** @type {string[]} */
const properties = [
...(node.flags || []).map(name => {
const alternativeVariableName = node.variableNames?.[name];
return alternativeVariableName ? `${name}: ${alternativeVariableName}` : name;
}),
...allFields
.filter(([fieldName]) => !node.hiddenFields?.includes(fieldName))
.map(field => getFieldProperty(field, node)),
...getFixedProperties(node),
...Object.entries(node.additionalFields || []).map(([key, value]) => `${key}: ${value}`)
];
return `function ${firstLetterLowercase(
name
)} (position, buffer${readStringArgument}): ${name}Node {
const start = buffer[position++];
const end = buffer[position++];
${definitions.join('')}return {
type: '${node.astType || name}',
start,
end,
${properties.join(',\n')}
};
}`;
}
);
/**
* @param {string} name
* @return {import("./ast-types.js").NodeDescription}
*/
export function getNode(name) {
const referencedNode = AST_NODES[name];
return referencedNode.hasSameFieldsAs
? AST_NODES[referencedNode.hasSameFieldsAs]
: referencedNode;
}
/**
* @param {import('./ast-types.js').FieldWithType} field
* @param {string} name
* @param {boolean} isInlined
* @param {boolean} isLastField
* @returns {string}
*/
function getFieldDefinition([fieldName, fieldType], name, isInlined, isLastField) {
const originalNode = AST_NODES[name];
const node = getNode(name);
const typeCast = originalNode.fieldTypes?.[fieldName] || node.fieldTypes?.[fieldName];
const typeCastString = typeCast ? ` as ${typeCast}` : '';
const getAndUpdatePosition = isLastField ? 'position' : 'position++';
const dataStart = isInlined ? getAndUpdatePosition : `buffer[${getAndUpdatePosition}]`;
const variableName = node.variableNames?.[fieldName] || fieldName;
switch (fieldType) {
case 'Node': {
return `const ${variableName} = convertNode(${dataStart}, buffer, readString)${typeCastString};`;
}
case 'OptionalNode': {
let definition = `const ${fieldName}Position = buffer[${getAndUpdatePosition}];`;
if (!node.optionalFallback?.[fieldName]) {
definition += `\nconst ${variableName} = ${fieldName}Position === 0 ? null : convertNode(${fieldName}Position, buffer, readString)${typeCastString};`;
}
return definition;
}
case 'NodeList': {
return `const ${variableName} = convertNodeList(${dataStart}, buffer, readString)${typeCastString};`;
}
case 'Annotations':
case 'InvalidAnnotations': {
return `const ${variableName} = convertAnnotations(${dataStart}, buffer)${typeCastString};`;
}
case 'String': {
return `const ${variableName} = convertString(${dataStart}, buffer, readString)${typeCastString};`;
}
case 'OptionalString': {
return `const ${fieldName}Position = buffer[${getAndUpdatePosition}];\nconst ${variableName} = ${fieldName}Position === 0 ? undefined : convertString(${fieldName}Position, buffer, readString)${typeCastString};`;
}
case 'FixedString': {
return `const ${variableName} = FIXED_STRINGS[buffer[${getAndUpdatePosition}]]${typeCastString};`;
}
case 'Float': {
return `const ${variableName} = new DataView(buffer.buffer).getFloat64(${getAndUpdatePosition} << 2, true);`;
}
default: {
throw new Error(`Unknown field type: ${fieldType}`);
}
}
}
/**
* @param {import('./ast-types.js').FieldWithType} field
* @param {import('./ast-types.js').NodeDescription} node
* @returns {string}
*/
function getFieldProperty([fieldName, fieldType], node) {
switch (fieldType) {
case 'Annotations': {
return `...(${fieldName}.length > 0 ? { [ANNOTATION_KEY]: ${fieldName} } : {})`;
}
case 'InvalidAnnotations': {
return `...(${fieldName}.length > 0 ? { [INVALID_ANNOTATION_KEY]: ${fieldName} } : {})`;
}
default: {
const variableName = node.variableNames?.[fieldName] || fieldName;
const fallback = node.optionalFallback?.[fieldName];
const value = fallback
? `${fieldName}Position === 0 ? { ...${fallback} } : convertNode(${fieldName}Position, buffer, readString)`
: variableName;
return value === fieldName ? fieldName : `${fieldName}: ${value}`;
}
}
}
/**
* @param {import('./ast-types.js').NodeDescription} node
* @return {string[]}
*/
function getFixedProperties(node) {
return Object.entries(node.fixed || {}).map(([key, value]) => `${key}: ${JSON.stringify(value)}`);
}
const types = astNodeNamesWithFieldOrder.map(({ name }) => {
const node = getNode(name);
let typeDefinition = `export type ${name}Node = RollupAstNode<${node.estreeType || `estree.${name}`}>`;
/** @type {string[]} */
const additionalFieldTypes = [];
if ((node.fields || []).some(([, fieldType]) => fieldType === 'Annotations')) {
additionalFieldTypes.push('[ANNOTATION_KEY]?: RollupAnnotation[]');
}
if ((node.fields || []).some(([, fieldType]) => fieldType === 'InvalidAnnotations')) {
additionalFieldTypes.push('[INVALID_ANNOTATION_KEY]?: RollupAnnotation[]');
}
const fixedProperties = getFixedProperties(node);
if (fixedProperties.length > 0) {
additionalFieldTypes.push(...getFixedProperties(node));
}
if (additionalFieldTypes.length > 0) {
typeDefinition += ` & { ${additionalFieldTypes.join('; ')} }`;
}
typeDefinition += ';';
return typeDefinition;
});
const bufferToJsAst = `// This file is generated by scripts/generate-ast-converters.js.
// Do not edit this file directly.
import type * as estree from 'estree';
import { PanicError, ParseError } from '../ast/nodes/NodeType';import type { RollupAstNode } from '../rollup/types';
import type { RollupAnnotation } from './astConverterHelpers';
import {
ANNOTATION_KEY,
convertAnnotations,
convertString,
INVALID_ANNOTATION_KEY
} from './astConverterHelpers';
import { FIXED_STRINGS } from './convert-ast-strings';
import type { ReadString } from './getReadStringFunction';
import { error, getRollupError, logParseError } from './logs';
export function convertProgram(buffer: ArrayBuffer, readString: ReadString): ProgramNode {
const node = convertNode(0, new Uint32Array(buffer), readString);
switch (node.type) {
case PanicError: {
return error(getRollupError(logParseError(node.message)));
}
case ParseError: {
return error(getRollupError(logParseError(node.message, node.start)));
}
default: {
return node;
}
}
}
/* eslint-disable sort-keys */
const nodeConverters: ((position: number, buffer: Uint32Array, readString: ReadString) => any)[] = [
${jsConverters.join(',\n')}
];
${types.join('\n')}
export function convertNode(position: number, buffer: Uint32Array, readString: ReadString): any {
const nodeType = buffer[position];
const converter = nodeConverters[nodeType];
/* istanbul ignore if: This should never be executed but is a safeguard against faulty buffers */
if (!converter) {
console.trace();
throw new Error(\`Unknown node type: \${nodeType}\`);
}
return converter(position + 1, buffer, readString);
}
function convertNodeList(position: number, buffer: Uint32Array, readString: ReadString): any[] {
const length = buffer[position++];
const list: any[] = [];
for (let index = 0; index < length; index++) {
const nodePosition = buffer[position++];
list.push(nodePosition ? convertNode(nodePosition, buffer, readString) : null);
}
return list;
}
`;
await writeFile(bufferToJsAstFile, bufferToJsAst);
await lintFile(bufferToJsAstFile);