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