diff --git a/code-generation/ensureOverloadStructuresMatch.ts b/code-generation/ensureOverloadStructuresMatch.ts index 4b9ba4b20..05b038a15 100644 --- a/code-generation/ensureOverloadStructuresMatch.ts +++ b/code-generation/ensureOverloadStructuresMatch.ts @@ -24,11 +24,11 @@ for (const overloadVM of overloadVMs) { } for (const remainingOverloadExtension of flattenedOverloadExtensions) { - problems.push(`Overload extension of ${remainingOverloadExtension.name} does not exist on main structure.`); + problems.push(`${overloadVM.name}: Overload extension of ${remainingOverloadExtension.name} does not exist on main structure.`); } for (const remainingStructureExtension of flattenedStructureExtensions) { - problems.push(`Structure extension of ${remainingStructureExtension.name} does not exist on overload structure.`); + problems.push(`${overloadVM.name}: Structure extension of ${remainingStructureExtension.name} does not exist on overload structure.`); } } @@ -39,7 +39,8 @@ if (problems.length > 0) { function isAllowedStructure(vm: InterfaceViewModel) { switch (vm.name) { - case "NamedStructure": + case "NamedNodeStructure": + case "PropertyNamedNodeStructure": case "FunctionLikeDeclarationStructure": case "BodiedNodeStructure": case "BodyableNodeStructure": diff --git a/code-generation/ensureStructuresMatchClasses.ts b/code-generation/ensureStructuresMatchClasses.ts index 1940471d3..4fc1f5e32 100644 --- a/code-generation/ensureStructuresMatchClasses.ts +++ b/code-generation/ensureStructuresMatchClasses.ts @@ -41,11 +41,6 @@ function getStructureName(vm: ClassViewModel | MixinViewModel) { } function isStructureToIgnore(structure: InterfaceViewModel) { - switch (structure.name) { - case "NamedStructure": - return true; - } - if (/SpecificStructure$/.test(structure.name)) return true; diff --git a/docs/details/classes.md b/docs/details/classes.md index b5752adb8..9ddc04b30 100644 --- a/docs/details/classes.md +++ b/docs/details/classes.md @@ -161,6 +161,18 @@ Add or insert properties by using `insertProperties()`, `insertProperty`, `addPr const property = classDeclaration.addProperty({ isStatic: true, name: "prop", type: "string" }); ``` +Add or insert get accessors by using `insertGetAccessors()`, `insertGetAccessor`, `addGetAccessor`, or `addGetAccessors`: + +```typescript +const getAccessor = classDeclaration.addGetAccessor({ name: "someNumber", returnType: "number", body: "return 5;" }); +``` + +Add or insert set accessors by using `insertSetAccessors()`, `insertSetAccessor`, `addSetAccessor`, or `addSetAccessors`: + +```typescript +const setAccessor = classDeclaration.addSetAccessor({ name: "someNumber", parameters: [{ name: "value", type: "number" }], body: "_someNumber = value;" }); +``` + #### Remove Call `.remove()`: diff --git a/package.json b/package.json index 2613099dd..9eecd02d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-simple-ast", - "version": "0.89.0", + "version": "0.90.0", "description": "TypeScript compiler wrapper for AST navigation and code generation.", "main": "dist/main.js", "typings": "dist/main.d.ts", diff --git a/src/compiler/base/name/NamedNode.ts b/src/compiler/base/name/NamedNode.ts index ef601216a..44368a64a 100644 --- a/src/compiler/base/name/NamedNode.ts +++ b/src/compiler/base/name/NamedNode.ts @@ -1,7 +1,9 @@ import * as ts from "typescript"; import {Constructor} from "./../../../Constructor"; import * as errors from "./../../../errors"; +import {NamedNodeStructure} from "./../../../structures"; import {Node, Identifier} from "./../../common"; +import {callBaseFill} from "./../../callBaseFill"; // todo: make name optional, but in a different class because TypeParameterDeclaration expects a name // (maybe NameableNode and rename some of the other "-ed" classes) @@ -41,5 +43,14 @@ export function NamedNode>(Base: T this.getNameIdentifier().rename(newName); return this; } + + fill(structure: Partial) { + callBaseFill(Base.prototype, this, structure); + + if (structure.name != null) + this.rename(structure.name); + + return this; + } }; } diff --git a/src/compiler/base/name/PropertyNamedNode.ts b/src/compiler/base/name/PropertyNamedNode.ts index bc5e0f9e2..3929a1b32 100644 --- a/src/compiler/base/name/PropertyNamedNode.ts +++ b/src/compiler/base/name/PropertyNamedNode.ts @@ -1,7 +1,9 @@ import * as ts from "typescript"; import {Constructor} from "./../../../Constructor"; import * as errors from "./../../../errors"; +import {PropertyNamedNodeStructure} from "./../../../structures"; import {Node, Identifier} from "./../../common"; +import {callBaseFill} from "./../../callBaseFill"; export type PropertyNamedNodeExtensionType = Node; @@ -34,5 +36,14 @@ export function PropertyNamedNode) { + callBaseFill(Base.prototype, this, structure); + + if (structure.name != null) + this.rename(structure.name); + + return this; + } }; } diff --git a/src/compiler/class/ClassDeclaration.ts b/src/compiler/class/ClassDeclaration.ts index bb0ee24e5..c656d9955 100644 --- a/src/compiler/class/ClassDeclaration.ts +++ b/src/compiler/class/ClassDeclaration.ts @@ -3,7 +3,8 @@ import * as errors from "./../../errors"; import {insertIntoCreatableSyntaxList, insertIntoParent, getEndIndexFromArray, insertIntoBracesOrSourceFileWithFillAndGetChildren, verifyAndGetIndex, removeStatementedNodeChild} from "./../../manipulation"; import {getNamedNodeByNameOrFindFunction, getNotFoundErrorMessageForNameOrFindFunction, TypeGuards, StringUtils} from "./../../utils"; -import {PropertyDeclarationStructure, MethodDeclarationStructure, ConstructorDeclarationStructure, ClassDeclarationStructure} from "./../../structures"; +import {PropertyDeclarationStructure, MethodDeclarationStructure, ConstructorDeclarationStructure, GetAccessorDeclarationStructure, + SetAccessorDeclarationStructure, ClassDeclarationStructure} from "./../../structures"; import {Node} from "./../common"; import {NamedNode, ExportableNode, ModifierableNode, AmbientableNode, DocumentationableNode, TypeParameteredNode, DecoratableNode, HeritageClauseableNode, ImplementsClauseableNode, TextInsertableNode} from "./../base"; @@ -43,6 +44,10 @@ export class ClassDeclaration extends ClassDeclarationBase this.addConstructor(structure.ctor); if (structure.properties != null) this.addProperties(structure.properties); + if (structure.getAccessors != null) + this.addGetAccessors(structure.getAccessors); + if (structure.setAccessors != null) + this.addSetAccessors(structure.setAccessors); if (structure.methods != null) this.addMethods(structure.methods); @@ -166,6 +171,130 @@ export class ClassDeclaration extends ClassDeclarationBase return this.getAllMembers().filter(m => TypeGuards.isConstructorDeclaration(m)) as ConstructorDeclaration[]; } + /** + * Add get accessor. + * @param structure - Structure representing the get accessor. + */ + addGetAccessor(structure: GetAccessorDeclarationStructure) { + return this.addGetAccessors([structure])[0]; + } + + /** + * Add properties. + * @param structures - Structures representing the properties. + */ + addGetAccessors(structures: GetAccessorDeclarationStructure[]) { + return this.insertGetAccessors(getEndIndexFromArray(this.getBodyMembers()), structures); + } + + /** + * Insert get accessor. + * @param index - Index to insert at. + * @param structure - Structure representing the get accessor. + */ + insertGetAccessor(index: number, structure: GetAccessorDeclarationStructure) { + return this.insertGetAccessors(index, [structure])[0]; + } + + /** + * Insert properties. + * @param index - Index to insert at. + * @param structures - Structures representing the properties. + */ + insertGetAccessors(index: number, structures: GetAccessorDeclarationStructure[]) { + const indentationText = this.getChildIndentationText(); + const newLineKind = this.global.manipulationSettings.getNewLineKind(); + + // create code + const codes: string[] = []; + for (const structure of structures) { + let code = `${indentationText}`; + if (structure.isStatic) + code += "static "; + code += `get ${structure.name}()`; + if (structure.returnType != null && structure.returnType.length > 0) + code += `: ${structure.returnType}`; + code += " {" + newLineKind + indentationText + "}"; + codes.push(code); + } + + return insertIntoBracesOrSourceFileWithFillAndGetChildren({ + getIndexedChildren: () => this.getBodyMembers(), + sourceFile: this.getSourceFile(), + parent: this, + index, + childCodes: codes, + structures, + previousBlanklineWhen: () => true, + separatorNewlineWhen: () => true, + nextBlanklineWhen: () => true, + expectedKind: ts.SyntaxKind.GetAccessor, + fillFunction: (node, structure) => node.fill(structure) + }); + } + + /** + * Add set accessor. + * @param structure - Structure representing the set accessor. + */ + addSetAccessor(structure: SetAccessorDeclarationStructure) { + return this.addSetAccessors([structure])[0]; + } + + /** + * Add properties. + * @param structures - Structures representing the properties. + */ + addSetAccessors(structures: SetAccessorDeclarationStructure[]) { + return this.insertSetAccessors(getEndIndexFromArray(this.getBodyMembers()), structures); + } + + /** + * Insert set accessor. + * @param index - Index to insert at. + * @param structure - Structure representing the set accessor. + */ + insertSetAccessor(index: number, structure: SetAccessorDeclarationStructure) { + return this.insertSetAccessors(index, [structure])[0]; + } + + /** + * Insert properties. + * @param index - Index to insert at. + * @param structures - Structures representing the properties. + */ + insertSetAccessors(index: number, structures: SetAccessorDeclarationStructure[]) { + const indentationText = this.getChildIndentationText(); + const newLineKind = this.global.manipulationSettings.getNewLineKind(); + + // create code + const codes: string[] = []; + for (const structure of structures) { + let code = `${indentationText}`; + if (structure.isStatic) + code += "static "; + code += `set ${structure.name}()`; + if (structure.returnType != null && structure.returnType.length > 0) + code += `: ${structure.returnType}`; + code += " {" + newLineKind + indentationText + "}"; + codes.push(code); + } + + return insertIntoBracesOrSourceFileWithFillAndGetChildren({ + getIndexedChildren: () => this.getBodyMembers(), + sourceFile: this.getSourceFile(), + parent: this, + index, + childCodes: codes, + structures, + previousBlanklineWhen: () => true, + separatorNewlineWhen: () => true, + nextBlanklineWhen: () => true, + expectedKind: ts.SyntaxKind.SetAccessor, + fillFunction: (node, structure) => node.fill(structure) + }); + } + /** * Add property. * @param structure - Structure representing the property. diff --git a/src/compiler/class/GetAccessorDeclaration.ts b/src/compiler/class/GetAccessorDeclaration.ts index 0890840c5..6ba2e42bb 100644 --- a/src/compiler/class/GetAccessorDeclaration.ts +++ b/src/compiler/class/GetAccessorDeclaration.ts @@ -1,6 +1,8 @@ import * as ts from "typescript"; import * as errors from "./../../errors"; import {removeClassMember} from "./../../manipulation"; +import {GetAccessorDeclarationStructure} from "./../../structures"; +import {callBaseFill} from "./../callBaseFill"; import {Node} from "./../common"; import {PropertyNamedNode, StaticableNode, ScopedNode, DecoratableNode, BodiedNode, TextInsertableNode} from "./../base"; import {FunctionLikeDeclaration} from "./../function"; @@ -12,6 +14,15 @@ export const GetAccessorDeclarationBase = TextInsertableNode(DecoratableNode(Abs BodiedNode(FunctionLikeDeclaration(PropertyNamedNode(Node))) ))))); export class GetAccessorDeclaration extends GetAccessorDeclarationBase { + /** + * Fills the node from a structure. + * @param structure - Structure to fill. + */ + fill(structure: Partial) { + callBaseFill(GetAccessorDeclarationBase.prototype, this, structure); + return this; + } + /** * Gets the corresponding set accessor if one exists. */ diff --git a/src/compiler/class/SetAccessorDeclaration.ts b/src/compiler/class/SetAccessorDeclaration.ts index 08f39623e..b1c77f8e7 100644 --- a/src/compiler/class/SetAccessorDeclaration.ts +++ b/src/compiler/class/SetAccessorDeclaration.ts @@ -1,6 +1,8 @@ import * as ts from "typescript"; import * as errors from "./../../errors"; import {removeClassMember} from "./../../manipulation"; +import {SetAccessorDeclarationStructure} from "./../../structures"; +import {callBaseFill} from "./../callBaseFill"; import {Node} from "./../common"; import {PropertyNamedNode, StaticableNode, ScopedNode, DecoratableNode, BodiedNode, TextInsertableNode} from "./../base"; import {FunctionLikeDeclaration} from "./../function"; @@ -12,6 +14,15 @@ export const SetAccessorDeclarationBase = TextInsertableNode(DecoratableNode(Abs BodiedNode(FunctionLikeDeclaration(PropertyNamedNode(Node)) )))))); export class SetAccessorDeclaration extends SetAccessorDeclarationBase { + /** + * Fills the node from a structure. + * @param structure - Structure to fill. + */ + fill(structure: Partial) { + callBaseFill(SetAccessorDeclarationBase.prototype, this, structure); + return this; + } + /** * Gets the corresponding get accessor if one exists. */ diff --git a/src/manipulation/getMixinStructureFunctions.ts b/src/manipulation/getMixinStructureFunctions.ts index 978c5c968..d76b3299a 100644 --- a/src/manipulation/getMixinStructureFunctions.ts +++ b/src/manipulation/getMixinStructureFunctions.ts @@ -97,7 +97,7 @@ export function fromInitializerExpressionableNode(node: compiler.InitializerExpr }; } -export function fromNamedNode(node: compiler.NamedNode): MakeRequired { +export function fromNamedNode(node: compiler.NamedNode): MakeRequired { return { name: node.getName() }; diff --git a/src/structures/base.ts b/src/structures/base.ts index 4194effef..398aa8077 100644 --- a/src/structures/base.ts +++ b/src/structures/base.ts @@ -10,7 +10,7 @@ export * from "./base/GeneratorableNodeStructure"; export * from "./base/ExtendsClauseableNodeStructure"; export * from "./base/ImplementsClauseableNodeStructure"; export * from "./base/InitializerExpressionableNodeStructure"; -export * from "./base/NamedStructure"; +export * from "./base/name"; export * from "./base/QuestionTokenableNodeStructure"; export * from "./base/ParameteredNodeStructure"; export * from "./base/ReadonlyableNodeStructure"; diff --git a/src/structures/base/NamedStructure.ts b/src/structures/base/NamedStructure.ts deleted file mode 100644 index a0ee0a2c6..000000000 --- a/src/structures/base/NamedStructure.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface NamedStructure { - name: string; -} diff --git a/src/structures/base/name.ts b/src/structures/base/name.ts new file mode 100644 index 000000000..ddd5dec0e --- /dev/null +++ b/src/structures/base/name.ts @@ -0,0 +1,4 @@ +export * from "./name/BindingNamedNodeStructure"; +export * from "./name/DeclarationNamedNodeStructure"; +export * from "./name/NamedNodeStructure"; +export * from "./name/PropertyNamedNodeStructure"; diff --git a/src/structures/base/name/BindingNamedNodeStructure.ts b/src/structures/base/name/BindingNamedNodeStructure.ts new file mode 100644 index 000000000..105d348c9 --- /dev/null +++ b/src/structures/base/name/BindingNamedNodeStructure.ts @@ -0,0 +1,3 @@ +export interface BindingNamedNodeStructure { + name: string; +} diff --git a/src/structures/base/name/DeclarationNamedNodeStructure.ts b/src/structures/base/name/DeclarationNamedNodeStructure.ts new file mode 100644 index 000000000..7fb1beb5a --- /dev/null +++ b/src/structures/base/name/DeclarationNamedNodeStructure.ts @@ -0,0 +1,3 @@ +export interface DeclarationNamedNodeStructure { + name: string; +} diff --git a/src/structures/base/name/NamedNodeStructure.ts b/src/structures/base/name/NamedNodeStructure.ts new file mode 100644 index 000000000..9c01db80f --- /dev/null +++ b/src/structures/base/name/NamedNodeStructure.ts @@ -0,0 +1,3 @@ +export interface NamedNodeStructure { + name: string; +} diff --git a/src/structures/base/name/PropertyNamedNodeStructure.ts b/src/structures/base/name/PropertyNamedNodeStructure.ts new file mode 100644 index 000000000..212e580c8 --- /dev/null +++ b/src/structures/base/name/PropertyNamedNodeStructure.ts @@ -0,0 +1,3 @@ +export interface PropertyNamedNodeStructure { + name: string; +} diff --git a/src/structures/class.ts b/src/structures/class.ts index 9b457556f..f6bab1eb1 100644 --- a/src/structures/class.ts +++ b/src/structures/class.ts @@ -1,4 +1,6 @@ export * from "./class/ClassDeclarationStructure"; export * from "./class/ConstructorDeclarationStructure"; +export * from "./class/GetAccessorDeclarationStructure"; export * from "./class/MethodDeclarationStructure"; export * from "./class/PropertyDeclarationStructure"; +export * from "./class/SetAccessorDeclarationStructure"; diff --git a/src/structures/class/ClassDeclarationStructure.ts b/src/structures/class/ClassDeclarationStructure.ts index 8ff4d9e04..772dd99d5 100644 --- a/src/structures/class/ClassDeclarationStructure.ts +++ b/src/structures/class/ClassDeclarationStructure.ts @@ -1,11 +1,13 @@ -import {NamedStructure, ImplementsClauseableNodeStructure, DecoratableNodeStructure, TypeParameteredNodeStructure, +import {NamedNodeStructure, ImplementsClauseableNodeStructure, DecoratableNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, AbstractableNodeStructure, ExportableNodeStructure} from "./../base"; import {PropertyDeclarationStructure} from "./PropertyDeclarationStructure"; import {MethodDeclarationStructure} from "./MethodDeclarationStructure"; import {ConstructorDeclarationStructure} from "./ConstructorDeclarationStructure"; +import {GetAccessorDeclarationStructure} from "./GetAccessorDeclarationStructure"; +import {SetAccessorDeclarationStructure} from "./SetAccessorDeclarationStructure"; export interface ClassDeclarationStructure - extends NamedStructure, ClassDeclarationSpecificStructure, ImplementsClauseableNodeStructure, DecoratableNodeStructure, + extends NamedNodeStructure, ClassDeclarationSpecificStructure, ImplementsClauseableNodeStructure, DecoratableNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, AbstractableNodeStructure, ExportableNodeStructure { } @@ -14,5 +16,7 @@ export interface ClassDeclarationSpecificStructure { extends?: string; ctor?: ConstructorDeclarationStructure; properties?: PropertyDeclarationStructure[]; + getAccessors?: GetAccessorDeclarationStructure[]; + setAccessors?: SetAccessorDeclarationStructure[]; methods?: MethodDeclarationStructure[]; } diff --git a/src/structures/class/GetAccessorDeclarationStructure.ts b/src/structures/class/GetAccessorDeclarationStructure.ts new file mode 100644 index 000000000..e070e10ae --- /dev/null +++ b/src/structures/class/GetAccessorDeclarationStructure.ts @@ -0,0 +1,12 @@ +import {NamedNodeStructure, StaticableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure, ScopedNodeStructure, + AsyncableNodeStructure, GeneratorableNodeStructure, DocumentationableNodeStructure, BodiedNodeStructure, PropertyNamedNodeStructure} from "./../base"; +import {FunctionLikeDeclarationStructure, SignaturedDeclarationStructure} from "./../function"; + +export interface GetAccessorDeclarationStructure + extends GetAccessorDeclarationSpecificStructure, PropertyNamedNodeStructure, StaticableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure, + ScopedNodeStructure, FunctionLikeDeclarationStructure, BodiedNodeStructure +{ +} + +export interface GetAccessorDeclarationSpecificStructure { +} diff --git a/src/structures/class/MethodDeclarationStructure.ts b/src/structures/class/MethodDeclarationStructure.ts index 33d6c4ba8..6e6c918af 100644 --- a/src/structures/class/MethodDeclarationStructure.ts +++ b/src/structures/class/MethodDeclarationStructure.ts @@ -1,9 +1,9 @@ -import {NamedStructure, StaticableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure, ScopedNodeStructure, +import {PropertyNamedNodeStructure, StaticableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure, ScopedNodeStructure, AsyncableNodeStructure, GeneratorableNodeStructure, DocumentationableNodeStructure, BodyableNodeStructure} from "./../base"; import {FunctionLikeDeclarationStructure, SignaturedDeclarationStructure} from "./../function"; export interface MethodDeclarationStructure - extends MethodDeclarationSpecificStructure, NamedStructure, StaticableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure, + extends MethodDeclarationSpecificStructure, PropertyNamedNodeStructure, StaticableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure, ScopedNodeStructure, AsyncableNodeStructure, GeneratorableNodeStructure, FunctionLikeDeclarationStructure, BodyableNodeStructure { } diff --git a/src/structures/class/PropertyDeclarationStructure.ts b/src/structures/class/PropertyDeclarationStructure.ts index 156b7fafb..9f18a1bd9 100644 --- a/src/structures/class/PropertyDeclarationStructure.ts +++ b/src/structures/class/PropertyDeclarationStructure.ts @@ -1,8 +1,8 @@ -import {NamedStructure, TypedNodeStructure, QuestionTokenableNodeStructure, StaticableNodeStructure, ScopedNodeStructure, DocumentationableNodeStructure, +import {PropertyNamedNodeStructure, TypedNodeStructure, QuestionTokenableNodeStructure, StaticableNodeStructure, ScopedNodeStructure, DocumentationableNodeStructure, ReadonlyableNodeStructure, InitializerExpressionableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure} from "./../base"; export interface PropertyDeclarationStructure - extends NamedStructure, TypedNodeStructure, QuestionTokenableNodeStructure, StaticableNodeStructure, ScopedNodeStructure, DocumentationableNodeStructure, + extends PropertyNamedNodeStructure, TypedNodeStructure, QuestionTokenableNodeStructure, StaticableNodeStructure, ScopedNodeStructure, DocumentationableNodeStructure, ReadonlyableNodeStructure, InitializerExpressionableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure { } diff --git a/src/structures/class/SetAccessorDeclarationStructure.ts b/src/structures/class/SetAccessorDeclarationStructure.ts new file mode 100644 index 000000000..9874698b5 --- /dev/null +++ b/src/structures/class/SetAccessorDeclarationStructure.ts @@ -0,0 +1,12 @@ +import {NamedNodeStructure, StaticableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure, ScopedNodeStructure, + AsyncableNodeStructure, GeneratorableNodeStructure, DocumentationableNodeStructure, BodiedNodeStructure, PropertyNamedNodeStructure} from "./../base"; +import {FunctionLikeDeclarationStructure, SignaturedDeclarationStructure} from "./../function"; + +export interface SetAccessorDeclarationStructure + extends SetAccessorDeclarationSpecificStructure, PropertyNamedNodeStructure, StaticableNodeStructure, DecoratableNodeStructure, AbstractableNodeStructure, + ScopedNodeStructure, FunctionLikeDeclarationStructure, BodiedNodeStructure +{ +} + +export interface SetAccessorDeclarationSpecificStructure { +} diff --git a/src/structures/decorator/DecoratorStructure.ts b/src/structures/decorator/DecoratorStructure.ts index cc14db49d..1a79f76ed 100644 --- a/src/structures/decorator/DecoratorStructure.ts +++ b/src/structures/decorator/DecoratorStructure.ts @@ -1,5 +1,4 @@ -import {NamedStructure} from "./../base"; - -export interface DecoratorStructure extends NamedStructure { +export interface DecoratorStructure { + name: string; arguments?: string[]; } diff --git a/src/structures/enum/EnumDeclarationStructure.ts b/src/structures/enum/EnumDeclarationStructure.ts index a1522fdc1..3e89eae21 100644 --- a/src/structures/enum/EnumDeclarationStructure.ts +++ b/src/structures/enum/EnumDeclarationStructure.ts @@ -1,8 +1,8 @@ -import {NamedStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure} from "./../base"; +import {NamedNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure} from "./../base"; import {EnumMemberStructure} from "./EnumMemberStructure"; export interface EnumDeclarationStructure - extends NamedStructure, EnumDeclarationSpecificStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure + extends NamedNodeStructure, EnumDeclarationSpecificStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure { } diff --git a/src/structures/enum/EnumMemberStructure.ts b/src/structures/enum/EnumMemberStructure.ts index 3a0633a9d..f0853a344 100644 --- a/src/structures/enum/EnumMemberStructure.ts +++ b/src/structures/enum/EnumMemberStructure.ts @@ -1,6 +1,6 @@ -import {NamedStructure, DocumentationableNodeStructure, InitializerExpressionableNodeStructure} from "./../base"; +import {PropertyNamedNodeStructure, DocumentationableNodeStructure, InitializerExpressionableNodeStructure} from "./../base"; -export interface EnumMemberStructure extends EnumMemberSpecificStructure, NamedStructure, DocumentationableNodeStructure, InitializerExpressionableNodeStructure { +export interface EnumMemberStructure extends EnumMemberSpecificStructure, PropertyNamedNodeStructure, DocumentationableNodeStructure, InitializerExpressionableNodeStructure { } export interface EnumMemberSpecificStructure { diff --git a/src/structures/function/FunctionDeclarationStructure.ts b/src/structures/function/FunctionDeclarationStructure.ts index 9419d0f1a..350976c9e 100644 --- a/src/structures/function/FunctionDeclarationStructure.ts +++ b/src/structures/function/FunctionDeclarationStructure.ts @@ -1,11 +1,11 @@ -import {NamedStructure, AsyncableNodeStructure, GeneratorableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure, +import {NamedNodeStructure, AsyncableNodeStructure, GeneratorableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure, DocumentationableNodeStructure, BodyableNodeStructure} from "./../base"; import {FunctionLikeDeclarationStructure} from "./FunctionLikeDeclarationStructure"; import {SignaturedDeclarationStructure} from "./SignaturedDeclarationStructure"; import {StatementedNodeStructure} from "./../statement"; export interface FunctionDeclarationStructure - extends FunctionDeclarationSpecificStructure, NamedStructure, FunctionLikeDeclarationStructure, StatementedNodeStructure, AsyncableNodeStructure, + extends FunctionDeclarationSpecificStructure, NamedNodeStructure, FunctionLikeDeclarationStructure, StatementedNodeStructure, AsyncableNodeStructure, GeneratorableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure, BodyableNodeStructure { } diff --git a/src/structures/function/ParameterDeclarationStructure.ts b/src/structures/function/ParameterDeclarationStructure.ts index e1752ed83..c61c25349 100644 --- a/src/structures/function/ParameterDeclarationStructure.ts +++ b/src/structures/function/ParameterDeclarationStructure.ts @@ -1,8 +1,8 @@ -import {NamedStructure, TypedNodeStructure, ReadonlyableNodeStructure, DecoratableNodeStructure, QuestionTokenableNodeStructure, ScopeableNodeStructure, +import {DeclarationNamedNodeStructure, TypedNodeStructure, ReadonlyableNodeStructure, DecoratableNodeStructure, QuestionTokenableNodeStructure, ScopeableNodeStructure, InitializerExpressionableNodeStructure} from "./../base"; export interface ParameterDeclarationStructure - extends NamedStructure, TypedNodeStructure, ReadonlyableNodeStructure, DecoratableNodeStructure, QuestionTokenableNodeStructure, ScopeableNodeStructure, + extends DeclarationNamedNodeStructure, TypedNodeStructure, ReadonlyableNodeStructure, DecoratableNodeStructure, QuestionTokenableNodeStructure, ScopeableNodeStructure, InitializerExpressionableNodeStructure, ParameterDeclarationSpecificStructure { } diff --git a/src/structures/interface/InterfaceDeclarationStructure.ts b/src/structures/interface/InterfaceDeclarationStructure.ts index 819dae1ab..a0d82263e 100644 --- a/src/structures/interface/InterfaceDeclarationStructure.ts +++ b/src/structures/interface/InterfaceDeclarationStructure.ts @@ -1,11 +1,11 @@ -import {NamedStructure, ScopeableNodeStructure, ExtendsClauseableNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, +import {NamedNodeStructure, ScopeableNodeStructure, ExtendsClauseableNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure} from "./../base"; import {PropertySignatureStructure} from "./PropertySignatureStructure"; import {MethodSignatureStructure} from "./MethodSignatureStructure"; import {ConstructSignatureDeclarationStructure} from "./ConstructSignatureDeclarationStructure"; export interface InterfaceDeclarationStructure - extends NamedStructure, InterfaceDeclarationSpecificStructure, ExtendsClauseableNodeStructure, TypeParameteredNodeStructure, + extends NamedNodeStructure, InterfaceDeclarationSpecificStructure, ExtendsClauseableNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure { } diff --git a/src/structures/interface/MethodSignatureStructure.ts b/src/structures/interface/MethodSignatureStructure.ts index 021667039..18b916e5c 100644 --- a/src/structures/interface/MethodSignatureStructure.ts +++ b/src/structures/interface/MethodSignatureStructure.ts @@ -1,7 +1,7 @@ -import {NamedStructure, QuestionTokenableNodeStructure, DocumentationableNodeStructure} from "./../base"; +import {PropertyNamedNodeStructure, QuestionTokenableNodeStructure, DocumentationableNodeStructure} from "./../base"; import {SignaturedDeclarationStructure} from "./../function"; export interface MethodSignatureStructure - extends NamedStructure, QuestionTokenableNodeStructure, DocumentationableNodeStructure, SignaturedDeclarationStructure + extends PropertyNamedNodeStructure, QuestionTokenableNodeStructure, DocumentationableNodeStructure, SignaturedDeclarationStructure { } diff --git a/src/structures/interface/PropertySignatureStructure.ts b/src/structures/interface/PropertySignatureStructure.ts index 9b76b7feb..9a7ec35ab 100644 --- a/src/structures/interface/PropertySignatureStructure.ts +++ b/src/structures/interface/PropertySignatureStructure.ts @@ -1,7 +1,8 @@ -import {NamedStructure, TypedNodeStructure, QuestionTokenableNodeStructure, DocumentationableNodeStructure, ReadonlyableNodeStructure, +import {PropertyNamedNodeStructure, TypedNodeStructure, QuestionTokenableNodeStructure, DocumentationableNodeStructure, ReadonlyableNodeStructure, InitializerExpressionableNodeStructure} from "./../base"; export interface PropertySignatureStructure - extends NamedStructure, TypedNodeStructure, QuestionTokenableNodeStructure, DocumentationableNodeStructure, ReadonlyableNodeStructure, InitializerExpressionableNodeStructure + extends PropertyNamedNodeStructure, TypedNodeStructure, QuestionTokenableNodeStructure, DocumentationableNodeStructure, ReadonlyableNodeStructure, + InitializerExpressionableNodeStructure { } diff --git a/src/structures/namespace/NamespaceDeclarationStructure.ts b/src/structures/namespace/NamespaceDeclarationStructure.ts index a2813353a..965f7b8c2 100644 --- a/src/structures/namespace/NamespaceDeclarationStructure.ts +++ b/src/structures/namespace/NamespaceDeclarationStructure.ts @@ -1,8 +1,8 @@ -import {NamedStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure, BodiedNodeStructure} from "./../base"; +import {NamedNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure, BodiedNodeStructure} from "./../base"; import {StatementedNodeStructure} from "./../statement"; export interface NamespaceDeclarationStructure - extends NamedStructure, NamespaceDeclarationSpecificStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure, StatementedNodeStructure, + extends NamedNodeStructure, NamespaceDeclarationSpecificStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure, StatementedNodeStructure, BodiedNodeStructure { } diff --git a/src/structures/statement/VariableDeclarationStructure.ts b/src/structures/statement/VariableDeclarationStructure.ts index cab20f575..c6be09b4c 100644 --- a/src/structures/statement/VariableDeclarationStructure.ts +++ b/src/structures/statement/VariableDeclarationStructure.ts @@ -1,4 +1,4 @@ -import {NamedStructure, InitializerExpressionableNodeStructure, TypedNodeStructure} from "./../base"; +import {BindingNamedNodeStructure, InitializerExpressionableNodeStructure, TypedNodeStructure} from "./../base"; -export interface VariableDeclarationStructure extends NamedStructure, InitializerExpressionableNodeStructure, TypedNodeStructure { +export interface VariableDeclarationStructure extends BindingNamedNodeStructure, InitializerExpressionableNodeStructure, TypedNodeStructure { } diff --git a/src/structures/type/TypeAliasDeclarationStructure.ts b/src/structures/type/TypeAliasDeclarationStructure.ts index 27fa59808..d302cd278 100644 --- a/src/structures/type/TypeAliasDeclarationStructure.ts +++ b/src/structures/type/TypeAliasDeclarationStructure.ts @@ -1,7 +1,7 @@ -import {NamedStructure, TypedNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure} from "./../base"; +import {NamedNodeStructure, TypedNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure} from "./../base"; export interface TypeAliasDeclarationStructure - extends NamedStructure, TypedNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure + extends NamedNodeStructure, TypedNodeStructure, TypeParameteredNodeStructure, DocumentationableNodeStructure, AmbientableNodeStructure, ExportableNodeStructure { type: string; // make required (from base) } diff --git a/src/structures/type/TypeParameterDeclarationStructure.ts b/src/structures/type/TypeParameterDeclarationStructure.ts index 8769fc549..10ae79975 100644 --- a/src/structures/type/TypeParameterDeclarationStructure.ts +++ b/src/structures/type/TypeParameterDeclarationStructure.ts @@ -1,5 +1,5 @@ -import {NamedStructure} from "./../base"; +import {NamedNodeStructure} from "./../base"; -export interface TypeParameterDeclarationStructure extends NamedStructure { +export interface TypeParameterDeclarationStructure extends NamedNodeStructure { constraint?: string; } diff --git a/src/tests/compiler/base/name/namedNodeTests.ts b/src/tests/compiler/base/name/namedNodeTests.ts index 109a8fdc3..15975a0e6 100644 --- a/src/tests/compiler/base/name/namedNodeTests.ts +++ b/src/tests/compiler/base/name/namedNodeTests.ts @@ -52,4 +52,12 @@ describe(nameof(NamedNode), () => { expect(nameNode).to.be.instanceOf(Identifier); }); }); + + describe("fill", () => { + it("should fill the node with a new name via a rename", () => { + const {firstChild, sourceFile} = getInfoFromText("enum MyEnum {}\nlet myEnum: MyEnum;"); + firstChild.fill({ name: "MyNewEnum" }); + expect(sourceFile.getFullText()).to.equal("enum MyNewEnum {}\nlet myEnum: MyNewEnum;"); + }); + }); }); diff --git a/src/tests/compiler/class/classDeclarationTests.ts b/src/tests/compiler/class/classDeclarationTests.ts index 5e0974b8d..50f947e7b 100644 --- a/src/tests/compiler/class/classDeclarationTests.ts +++ b/src/tests/compiler/class/classDeclarationTests.ts @@ -1,7 +1,8 @@ import {expect} from "chai"; import {ClassDeclaration, MethodDeclaration, PropertyDeclaration, GetAccessorDeclaration, SetAccessorDeclaration, ExpressionWithTypeArguments, ConstructorDeclaration, ParameterDeclaration, Scope} from "./../../../compiler"; -import {PropertyDeclarationStructure, MethodDeclarationStructure, ConstructorDeclarationStructure, ClassDeclarationSpecificStructure} from "./../../../structures"; +import {PropertyDeclarationStructure, MethodDeclarationStructure, ConstructorDeclarationStructure, ClassDeclarationSpecificStructure, + GetAccessorDeclarationStructure} from "./../../../structures"; import {getInfoFromText} from "./../testHelpers"; describe(nameof(ClassDeclaration), () => { @@ -21,9 +22,12 @@ describe(nameof(ClassDeclaration), () => { extends: "Other", ctor: {}, properties: [{ name: "p" }], + getAccessors: [{ name: "g" }], + setAccessors: [{ name: "s", parameters: [{ name: "value", type: "string" }] }], methods: [{ name: "m" }] }; - doTest("class Identifier {\n}", structure, "class Identifier extends Other {\n constructor() {\n }\n\n p;\n\n m() {\n }\n}"); + doTest("class Identifier {\n}", structure, "class Identifier extends Other {\n constructor() {\n }\n\n p;\n\n get g() {\n }" + + "\n\n set s(value: string) {\n }\n\n m() {\n }\n}"); }); }); @@ -155,6 +159,66 @@ describe(nameof(ClassDeclaration), () => { }); }); + describe(nameof(d => d.insertGetAccessors), () => { + function doTest(startCode: string, insertIndex: number, structures: GetAccessorDeclarationStructure[], expectedCode: string) { + const {firstChild} = getInfoFromText(startCode); + const result = firstChild.insertGetAccessors(insertIndex, structures); + expect(firstChild.getText()).to.equal(expectedCode); + expect(result.length).to.equal(structures.length); + } + + it("should insert when none exists", () => { + doTest("class c {\n}", 0, [{ name: "identifier" }], "class c {\n get identifier() {\n }\n}"); + }); + + it("should insert multiple into other methods", () => { + doTest("class c {\n m1() {\n }\n\n m4() {\n }\n}", 1, [{ isStatic: true, name: "m2", returnType: "string" }, { name: "m3" }], + "class c {\n m1() {\n }\n\n static get m2(): string {\n }\n\n get m3() {\n }\n\n m4() {\n }\n}"); + }); + }); + + describe(nameof(d => d.insertGetAccessor), () => { + function doTest(startCode: string, insertIndex: number, structure: GetAccessorDeclarationStructure, expectedCode: string) { + const {firstChild} = getInfoFromText(startCode); + const result = firstChild.insertGetAccessor(insertIndex, structure); + expect(firstChild.getText()).to.equal(expectedCode); + expect(result).to.be.instanceOf(GetAccessorDeclaration); + } + + it("should insert", () => { + doTest("class c {\n m1() {\n }\n\n m3() {\n }\n}", 1, { name: "m2" }, + "class c {\n m1() {\n }\n\n get m2() {\n }\n\n m3() {\n }\n}"); + }); + }); + + describe(nameof(d => d.addGetAccessors), () => { + function doTest(startCode: string, structures: GetAccessorDeclarationStructure[], expectedCode: string) { + const {firstChild} = getInfoFromText(startCode); + const result = firstChild.addGetAccessors(structures); + expect(firstChild.getText()).to.equal(expectedCode); + expect(result.length).to.equal(structures.length); + } + + it("should add multiple", () => { + doTest("class c {\n m1() {\n }\n}", [{ name: "m2" }, { name: "m3" }], + "class c {\n m1() {\n }\n\n get m2() {\n }\n\n get m3() {\n }\n}"); + }); + }); + + describe(nameof(d => d.addGetAccessor), () => { + function doTest(startCode: string, structure: GetAccessorDeclarationStructure, expectedCode: string) { + const {firstChild} = getInfoFromText(startCode); + const result = firstChild.addGetAccessor(structure); + expect(firstChild.getText()).to.equal(expectedCode); + expect(result).to.be.instanceOf(GetAccessorDeclaration); + } + + it("should insert", () => { + doTest("class c {\n m1() {\n }\n}", { name: "m2" }, + "class c {\n m1() {\n }\n\n get m2() {\n }\n}"); + }); + }); + describe(nameof(d => d.insertProperties), () => { function doTest(startCode: string, insertIndex: number, structures: PropertyDeclarationStructure[], expectedCode: string) { const {firstChild} = getInfoFromText(startCode); diff --git a/src/tests/manipulation/getMixinStructureFunctionsTests.ts b/src/tests/manipulation/getMixinStructureFunctionsTests.ts index 47af2225b..130c77fab 100644 --- a/src/tests/manipulation/getMixinStructureFunctionsTests.ts +++ b/src/tests/manipulation/getMixinStructureFunctionsTests.ts @@ -238,7 +238,7 @@ describe(nameof(getMixinStructureFuncs.fromInitializerExpressionableNode), () => }); describe(nameof(getMixinStructureFuncs.fromNamedNode), () => { - function doTest(startingCode: string, expectedStructure: MakeRequired) { + function doTest(startingCode: string, expectedStructure: MakeRequired) { const {firstChild} = getInfoFromText(startingCode); expect(getMixinStructureFuncs.fromNamedNode(firstChild)).to.deep.equal(expectedStructure); }