Skip to content

Commit

Permalink
fix: Write more with hanging indents (#461)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: More declarations are being written with hanging indents.
  • Loading branch information
dsherret committed Oct 11, 2018
1 parent 5a742b4 commit 687e467
Show file tree
Hide file tree
Showing 50 changed files with 750 additions and 255 deletions.
28 changes: 15 additions & 13 deletions lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ export interface ArgumentedNode {
* Adds arguments.
* @param argumentTexts - Argument texts to add.
*/
addArguments(argumentTexts: ReadonlyArray<string | WriterFunction>): Node[];
addArguments(argumentTexts: ReadonlyArray<string | WriterFunction> | WriterFunction): Node[];
/**
* Inserts an argument.
* @param index - Child index to insert at.
Expand All @@ -1869,7 +1869,7 @@ export interface ArgumentedNode {
* @param index - Child index to insert at.
* @param argumentTexts - Argument texts to insert.
*/
insertArguments(index: number, argumentTexts: ReadonlyArray<string | WriterFunction>): Node[];
insertArguments(index: number, argumentTexts: ReadonlyArray<string | WriterFunction> | WriterFunction): Node[];
/**
* Removes an argument.
* @param arg - Argument to remove.
Expand Down Expand Up @@ -2146,7 +2146,7 @@ export interface ExtendsClauseableNode {
* Adds multiple extends clauses.
* @param texts - Texts to add for the extends clause.
*/
addExtends(texts: ReadonlyArray<string>): ExpressionWithTypeArguments[];
addExtends(texts: ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[];
/**
* Adds an extends clause.
* @param text - Text to add for the extends clause.
Expand All @@ -2156,7 +2156,7 @@ export interface ExtendsClauseableNode {
* Inserts multiple extends clauses.
* @param texts - Texts to insert for the extends clause.
*/
insertExtends(index: number, texts: ReadonlyArray<string>): ExpressionWithTypeArguments[];
insertExtends(index: number, texts: ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[];
/**
* Inserts an extends clause.
* @param text - Text to insert for the extends clause.
Expand Down Expand Up @@ -2241,12 +2241,12 @@ export interface ImplementsClauseableNode {
* Adds multiple implements clauses.
* @param text - Texts to add for the implements clause.
*/
addImplements(text: ReadonlyArray<string>): ExpressionWithTypeArguments[];
addImplements(text: ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[];
/**
* Inserts an implements clause.
* @param text - Text to insert for the implements clause.
*/
insertImplements(index: number, texts: ReadonlyArray<string>): ExpressionWithTypeArguments[];
insertImplements(index: number, texts: ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[];
/**
* Inserts multiple implements clauses.
* @param text - Texts to insert for the implements clause.
Expand Down Expand Up @@ -3381,7 +3381,7 @@ export declare class ClassDeclaration extends ClassDeclarationBase<ts.ClassDecla
* Sets the extends expression.
* @param text - Text to set as the extends expression.
*/
setExtends(text: string): this;
setExtends(text: string | WriterFunction): this;
/**
* Removes the extends expression, if it exists.
*/
Expand Down Expand Up @@ -4726,7 +4726,7 @@ export declare class Decorator extends DecoratorBase<ts.Decorator> {
* Adds arguments.
* @param argumentTexts - Argument texts.
*/
addArguments(argumentTexts: ReadonlyArray<string | WriterFunction>): Node<ts.Node>[];
addArguments(argumentTexts: ReadonlyArray<string | WriterFunction> | WriterFunction): Node<ts.Node>[];
/**
* Inserts an argument.
* @param index - Child index to insert at.
Expand All @@ -4738,7 +4738,7 @@ export declare class Decorator extends DecoratorBase<ts.Decorator> {
* @param index - Child index to insert at.
* @param argumentTexts - Argument texts.
*/
insertArguments(index: number, argumentTexts: ReadonlyArray<string | WriterFunction>): Node<ts.Node>[];
insertArguments(index: number, argumentTexts: ReadonlyArray<string | WriterFunction> | WriterFunction): Node<ts.Node>[];
/**
* Removes an argument based on the node.
* @param node - Argument's node to remove.
Expand Down Expand Up @@ -9710,14 +9710,16 @@ export interface ExportableNodeStructure {
isExported?: boolean;
isDefaultExport?: boolean;
}

export interface ExtendsClauseableNodeStructure {
extends?: string[];
extends?: (string | WriterFunction)[] | WriterFunction;
}
export interface GeneratorableNodeStructure {
isGenerator?: boolean;
}

export interface ImplementsClauseableNodeStructure {
implements?: string[];
implements?: (string | WriterFunction)[] | WriterFunction;
}

export interface InitializerExpressionableNodeStructure extends InitializerSetExpressionableNodeStructure {
Expand Down Expand Up @@ -9807,7 +9809,7 @@ export interface ClassDeclarationStructure extends NameableNodeStructure, ClassD
}

interface ClassDeclarationSpecificStructure {
extends?: string;
extends?: string | WriterFunction;
ctors?: ConstructorDeclarationStructure[];
properties?: PropertyDeclarationStructure[];
getAccessors?: GetAccessorDeclarationStructure[];
Expand Down Expand Up @@ -9855,7 +9857,7 @@ interface SetAccessorDeclarationSpecificStructure {

export interface DecoratorStructure {
name: string;
arguments?: (string | WriterFunction)[];
arguments?: (string | WriterFunction)[] | WriterFunction;
typeArguments?: string[];
}

Expand Down
20 changes: 12 additions & 8 deletions src/compiler/ast/base/ArgumentedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface ArgumentedNode {
* Adds arguments.
* @param argumentTexts - Argument texts to add.
*/
addArguments(argumentTexts: ReadonlyArray<string | WriterFunction>): Node[];
addArguments(argumentTexts: ReadonlyArray<string | WriterFunction> | WriterFunction): Node[];
/**
* Inserts an argument.
* @param index - Child index to insert at.
Expand All @@ -33,7 +33,7 @@ export interface ArgumentedNode {
* @param index - Child index to insert at.
* @param argumentTexts - Argument texts to insert.
*/
insertArguments(index: number, argumentTexts: ReadonlyArray<string | WriterFunction>): Node[];
insertArguments(index: number, argumentTexts: ReadonlyArray<string | WriterFunction> | WriterFunction): Node[];
/**
* Removes an argument.
* @param arg - Argument to remove.
Expand All @@ -60,20 +60,23 @@ export function ArgumentedNode<T extends Constructor<ArgumentedNodeExtensionType
return this.addArguments([argumentText])[0];
}

addArguments(argumentTexts: ReadonlyArray<string | WriterFunction>) {
addArguments(argumentTexts: ReadonlyArray<string | WriterFunction> | WriterFunction) {
return this.insertArguments(this.getArguments().length, argumentTexts);
}

insertArgument(index: number, argumentText: string | WriterFunction) {
return this.insertArguments(index, [argumentText])[0];
}

insertArguments(index: number, argumentTexts: ReadonlyArray<string | WriterFunction>) {
insertArguments(index: number, argumentTexts: ReadonlyArray<string | WriterFunction> | WriterFunction) {
if (argumentTexts instanceof Function)
argumentTexts = [argumentTexts];

if (ArrayUtils.isNullOrEmpty(argumentTexts))
return [];

const args = this.getArguments();
index = verifyAndGetIndex(index, args.length);
const originalArgs = this.getArguments();
index = verifyAndGetIndex(index, originalArgs.length);

const writer = this.getWriterWithQueuedChildIndentation();
for (let i = 0; i < argumentTexts.length; i++) {
Expand All @@ -83,12 +86,13 @@ export function ArgumentedNode<T extends Constructor<ArgumentedNodeExtensionType

insertIntoCommaSeparatedNodes({
parent: this.getFirstChildByKindOrThrow(SyntaxKind.OpenParenToken).getNextSiblingIfKindOrThrow(SyntaxKind.SyntaxList),
currentNodes: args,
currentNodes: originalArgs,
insertIndex: index,
newText: writer.toString()
});

return getNodesToReturn(this.getArguments(), index, argumentTexts.length);
const newArgs = this.getArguments();
return getNodesToReturn(newArgs, index, newArgs.length - originalArgs.length);
}

removeArgument(arg: Node): this;
Expand Down
58 changes: 30 additions & 28 deletions src/compiler/ast/base/ExtendsClauseableNode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as errors from "../../../errors";
import { getNodeOrNodesToReturn, insertIntoCommaSeparatedNodes, insertIntoParentTextRange, verifyAndGetIndex } from "../../../manipulation";
import { getNodesToReturn, insertIntoCommaSeparatedNodes, insertIntoParentTextRange, verifyAndGetIndex } from "../../../manipulation";
import { CommaSeparatedStructuresPrinter, StringStructurePrinter } from "../../../structurePrinters";
import { ExtendsClauseableNodeStructure } from "../../../structures";
import { Constructor } from "../../../types";
import { Constructor, WriterFunction } from "../../../types";
import { SyntaxKind } from "../../../typescript";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
Expand All @@ -21,7 +21,7 @@ export interface ExtendsClauseableNode {
* Adds multiple extends clauses.
* @param texts - Texts to add for the extends clause.
*/
addExtends(texts: ReadonlyArray<string>): ExpressionWithTypeArguments[];
addExtends(texts: ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[];
/**
* Adds an extends clause.
* @param text - Text to add for the extends clause.
Expand All @@ -31,7 +31,7 @@ export interface ExtendsClauseableNode {
* Inserts multiple extends clauses.
* @param texts - Texts to insert for the extends clause.
*/
insertExtends(index: number, texts: ReadonlyArray<string>): ExpressionWithTypeArguments[];
insertExtends(index: number, texts: ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[];
/**
* Inserts an extends clause.
* @param text - Text to insert for the extends clause.
Expand All @@ -56,16 +56,18 @@ export function ExtendsClauseableNode<T extends Constructor<ExtendsClauseableNod
return extendsClause == null ? [] : extendsClause.getTypeNodes();
}

addExtends(texts: ReadonlyArray<string>): ExpressionWithTypeArguments[];
addExtends(texts: ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[];
addExtends(text: string): ExpressionWithTypeArguments;
addExtends(text: string | ReadonlyArray<string>): ExpressionWithTypeArguments[] | ExpressionWithTypeArguments {
addExtends(text: string | ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[] | ExpressionWithTypeArguments {
return this.insertExtends(this.getExtends().length, text as any);
}

insertExtends(index: number, texts: ReadonlyArray<string>): ExpressionWithTypeArguments[];
insertExtends(index: number, texts: ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[];
insertExtends(index: number, text: string): ExpressionWithTypeArguments;
insertExtends(index: number, texts: string | ReadonlyArray<string>): ExpressionWithTypeArguments[] | ExpressionWithTypeArguments {
const length = texts instanceof Array ? texts.length : 0;
insertExtends(index: number, texts: string | ReadonlyArray<string | WriterFunction> | WriterFunction): ExpressionWithTypeArguments[] | ExpressionWithTypeArguments {
const originalExtends = this.getExtends();
const wasStringInput = typeof texts === "string";

if (typeof texts === "string") {
errors.throwIfWhitespaceOrNotString(texts, nameof(texts));
texts = [texts];
Expand All @@ -79,34 +81,34 @@ export function ExtendsClauseableNode<T extends Constructor<ExtendsClauseableNod

structurePrinter.printText(writer, texts);

const extendsTypes = this.getExtends();
index = verifyAndGetIndex(index, extendsTypes.length);
index = verifyAndGetIndex(index, originalExtends.length);

if (extendsTypes.length > 0) {
if (originalExtends.length > 0) {
const extendsClause = this.getHeritageClauseByKindOrThrow(SyntaxKind.ExtendsKeyword);
insertIntoCommaSeparatedNodes({
parent: extendsClause.getFirstChildByKindOrThrow(SyntaxKind.SyntaxList),
currentNodes: extendsTypes,
currentNodes: originalExtends,
insertIndex: index,
newText: writer.toString()
});
return getNodeOrNodesToReturn(this.getExtends(), index, length);
}
else {
const openBraceToken = this.getFirstChildByKindOrThrow(SyntaxKind.OpenBraceToken);
const openBraceStart = openBraceToken.getStart();
const isLastSpace = /\s/.test(this.getSourceFile().getFullText()[openBraceStart - 1]);
let insertText = `extends ${writer.toString()} `;
if (!isLastSpace)
insertText = " " + insertText;

insertIntoParentTextRange({
parent: this,
insertPos: openBraceStart,
newText: insertText
});
}

const openBraceToken = this.getFirstChildByKindOrThrow(SyntaxKind.OpenBraceToken);
const openBraceStart = openBraceToken.getStart();
const isLastSpace = /\s/.test(this.getSourceFile().getFullText()[openBraceStart - 1]);
let insertText = `extends ${writer.toString()} `;
if (!isLastSpace)
insertText = " " + insertText;

insertIntoParentTextRange({
parent: this,
insertPos: openBraceStart,
newText: insertText
});

return getNodeOrNodesToReturn(this.getExtends(), index, length);
const newExtends = this.getExtends();
return wasStringInput ? newExtends[index] : getNodesToReturn(newExtends, index, newExtends.length - originalExtends.length);
}

removeExtends(index: number): this;
Expand Down

0 comments on commit 687e467

Please sign in to comment.