validators.js 1.3 KB

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