validators.js 1.4 KB

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