51b3fadc41548d00dff324d50cd3a49c55c82ee9aa71340dd1ee792296cd8e5eaafd61006f6bf5e0776b54635142cb4cee978f6fbe0a338212659b94720628 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. fields: {
  244. operator: (0, _utils.validate)((0, _utils.assertValueType)("string")),
  245. typeAnnotation: (0, _utils.validateType)("TSType")
  246. }
  247. });
  248. defineType("TSIndexedAccessType", {
  249. aliases: ["TSType"],
  250. visitor: ["objectType", "indexType"],
  251. fields: {
  252. objectType: (0, _utils.validateType)("TSType"),
  253. indexType: (0, _utils.validateType)("TSType")
  254. }
  255. });
  256. defineType("TSMappedType", {
  257. aliases: ["TSType"],
  258. visitor: ["typeParameter", "nameType", "typeAnnotation"],
  259. builder: ["typeParameter", "typeAnnotation", "nameType"],
  260. fields: Object.assign({}, {
  261. typeParameter: (0, _utils.validateType)("TSTypeParameter")
  262. }, {
  263. readonly: (0, _utils.validateOptional)((0, _utils.assertOneOf)(true, false, "+", "-")),
  264. optional: (0, _utils.validateOptional)((0, _utils.assertOneOf)(true, false, "+", "-")),
  265. typeAnnotation: (0, _utils.validateOptionalType)("TSType"),
  266. nameType: (0, _utils.validateOptionalType)("TSType")
  267. })
  268. });
  269. defineType("TSTemplateLiteralType", {
  270. aliases: ["TSType", "TSBaseType"],
  271. visitor: ["quasis", "types"],
  272. fields: {
  273. quasis: (0, _utils.validateArrayOfType)("TemplateElement"),
  274. types: {
  275. validate: (0, _utils.chain)((0, _utils.assertValueType)("array"), (0, _utils.assertEach)((0, _utils.assertNodeType)("TSType")), function (node, key, val) {
  276. if (node.quasis.length !== val.length + 1) {
  277. 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}`);
  278. }
  279. })
  280. }
  281. }
  282. });
  283. defineType("TSLiteralType", {
  284. aliases: ["TSType", "TSBaseType"],
  285. visitor: ["literal"],
  286. fields: {
  287. literal: {
  288. validate: function () {
  289. const unaryExpression = (0, _utils.assertNodeType)("NumericLiteral", "BigIntLiteral");
  290. const unaryOperator = (0, _utils.assertOneOf)("-");
  291. const literal = (0, _utils.assertNodeType)("NumericLiteral", "StringLiteral", "BooleanLiteral", "BigIntLiteral", "TemplateLiteral");
  292. function validator(parent, key, node) {
  293. if ((0, _is.default)("UnaryExpression", node)) {
  294. unaryOperator(node, "operator", node.operator);
  295. unaryExpression(node, "argument", node.argument);
  296. } else {
  297. literal(parent, key, node);
  298. }
  299. }
  300. validator.oneOfNodeTypes = ["NumericLiteral", "StringLiteral", "BooleanLiteral", "BigIntLiteral", "TemplateLiteral", "UnaryExpression"];
  301. return validator;
  302. }()
  303. }
  304. }
  305. });
  306. {
  307. defineType("TSExpressionWithTypeArguments", {
  308. aliases: ["TSType"],
  309. visitor: ["expression", "typeParameters"],
  310. fields: {
  311. expression: (0, _utils.validateType)("TSEntityName"),
  312. typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
  313. }
  314. });
  315. }
  316. defineType("TSInterfaceDeclaration", {
  317. aliases: ["Statement", "Declaration"],
  318. visitor: ["id", "typeParameters", "extends", "body"],
  319. fields: {
  320. declare: (0, _utils.validateOptional)(bool),
  321. id: (0, _utils.validateType)("Identifier"),
  322. typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterDeclaration"),
  323. extends: (0, _utils.validateOptional)((0, _utils.arrayOfType)("TSExpressionWithTypeArguments")),
  324. body: (0, _utils.validateType)("TSInterfaceBody")
  325. }
  326. });
  327. defineType("TSInterfaceBody", {
  328. visitor: ["body"],
  329. fields: {
  330. body: (0, _utils.validateArrayOfType)("TSTypeElement")
  331. }
  332. });
  333. defineType("TSTypeAliasDeclaration", {
  334. aliases: ["Statement", "Declaration"],
  335. visitor: ["id", "typeParameters", "typeAnnotation"],
  336. fields: {
  337. declare: (0, _utils.validateOptional)(bool),
  338. id: (0, _utils.validateType)("Identifier"),
  339. typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterDeclaration"),
  340. typeAnnotation: (0, _utils.validateType)("TSType")
  341. }
  342. });
  343. defineType("TSInstantiationExpression", {
  344. aliases: ["Expression"],
  345. visitor: ["expression", "typeParameters"],
  346. fields: {
  347. expression: (0, _utils.validateType)("Expression"),
  348. ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
  349. }
  350. });
  351. const TSTypeExpression = {
  352. aliases: ["Expression", "LVal", "PatternLike"],
  353. visitor: ["expression", "typeAnnotation"],
  354. fields: {
  355. expression: (0, _utils.validateType)("Expression"),
  356. typeAnnotation: (0, _utils.validateType)("TSType")
  357. }
  358. };
  359. defineType("TSAsExpression", TSTypeExpression);
  360. defineType("TSSatisfiesExpression", TSTypeExpression);
  361. defineType("TSTypeAssertion", {
  362. aliases: ["Expression", "LVal", "PatternLike"],
  363. visitor: ["typeAnnotation", "expression"],
  364. fields: {
  365. typeAnnotation: (0, _utils.validateType)("TSType"),
  366. expression: (0, _utils.validateType)("Expression")
  367. }
  368. });
  369. defineType("TSEnumBody", {
  370. visitor: ["members"],
  371. fields: {
  372. members: (0, _utils.validateArrayOfType)("TSEnumMember")
  373. }
  374. });
  375. {
  376. defineType("TSEnumDeclaration", {
  377. aliases: ["Statement", "Declaration"],
  378. visitor: ["id", "members"],
  379. fields: {
  380. declare: (0, _utils.validateOptional)(bool),
  381. const: (0, _utils.validateOptional)(bool),
  382. id: (0, _utils.validateType)("Identifier"),
  383. members: (0, _utils.validateArrayOfType)("TSEnumMember"),
  384. initializer: (0, _utils.validateOptionalType)("Expression"),
  385. body: (0, _utils.validateOptionalType)("TSEnumBody")
  386. }
  387. });
  388. }
  389. defineType("TSEnumMember", {
  390. visitor: ["id", "initializer"],
  391. fields: {
  392. id: (0, _utils.validateType)("Identifier", "StringLiteral"),
  393. initializer: (0, _utils.validateOptionalType)("Expression")
  394. }
  395. });
  396. defineType("TSModuleDeclaration", {
  397. aliases: ["Statement", "Declaration"],
  398. visitor: ["id", "body"],
  399. fields: Object.assign({
  400. kind: {
  401. validate: (0, _utils.assertOneOf)("global", "module", "namespace")
  402. },
  403. declare: (0, _utils.validateOptional)(bool)
  404. }, {
  405. global: (0, _utils.validateOptional)(bool)
  406. }, {
  407. id: (0, _utils.validateType)("Identifier", "StringLiteral"),
  408. body: (0, _utils.validateType)("TSModuleBlock", "TSModuleDeclaration")
  409. })
  410. });
  411. defineType("TSModuleBlock", {
  412. aliases: ["Scopable", "Block", "BlockParent", "FunctionParent"],
  413. visitor: ["body"],
  414. fields: {
  415. body: (0, _utils.validateArrayOfType)("Statement")
  416. }
  417. });
  418. defineType("TSImportType", {
  419. aliases: ["TSType"],
  420. builder: ["argument", "qualifier", "typeParameters"],
  421. visitor: ["argument", "options", "qualifier", "typeParameters"],
  422. fields: {
  423. argument: (0, _utils.validateType)("StringLiteral"),
  424. qualifier: (0, _utils.validateOptionalType)("TSEntityName"),
  425. ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation"),
  426. options: {
  427. validate: (0, _utils.assertNodeType)("ObjectExpression"),
  428. optional: true
  429. }
  430. }
  431. });
  432. defineType("TSImportEqualsDeclaration", {
  433. aliases: ["Statement", "Declaration"],
  434. visitor: ["id", "moduleReference"],
  435. fields: Object.assign({}, {
  436. isExport: (0, _utils.validate)(bool)
  437. }, {
  438. id: (0, _utils.validateType)("Identifier"),
  439. moduleReference: (0, _utils.validateType)("TSEntityName", "TSExternalModuleReference"),
  440. importKind: {
  441. validate: (0, _utils.assertOneOf)("type", "value"),
  442. optional: true
  443. }
  444. })
  445. });
  446. defineType("TSExternalModuleReference", {
  447. visitor: ["expression"],
  448. fields: {
  449. expression: (0, _utils.validateType)("StringLiteral")
  450. }
  451. });
  452. defineType("TSNonNullExpression", {
  453. aliases: ["Expression", "LVal", "PatternLike"],
  454. visitor: ["expression"],
  455. fields: {
  456. expression: (0, _utils.validateType)("Expression")
  457. }
  458. });
  459. defineType("TSExportAssignment", {
  460. aliases: ["Statement"],
  461. visitor: ["expression"],
  462. fields: {
  463. expression: (0, _utils.validateType)("Expression")
  464. }
  465. });
  466. defineType("TSNamespaceExportDeclaration", {
  467. aliases: ["Statement"],
  468. visitor: ["id"],
  469. fields: {
  470. id: (0, _utils.validateType)("Identifier")
  471. }
  472. });
  473. defineType("TSTypeAnnotation", {
  474. visitor: ["typeAnnotation"],
  475. fields: {
  476. typeAnnotation: {
  477. validate: (0, _utils.assertNodeType)("TSType")
  478. }
  479. }
  480. });
  481. defineType("TSTypeParameterInstantiation", {
  482. visitor: ["params"],
  483. fields: {
  484. params: (0, _utils.validateArrayOfType)("TSType")
  485. }
  486. });
  487. defineType("TSTypeParameterDeclaration", {
  488. visitor: ["params"],
  489. fields: {
  490. params: (0, _utils.validateArrayOfType)("TSTypeParameter")
  491. }
  492. });
  493. defineType("TSTypeParameter", {
  494. builder: ["constraint", "default", "name"],
  495. visitor: ["constraint", "default"],
  496. fields: {
  497. name: {
  498. validate: (0, _utils.assertValueType)("string")
  499. },
  500. in: {
  501. validate: (0, _utils.assertValueType)("boolean"),
  502. optional: true
  503. },
  504. out: {
  505. validate: (0, _utils.assertValueType)("boolean"),
  506. optional: true
  507. },
  508. const: {
  509. validate: (0, _utils.assertValueType)("boolean"),
  510. optional: true
  511. },
  512. constraint: {
  513. validate: (0, _utils.assertNodeType)("TSType"),
  514. optional: true
  515. },
  516. default: {
  517. validate: (0, _utils.assertNodeType)("TSType"),
  518. optional: true
  519. }
  520. }
  521. });
  522. //# sourceMappingURL=typescript.js.map