-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #242 - Wrap IndexSignatureDeclaration.
- Loading branch information
Showing
15 changed files
with
558 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import * as ts from "typescript"; | ||
import {IndexSignatureDeclarationStructure} from "./../../structures"; | ||
import {removeInterfaceMember} from "./../../manipulation"; | ||
import {callBaseFill} from "./../callBaseFill"; | ||
import {Node, Identifier} from "./../common"; | ||
import {TypeNode, Type} from "./../type"; | ||
import {JSDocableNode, ChildOrderableNode} from "./../base"; | ||
|
||
export const IndexSignatureDeclarationBase = ChildOrderableNode(JSDocableNode(Node)); | ||
export class IndexSignatureDeclaration extends IndexSignatureDeclarationBase<ts.IndexSignatureDeclaration> { | ||
/** | ||
* Fills the node from a structure. | ||
* @param structure - Structure to fill. | ||
*/ | ||
fill(structure: Partial<IndexSignatureDeclarationStructure>) { | ||
callBaseFill(IndexSignatureDeclarationBase.prototype, this, structure); | ||
|
||
if (structure.keyName != null) | ||
this.setKeyName(structure.keyName); | ||
if (structure.keyType != null) | ||
this.setKeyType(structure.keyType); | ||
if (structure.returnType != null) | ||
this.setReturnType(structure.returnType); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Gets the key name. | ||
*/ | ||
getKeyName() { | ||
return this.getKeyNameNode().getText(); | ||
} | ||
|
||
/** | ||
* Sets the key name. | ||
* @param name - New name. | ||
*/ | ||
setKeyName(name: string) { | ||
if (this.getKeyName() === name) | ||
return; | ||
|
||
this.getKeyNameNode().replaceWithText(name); | ||
} | ||
|
||
/** | ||
* Gets the key name node. | ||
*/ | ||
getKeyNameNode() { | ||
const param = this.compilerNode.parameters[0]; | ||
return this.getNodeFromCompilerNode<Identifier>(param.name); | ||
} | ||
|
||
/** | ||
* Gets the key type. | ||
*/ | ||
getKeyType(): Type { | ||
return this.getKeyTypeNode().getType(); | ||
} | ||
|
||
/** | ||
* Sets the key type. | ||
* @param type - Type. | ||
*/ | ||
setKeyType(type: string) { | ||
if (this.getKeyTypeNode().getText() === type) | ||
return; | ||
this.getKeyTypeNode().replaceWithText(type); | ||
} | ||
|
||
/** | ||
* Gets the key type node. | ||
*/ | ||
getKeyTypeNode() { | ||
const param = this.compilerNode.parameters[0]; | ||
return this.getNodeFromCompilerNode<TypeNode>(param.type!); | ||
} | ||
|
||
/** | ||
* Gets the return type. | ||
*/ | ||
getReturnType() { | ||
return this.getReturnTypeNode().getType(); | ||
} | ||
|
||
/** | ||
* Gets the return type node. | ||
*/ | ||
getReturnTypeNode() { | ||
return this.getNodeFromCompilerNode<TypeNode>(this.compilerNode.type!); | ||
} | ||
|
||
/** | ||
* Sets the return type. | ||
* @param text | ||
*/ | ||
setReturnType(text: string) { | ||
const returnTypeNode = this.getReturnTypeNode(); | ||
if (returnTypeNode.getText() === text) | ||
return this; | ||
|
||
returnTypeNode.replaceWithText(text); | ||
return this; | ||
} | ||
|
||
/** | ||
* Removes this index signature. | ||
*/ | ||
remove() { | ||
removeInterfaceMember(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./CallSignatureDeclaration"; | ||
export * from "./ConstructSignatureDeclaration"; | ||
export * from "./IndexSignatureDeclaration"; | ||
export * from "./InterfaceDeclaration"; | ||
export * from "./MethodSignature"; | ||
export * from "./PropertySignature"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/structureToTexts/interface/IndexSignatureDeclarationStructureToText.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { IndexSignatureDeclarationStructure } from "./../../structures"; | ||
import { StructureToText } from "./../StructureToText"; | ||
|
||
export class IndexSignatureDeclarationStructureToText extends StructureToText<IndexSignatureDeclarationStructure> { | ||
writeText(structure: IndexSignatureDeclarationStructure) { | ||
this.writer.write(`[${structure.keyName || "key"}: ${structure.keyType || "string"}]: ${structure.returnType};`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./CallSignatureDeclarationStructureToText"; | ||
export * from "./ConstructSignatureDeclarationStructureToText"; | ||
export * from "./IndexSignatureDeclarationStructureToText"; | ||
export * from "./InterfaceDeclarationStructureToText"; | ||
export * from "./MethodSignatureStructureToText"; | ||
export * from "./PropertySignatureStructureToText"; |
7 changes: 7 additions & 0 deletions
7
src/structures/interface/IndexSignatureDeclarationStructure.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {JSDocableNodeStructure} from "./../base"; | ||
|
||
export interface IndexSignatureDeclarationStructure extends JSDocableNodeStructure { | ||
keyName?: string; | ||
keyType?: string; | ||
returnType: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./CallSignatureDeclarationStructure"; | ||
export * from "./ConstructSignatureDeclarationStructure"; | ||
export * from "./IndexSignatureDeclarationStructure"; | ||
export * from "./InterfaceDeclarationStructure"; | ||
export * from "./MethodSignatureStructure"; | ||
export * from "./PropertySignatureStructure"; |
Oops, something went wrong.