validators.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import * as t from "@babel/types";
  2. import * as 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. // TODO: Remove this check once we stop compiling to CJS
  18. if (type === "default" || type === "__esModule") continue;
  19. const { types } = virtualTypes[type];
  20. if (type[0] === "_") continue;
  21. if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {
  22. output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
  23. } else if (types /* in VirtualTypeAliases */) {
  24. output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;
  25. } else if (type === "Pure") {
  26. output += `isPure(constantsOnly?: boolean): boolean;`;
  27. } else {
  28. // if it don't have types, then VirtualTypeAliases[type] is t.Node
  29. // which TS marked as always true
  30. // eg. if (path.isBlockScope()) return;
  31. // path resolved to `never` here
  32. // so we have to return boolean instead of this is NodePath<t.Node> here
  33. output += `is${type}(opts?: object): boolean;`;
  34. }
  35. }
  36. output += `
  37. }
  38. `;
  39. return output;
  40. }