Skip to content

Commit

Permalink
feat: #418 - Add new .set methods and deprecate .fill
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `.fill(...)` is now `.set(...)` and will replace existing nodes.

If you want the old behaviour, use the `.addX` methods or provide the structures of the nodes by using `.getStructure()` (Ex. `functionDeclaration.fill({ parameters: [...functionDeclaration.getParameters().map(p => p.getStructure()), { name: "myParam" }] });`)
  • Loading branch information
dsherret committed Sep 16, 2018
1 parent e81412c commit da40d99
Show file tree
Hide file tree
Showing 118 changed files with 1,058 additions and 735 deletions.
206 changes: 92 additions & 114 deletions lib/ts-simple-ast.d.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/compiler/base/AmbientableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AmbientableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind } from "../../typescript";
import { isNodeAmbientOrInAmbientContext, TypeGuards } from "../../utils";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { ModifierableNode } from "./ModifierableNode";
import { callBaseGetStructure } from "../callBaseGetStructure";
Expand Down Expand Up @@ -61,8 +61,8 @@ export function AmbientableNode<T extends Constructor<AmbientableNodeExtensionTy
return this;
}

fill(structure: Partial<AmbientableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<AmbientableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.hasDeclareKeyword != null)
this.setHasDeclareKeyword(structure.hasDeclareKeyword);
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/base/AsyncableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { AsyncableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { ModifierableNode } from "./ModifierableNode";
import { callBaseGetStructure } from "../callBaseGetStructure";
Expand Down Expand Up @@ -48,8 +48,8 @@ export function AsyncableNode<T extends Constructor<AsyncableNodeExtensionType>>
return this;
}

fill(structure: Partial<AsyncableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<AsyncableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.isAsync != null)
this.setIsAsync(structure.isAsync);
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/base/AwaitableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { insertIntoParentTextRange, removeChildren } from "../../manipulation";
import { AwaitableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";

export type AwaitableNodeExtensionType = Node<ts.Node & { awaitModifier?: ts.AwaitKeywordToken; }>;
Expand Down Expand Up @@ -67,8 +67,8 @@ export function AwaitableNode<T extends Constructor<AwaitableNodeExtensionType>>
return this;
}

fill(structure: Partial<AwaitableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<AwaitableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.isAwaited != null)
this.setIsAwaited(structure.isAwaited);
Expand Down
15 changes: 5 additions & 10 deletions src/compiler/base/BodiedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as errors from "../../errors";
import { BodiedNodeStructure } from "../../structures";
import { Constructor, WriterFunction } from "../../types";
import { ts } from "../../typescript";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { getBodyTextForStructure, setBodyTextForNode } from "./helpers";
import { callBaseGetStructure } from "../callBaseGetStructure";
Expand All @@ -16,14 +16,9 @@ export interface BodiedNode {
getBody(): Node;
/**
* Sets the body text.
* @param writerFunction - Write the text using the provided writer.
* @param textOrWriterFunction - Text or writer function to set as the body.
*/
setBodyText(writerFunction: WriterFunction): this;
/**
* Sets the body text.
* @param text - Text to set as the body.
*/
setBodyText(text: string): this;
setBodyText(textOrWriterFunction: string | WriterFunction): this;
}

