UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

245 lines (243 loc) 9.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const manipulation_1 = require("./../../manipulation"); const errors = require("./../../errors"); const utils_1 = require("./../../utils"); const callBaseFill_1 = require("./../callBaseFill"); const common_1 = require("./../common"); const base_1 = require("./../base"); const namespace_1 = require("./../namespace"); exports.InterfaceDeclarationBase = base_1.TextInsertableNode(base_1.ExtendsClauseableNode(base_1.HeritageClauseableNode(base_1.TypeParameteredNode(base_1.DocumentationableNode(base_1.AmbientableNode(namespace_1.NamespaceChildableNode(base_1.ExportableNode(base_1.ModifierableNode(base_1.NamedNode(common_1.Node)))))))))); class InterfaceDeclaration extends exports.InterfaceDeclarationBase { /** * Fills the node from a structure. * @param structure - Structure to fill. */ fill(structure) { callBaseFill_1.callBaseFill(exports.InterfaceDeclarationBase.prototype, this, structure); if (structure.constructSignatures != null) this.addConstructSignatures(structure.constructSignatures); if (structure.properties != null) this.addProperties(structure.properties); if (structure.methods != null) this.addMethods(structure.methods); return this; } /** * Add construct signature. * @param structure - Structure representing the construct signature. */ addConstructSignature(structure) { return this.addConstructSignatures([structure])[0]; } /** * Add construct signatures. * @param structures - Structures representing the construct signatures. */ addConstructSignatures(structures) { return this.insertConstructSignatures(manipulation_1.getEndIndexFromArray(this.compilerNode.members), structures); } /** * Insert construct signature. * @param index - Index to insert at. * @param structure - Structure representing the construct signature. */ insertConstructSignature(index, structure) { return this.insertConstructSignatures(index, [structure])[0]; } /** * Insert properties. * @param index - Index to insert at. * @param structures - Structures representing the construct signatures. */ insertConstructSignatures(index, structures) { const indentationText = this.getChildIndentationText(); // create code const codes = []; for (const structure of structures) { let code = `${indentationText}new()`; if (structure.returnType != null && structure.returnType.length > 0) code += `: ${structure.returnType}`; code += ";"; codes.push(code); } return manipulation_1.insertIntoBracesOrSourceFileWithFillAndGetChildren({ getIndexedChildren: () => this.getAllMembers(), sourceFile: this.getSourceFile(), parent: this, index, childCodes: codes, structures, expectedKind: ts.SyntaxKind.ConstructSignature, fillFunction: (node, structure) => node.fill(structure) }); } /** * Gets the first construct signature by a find function. * @param findFunction - Function to find the construct signature by. */ getConstructSignature(findFunction) { return this.getConstructSignatures().find(findFunction); } /** * Gets the first construct signature by a find function or throws if not found. * @param findFunction - Function to find the construct signature by. */ getConstructSignatureOrThrow(findFunction) { return errors.throwIfNullOrUndefined(this.getConstructSignature(findFunction), "Expected to find a construct signature with the provided condition."); } /** * Gets the interface method signatures. */ getConstructSignatures() { return this.compilerNode.members.filter(m => m.kind === ts.SyntaxKind.ConstructSignature) .map(m => this.global.compilerFactory.getNodeFromCompilerNode(m, this.sourceFile)); } /** * Add method. * @param structure - Structure representing the method. */ addMethod(structure) { return this.addMethods([structure])[0]; } /** * Add methods. * @param structures - Structures representing the methods. */ addMethods(structures) { return this.insertMethods(manipulation_1.getEndIndexFromArray(this.compilerNode.members), structures); } /** * Insert method. * @param index - Index to insert at. * @param structure - Structure representing the method. */ insertMethod(index, structure) { return this.insertMethods(index, [structure])[0]; } /** * Insert methods. * @param index - Index to insert at. * @param structures - Structures representing the methods. */ insertMethods(index, structures) { const indentationText = this.getChildIndentationText(); // create code const codes = []; for (const structure of structures) { let code = indentationText; code += structure.name; if (structure.hasQuestionToken) code += "?"; code += "()"; if (structure.returnType != null && structure.returnType.length > 0) code += `: ${structure.returnType}`; code += ";"; codes.push(code); } // insert, fill, and get created nodes return manipulation_1.insertIntoBracesOrSourceFileWithFillAndGetChildren({ getIndexedChildren: () => this.getAllMembers(), sourceFile: this.getSourceFile(), parent: this, index, childCodes: codes, structures, expectedKind: ts.SyntaxKind.MethodSignature, fillFunction: (node, structure) => node.fill(structure) }); } getMethod(nameOrFindFunction) { return utils_1.getNamedNodeByNameOrFindFunction(this.getMethods(), nameOrFindFunction); } getMethodOrThrow(nameOrFindFunction) { return errors.throwIfNullOrUndefined(this.getMethod(nameOrFindFunction), () => utils_1.getNotFoundErrorMessageForNameOrFindFunction("interface method signature", nameOrFindFunction)); } /** * Gets the interface method signatures. */ getMethods() { return this.compilerNode.members.filter(m => m.kind === ts.SyntaxKind.MethodSignature) .map(m => this.global.compilerFactory.getNodeFromCompilerNode(m, this.sourceFile)); } /** * Add property. * @param structure - Structure representing the property. */ addProperty(structure) { return this.addProperties([structure])[0]; } /** * Add properties. * @param structures - Structures representing the properties. */ addProperties(structures) { return this.insertProperties(manipulation_1.getEndIndexFromArray(this.compilerNode.members), structures); } /** * Insert property. * @param index - Index to insert at. * @param structure - Structure representing the property. */ insertProperty(index, structure) { return this.insertProperties(index, [structure])[0]; } /** * Insert properties. * @param index - Index to insert at. * @param structures - Structures representing the properties. */ insertProperties(index, structures) { const indentationText = this.getChildIndentationText(); // create code const codes = []; for (const structure of structures) { let code = `${indentationText}`; code += structure.name; if (structure.hasQuestionToken) code += "?"; if (structure.type != null && structure.type.length > 0) code += `: ${structure.type}`; code += ";"; codes.push(code); } return manipulation_1.insertIntoBracesOrSourceFileWithFillAndGetChildren({ getIndexedChildren: () => this.getAllMembers(), sourceFile: this.getSourceFile(), parent: this, index, childCodes: codes, structures, expectedKind: ts.SyntaxKind.PropertySignature, fillFunction: (node, structure) => node.fill(structure) }); } getProperty(nameOrFindFunction) { return utils_1.getNamedNodeByNameOrFindFunction(this.getProperties(), nameOrFindFunction); } getPropertyOrThrow(nameOrFindFunction) { return errors.throwIfNullOrUndefined(this.getProperty(nameOrFindFunction), () => utils_1.getNotFoundErrorMessageForNameOrFindFunction("interface property signature", nameOrFindFunction)); } /** * Gets the interface property signatures. */ getProperties() { return this.compilerNode.members.filter(m => m.kind === ts.SyntaxKind.PropertySignature) .map(m => this.global.compilerFactory.getNodeFromCompilerNode(m, this.sourceFile)); } /** * Gets all members. */ getAllMembers() { return this.compilerNode.members.map(m => this.global.compilerFactory.getNodeFromCompilerNode(m, this.sourceFile)); } /** * Removes this interface declaration. */ remove() { manipulation_1.removeStatementedNodeChild(this); } } exports.InterfaceDeclaration = InterfaceDeclaration; //# sourceMappingURL=InterfaceDeclaration.js.map