1 | import t from "../../lib/index.js";
|
---|
2 | import definitions from "../../lib/definitions/index.js";
|
---|
3 | import formatBuilderName from "../utils/formatBuilderName.js";
|
---|
4 | import lowerFirst from "../utils/lowerFirst.js";
|
---|
5 | import stringifyValidator from "../utils/stringifyValidator.js";
|
---|
6 |
|
---|
7 | function areAllRemainingFieldsNullable(fieldName, fieldNames, fields) {
|
---|
8 | const index = fieldNames.indexOf(fieldName);
|
---|
9 | return fieldNames.slice(index).every(_ => isNullable(fields[_]));
|
---|
10 | }
|
---|
11 |
|
---|
12 | function hasDefault(field) {
|
---|
13 | return field.default != null;
|
---|
14 | }
|
---|
15 |
|
---|
16 | function isNullable(field) {
|
---|
17 | return field.optional || hasDefault(field);
|
---|
18 | }
|
---|
19 |
|
---|
20 | function sortFieldNames(fields, type) {
|
---|
21 | return fields.sort((fieldA, fieldB) => {
|
---|
22 | const indexA = t.BUILDER_KEYS[type].indexOf(fieldA);
|
---|
23 | const indexB = t.BUILDER_KEYS[type].indexOf(fieldB);
|
---|
24 | if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
|
---|
25 | if (indexA === -1) return 1;
|
---|
26 | if (indexB === -1) return -1;
|
---|
27 | return indexA - indexB;
|
---|
28 | });
|
---|
29 | }
|
---|
30 |
|
---|
31 | function generateBuilderArgs(type) {
|
---|
32 | const fields = t.NODE_FIELDS[type];
|
---|
33 | const fieldNames = sortFieldNames(Object.keys(t.NODE_FIELDS[type]), type);
|
---|
34 | const builderNames = t.BUILDER_KEYS[type];
|
---|
35 |
|
---|
36 | const args = [];
|
---|
37 |
|
---|
38 | fieldNames.forEach(fieldName => {
|
---|
39 | const field = fields[fieldName];
|
---|
40 | // Future / annoying TODO:
|
---|
41 | // MemberExpression.property, ObjectProperty.key and ObjectMethod.key need special cases; either:
|
---|
42 | // - convert the declaration to chain() like ClassProperty.key and ClassMethod.key,
|
---|
43 | // - declare an alias type for valid keys, detect the case and reuse it here,
|
---|
44 | // - declare a disjoint union with, for example, ObjectPropertyBase,
|
---|
45 | // ObjectPropertyLiteralKey and ObjectPropertyComputedKey, and declare ObjectProperty
|
---|
46 | // as "ObjectPropertyBase & (ObjectPropertyLiteralKey | ObjectPropertyComputedKey)"
|
---|
47 | let typeAnnotation = stringifyValidator(field.validate, "t.");
|
---|
48 |
|
---|
49 | if (isNullable(field) && !hasDefault(field)) {
|
---|
50 | typeAnnotation += " | null";
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (builderNames.includes(fieldName)) {
|
---|
54 | const bindingIdentifierName = t.toBindingIdentifierName(fieldName);
|
---|
55 | if (areAllRemainingFieldsNullable(fieldName, builderNames, fields)) {
|
---|
56 | args.push(
|
---|
57 | `${bindingIdentifierName}${
|
---|
58 | isNullable(field) ? "?:" : ":"
|
---|
59 | } ${typeAnnotation}`
|
---|
60 | );
|
---|
61 | } else {
|
---|
62 | args.push(
|
---|
63 | `${bindingIdentifierName}: ${typeAnnotation}${
|
---|
64 | isNullable(field) ? " | undefined" : ""
|
---|
65 | }`
|
---|
66 | );
|
---|
67 | }
|
---|
68 | }
|
---|
69 | });
|
---|
70 |
|
---|
71 | return args;
|
---|
72 | }
|
---|
73 |
|
---|
74 | export default function generateBuilders(kind) {
|
---|
75 | return kind === "uppercase.js"
|
---|
76 | ? generateUppercaseBuilders()
|
---|
77 | : generateLowercaseBuilders();
|
---|
78 | }
|
---|
79 |
|
---|
80 | function generateLowercaseBuilders() {
|
---|
81 | let output = `/*
|
---|
82 | * This file is auto-generated! Do not modify it directly.
|
---|
83 | * To re-generate run 'make build'
|
---|
84 | */
|
---|
85 | import builder from "../builder";
|
---|
86 | import type * as t from "../..";
|
---|
87 |
|
---|
88 | /* eslint-disable @typescript-eslint/no-unused-vars */
|
---|
89 |
|
---|
90 | `;
|
---|
91 |
|
---|
92 | const reservedNames = new Set(["super", "import"]);
|
---|
93 | Object.keys(definitions.BUILDER_KEYS).forEach(type => {
|
---|
94 | const defArgs = generateBuilderArgs(type);
|
---|
95 | const formatedBuilderName = formatBuilderName(type);
|
---|
96 | const formatedBuilderNameLocal = reservedNames.has(formatedBuilderName)
|
---|
97 | ? `_${formatedBuilderName}`
|
---|
98 | : formatedBuilderName;
|
---|
99 | output += `${
|
---|
100 | formatedBuilderNameLocal === formatedBuilderName ? "export " : ""
|
---|
101 | }function ${formatedBuilderNameLocal}(${defArgs.join(
|
---|
102 | ", "
|
---|
103 | )}): t.${type} { return builder("${type}", ...arguments); }\n`;
|
---|
104 | if (formatedBuilderNameLocal !== formatedBuilderName) {
|
---|
105 | output += `export { ${formatedBuilderNameLocal} as ${formatedBuilderName} };\n`;
|
---|
106 | }
|
---|
107 |
|
---|
108 | // This is needed for backwards compatibility.
|
---|
109 | // It should be removed in the next major version.
|
---|
110 | // JSXIdentifier -> jSXIdentifier
|
---|
111 | if (/^[A-Z]{2}/.test(type)) {
|
---|
112 | output += `export { ${formatedBuilderNameLocal} as ${lowerFirst(
|
---|
113 | type
|
---|
114 | )} }\n`;
|
---|
115 | }
|
---|
116 | });
|
---|
117 |
|
---|
118 | Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
|
---|
119 | const newType = definitions.DEPRECATED_KEYS[type];
|
---|
120 | const formatedBuilderName = formatBuilderName(type);
|
---|
121 | output += `/** @deprecated */
|
---|
122 | function ${type}(...args: Array<any>): any {
|
---|
123 | console.trace("The node type ${type} has been renamed to ${newType}");
|
---|
124 | return builder("${type}", ...args);
|
---|
125 | }
|
---|
126 | export { ${type} as ${formatedBuilderName} };\n`;
|
---|
127 | // This is needed for backwards compatibility.
|
---|
128 | // It should be removed in the next major version.
|
---|
129 | // JSXIdentifier -> jSXIdentifier
|
---|
130 | if (/^[A-Z]{2}/.test(type)) {
|
---|
131 | output += `export { ${type} as ${lowerFirst(type)} }\n`;
|
---|
132 | }
|
---|
133 | });
|
---|
134 |
|
---|
135 | return output;
|
---|
136 | }
|
---|
137 |
|
---|
138 | function generateUppercaseBuilders() {
|
---|
139 | let output = `/*
|
---|
140 | * This file is auto-generated! Do not modify it directly.
|
---|
141 | * To re-generate run 'make build'
|
---|
142 | */
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * This file is written in JavaScript and not TypeScript because uppercase builders
|
---|
146 | * conflict with AST types. TypeScript reads the uppercase.d.ts file instead.
|
---|
147 | */
|
---|
148 |
|
---|
149 | export {\n`;
|
---|
150 |
|
---|
151 | Object.keys(definitions.BUILDER_KEYS).forEach(type => {
|
---|
152 | const formatedBuilderName = formatBuilderName(type);
|
---|
153 | output += ` ${formatedBuilderName} as ${type},\n`;
|
---|
154 | });
|
---|
155 |
|
---|
156 | Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
|
---|
157 | const formatedBuilderName = formatBuilderName(type);
|
---|
158 | output += ` ${formatedBuilderName} as ${type},\n`;
|
---|
159 | });
|
---|
160 |
|
---|
161 | output += ` } from './index';\n`;
|
---|
162 | return output;
|
---|
163 | }
|
---|