e46ad13c60229097d63afa78b1b6b7e145fdcfb849e86cdc531e76073cba9ba026105f9cc1be453501ad962bffc358035b3abdfab68ea995eb29330b1f03a8 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. "use strict";
  2. var _utils = require("./utils.js");
  3. var _core = require("./core.js");
  4. var _is = require("../validators/is.js");
  5. const defineType = (0, _utils.defineAliasedType)("TypeScript");
  6. const bool = (0, _utils.assertValueType)("boolean");
  7. const tSFunctionTypeAnnotationCommon = () => ({
  8. returnType: {
  9. validate: (0, _utils.assertNodeType)("TSTypeAnnotation", "Noop"),
  10. optional: true
  11. },
  12. typeParameters: {
  13. validate: (0, _utils.assertNodeType)("TSTypeParameterDeclaration", "Noop"),
  14. optional: true
  15. }
  16. });
  17. defineType("TSParameterProperty", {
  18. aliases: ["LVal"],
  19. visitor: ["parameter"],
  20. fields: {
  21. accessibility: {
  22. validate: (0, _utils.assertOneOf)("public", "private", "protected"),
  23. optional: true
  24. },
  25. readonly: {
  26. validate: (0, _utils.assertValueType)("boolean"),
  27. optional: true
  28. },
  29. parameter: {
  30. validate: (0, _utils.assertNodeType)("Identifier", "AssignmentPattern")
  31. },
  32. override: {
  33. validate: (0, _utils.assertValueType)("boolean"),
  34. optional: true
  35. },
  36. decorators: {
  37. validate: (0, _utils.arrayOfType)("Decorator"),
  38. optional: true
  39. }
  40. }
  41. });
  42. defineType("TSDeclareFunction", {
  43. aliases: ["Statement", "Declaration"],
  44. visitor: ["id", "typeParameters", "params", "returnType"],
  45. fields: Object.assign({}, (0, _core.functionDeclarationCommon)(), tSFunctionTypeAnnotationCommon())
  46. });
  47. defineType("TSDeclareMethod", {
  48. visitor: ["decorators", "key", "typeParameters", "params", "returnType"],
  49. fields: Object.assign({}, (0, _core.classMethodOrDeclareMethodCommon)(), tSFunctionTypeAnnotationCommon())
  50. });
  51. defineType("TSQualifiedName", {
  52. aliases: ["TSEntityName"],
  53. visitor: ["left", "right"],
  54. fields: {
  55. left: (0, _utils.validateType)("TSEntityName"),
  56. right: (0, _utils.validateType)("Identifier")
  57. }
  58. });
  59. const signatureDeclarationCommon = () => ({
  60. typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterDeclaration"),
  61. ["parameters"]: (0, _utils.validateArrayOfType)("ArrayPattern", "Identifier", "ObjectPattern", "RestElement"),
  62. ["typeAnnotation"]: (0, _utils.validateOptionalType)("TSTypeAnnotation")
  63. });
  64. const callConstructSignatureDeclaration = {
  65. aliases: ["TSTypeElement"],
  66. visitor: ["typeParameters", "parameters", "typeAnnotation"],
  67. fields: signatureDeclarationCommon()
  68. };
  69. defineType("TSCallSignatureDeclaration", callConstructSignatureDeclaration);
  70. defineType("TSConstructSignatureDeclaration", callConstructSignatureDeclaration);
  71. const namedTypeElementCommon = () => ({
  72. key: (0, _utils.validateType)("Expression"),
  73. computed: {
  74. default: false
  75. },
  76. optional: (0, _utils.validateOptional)(bool)
  77. });
  78. defineType("TSPropertySignature", {
  79. aliases: ["TSTypeElement"],
  80. visitor: ["key", "typeAnnotation"],
  81. fields: Object.assign({}, namedTypeElementCommon(), {
  82. readonly: (0, _utils.validateOptional)(bool),
  83. typeAnnotation: (0, _utils.validateOptionalType)("TSTypeAnnotation"),
  84. kind: {
  85. optional: true,
  86. validate: (0, _utils.assertOneOf)("get", "set")
  87. }
  88. })
  89. });
  90. defineType("TSMethodSignature", {
  91. aliases: ["TSTypeElement"],
  92. visitor: ["key", "typeParameters", "parameters", "typeAnnotation"],
  93. fields: Object.assign({}, signatureDeclarationCommon(), namedTypeElementCommon(), {
  94. kind: {
  95. validate: (0, _utils.assertOneOf)("method", "get", "set")
  96. }
  97. })
  98. });
  99. defineType("TSIndexSignature", {
  100. aliases: ["TSTypeElement"],
  101. visitor: ["parameters", "typeAnnotation"],
  102. fields: {
  103. readonly: (0, _utils.validateOptional)(bool),
  104. static: (0, _utils.validateOptional)(bool),
  105. parameters: (0, _utils.validateArrayOfType)("Identifier"),
  106. typeAnnotation: (0, _utils.validateOptionalType)("TSTypeAnnotation")
  107. }
  108. });
  109. const tsKeywordTypes = ["TSAnyKeyword", "TSBooleanKeyword", "TSBigIntKeyword", "TSIntrinsicKeyword", "TSNeverKeyword", "TSNullKeyword", "TSNumberKeyword", "TSObjectKeyword", "TSStringKeyword", "TSSymbolKeyword", "TSUndefinedKeyword", "TSUnknownKeyword", "TSVoidKeyword"];
  110. for (const type of tsKeywordTypes) {
  111. defineType(type, {
  112. aliases: ["TSType", "TSBaseType"],
  113. visitor: [],
  114. fields: {}
  115. });
  116. }
  117. defineType("TSThisType", {
  118. aliases: ["TSType", "TSBaseType"],
  119. visitor: [],
  120. fields: {}
  121. });
  122. const fnOrCtrBase = {
  123. aliases: ["TSType"],
  124. visitor: ["typeParameters", "parameters", "typeAnnotation"]
  125. };
  126. defineType("TSFunctionType", Object.assign({}, fnOrCtrBase, {
  127. fields: signatureDeclarationCommon()
  128. }));
  129. defineType("TSConstructorType", Object.assign({}, fnOrCtrBase, {
  130. fields: Object.assign({}, signatureDeclarationCommon(), {
  131. abstract: (0, _utils.validateOptional)(bool)
  132. })
  133. }));
  134. defineType("TSTypeReference", {
  135. aliases: ["TSType"],
  136. visitor: ["typeName", "typeParameters"],
  137. fields: {
  138. typeName: (0, _utils.validateType)("TSEntityName"),
  139. ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
  140. }
  141. });
  142. defineType("TSTypePredicate", {
  143. aliases: ["TSType"],
  144. visitor: ["parameterName", "typeAnnotation"],
  145. builder: ["parameterName", "typeAnnotation", "asserts"],
  146. fields: {
  147. parameterName: (0, _utils.validateType)("Identifier", "TSThisType"),
  148. typeAnnotation: (0, _utils.validateOptionalType)("TSTypeAnnotation"),
  149. asserts: (0, _utils.validateOptional)(bool)
  150. }
  151. });
  152. defineType("TSTypeQuery", {
  153. aliases: ["TSType"],
  154. visitor: ["exprName", "typeParameters"],
  155. fields: {
  156. exprName: (0, _utils.validateType)("TSEntityName", "TSImportType"),
  157. ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
  158. }
  159. });
  160. defineType("TSTypeLiteral", {
  161. aliases: ["TSType"],
  162. visitor: ["members"],
  163. fields: {
  164. members: (0, _utils.validateArrayOfType)("TSTypeElement")
  165. }
  166. });
  167. defineType("TSArrayType", {
  168. aliases: ["TSType"],
  169. visitor: ["elementType"],
  170. fields: {
  171. elementType: (0, _utils.validateType)("TSType")
  172. }
  173. });
  174. defineType("TSTupleType", {
  175. aliases: ["TSType"],
  176. visitor: ["elementTypes"],
  177. fields: {
  178. elementTypes: (0, _utils.validateArrayOfType)("TSType", "TSNamedTupleMember")
  179. }
  180. });
  181. defineType("TSOptionalType", {
  182. aliases: ["TSType"],
  183. visitor: ["typeAnnotation"],
  184. fields: {
  185. typeAnnotation: (0, _utils.validateType)("TSType")
  186. }
  187. });
  188. defineType("TSRestType", {
  189. aliases: ["TSType"],
  190. visitor: ["typeAnnotation"],
  191. fields: {
  192. typeAnnotation: (0, _utils.validateType)("TSType")
  193. }
  194. });
  195. defineType("TSNamedTupleMember", {
  196. visitor: ["label", "elementType"],
  197. builder: ["label", "elementType", "optional"],
  198. fields: {
  199. label: (0, _utils.validateType)("Identifier"),
  200. optional: {
  201. validate: bool,
  202. default: false
  203. },
  204. elementType: (0, _utils.validateType)("TSType")
  205. }
  206. });
  207. const unionOrIntersection = {
  208. aliases: ["TSType"],
  209. visitor: ["types"],
  210. fields: {
  211. types: (0, _utils.validateArrayOfType)("TSType")
  212. }
  213. };
  214. defineType("TSUnionType", unionOrIntersection);
  215. defineType("TSIntersectionType", unionOrIntersection);
  216. defineType("TSConditionalType", {
  217. aliases: ["TSType"],
  218. visitor: ["checkType", "extendsType", "trueType", "falseType"],
  219. fields: {
  220. checkType: (0, _utils.validateType)("TSType"),
  221. extendsType: (0, _utils.validateType)("TSType"),
  222. trueType: (0, _utils.validateType)("TSType"),
  223. falseType: (0, _utils.validateType)("TSType")
  224. }
  225. });
  226. defineType("TSInferType", {
  227. aliases: ["TSType"],
  228. visitor: ["typeParameter"],
  229. fields: {
  230. typeParameter: (0, _utils.validateType)("TSTypeParameter")
  231. }
  232. });
  233. defineType("TSParenthesizedType", {
  234. aliases: ["TSType"],
  235. visitor: ["typeAnnotation"],
  236. fields: {
  237. typeAnnotation: (0, _utils.validateType)("TSType")
  238. }
  239. });
  240. defineType("TSTypeOperator", {
  241. aliases: ["TSType"],
  242. visitor: ["typeAnnotation"],
  243. builder: ["typeAnnotation", "operator"],
  244. fields: {
  245. operator: {
  246. validate: (0, _utils.assertValueType)("string"),
  247. default: "keyof"
  248. },
  249. typeAnnotation: (0, _utils.validateType)("TSType")
  250. }
  251. });
  252. defineType("TSIndexedAccessType", {
  253. aliases: ["TSType"],
  254. visitor: ["objectType", "indexType"],
  255. fields: {
  256. objectType: (0, _utils.validateType)("TSType"),
  257. indexType: (0, _utils.validateType)("TSType")
  258. }
  259. });
  260. defineType("TSMappedType", {
  261. aliases: ["TSType"],
  262. visitor: ["typeParameter", "nameType", "typeAnnotation"],
  263. builder: ["typeParameter", "typeAnnotation", "nameType"],
  264. fields: Object.assign({}, {
  265. typeParameter: (0, _utils.validateType)("TSTypeParameter")
  266. }, {
  267. readonly: (0, _utils.validateOptional)((0, _utils.assertOneOf)(true, false, "+", "-")),
  268. optional: (0, _utils.validateOptional)((0, _utils.assertOneOf)(true, false, "+", "-")),
  269. typeAnnotation: (0, _utils.validateOptionalType)("TSType"),
  270. nameType: (0, _utils.validateOptionalType)("TSType")
  271. })
  272. });
  273. defineType("TSTemplateLiteralType", {
  274. aliases: ["TSType", "TSBaseType"],
  275. visitor: ["quasis", "types"],
  276. fields: {
  277. quasis: (0, _utils.validateArrayOfType)("TemplateElement"),
  278. types: {
  279. validate: (0, _utils.chain)((0, _utils.assertValueType)("array"), (0, _utils.assertEach)((0, _utils.assertNodeType)("TSType")), function (node, key, val) {
  280. if (node.quasis.length !== val.length + 1) {
  281. throw new TypeError(`Number of ${node.type} quasis should be exactly one more than the number of types.\nExpected ${val.length + 1} quasis but got ${node.quasis.length}`);
  282. }
  283. })
  284. }
  285. }
  286. });
  287. defineType("TSLiteralType", {
  288. aliases: ["TSType", "TSBaseType"],
  289. visitor: ["literal"],
  290. fields: {
  291. literal: {
  292. validate: function () {
  293. const unaryExpression = (0, _utils.assertNodeType)("NumericLiteral", "BigIntLiteral");
  294. const unaryOperator = (0, _utils.assertOneOf)("-");
  295. const literal = (0, _utils.assertNodeType)("NumericLiteral", "StringLiteral", "BooleanLiteral", "BigIntLiteral", "TemplateLiteral");
  296. function validator(parent, key, node) {
  297. if ((0, _is.default)("UnaryExpression", node)) {
  298. unaryOperator(node, "operator", node.operator);
  299. unaryExpression(node, "argument", node.argument);
  300. } else {
  301. literal(parent, key, node);
  302. }
  303. }
  304. validator.oneOfNodeTypes = ["NumericLiteral", "StringLiteral", "BooleanLiteral", "BigIntLiteral", "TemplateLiteral", "UnaryExpression"];
  305. return validator;
  306. }()
  307. }
  308. }
  309. });
  310. {
  311. defineType("TSExpressionWithTypeArguments", {
  312. aliases: ["TSType"],
  313. visitor: ["expression", "typeParameters"],
  314. fields: {
  315. expression: (0, _utils.validateType)("TSEntityName"),
  316. typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
  317. }
  318. });
  319. }
  320. defineType("TSInterfaceDeclaration", {
  321. aliases: ["Statement", "Declaration"],
  322. visitor: ["id", "typeParameters", "extends", "body"],
  323. fields: {
  324. declare: (0, _utils.validateOptional)(bool),
  325. id: (0, _utils.validateType)("Identifier"),
  326. typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterDeclaration"),
  327. extends: (0, _utils.validateOptional)((0, _utils.arrayOfType)("TSExpressionWithTypeArguments")),
  328. body: (0, _utils.validateType)("TSInterfaceBody")
  329. }
  330. });
  331. defineType("TSInterfaceBody", {
  332. visitor: ["body"],
  333. fields: {
  334. body: (0, _utils.validateArrayOfType)("TSTypeElement")
  335. }
  336. });
  337. defineType("TSTypeAliasDeclaration", {
  338. aliases: ["Statement", "Declaration"],
  339. visitor: ["id", "typeParameters", "typeAnnotation"],
  340. fields: {
  341. declare: (0, _utils.validateOptional)(bool),
  342. id: (0, _utils.validateType)("Identifier"),
  343. typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterDeclaration"),
  344. typeAnnotation: (0, _utils.validateType)("TSType")
  345. }
  346. });
  347. defineType("TSInstantiationExpression", {
  348. aliases: ["Expression"],
  349. visitor: ["expression", "typeParameters"],
  350. fields: {
  351. expression: (0, _utils.validateType)("Expression"),
  352. ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
  353. }
  354. });
  355. const TSTypeExpression = {
  356. aliases: ["Expression", "LVal", "PatternLike"],
  357. visitor: ["expression", "typeAnnotation"],
  358. fields: {
  359. expression: (0, _utils.validateType)("Expression"),
  360. typeAnnotation: (0, _utils.validateType)("TSType")
  361. }
  362. };
  363. defineType("TSAsExpression", TSTypeExpression);
  364. defineType("TSSatisfiesExpression", TSTypeExpression);
  365. defineType("TSTypeAssertion", {
  366. aliases: ["Expression", "LVal", "PatternLike"],
  367. visitor: ["typeAnnotation", "expression"],
  368. fields: {
  369. typeAnnotation: (0, _utils.validateType)("TSType"),
  370. expression: (0, _utils.validateType)("Expression")
  371. }
  372. });
  373. defineType("TSEnumBody", {
  374. visitor: ["members"],
  375. fields: {
  376. members: (0, _utils.validateArrayOfType)("TSEnumMember")
  377. }
  378. });
  379. {
  380. defineType("TSEnumDeclaration", {
  381. aliases: ["Statement", "Declaration"],
  382. visitor: ["id", "members"],
  383. fields: {
  384. declare: (0, _utils.validateOptional)(bool),
  385. const: (0, _utils.validateOptional)(bool),
  386. id: (0, _utils.validateType)("Identifier"),
  387. members: (0, _utils.validateArrayOfType)("TSEnumMember"),
  388. initializer: (0, _utils.validateOptionalType)("Expression"),
  389. body: (0, _utils.validateOptionalType)("TSEnumBody")
  390. }
  391. });
  392. }
  393. defineType("TSEnumMember", {
  394. visitor: ["id", "initializer"],
  395. fields: {
  396. id: (0, _utils.validateType)("Identifier", "StringLiteral"),
  397. initializer: (0, _utils.validateOptionalType)("Expression")
  398. }
  399. });
  400. defineType("TSModuleDeclaration", {
  401. aliases: ["Statement", "Declaration"],
  402. visitor: ["id", "body"],
  403. fields: Object.assign({
  404. kind: {
  405. validate: (0, _utils.assertOneOf)("global", "module", "namespace")
  406. },
  407. declare: (0, _utils.validateOptional)(bool)
  408. }, {
  409. global: (0, _utils.validateOptional)(bool)
  410. }, {
  411. id: (0, _utils.validateType)("Identifier", "StringLiteral"),
  412. body: (0, _utils.validateType)("TSModuleBlock", "TSModuleDeclaration")
  413. })
  414. });
  415. defineType("TSModuleBlock", {
  416. aliases: ["Scopable", "Block", "BlockParent", "FunctionParent"],
  417. visitor: ["body"],
  418. fields: {
  419. body: (0, _utils.validateArrayOfType)("Statement")
  420. }
  421. });
  422. defineType("TSImportType", {
  423. aliases: ["TSType"],
  424. builder: ["argument", "qualifier", "typeParameters"],
  425. visitor: ["argument", "options", "qualifier", "typeParameters"],
  426. fields: {
  427. argument: (0, _utils.validateType)("StringLiteral"),
  428. qualifier: (0, _utils.validateOptionalType)("TSEntityName"),
  429. ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation"),
  430. options: {
  431. validate: (0, _utils.assertNodeType)("ObjectExpression"),
  432. optional: true
  433. }
  434. }
  435. });
  436. defineType("TSImportEqualsDeclaration", {
  437. aliases: ["Statement", "Declaration"],
  438. visitor: ["id", "moduleReference"],
  439. fields: Object.assign({}, {
  440. isExport: (0, _utils.validate)(bool)
  441. }, {
  442. id: (0, _utils.validateType)("Identifier"),
  443. moduleReference: (0, _utils.validateType)("TSEntityName", "TSExternalModuleReference"),
  444. importKind: {
  445. validate: (0, _utils.assertOneOf)("type", "value"),
  446. optional: true
  447. }
  448. })
  449. });
  450. defineType("TSExternalModuleReference", {
  451. visitor: ["expression"],
  452. fields: {
  453. expression: (0, _utils.validateType)("StringLiteral")
  454. }
  455. });
  456. defineType("TSNonNullExpression", {
  457. aliases: ["Expression", "LVal", "PatternLike"],
  458. visitor: ["expression"],
  459. fields: {
  460. expression: (0, _utils.validateType)("Expression")
  461. }
  462. });
  463. defineType("TSExportAssignment", {
  464. aliases: ["Statement"],
  465. visitor: ["expression"],
  466. fields: {
  467. expression: (0, _utils.validateType)("Expression")
  468. }
  469. });
  470. defineType("TSNamespaceExportDeclaration", {
  471. aliases: ["Statement"],
  472. visitor: ["id"],
  473. fields: {
  474. id: (0, _utils.validateType)("Identifier")
  475. }
  476. });
  477. defineType("TSTypeAnnotation", {
  478. visitor: ["typeAnnotation"],
  479. fields: {
  480. typeAnnotation: {
  481. validate: (0, _utils.assertNodeType)("TSType")
  482. }
  483. }
  484. });
  485. defineType("TSTypeParameterInstantiation", {
  486. visitor: ["params"],
  487. fields: {
  488. params: (0, _utils.validateArrayOfType)("TSType")
  489. }
  490. });
  491. defineType("TSTypeParameterDeclaration", {
  492. visitor: ["params"],
  493. fields: {
  494. params: (0, _utils.validateArrayOfType)("TSTypeParameter")
  495. }
  496. });
  497. defineType("TSTypeParameter", {
  498. builder: ["constraint", "default", "name"],
  499. visitor: ["constraint", "default"],
  500. fields: {
  501. name: {
  502. validate: (0, _utils.assertValueType)("string")
  503. },
  504. in: {
  505. validate: (0, _utils.assertValueType)("boolean"),
  506. optional: true
  507. },
  508. out: {
  509. validate: (0, _utils.assertValueType)("boolean"),
  510. optional: true
  511. },
  512. const: {
  513. validate: (0, _utils.assertValueType)("boolean"),
  514. optional: true
  515. },
  516. constraint: {
  517. validate: (0, _utils.assertNodeType)("TSType"),
  518. optional: true
  519. },
  520. default: {
  521. validate: (0, _utils.assertNodeType)("TSType"),
  522. optional: true
  523. }
  524. }
  525. });
  526. //# sourceMappingURL=typescript.js.map