Skip to content

Commit

Permalink
fix: #778 - Fixes error being thrown when inserting after a jsdoc and…
Browse files Browse the repository at this point in the history
… the jsdoc is the last member or statement.

Closes #778.
  • Loading branch information
dsherret committed Feb 8, 2020
1 parent 9bfc32e commit e069eff
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ export function ClassLikeDeclarationBaseSpecific<T extends Constructor<ClassLike
structures,
expectedKind: SyntaxKind.Constructor,
write: (writer, info) => {
if (!isAmbient && info.previousMember != null)
if (!isAmbient && info.previousMember != null && !Node.isCommentNode(info.previousMember))
writer.blankLineIfLastNot();
else
writer.newLineIfLastNot();
Expand Down Expand Up @@ -705,7 +705,7 @@ export function ClassLikeDeclarationBaseSpecific<T extends Constructor<ClassLike
structures,
expectedKind: SyntaxKind.GetAccessor,
write: (writer, info) => {
if (info.previousMember != null)
if (info.previousMember != null && !Node.isCommentNode(info.previousMember))
writer.blankLineIfLastNot();
else
writer.newLineIfLastNot();
Expand Down Expand Up @@ -740,7 +740,7 @@ export function ClassLikeDeclarationBaseSpecific<T extends Constructor<ClassLike
structures,
expectedKind: SyntaxKind.SetAccessor,
write: (writer, info) => {
if (info.previousMember != null)
if (info.previousMember != null && !Node.isCommentNode(info.previousMember))
writer.blankLineIfLastNot();
else
writer.newLineIfLastNot();
Expand Down Expand Up @@ -808,7 +808,7 @@ export function ClassLikeDeclarationBaseSpecific<T extends Constructor<ClassLike
return insertChildren<MethodDeclaration>(this, {
index,
write: (writer, info) => {
if (!isAmbient && info.previousMember != null)
if (!isAmbient && info.previousMember != null && !Node.isCommentNode(info.previousMember))
writer.blankLineIfLastNot();
else
writer.newLineIfLastNot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
writeStructures: () => void,
opts: StandardWriteOptions = {}
) {
if (info.previousMember != null && (opts.previousNewLine == null || !opts.previousNewLine(info.previousMember)))
if (info.previousMember != null && (opts.previousNewLine == null || !opts.previousNewLine(info.previousMember)) && !Node.isCommentNode(info.previousMember))
writer.blankLine();
else if (!info.isStartOfFile)
writer.newLineIfLastNot();
Expand Down
22 changes: 21 additions & 1 deletion packages/ts-morph/src/manipulation/manipulations/insertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export function insertIntoBracesOrSourceFileWithGetChildren<TNode extends Node>(
const startChildren = opts.getIndexedChildren();
const parentSyntaxList = opts.parent.getChildSyntaxListOrThrow();
const index = verifyAndGetIndex(opts.index, startChildren.length);
const previousJsDocCount = getPreviousJsDocCount(); // the inserting node will take ownership

insertIntoBracesOrSourceFile({
parent: opts.parent,
Expand All @@ -316,7 +317,7 @@ export function insertIntoBracesOrSourceFileWithGetChildren<TNode extends Node>(
write: opts.write
});

return getRangeWithoutCommentsFromArray<TNode>(opts.getIndexedChildren(), opts.index, opts.structures.length, opts.expectedKind);
return getRangeWithoutCommentsFromArray<TNode>(opts.getIndexedChildren(), opts.index - previousJsDocCount, opts.structures.length, opts.expectedKind);

function getChildIndex() {
if (index === 0)
Expand All @@ -325,6 +326,25 @@ export function insertIntoBracesOrSourceFileWithGetChildren<TNode extends Node>(
// get the previous member in order to get the implementation signature + 1
return startChildren[index - 1].getChildIndex() + 1;
}

function getPreviousJsDocCount() {
let commentCount = 0;
let count = 0;
for (let i = index - 1; i >= 0; i--) {
const node = startChildren[i];
if (Node.isCommentNode(node)) {
commentCount++;

if (node.getText().startsWith("/**"))
count = commentCount;
}
else {
break;
}
}

return count;
}
}

export interface InsertIntoBracesOrSourceFileWithGetChildrenWithCommentsOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/ts-morph/src/tests/issues/654tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SyntaxKind } from "@ts-morph/common";
import { expect } from "chai";
import { getInfoFromText } from "../compiler/testHelpers";

describe("tests for issue #654", () => {
it("should add a new property where there exists a comment", () => {
const { sourceFile } = getInfoFromText("const ole = {\n foo: 'foo',\n // c\n bar: 'bar'\n}");
Expand Down
4 changes: 2 additions & 2 deletions packages/ts-morph/src/tests/issues/745tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("tests for issue #45", () => {
statements: "console.log('test');"
});

expect(sourceFile.getFullText()).to.equal("class Foo {\n//\n\n constructor() {\n console.log('test');\n }\n}");
expect(sourceFile.getFullText()).to.equal("class Foo {\n//\n constructor() {\n console.log('test');\n }\n}");
});

it("should not error when there is a comment on the close brace token", () => {
Expand All @@ -25,6 +25,6 @@ describe("tests for issue #45", () => {
statements: "console.log('test');"
});

expect(sourceFile.getFullText()).to.equal("class Foo {\n//\n\n constructor() {\n console.log('test');\n }\n\n/*b*/}");
expect(sourceFile.getFullText()).to.equal("class Foo {\n//\n constructor() {\n console.log('test');\n }\n\n/*b*/}");
});
});
64 changes: 64 additions & 0 deletions packages/ts-morph/src/tests/issues/778tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect } from "chai";
import { getInfoFromText } from "../compiler/testHelpers";

describe("tests for issue #654", () => {
it("should add a new member where there exists a js doc comment before at the end of the class", () => {
const { sourceFile } = getInfoFromText(`
class Foo {
/**
*
*/
}`);
const classDec = sourceFile.getClassOrThrow("Foo");
classDec.addConstructor({});
expect(sourceFile.getFullText()).to.equal(`
class Foo {
/**
*
*/
constructor() {
}
}`);
});

it("should add a new member where there exists multiple comments and js docs at the end of the class", () => {
const { sourceFile } = getInfoFromText(`
class Foo {
// asdf
/** test */
// test
/** test */
}`);
const classDec = sourceFile.getClassOrThrow("Foo");
classDec.addConstructor({});
expect(sourceFile.getFullText()).to.equal(`
class Foo {
// asdf
/** test */
// test
/** test */
constructor() {
}
}`);
});

it("should add a new source file statement where there exists multiple comments and js docs before at the end of the file", () => {
const { sourceFile } = getInfoFromText(`
// asdf
/** test */
// test
/** test */
// test
`);
sourceFile.addFunction({ name: "test" });
expect(sourceFile.getFullText()).to.equal(`
// asdf
/** test */
// test
/** test */
// test
function test() {
}
`);
});
});

0 comments on commit e069eff

Please sign in to comment.