export function BodiedNode<T extends Constructor<BodiedNodeExtensionType>>(Base: T): Constructor<BodiedNode> & T {
Expand All @@ -42,8 +37,8 @@ export function BodiedNode<T extends Constructor<BodiedNodeExtensionType>>(Base:
return this;
}

fill(structure: Partial<BodiedNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<BodiedNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.bodyText != null)
this.setBodyText(structure.bodyText);
Expand Down
15 changes: 5 additions & 10 deletions src/compiler/base/BodyableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { insertIntoParentTextRange } from "../../manipulation";
import { BodyableNodeStructure } from "../../structures";
import { Constructor, WriterFunction } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { Node } from "../common/Node";
import { getBodyTextForStructure, setBodyTextForNode } from "./helpers";
Expand All @@ -25,14 +25,9 @@ export interface BodyableNode {
hasBody(): boolean;
/**
* Sets the body text. A body is required to do this operation.
* @param writerFunction - Write the text using the provided writer.
* @param textOrWriterFunction - Text or writer function to set as the body.
*/
setBodyText(writerFunction: WriterFunction): this;
/**
* Sets the body text. A body is required to do this operation.
* @param text - Text to set as the body.
*/
setBodyText(text: string): this;
setBodyText(textOrWriterFunction: string | WriterFunction): this;
/**
* Adds a body if it doesn't exists.
*/
Expand Down Expand Up @@ -98,8 +93,8 @@ export function BodyableNode<T extends Constructor<BodyableNodeExtensionType>>(B
return this;
}

fill(structure: Partial<BodyableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<BodyableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.bodyText != null)
this.setBodyText(structure.bodyText);
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/base/DecoratableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DecoratableNodeStructure, DecoratorStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { ArrayUtils, getNodeByNameOrFindFunction, getNotFoundErrorMessageForNameOrFindFunction } from "../../utils";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { Decorator } from "../decorator/Decorator";
import { callBaseGetStructure } from "../callBaseGetStructure";
Expand Down Expand Up @@ -116,11 +116,13 @@ export function DecoratableNode<T extends Constructor<DecoratableNodeExtensionTy
return getNodesToReturn(this.getDecorators(), index, structures.length);
}

fill(structure: Partial<DecoratableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<DecoratableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.decorators != null && structure.decorators.length > 0)
if (structure.decorators != null) {
this.getDecorators().forEach(d => d.remove());
this.addDecorators(structure.decorators);
}

return this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/base/ExclamationTokenableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ExclamationTokenableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { TypeGuards } from "../../utils";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { callBaseGetStructure } from "../callBaseGetStructure";

Expand Down Expand Up @@ -71,8 +71,8 @@ export function ExclamationTokenableNode<T extends Constructor<ExclamationTokena
return this;
}

fill(structure: Partial<ExclamationTokenableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<ExclamationTokenableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.hasExclamationToken != null)
this.setHasExclamationToken(structure.hasExclamationToken);
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/base/ExportableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ExportableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind } from "../../typescript";
import { TypeGuards } from "../../utils";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { ModifierableNode } from "./ModifierableNode";
import { callBaseGetStructure } from "../callBaseGetStructure";
Expand Down Expand Up @@ -176,8 +176,8 @@ export function ExportableNode<T extends Constructor<ExportableNodeExtensionType
return this;
}

fill(structure: Partial<ExportableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<ExportableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.isExported != null)
this.setIsExported(structure.isExported);
Expand Down
12 changes: 7 additions & 5 deletions src/compiler/base/ExtendsClauseableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CommaSeparatedStructuresPrinter, StringStructurePrinter } from "../../s
import { ExtendsClauseableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind } from "../../typescript";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { ExpressionWithTypeArguments } from "../type/ExpressionWithTypeArguments";
import { HeritageClauseableNode } from "./HeritageClauseableNode";
Expand Down Expand Up @@ -67,7 +67,7 @@ export function ExtendsClauseableNode<T extends Constructor<ExtendsClauseableNod
insertExtends(index: number, texts: string | ReadonlyArray<string>): ExpressionWithTypeArguments[] | ExpressionWithTypeArguments {
const length = texts instanceof Array ? texts.length : 0;
if (typeof texts === "string") {
errors.throwIfNotStringOrWhitespace(texts, nameof(texts));
errors.throwIfWhitespaceOrNotString(texts, nameof(texts));
texts = [texts];
}
else if (texts.length === 0) {
Expand Down Expand Up @@ -120,11 +120,13 @@ export function ExtendsClauseableNode<T extends Constructor<ExtendsClauseableNod
return this;
}

fill(structure: Partial<ExtendsClauseableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<ExtendsClauseableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.extends != null && structure.extends.length > 0)
if (structure.extends != null) {
this.getExtends().forEach(e => this.removeExtends(e));
this.addExtends(structure.extends);
}

return this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/base/GeneratorableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GeneratorableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { NamedNode } from "../base";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { callBaseGetStructure } from "../callBaseGetStructure";

Expand Down Expand Up @@ -68,8 +68,8 @@ export function GeneratorableNode<T extends Constructor<GeneratorableNodeExtensi
return this;
}

