[6a3a178] | 1 | import definitions from "../../lib/definitions/index.js";
|
---|
| 2 |
|
---|
| 3 | function addAssertHelper(type) {
|
---|
| 4 | const result =
|
---|
| 5 | definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]
|
---|
| 6 | ? `node is t.${type}`
|
---|
| 7 | : "boolean";
|
---|
| 8 |
|
---|
| 9 | return `export function assert${type}(node: object | null | undefined, opts?: object | null): asserts ${
|
---|
| 10 | result === "boolean" ? "node" : result
|
---|
| 11 | } {
|
---|
| 12 | assert("${type}", node, opts) }
|
---|
| 13 | `;
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | export default function generateAsserts() {
|
---|
| 17 | let output = `/*
|
---|
| 18 | * This file is auto-generated! Do not modify it directly.
|
---|
| 19 | * To re-generate run 'make build'
|
---|
| 20 | */
|
---|
| 21 | import is from "../../validators/is";
|
---|
| 22 | import type * as t from "../..";
|
---|
| 23 |
|
---|
| 24 | function assert(type: string, node: any, opts?: any): void {
|
---|
| 25 | if (!is(type, node, opts)) {
|
---|
| 26 | throw new Error(
|
---|
| 27 | \`Expected type "\${type}" with option \${JSON.stringify(opts)}, \` +
|
---|
| 28 | \`but instead got "\${node.type}".\`,
|
---|
| 29 | );
|
---|
| 30 | }
|
---|
| 31 | }\n\n`;
|
---|
| 32 |
|
---|
| 33 | Object.keys(definitions.VISITOR_KEYS).forEach(type => {
|
---|
| 34 | output += addAssertHelper(type);
|
---|
| 35 | });
|
---|
| 36 |
|
---|
| 37 | Object.keys(definitions.FLIPPED_ALIAS_KEYS).forEach(type => {
|
---|
| 38 | output += addAssertHelper(type);
|
---|
| 39 | });
|
---|
| 40 |
|
---|
| 41 | Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
|
---|
| 42 | const newType = definitions.DEPRECATED_KEYS[type];
|
---|
| 43 | output += `export function assert${type}(node: any, opts: any): void {
|
---|
| 44 | console.trace("The node type ${type} has been renamed to ${newType}");
|
---|
| 45 | assert("${type}", node, opts);
|
---|
| 46 | }\n`;
|
---|
| 47 | });
|
---|
| 48 |
|
---|
| 49 | return output;
|
---|
| 50 | }
|
---|