UNPKG

5.44 kBJavaScriptView Raw
1const definitions = require("../src/definitions");
2const flatMap = require("array.prototype.flatmap");
3const {
4 typeSignature,
5 iterateProps,
6 mapProps,
7 filterProps,
8 unique,
9} = require("./util");
10
11const stdout = process.stdout;
12
13const jsTypes = ["string", "number", "boolean"];
14
15const quote = (value) => `"${value}"`;
16
17function params(fields) {
18 const optionalDefault = (field) =>
19 field.default ? ` = ${field.default}` : "";
20 return mapProps(fields)
21 .map((field) => `${typeSignature(field)}${optionalDefault(field)}`)
22 .join(",");
23}
24
25function assertParamType({ assertNodeType, array, name, type }) {
26 if (array) {
27 // TODO - assert contents of array?
28 return `assert(typeof ${name} === "object" && typeof ${name}.length !== "undefined")\n`;
29 } else {
30 if (jsTypes.includes(type)) {
31 return `assert(
32 typeof ${name} === "${type}",
33 "Argument ${name} must be of type ${type}, given: " + typeof ${name}
34 )`;
35 }
36
37 if (assertNodeType === true) {
38 return `assert(
39 ${name}.type === "${type}",
40 "Argument ${name} must be of type ${type}, given: " + ${name}.type
41 )`;
42 }
43
44 return "";
45 }
46}
47
48function assertParam(meta) {
49 const paramAssertion = assertParamType(meta);
50
51 if (paramAssertion === "") {
52 return "";
53 }
54
55 if (meta.maybe || meta.optional) {
56 return `
57 if (${meta.name} !== null && ${meta.name} !== undefined) {
58 ${paramAssertion};
59 }
60 `;
61 } else {
62 return paramAssertion;
63 }
64}
65
66function assertParams(fields) {
67 return mapProps(fields).map(assertParam).join("\n");
68}
69
70function buildObject(typeDef) {
71 const optionalField = (meta) => {
72 if (meta.array) {
73 // omit optional array properties if the constructor function was supplied
74 // with an empty array
75 return `
76 if (typeof ${meta.name} !== "undefined" && ${meta.name}.length > 0) {
77 node.${meta.name} = ${meta.name};
78 }
79 `;
80 } else if (meta.type === "Object") {
81 // omit optional object properties if they have no keys
82 return `
83 if (typeof ${meta.name} !== "undefined" && Object.keys(${meta.name}).length !== 0) {
84 node.${meta.name} = ${meta.name};
85 }
86 `;
87 } else if (meta.type === "boolean") {
88 // omit optional boolean properties if they are not true
89 return `
90 if (${meta.name} === true) {
91 node.${meta.name} = true;
92 }
93 `;
94 } else {
95 return `
96 if (typeof ${meta.name} !== "undefined") {
97 node.${meta.name} = ${meta.name};
98 }
99 `;
100 }
101 };
102
103 const fields = mapProps(typeDef.fields)
104 .filter((f) => !f.optional && !f.constant)
105 .map((f) => f.name);
106
107 const constants = mapProps(typeDef.fields)
108 .filter((f) => f.constant)
109 .map((f) => `${f.name}: "${f.value}"`);
110
111 return `
112 const node: ${typeDef.flowTypeName || typeDef.name} = {
113 type: "${typeDef.name}",
114 ${constants.concat(fields).join(",")}
115 }
116
117 ${mapProps(typeDef.fields)
118 .filter((f) => f.optional)
119 .map(optionalField)
120 .join("")}
121 `;
122}
123
124function lowerCamelCase(name) {
125 return name.substring(0, 1).toLowerCase() + name.substring(1);
126}
127
128function generate() {
129 stdout.write(`
130 // @flow
131
132 // THIS FILE IS AUTOGENERATED
133 // see scripts/generateNodeUtils.js
134
135 import { assert } from "mamacro";
136
137 function isTypeOf(t: string) {
138 return (n: Node) => n.type === t;
139 }
140
141 function assertTypeOf(t: string) {
142 return (n: Node) => assert(n.type === t);
143 }
144 `);
145
146 // Node builders
147 iterateProps(definitions, (typeDefinition) => {
148 stdout.write(`
149 export function ${lowerCamelCase(typeDefinition.name)} (
150 ${params(filterProps(typeDefinition.fields, (f) => !f.constant))}
151 ): ${typeDefinition.name} {
152
153 ${assertParams(filterProps(typeDefinition.fields, (f) => !f.constant))}
154 ${buildObject(typeDefinition)}
155
156 return node;
157 }
158 `);
159 });
160
161 // Node testers
162 iterateProps(definitions, (typeDefinition) => {
163 stdout.write(`
164 export const is${typeDefinition.name}: ((n: Node) => boolean) =
165 isTypeOf("${typeDefinition.name}");
166 `);
167 });
168
169 // Node union type testers
170 const unionTypes = unique(
171 flatMap(
172 mapProps(definitions).filter((d) => d.unionType),
173 (d) => d.unionType
174 )
175 );
176 unionTypes.forEach((unionType) => {
177 stdout.write(
178 `
179 export const is${unionType} = (node: Node): boolean => ` +
180 mapProps(definitions)
181 .filter((d) => d.unionType && d.unionType.includes(unionType))
182 .map((d) => `is${d.name}(node) `)
183 .join("||") +
184 ";\n\n"
185 );
186 });
187
188 // Node assertion
189 iterateProps(definitions, (typeDefinition) => {
190 stdout.write(`
191 export const assert${typeDefinition.name}: ((n: Node) => void) =
192 assertTypeOf("${typeDefinition.name}");
193 `);
194 });
195
196 // a map from node type to its set of union types
197 stdout.write(
198 `
199 export const unionTypesMap = {` +
200 mapProps(definitions)
201 .filter((d) => d.unionType)
202 .map((t) => `"${t.name}": [${t.unionType.map(quote).join(",")}]\n`) +
203 `};
204 `
205 );
206
207 // an array of all node and union types
208 stdout.write(
209 `
210 export const nodeAndUnionTypes = [` +
211 mapProps(definitions)
212 .map((t) => `"${t.name}"`)
213 .concat(unionTypes.map(quote))
214 .join(",") +
215 `];`
216 );
217}
218
219generate();