Skip to content

Commit

Permalink
fix: #745 - Inserting a member when a comment was the last member wou…
Browse files Browse the repository at this point in the history
…ld throw.
  • Loading branch information
dsherret committed Oct 22, 2019
1 parent d78e660 commit d83c031
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/manipulation/helpers/getInsertPosFromIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function getEndPosFromIndex(index: number, parent: Node, children: Node[]
if (closeBraceToken == null)
endPos = parent.getEnd();
else
endPos = closeBraceToken.getNonWhitespaceStart();
endPos = closeBraceToken.getStart();
}
}
else {
Expand Down
5 changes: 3 additions & 2 deletions src/manipulation/manipulations/insertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,14 @@ export interface InsertIntoBracesOrSourceFileOptions {
export function insertIntoBracesOrSourceFile(opts: InsertIntoBracesOrSourceFileOptions) {
const { parent, index, children } = opts;
const fullText = parent._sourceFile.getFullText();
const insertPos = getInsertPosFromIndex(index, parent.getChildSyntaxListOrThrow(), children);
const childSyntaxList = parent.getChildSyntaxListOrThrow();
const insertPos = getInsertPosFromIndex(index, childSyntaxList, children);
const endPos = getEndPosFromIndex(index, parent, children, fullText);
const replacingLength = endPos - insertPos;
const newText = getNewText();

doManipulation(parent._sourceFile, new InsertionTextManipulator({ insertPos, replacingLength, newText }), new NodeHandlerFactory().getForParentRange({
parent: parent.getChildSyntaxListOrThrow(),
parent: childSyntaxList,
start: insertPos,
end: insertPos + newText.length,
replacingLength
Expand Down
30 changes: 30 additions & 0 deletions src/tests/issues/745tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from "chai";
import { Project } from "../../Project";

describe("tests for issue #45", () => {
it("should not error when adding a constructor and the class only has an empty comment in the body", () => {
const project = new Project({ useVirtualFileSystem: true });
const sourceFile = project.createSourceFile("src/MyClass.ts", `class Foo {
//
}`);
const classDec = sourceFile.getClasses()[0];
classDec.addConstructor({
statements: "console.log('test');",
});

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

it("should not error when there is a comment on the close brace token", () => {
const project = new Project({ useVirtualFileSystem: true });
const sourceFile = project.createSourceFile("src/MyClass.ts", `class Foo {
//
/*b*/}`);
const classDec = sourceFile.getClasses()[0];
classDec.insertConstructor(1, {
statements: "console.log('test');",
});

expect(sourceFile.getFullText()).to.equal("class Foo {\n//\n\n constructor() {\n console.log('test');\n }\n\n/*b*/}");
});
});
1 change: 1 addition & 0 deletions src/tests/projectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ describe(nameof(Project), () => {

describe(nameof<Project>(t => t.forgetNodesCreatedInBlock), () => {
describe("synchronous", () => {
// todo: this should be moved into an "it" block
const project = new Project({ useVirtualFileSystem: true });
let sourceFile: SourceFile;
let sourceFileNotNavigated: SourceFile;
Expand Down

0 comments on commit d83c031

Please sign in to comment.