validators.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 if (type === "Pure") {
  24. output += `isPure(constantsOnly?: boolean): boolean;`;
  25. } else {
  26. // if it don't have types, then VirtualTypeAliases[type] is t.Node
  27. // which TS marked as always true
  28. // eg. if (path.isBlockScope()) return;
  29. // path resolved to `never` here
  30. // so we have to return boolean instead of this is NodePath<t.Node> here
  31. output += `is${type}(opts?: object): boolean;`;
  32. }
  33. }
  34. output += `
  35. }
  36. `;
  37. return output;
  38. }