fill(structure: Partial<GeneratorableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<GeneratorableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.isGenerator != null)
this.setIsGenerator(structure.isGenerator);
Expand Down
12 changes: 7 additions & 5 deletions src/compiler/base/ImplementsClauseableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CommaSeparatedStructuresPrinter, StringStructurePrinter } from "../../s
import { ImplementsClauseableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind } from "../../typescript";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { ExpressionWithTypeArguments } from "../type/ExpressionWithTypeArguments";
import { HeritageClauseableNode } from "./HeritageClauseableNode";
Expand Down Expand Up @@ -67,7 +67,7 @@ export function ImplementsClauseableNode<T extends Constructor<ImplementsClausea
insertImplements(index: number, texts: string | ReadonlyArray<string>): ExpressionWithTypeArguments | ExpressionWithTypeArguments[] {
const length = texts instanceof Array ? texts.length : 0;
if (typeof texts === "string") {
errors.throwIfNotStringOrWhitespace(texts, nameof(texts));
errors.throwIfWhitespaceOrNotString(texts, nameof(texts));
texts = [texts];
}
else if (texts.length === 0) {
Expand Down Expand Up @@ -122,11 +122,13 @@ export function ImplementsClauseableNode<T extends Constructor<ImplementsClausea
return this;
}

fill(structure: Partial<ImplementsClauseableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<ImplementsClauseableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.implements != null && structure.implements.length > 0)
if (structure.implements != null) {
this.getImplements().forEach(expr => this.removeImplements(expr));
this.addImplements(structure.implements);
}

return this;
}
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/base/JSDocableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JSDocableNodeStructure, JSDocStructure } from "../../structures";
import { Constructor } from "../../types";
import { ts } from "../../typescript";
import { ArrayUtils } from "../../utils";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { WriterFunction } from "../../types";
import { Node } from "../common";
import { JSDoc } from "../doc/JSDoc";
Expand Down Expand Up @@ -83,11 +83,13 @@ export function JSDocableNode<T extends Constructor<JSDocableNodeExtensionType>>
return getNodesToReturn(this.getJsDocs(), index, structures.length);
}

fill(structure: Partial<JSDocableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<JSDocableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.docs != null && structure.docs.length > 0)
if (structure.docs != null) {
this.getJsDocs().forEach(doc => doc.remove());
this.addJsDocs(structure.docs);
}

return this;
}
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/base/ParameteredNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ParameterDeclarationStructure, ParameteredNodeStructure } from "../../s
import { Constructor } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { ArrayUtils, getNodeByNameOrFindFunction, getNotFoundErrorMessageForNameOrFindFunction } from "../../utils";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { ParameterDeclaration } from "../function/ParameterDeclaration";
import { callBaseGetStructure } from "../callBaseGetStructure";
Expand Down Expand Up @@ -109,11 +109,13 @@ export function ParameteredNode<T extends Constructor<ParameteredNodeExtensionTy
return getNodesToReturn(this.getParameters(), index, structures.length);
}

fill(structure: Partial<ParameteredNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<ParameteredNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.parameters != null && structure.parameters.length > 0)
if (structure.parameters != null) {
this.getParameters().forEach(p => p.remove());
this.addParameters(structure.parameters);
}

return this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/base/QuestionTokenableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { QuestionTokenableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { TypeGuards } from "../../utils";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { callBaseGetStructure } from "../callBaseGetStructure";

Expand Down Expand Up @@ -79,8 +79,8 @@ export function QuestionTokenableNode<T extends Constructor<QuestionTokenableNod
}
}

fill(structure: Partial<QuestionTokenableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<QuestionTokenableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.hasQuestionToken != null)
this.setHasQuestionToken(structure.hasQuestionToken);
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/base/ReadonlyableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as errors from "../../errors";
import { ReadonlyableNodeStructure } from "../../structures";
import { Constructor } from "../../types";
import { SyntaxKind } from "../../typescript";
import { callBaseFill } from "../callBaseFill";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { ModifierableNode } from "./ModifierableNode";
import { callBaseGetStructure } from "../callBaseGetStructure";
Expand Down Expand Up @@ -48,8 +48,8 @@ export function ReadonlyableNode<T extends Constructor<ReadonlyableNodeExtension
return this;
}

fill(structure: Partial<ReadonlyableNodeStructure>) {
callBaseFill(Base.prototype, this, structure);
set(structure: Partial<ReadonlyableNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

if (structure.isReadonly != null)
this.setIsReadonly(structure.isReadonly);
Expand Down

0 comments on commit da40d99

Please sign in to comment.