1 | import definitions from "../../lib/definitions/index.js";
|
---|
2 |
|
---|
3 | const has = Function.call.bind(Object.prototype.hasOwnProperty);
|
---|
4 |
|
---|
5 | function joinComparisons(leftArr, right) {
|
---|
6 | return (
|
---|
7 | leftArr.map(JSON.stringify).join(` === ${right} || `) + ` === ${right}`
|
---|
8 | );
|
---|
9 | }
|
---|
10 |
|
---|
11 | function addIsHelper(type, aliasKeys, deprecated) {
|
---|
12 | const targetType = JSON.stringify(type);
|
---|
13 | let aliasSource = "";
|
---|
14 | if (aliasKeys) {
|
---|
15 | aliasSource = joinComparisons(aliasKeys, "nodeType");
|
---|
16 | }
|
---|
17 |
|
---|
18 | let placeholderSource = "";
|
---|
19 | const placeholderTypes = [];
|
---|
20 | if (
|
---|
21 | definitions.PLACEHOLDERS.includes(type) &&
|
---|
22 | has(definitions.FLIPPED_ALIAS_KEYS, type)
|
---|
23 | ) {
|
---|
24 | placeholderTypes.push(type);
|
---|
25 | }
|
---|
26 | if (has(definitions.PLACEHOLDERS_FLIPPED_ALIAS, type)) {
|
---|
27 | placeholderTypes.push(...definitions.PLACEHOLDERS_FLIPPED_ALIAS[type]);
|
---|
28 | }
|
---|
29 | if (placeholderTypes.length > 0) {
|
---|
30 | placeholderSource =
|
---|
31 | ' || nodeType === "Placeholder" && (' +
|
---|
32 | joinComparisons(
|
---|
33 | placeholderTypes,
|
---|
34 | "(node as t.Placeholder).expectedNode"
|
---|
35 | ) +
|
---|
36 | ")";
|
---|
37 | }
|
---|
38 |
|
---|
39 | const result =
|
---|
40 | definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]
|
---|
41 | ? `node is t.${type}`
|
---|
42 | : "boolean";
|
---|
43 |
|
---|
44 | return `export function is${type}(node: object | null | undefined, opts?: object | null): ${result} {
|
---|
45 | ${deprecated || ""}
|
---|
46 | if (!node) return false;
|
---|
47 |
|
---|
48 | const nodeType = (node as t.Node).type;
|
---|
49 | if (${
|
---|
50 | aliasSource ? aliasSource : `nodeType === ${targetType}`
|
---|
51 | }${placeholderSource}) {
|
---|
52 | if (typeof opts === "undefined") {
|
---|
53 | return true;
|
---|
54 | } else {
|
---|
55 | return shallowEqual(node, opts);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | return false;
|
---|
60 | }
|
---|
61 | `;
|
---|
62 | }
|
---|
63 |
|
---|
64 | export default function generateValidators() {
|
---|
65 | let output = `/*
|
---|
66 | * This file is auto-generated! Do not modify it directly.
|
---|
67 | * To re-generate run 'make build'
|
---|
68 | */
|
---|
69 | import shallowEqual from "../../utils/shallowEqual";
|
---|
70 | import type * as t from "../..";\n\n`;
|
---|
71 |
|
---|
72 | Object.keys(definitions.VISITOR_KEYS).forEach(type => {
|
---|
73 | output += addIsHelper(type);
|
---|
74 | });
|
---|
75 |
|
---|
76 | Object.keys(definitions.FLIPPED_ALIAS_KEYS).forEach(type => {
|
---|
77 | output += addIsHelper(type, definitions.FLIPPED_ALIAS_KEYS[type]);
|
---|
78 | });
|
---|
79 |
|
---|
80 | Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
|
---|
81 | const newType = definitions.DEPRECATED_KEYS[type];
|
---|
82 | const deprecated = `console.trace("The node type ${type} has been renamed to ${newType}");`;
|
---|
83 | output += addIsHelper(type, null, deprecated);
|
---|
84 | });
|
---|
85 |
|
---|
86 | return output;
|
---|
87 | }
|
---|