123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- "use strict";
- var _utils = require("./utils.js");
- var _core = require("./core.js");
- var _is = require("../validators/is.js");
- const defineType = (0, _utils.defineAliasedType)("TypeScript");
- const bool = (0, _utils.assertValueType)("boolean");
- const tSFunctionTypeAnnotationCommon = () => ({
- returnType: {
- validate: (0, _utils.assertNodeType)("TSTypeAnnotation", "Noop"),
- optional: true
- },
- typeParameters: {
- validate: (0, _utils.assertNodeType)("TSTypeParameterDeclaration", "Noop"),
- optional: true
- }
- });
- defineType("TSParameterProperty", {
- aliases: ["LVal"],
- visitor: ["parameter"],
- fields: {
- accessibility: {
- validate: (0, _utils.assertOneOf)("public", "private", "protected"),
- optional: true
- },
- readonly: {
- validate: (0, _utils.assertValueType)("boolean"),
- optional: true
- },
- parameter: {
- validate: (0, _utils.assertNodeType)("Identifier", "AssignmentPattern")
- },
- override: {
- validate: (0, _utils.assertValueType)("boolean"),
- optional: true
- },
- decorators: {
- validate: (0, _utils.arrayOfType)("Decorator"),
- optional: true
- }
- }
- });
- defineType("TSDeclareFunction", {
- aliases: ["Statement", "Declaration"],
- visitor: ["id", "typeParameters", "params", "returnType"],
- fields: Object.assign({}, (0, _core.functionDeclarationCommon)(), tSFunctionTypeAnnotationCommon())
- });
- defineType("TSDeclareMethod", {
- visitor: ["decorators", "key", "typeParameters", "params", "returnType"],
- fields: Object.assign({}, (0, _core.classMethodOrDeclareMethodCommon)(), tSFunctionTypeAnnotationCommon())
- });
- defineType("TSQualifiedName", {
- aliases: ["TSEntityName"],
- visitor: ["left", "right"],
- fields: {
- left: (0, _utils.validateType)("TSEntityName"),
- right: (0, _utils.validateType)("Identifier")
- }
- });
- const signatureDeclarationCommon = () => ({
- typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterDeclaration"),
- ["parameters"]: (0, _utils.validateArrayOfType)("ArrayPattern", "Identifier", "ObjectPattern", "RestElement"),
- ["typeAnnotation"]: (0, _utils.validateOptionalType)("TSTypeAnnotation")
- });
- const callConstructSignatureDeclaration = {
- aliases: ["TSTypeElement"],
- visitor: ["typeParameters", "parameters", "typeAnnotation"],
- fields: signatureDeclarationCommon()
- };
- defineType("TSCallSignatureDeclaration", callConstructSignatureDeclaration);
- defineType("TSConstructSignatureDeclaration", callConstructSignatureDeclaration);
- const namedTypeElementCommon = () => ({
- key: (0, _utils.validateType)("Expression"),
- computed: {
- default: false
- },
- optional: (0, _utils.validateOptional)(bool)
- });
- defineType("TSPropertySignature", {
- aliases: ["TSTypeElement"],
- visitor: ["key", "typeAnnotation"],
- fields: Object.assign({}, namedTypeElementCommon(), {
- readonly: (0, _utils.validateOptional)(bool),
- typeAnnotation: (0, _utils.validateOptionalType)("TSTypeAnnotation"),
- kind: {
- optional: true,
- validate: (0, _utils.assertOneOf)("get", "set")
- }
- })
- });
- defineType("TSMethodSignature", {
- aliases: ["TSTypeElement"],
- visitor: ["key", "typeParameters", "parameters", "typeAnnotation"],
- fields: Object.assign({}, signatureDeclarationCommon(), namedTypeElementCommon(), {
- kind: {
- validate: (0, _utils.assertOneOf)("method", "get", "set")
- }
- })
- });
- defineType("TSIndexSignature", {
- aliases: ["TSTypeElement"],
- visitor: ["parameters", "typeAnnotation"],
- fields: {
- readonly: (0, _utils.validateOptional)(bool),
- static: (0, _utils.validateOptional)(bool),
- parameters: (0, _utils.validateArrayOfType)("Identifier"),
- typeAnnotation: (0, _utils.validateOptionalType)("TSTypeAnnotation")
- }
- });
- const tsKeywordTypes = ["TSAnyKeyword", "TSBooleanKeyword", "TSBigIntKeyword", "TSIntrinsicKeyword", "TSNeverKeyword", "TSNullKeyword", "TSNumberKeyword", "TSObjectKeyword", "TSStringKeyword", "TSSymbolKeyword", "TSUndefinedKeyword", "TSUnknownKeyword", "TSVoidKeyword"];
- for (const type of tsKeywordTypes) {
- defineType(type, {
- aliases: ["TSType", "TSBaseType"],
- visitor: [],
- fields: {}
- });
- }
- defineType("TSThisType", {
- aliases: ["TSType", "TSBaseType"],
- visitor: [],
- fields: {}
- });
- const fnOrCtrBase = {
- aliases: ["TSType"],
- visitor: ["typeParameters", "parameters", "typeAnnotation"]
- };
- defineType("TSFunctionType", Object.assign({}, fnOrCtrBase, {
- fields: signatureDeclarationCommon()
- }));
- defineType("TSConstructorType", Object.assign({}, fnOrCtrBase, {
- fields: Object.assign({}, signatureDeclarationCommon(), {
- abstract: (0, _utils.validateOptional)(bool)
- })
- }));
- defineType("TSTypeReference", {
- aliases: ["TSType"],
- visitor: ["typeName", "typeParameters"],
- fields: {
- typeName: (0, _utils.validateType)("TSEntityName"),
- ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
- }
- });
- defineType("TSTypePredicate", {
- aliases: ["TSType"],
- visitor: ["parameterName", "typeAnnotation"],
- builder: ["parameterName", "typeAnnotation", "asserts"],
- fields: {
- parameterName: (0, _utils.validateType)("Identifier", "TSThisType"),
- typeAnnotation: (0, _utils.validateOptionalType)("TSTypeAnnotation"),
- asserts: (0, _utils.validateOptional)(bool)
- }
- });
- defineType("TSTypeQuery", {
- aliases: ["TSType"],
- visitor: ["exprName", "typeParameters"],
- fields: {
- exprName: (0, _utils.validateType)("TSEntityName", "TSImportType"),
- ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
- }
- });
- defineType("TSTypeLiteral", {
- aliases: ["TSType"],
- visitor: ["members"],
- fields: {
- members: (0, _utils.validateArrayOfType)("TSTypeElement")
- }
- });
- defineType("TSArrayType", {
- aliases: ["TSType"],
- visitor: ["elementType"],
- fields: {
- elementType: (0, _utils.validateType)("TSType")
- }
- });
- defineType("TSTupleType", {
- aliases: ["TSType"],
- visitor: ["elementTypes"],
- fields: {
- elementTypes: (0, _utils.validateArrayOfType)("TSType", "TSNamedTupleMember")
- }
- });
- defineType("TSOptionalType", {
- aliases: ["TSType"],
- visitor: ["typeAnnotation"],
- fields: {
- typeAnnotation: (0, _utils.validateType)("TSType")
- }
- });
- defineType("TSRestType", {
- aliases: ["TSType"],
- visitor: ["typeAnnotation"],
- fields: {
- typeAnnotation: (0, _utils.validateType)("TSType")
- }
- });
- defineType("TSNamedTupleMember", {
- visitor: ["label", "elementType"],
- builder: ["label", "elementType", "optional"],
- fields: {
- label: (0, _utils.validateType)("Identifier"),
- optional: {
- validate: bool,
- default: false
- },
- elementType: (0, _utils.validateType)("TSType")
- }
- });
- const unionOrIntersection = {
- aliases: ["TSType"],
- visitor: ["types"],
- fields: {
- types: (0, _utils.validateArrayOfType)("TSType")
- }
- };
- defineType("TSUnionType", unionOrIntersection);
- defineType("TSIntersectionType", unionOrIntersection);
- defineType("TSConditionalType", {
- aliases: ["TSType"],
- visitor: ["checkType", "extendsType", "trueType", "falseType"],
- fields: {
- checkType: (0, _utils.validateType)("TSType"),
- extendsType: (0, _utils.validateType)("TSType"),
- trueType: (0, _utils.validateType)("TSType"),
- falseType: (0, _utils.validateType)("TSType")
- }
- });
- defineType("TSInferType", {
- aliases: ["TSType"],
- visitor: ["typeParameter"],
- fields: {
- typeParameter: (0, _utils.validateType)("TSTypeParameter")
- }
- });
- defineType("TSParenthesizedType", {
- aliases: ["TSType"],
- visitor: ["typeAnnotation"],
- fields: {
- typeAnnotation: (0, _utils.validateType)("TSType")
- }
- });
- defineType("TSTypeOperator", {
- aliases: ["TSType"],
- visitor: ["typeAnnotation"],
- fields: {
- operator: (0, _utils.validate)((0, _utils.assertValueType)("string")),
- typeAnnotation: (0, _utils.validateType)("TSType")
- }
- });
- defineType("TSIndexedAccessType", {
- aliases: ["TSType"],
- visitor: ["objectType", "indexType"],
- fields: {
- objectType: (0, _utils.validateType)("TSType"),
- indexType: (0, _utils.validateType)("TSType")
- }
- });
- defineType("TSMappedType", {
- aliases: ["TSType"],
- visitor: ["typeParameter", "nameType", "typeAnnotation"],
- builder: ["typeParameter", "typeAnnotation", "nameType"],
- fields: Object.assign({}, {
- typeParameter: (0, _utils.validateType)("TSTypeParameter")
- }, {
- readonly: (0, _utils.validateOptional)((0, _utils.assertOneOf)(true, false, "+", "-")),
- optional: (0, _utils.validateOptional)((0, _utils.assertOneOf)(true, false, "+", "-")),
- typeAnnotation: (0, _utils.validateOptionalType)("TSType"),
- nameType: (0, _utils.validateOptionalType)("TSType")
- })
- });
- defineType("TSTemplateLiteralType", {
- aliases: ["TSType", "TSBaseType"],
- visitor: ["quasis", "types"],
- fields: {
- quasis: (0, _utils.validateArrayOfType)("TemplateElement"),
- types: {
- validate: (0, _utils.chain)((0, _utils.assertValueType)("array"), (0, _utils.assertEach)((0, _utils.assertNodeType)("TSType")), function (node, key, val) {
- if (node.quasis.length !== val.length + 1) {
- 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}`);
- }
- })
- }
- }
- });
- defineType("TSLiteralType", {
- aliases: ["TSType", "TSBaseType"],
- visitor: ["literal"],
- fields: {
- literal: {
- validate: function () {
- const unaryExpression = (0, _utils.assertNodeType)("NumericLiteral", "BigIntLiteral");
- const unaryOperator = (0, _utils.assertOneOf)("-");
- const literal = (0, _utils.assertNodeType)("NumericLiteral", "StringLiteral", "BooleanLiteral", "BigIntLiteral", "TemplateLiteral");
- function validator(parent, key, node) {
- if ((0, _is.default)("UnaryExpression", node)) {
- unaryOperator(node, "operator", node.operator);
- unaryExpression(node, "argument", node.argument);
- } else {
- literal(parent, key, node);
- }
- }
- validator.oneOfNodeTypes = ["NumericLiteral", "StringLiteral", "BooleanLiteral", "BigIntLiteral", "TemplateLiteral", "UnaryExpression"];
- return validator;
- }()
- }
- }
- });
- {
- defineType("TSExpressionWithTypeArguments", {
- aliases: ["TSType"],
- visitor: ["expression", "typeParameters"],
- fields: {
- expression: (0, _utils.validateType)("TSEntityName"),
- typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
- }
- });
- }
- defineType("TSInterfaceDeclaration", {
- aliases: ["Statement", "Declaration"],
- visitor: ["id", "typeParameters", "extends", "body"],
- fields: {
- declare: (0, _utils.validateOptional)(bool),
- id: (0, _utils.validateType)("Identifier"),
- typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterDeclaration"),
- extends: (0, _utils.validateOptional)((0, _utils.arrayOfType)("TSExpressionWithTypeArguments")),
- body: (0, _utils.validateType)("TSInterfaceBody")
- }
- });
- defineType("TSInterfaceBody", {
- visitor: ["body"],
- fields: {
- body: (0, _utils.validateArrayOfType)("TSTypeElement")
- }
- });
- defineType("TSTypeAliasDeclaration", {
- aliases: ["Statement", "Declaration"],
- visitor: ["id", "typeParameters", "typeAnnotation"],
- fields: {
- declare: (0, _utils.validateOptional)(bool),
- id: (0, _utils.validateType)("Identifier"),
- typeParameters: (0, _utils.validateOptionalType)("TSTypeParameterDeclaration"),
- typeAnnotation: (0, _utils.validateType)("TSType")
- }
- });
- defineType("TSInstantiationExpression", {
- aliases: ["Expression"],
- visitor: ["expression", "typeParameters"],
- fields: {
- expression: (0, _utils.validateType)("Expression"),
- ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation")
- }
- });
- const TSTypeExpression = {
- aliases: ["Expression", "LVal", "PatternLike"],
- visitor: ["expression", "typeAnnotation"],
- fields: {
- expression: (0, _utils.validateType)("Expression"),
- typeAnnotation: (0, _utils.validateType)("TSType")
- }
- };
- defineType("TSAsExpression", TSTypeExpression);
- defineType("TSSatisfiesExpression", TSTypeExpression);
- defineType("TSTypeAssertion", {
- aliases: ["Expression", "LVal", "PatternLike"],
- visitor: ["typeAnnotation", "expression"],
- fields: {
- typeAnnotation: (0, _utils.validateType)("TSType"),
- expression: (0, _utils.validateType)("Expression")
- }
- });
- defineType("TSEnumBody", {
- visitor: ["members"],
- fields: {
- members: (0, _utils.validateArrayOfType)("TSEnumMember")
- }
- });
- {
- defineType("TSEnumDeclaration", {
- aliases: ["Statement", "Declaration"],
- visitor: ["id", "members"],
- fields: {
- declare: (0, _utils.validateOptional)(bool),
- const: (0, _utils.validateOptional)(bool),
- id: (0, _utils.validateType)("Identifier"),
- members: (0, _utils.validateArrayOfType)("TSEnumMember"),
- initializer: (0, _utils.validateOptionalType)("Expression"),
- body: (0, _utils.validateOptionalType)("TSEnumBody")
- }
- });
- }
- defineType("TSEnumMember", {
- visitor: ["id", "initializer"],
- fields: {
- id: (0, _utils.validateType)("Identifier", "StringLiteral"),
- initializer: (0, _utils.validateOptionalType)("Expression")
- }
- });
- defineType("TSModuleDeclaration", {
- aliases: ["Statement", "Declaration"],
- visitor: ["id", "body"],
- fields: Object.assign({
- kind: {
- validate: (0, _utils.assertOneOf)("global", "module", "namespace")
- },
- declare: (0, _utils.validateOptional)(bool)
- }, {
- global: (0, _utils.validateOptional)(bool)
- }, {
- id: (0, _utils.validateType)("Identifier", "StringLiteral"),
- body: (0, _utils.validateType)("TSModuleBlock", "TSModuleDeclaration")
- })
- });
- defineType("TSModuleBlock", {
- aliases: ["Scopable", "Block", "BlockParent", "FunctionParent"],
- visitor: ["body"],
- fields: {
- body: (0, _utils.validateArrayOfType)("Statement")
- }
- });
- defineType("TSImportType", {
- aliases: ["TSType"],
- builder: ["argument", "qualifier", "typeParameters"],
- visitor: ["argument", "options", "qualifier", "typeParameters"],
- fields: {
- argument: (0, _utils.validateType)("StringLiteral"),
- qualifier: (0, _utils.validateOptionalType)("TSEntityName"),
- ["typeParameters"]: (0, _utils.validateOptionalType)("TSTypeParameterInstantiation"),
- options: {
- validate: (0, _utils.assertNodeType)("ObjectExpression"),
- optional: true
- }
- }
- });
- defineType("TSImportEqualsDeclaration", {
- aliases: ["Statement", "Declaration"],
- visitor: ["id", "moduleReference"],
- fields: Object.assign({}, {
- isExport: (0, _utils.validate)(bool)
- }, {
- id: (0, _utils.validateType)("Identifier"),
- moduleReference: (0, _utils.validateType)("TSEntityName", "TSExternalModuleReference"),
- importKind: {
- validate: (0, _utils.assertOneOf)("type", "value"),
- optional: true
- }
- })
- });
- defineType("TSExternalModuleReference", {
- visitor: ["expression"],
- fields: {
- expression: (0, _utils.validateType)("StringLiteral")
- }
- });
- defineType("TSNonNullExpression", {
- aliases: ["Expression", "LVal", "PatternLike"],
- visitor: ["expression"],
- fields: {
- expression: (0, _utils.validateType)("Expression")
- }
- });
- defineType("TSExportAssignment", {
- aliases: ["Statement"],
- visitor: ["expression"],
- fields: {
- expression: (0, _utils.validateType)("Expression")
- }
- });
- defineType("TSNamespaceExportDeclaration", {
- aliases: ["Statement"],
- visitor: ["id"],
- fields: {
- id: (0, _utils.validateType)("Identifier")
- }
- });
- defineType("TSTypeAnnotation", {
- visitor: ["typeAnnotation"],
- fields: {
- typeAnnotation: {
- validate: (0, _utils.assertNodeType)("TSType")
- }
- }
- });
- defineType("TSTypeParameterInstantiation", {
- visitor: ["params"],
- fields: {
- params: (0, _utils.validateArrayOfType)("TSType")
- }
- });
- defineType("TSTypeParameterDeclaration", {
- visitor: ["params"],
- fields: {
- params: (0, _utils.validateArrayOfType)("TSTypeParameter")
- }
- });
- defineType("TSTypeParameter", {
- builder: ["constraint", "default", "name"],
- visitor: ["constraint", "default"],
- fields: {
- name: {
- validate: (0, _utils.assertValueType)("string")
- },
- in: {
- validate: (0, _utils.assertValueType)("boolean"),
- optional: true
- },
- out: {
- validate: (0, _utils.assertValueType)("boolean"),
- optional: true
- },
- const: {
- validate: (0, _utils.assertValueType)("boolean"),
- optional: true
- },
- constraint: {
- validate: (0, _utils.assertNodeType)("TSType"),
- optional: true
- },
- default: {
- validate: (0, _utils.assertNodeType)("TSType"),
- optional: true
- }
- }
- });
- //# sourceMappingURL=typescript.js.map
|