1 | import t from "@babel/types";
|
---|
2 | import virtualTypes from "../../lib/path/lib/virtual-types.js";
|
---|
3 | import definitions from "@babel/types/lib/definitions/index.js";
|
---|
4 |
|
---|
5 | export default function generateValidators() {
|
---|
6 | let output = `/*
|
---|
7 | * This file is auto-generated! Do not modify it directly.
|
---|
8 | * To re-generate run 'make build'
|
---|
9 | */
|
---|
10 | import * as t from "@babel/types";
|
---|
11 | import NodePath from "../index";
|
---|
12 | import type { VirtualTypeAliases } from "./virtual-types";
|
---|
13 |
|
---|
14 | export interface NodePathValidators {
|
---|
15 | `;
|
---|
16 |
|
---|
17 | for (const type of [...t.TYPES].sort()) {
|
---|
18 | output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
|
---|
19 | }
|
---|
20 |
|
---|
21 | for (const type of Object.keys(virtualTypes)) {
|
---|
22 | const { types } = virtualTypes[type];
|
---|
23 | if (type[0] === "_") continue;
|
---|
24 | if (definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]) {
|
---|
25 | output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
|
---|
26 | } else if (types /* in VirtualTypeAliases */) {
|
---|
27 | output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;
|
---|
28 | } else {
|
---|
29 | // if it don't have types, then VirtualTypeAliases[type] is t.Node
|
---|
30 | // which TS marked as always true
|
---|
31 | // eg. if (path.isBlockScope()) return;
|
---|
32 | // path resolved to `never` here
|
---|
33 | // so we have to return boolean instead of this is NodePath<t.Node> here
|
---|
34 | output += `is${type}(opts?: object): boolean;`;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | output += `
|
---|
39 | }
|
---|
40 | `;
|
---|
41 |
|
---|
42 | return output;
|
---|
43 | }
|
---|