1c77547a054a69b46e9ec5f830de917513c7a1278750a568e9b9b0f170a1863ecdbbb35051fb15d3cd94a085a76ec8d7b06c91670ba53badc1881927b4420c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.allExpandedTypes = exports.VISITOR_KEYS = exports.NODE_PARENT_VALIDATIONS = exports.NODE_FIELDS = exports.FLIPPED_ALIAS_KEYS = exports.DEPRECATED_KEYS = exports.BUILDER_KEYS = exports.ALIAS_KEYS = void 0;
  6. exports.arrayOf = arrayOf;
  7. exports.arrayOfType = arrayOfType;
  8. exports.assertEach = assertEach;
  9. exports.assertNodeOrValueType = assertNodeOrValueType;
  10. exports.assertNodeType = assertNodeType;
  11. exports.assertOneOf = assertOneOf;
  12. exports.assertOptionalChainStart = assertOptionalChainStart;
  13. exports.assertShape = assertShape;
  14. exports.assertValueType = assertValueType;
  15. exports.chain = chain;
  16. exports.default = defineType;
  17. exports.defineAliasedType = defineAliasedType;
  18. exports.validate = validate;
  19. exports.validateArrayOfType = validateArrayOfType;
  20. exports.validateOptional = validateOptional;
  21. exports.validateOptionalType = validateOptionalType;
  22. exports.validateType = validateType;
  23. var _is = require("../validators/is.js");
  24. var _validate = require("../validators/validate.js");
  25. const VISITOR_KEYS = exports.VISITOR_KEYS = {};
  26. const ALIAS_KEYS = exports.ALIAS_KEYS = {};
  27. const FLIPPED_ALIAS_KEYS = exports.FLIPPED_ALIAS_KEYS = {};
  28. const NODE_FIELDS = exports.NODE_FIELDS = {};
  29. const BUILDER_KEYS = exports.BUILDER_KEYS = {};
  30. const DEPRECATED_KEYS = exports.DEPRECATED_KEYS = {};
  31. const NODE_PARENT_VALIDATIONS = exports.NODE_PARENT_VALIDATIONS = {};
  32. function getType(val) {
  33. if (Array.isArray(val)) {
  34. return "array";
  35. } else if (val === null) {
  36. return "null";
  37. } else {
  38. return typeof val;
  39. }
  40. }
  41. function validate(validate) {
  42. return {
  43. validate
  44. };
  45. }
  46. function validateType(...typeNames) {
  47. return validate(assertNodeType(...typeNames));
  48. }
  49. function validateOptional(validate) {
  50. return {
  51. validate,
  52. optional: true
  53. };
  54. }
  55. function validateOptionalType(...typeNames) {
  56. return {
  57. validate: assertNodeType(...typeNames),
  58. optional: true
  59. };
  60. }
  61. function arrayOf(elementType) {
  62. return chain(assertValueType("array"), assertEach(elementType));
  63. }
  64. function arrayOfType(...typeNames) {
  65. return arrayOf(assertNodeType(...typeNames));
  66. }
  67. function validateArrayOfType(...typeNames) {
  68. return validate(arrayOfType(...typeNames));
  69. }
  70. function assertEach(callback) {
  71. const childValidator = process.env.BABEL_TYPES_8_BREAKING ? _validate.validateChild : () => {};
  72. function validator(node, key, val) {
  73. if (!Array.isArray(val)) return;
  74. let i = 0;
  75. const subKey = {
  76. toString() {
  77. return `${key}[${i}]`;
  78. }
  79. };
  80. for (; i < val.length; i++) {
  81. const v = val[i];
  82. callback(node, subKey, v);
  83. childValidator(node, subKey, v);
  84. }
  85. }
  86. validator.each = callback;
  87. return validator;
  88. }
  89. function assertOneOf(...values) {
  90. function validate(node, key, val) {
  91. if (!values.includes(val)) {
  92. throw new TypeError(`Property ${key} expected value to be one of ${JSON.stringify(values)} but got ${JSON.stringify(val)}`);
  93. }
  94. }
  95. validate.oneOf = values;
  96. return validate;
  97. }
  98. const allExpandedTypes = exports.allExpandedTypes = [];
  99. function assertNodeType(...types) {
  100. const expandedTypes = new Set();
  101. allExpandedTypes.push({
  102. types,
  103. set: expandedTypes
  104. });
  105. function validate(node, key, val) {
  106. const valType = val == null ? void 0 : val.type;
  107. if (valType != null) {
  108. if (expandedTypes.has(valType)) {
  109. (0, _validate.validateChild)(node, key, val);
  110. return;
  111. }
  112. if (valType === "Placeholder") {
  113. for (const type of types) {
  114. if ((0, _is.default)(type, val)) {
  115. (0, _validate.validateChild)(node, key, val);
  116. return;
  117. }
  118. }
  119. }
  120. }
  121. throw new TypeError(`Property ${key} of ${node.type} expected node to be of a type ${JSON.stringify(types)} but instead got ${JSON.stringify(valType)}`);
  122. }
  123. validate.oneOfNodeTypes = types;
  124. return validate;
  125. }
  126. function assertNodeOrValueType(...types) {
  127. function validate(node, key, val) {
  128. const primitiveType = getType(val);
  129. for (const type of types) {
  130. if (primitiveType === type || (0, _is.default)(type, val)) {
  131. (0, _validate.validateChild)(node, key, val);
  132. return;
  133. }
  134. }
  135. throw new TypeError(`Property ${key} of ${node.type} expected node to be of a type ${JSON.stringify(types)} but instead got ${JSON.stringify(val == null ? void 0 : val.type)}`);
  136. }
  137. validate.oneOfNodeOrValueTypes = types;
  138. return validate;
  139. }
  140. function assertValueType(type) {
  141. function validate(node, key, val) {
  142. if (getType(val) === type) {
  143. return;
  144. }
  145. throw new TypeError(`Property ${key} expected type of ${type} but got ${getType(val)}`);
  146. }
  147. validate.type = type;
  148. return validate;
  149. }
  150. function assertShape(shape) {
  151. const keys = Object.keys(shape);
  152. function validate(node, key, val) {
  153. const errors = [];
  154. for (const property of keys) {
  155. try {
  156. (0, _validate.validateField)(node, property, val[property], shape[property]);
  157. } catch (error) {
  158. if (error instanceof TypeError) {
  159. errors.push(error.message);
  160. continue;
  161. }
  162. throw error;
  163. }
  164. }
  165. if (errors.length) {
  166. throw new TypeError(`Property ${key} of ${node.type} expected to have the following:\n${errors.join("\n")}`);
  167. }
  168. }
  169. validate.shapeOf = shape;
  170. return validate;
  171. }
  172. function assertOptionalChainStart() {
  173. function validate(node) {
  174. var _current;
  175. let current = node;
  176. while (node) {
  177. const {
  178. type
  179. } = current;
  180. if (type === "OptionalCallExpression") {
  181. if (current.optional) return;
  182. current = current.callee;
  183. continue;
  184. }
  185. if (type === "OptionalMemberExpression") {
  186. if (current.optional) return;
  187. current = current.object;
  188. continue;
  189. }
  190. break;
  191. }
  192. throw new TypeError(`Non-optional ${node.type} must chain from an optional OptionalMemberExpression or OptionalCallExpression. Found chain from ${(_current = current) == null ? void 0 : _current.type}`);
  193. }
  194. return validate;
  195. }
  196. function chain(...fns) {
  197. function validate(...args) {
  198. for (const fn of fns) {
  199. fn(...args);
  200. }
  201. }
  202. validate.chainOf = fns;
  203. if (fns.length >= 2 && "type" in fns[0] && fns[0].type === "array" && !("each" in fns[1])) {
  204. throw new Error(`An assertValueType("array") validator can only be followed by an assertEach(...) validator.`);
  205. }
  206. return validate;
  207. }
  208. const validTypeOpts = new Set(["aliases", "builder", "deprecatedAlias", "fields", "inherits", "visitor", "validate"]);
  209. const validFieldKeys = new Set(["default", "optional", "deprecated", "validate"]);
  210. const store = {};
  211. function defineAliasedType(...aliases) {
  212. return (type, opts = {}) => {
  213. let defined = opts.aliases;
  214. if (!defined) {
  215. var _store$opts$inherits$;
  216. if (opts.inherits) defined = (_store$opts$inherits$ = store[opts.inherits].aliases) == null ? void 0 : _store$opts$inherits$.slice();
  217. defined != null ? defined : defined = [];
  218. opts.aliases = defined;
  219. }
  220. const additional = aliases.filter(a => !defined.includes(a));
  221. defined.unshift(...additional);
  222. defineType(type, opts);
  223. };
  224. }
  225. function defineType(type, opts = {}) {
  226. const inherits = opts.inherits && store[opts.inherits] || {};
  227. let fields = opts.fields;
  228. if (!fields) {
  229. fields = {};
  230. if (inherits.fields) {
  231. const keys = Object.getOwnPropertyNames(inherits.fields);
  232. for (const key of keys) {
  233. const field = inherits.fields[key];
  234. const def = field.default;
  235. if (Array.isArray(def) ? def.length > 0 : def && typeof def === "object") {
  236. throw new Error("field defaults can only be primitives or empty arrays currently");
  237. }
  238. fields[key] = {
  239. default: Array.isArray(def) ? [] : def,
  240. optional: field.optional,
  241. deprecated: field.deprecated,
  242. validate: field.validate
  243. };
  244. }
  245. }
  246. }
  247. const visitor = opts.visitor || inherits.visitor || [];
  248. const aliases = opts.aliases || inherits.aliases || [];
  249. const builder = opts.builder || inherits.builder || opts.visitor || [];
  250. for (const k of Object.keys(opts)) {
  251. if (!validTypeOpts.has(k)) {
  252. throw new Error(`Unknown type option "${k}" on ${type}`);
  253. }
  254. }
  255. if (opts.deprecatedAlias) {
  256. DEPRECATED_KEYS[opts.deprecatedAlias] = type;
  257. }
  258. for (const key of visitor.concat(builder)) {
  259. fields[key] = fields[key] || {};
  260. }
  261. for (const key of Object.keys(fields)) {
  262. const field = fields[key];
  263. if (field.default !== undefined && !builder.includes(key)) {
  264. field.optional = true;
  265. }
  266. if (field.default === undefined) {
  267. field.default = null;
  268. } else if (!field.validate && field.default != null) {
  269. field.validate = assertValueType(getType(field.default));
  270. }
  271. for (const k of Object.keys(field)) {
  272. if (!validFieldKeys.has(k)) {
  273. throw new Error(`Unknown field key "${k}" on ${type}.${key}`);
  274. }
  275. }
  276. }
  277. VISITOR_KEYS[type] = opts.visitor = visitor;
  278. BUILDER_KEYS[type] = opts.builder = builder;
  279. NODE_FIELDS[type] = opts.fields = fields;
  280. ALIAS_KEYS[type] = opts.aliases = aliases;
  281. aliases.forEach(alias => {
  282. FLIPPED_ALIAS_KEYS[alias] = FLIPPED_ALIAS_KEYS[alias] || [];
  283. FLIPPED_ALIAS_KEYS[alias].push(type);
  284. });
  285. if (opts.validate) {
  286. NODE_PARENT_VALIDATIONS[type] = opts.validate;
  287. }
  288. store[type] = opts;
  289. }
  290. //# sourceMappingURL=utils.js.map