UNPKG

@spec2ts/core

Version:

Core module for @spec2ts modules, includes codegen helpers and common parsing methods

125 lines (124 loc) 7.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createImportSpecifier = exports.updateVariableDeclarationInitializer = exports.createTypeOrInterfaceDeclaration = exports.createNamespaceImportDeclaration = exports.createDefaultImportDeclaration = exports.createNamedImportDeclaration = exports.createIndexSignature = exports.createPropertyAssignment = exports.createPropertySignature = exports.createParameter = exports.createMethod = exports.createConstructor = exports.createClassDeclaration = exports.createInterfaceDeclaration = exports.createFunctionDeclaration = exports.createTypeAliasDeclaration = void 0; const ts = require("typescript"); const common_1 = require("./common"); const expression_1 = require("./expression"); function createTypeAliasDeclaration({ modifiers, name, typeParameters, type }) { return ts.factory.createTypeAliasDeclaration(modifiers, name, typeParameters, type); } exports.createTypeAliasDeclaration = createTypeAliasDeclaration; function createFunctionDeclaration(name, { modifiers, asteriskToken, typeParameters, type }, parameters, body) { return ts.factory.createFunctionDeclaration(modifiers, asteriskToken, name, typeParameters, parameters, type, body); } exports.createFunctionDeclaration = createFunctionDeclaration; function createInterfaceDeclaration({ modifiers, name, typeParameters, heritageClauses, members }) { return ts.factory.createInterfaceDeclaration(modifiers, name, typeParameters, heritageClauses, members); } exports.createInterfaceDeclaration = createInterfaceDeclaration; function createClassDeclaration({ modifiers, name, typeParameters, heritageClauses, members }) { return ts.factory.createClassDeclaration(modifiers, name, typeParameters, heritageClauses, members); } exports.createClassDeclaration = createClassDeclaration; function createConstructor({ modifiers, parameters, body }) { return ts.factory.createConstructorDeclaration(modifiers, parameters, body); } exports.createConstructor = createConstructor; function createMethod(name, { modifiers, asteriskToken, questionToken, typeParameters, type } = {}, parameters = [], body) { return ts.factory.createMethodDeclaration(modifiers, asteriskToken, name, (0, common_1.createQuestionToken)(questionToken), typeParameters, parameters, type, body); } exports.createMethod = createMethod; function createParameter(name, { modifiers, dotDotDotToken, questionToken, type, initializer }) { return ts.factory.createParameterDeclaration(modifiers, dotDotDotToken, name, (0, common_1.createQuestionToken)(questionToken), type, initializer); } exports.createParameter = createParameter; function createPropertySignature({ modifiers, name, questionToken, type, }) { return ts.factory.createPropertySignature(modifiers, (0, expression_1.toPropertyName)(name), (0, common_1.createQuestionToken)(questionToken), type); } exports.createPropertySignature = createPropertySignature; function createPropertyAssignment(name, expression) { if (ts.isIdentifier(expression)) { if (expression.text === name) { return ts.factory.createShorthandPropertyAssignment(name); } } return ts.factory.createPropertyAssignment((0, expression_1.toPropertyName)(name), expression); } exports.createPropertyAssignment = createPropertyAssignment; function createIndexSignature(type, { modifiers, indexName = "key", indexType = common_1.keywordType.string } = {}) { return ts.factory.createIndexSignature(modifiers, [createParameter(indexName, { type: indexType })], type); } exports.createIndexSignature = createIndexSignature; function createNamedImportDeclaration({ modifiers, bindings, isTypeOnly, moduleSpecifier }) { return ts.factory.createImportDeclaration(modifiers, ts.factory.createImportClause(isTypeOnly || false, undefined, ts.factory.createNamedImports(bindings.map(createImportSpecifier))), (0, expression_1.toLiteral)(moduleSpecifier)); } exports.createNamedImportDeclaration = createNamedImportDeclaration; function createDefaultImportDeclaration({ modifiers, name, bindings, isTypeOnly, moduleSpecifier }) { return ts.factory.createImportDeclaration(modifiers, ts.factory.createImportClause(isTypeOnly || false, (0, expression_1.toIdentifier)(name), bindings ? ts.factory.createNamedImports(bindings.map(createImportSpecifier)) : undefined), (0, expression_1.toLiteral)(moduleSpecifier)); } exports.createDefaultImportDeclaration = createDefaultImportDeclaration; function createNamespaceImportDeclaration({ modifiers, name, isTypeOnly, moduleSpecifier }) { return ts.factory.createImportDeclaration(modifiers, ts.factory.createImportClause(isTypeOnly || false, undefined, ts.factory.createNamespaceImport((0, expression_1.toIdentifier)(name))), (0, expression_1.toLiteral)(moduleSpecifier)); } exports.createNamespaceImportDeclaration = createNamespaceImportDeclaration; function createTypeOrInterfaceDeclaration({ modifiers, name, type }) { if (ts.isTypeLiteralNode(type)) { return createInterfaceDeclaration({ modifiers, name, members: type.members }); } if (ts.isIntersectionTypeNode(type)) { const isExtendCompatible = type.types.every(t => ts.isTypeLiteralNode(t) || (ts.isTypeReferenceNode(t) && ts.isIdentifier(t.typeName))); if (isExtendCompatible) { const members = []; const extend = []; type.types.forEach(t => { if (ts.isTypeLiteralNode(t)) { members.push(...t.members); } if (ts.isTypeReferenceNode(t) && ts.isIdentifier(t.typeName)) { extend.push(t.typeName); } }); return createInterfaceDeclaration({ modifiers, name, members, heritageClauses: [ ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, extend.map(t => ts.factory.createExpressionWithTypeArguments(t, undefined))) ] }); } } return createTypeAliasDeclaration({ modifiers, name, type }); } exports.createTypeOrInterfaceDeclaration = createTypeOrInterfaceDeclaration; function updateVariableDeclarationInitializer(declaration, initializer) { return ts.factory.updateVariableDeclaration(declaration, declaration.name, declaration.exclamationToken, declaration.type, initializer); } exports.updateVariableDeclarationInitializer = updateVariableDeclarationInitializer; function createImportSpecifier(binding) { if (typeof binding === "string" || (0, expression_1.isIdentifier)(binding)) { if (ts.factory.createImportSpecifier.length === 2) { return ts.factory.createImportSpecifier(undefined, (0, expression_1.toIdentifier)(binding)); } else { return ts.factory.createImportSpecifier(false, undefined, (0, expression_1.toIdentifier)(binding)); } } if (ts.factory.createImportSpecifier.length === 2) { return ts.factory.createImportSpecifier((0, expression_1.toIdentifier)(binding.propertyName), (0, expression_1.toIdentifier)(binding.name)); } else { return ts.factory.createImportSpecifier(binding.type || false, (0, expression_1.toIdentifier)(binding.propertyName), (0, expression_1.toIdentifier)(binding.name)); } } exports.createImportSpecifier = createImportSpecifier;