Skip to content

Commit

Permalink
fix: #808 - Unwrap would throw an error if body starts or ends with c…
Browse files Browse the repository at this point in the history
…omments.
  • Loading branch information
dsherret committed Apr 26, 2020
1 parent f898922 commit eba6037
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StringUtils } from "@ts-morph/common";
import { StringUtils, SyntaxKind, errors } from "@ts-morph/common";
import { Node } from "../../compiler";
import { InsertionTextManipulator } from "./InsertionTextManipulator";

Expand All @@ -13,13 +13,30 @@ export class UnwrapTextManipulator extends InsertionTextManipulator {
}

function getReplacementText(node: Node) {
const childSyntaxList = node.getChildSyntaxListOrThrow();
const sourceFile = node._sourceFile;
const startPos = childSyntaxList.getPos();
const range = getInnerBraceRange();
const startPos = range[0];
const text = sourceFile.getFullText().substring(range[0], range[1]);

return StringUtils.indent(childSyntaxList.getFullText(), -1, {
return StringUtils.indent(text, -1, {
indentText: sourceFile._context.manipulationSettings.getIndentationText(),
indentSizeInSpaces: sourceFile._context.manipulationSettings._getIndentSizeInSpaces(),
isInStringAtPos: pos => sourceFile.isInStringAtPos(startPos + pos),
}).trimLeft();
}).trim();

function getInnerBraceRange() {
const bodyNode = getBodyNode();
return [bodyNode.getStart() + 1, bodyNode.getEnd() - 1] as const;

function getBodyNode() {
if (Node.isNamespaceDeclaration(node))
return node._getInnerBody();
else if (Node.isBodiedNode(node))
return node.getBody();
else if (Node.isBodyableNode(node))
return node.getBodyOrThrow();
else
throw new errors.NotImplementedError(`Not implemented unwrap scenario for ${node.getKindName()}.`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,21 @@ var v = \`some text
var t: string;
}`, `var t: string;`);
});

it("should unwrap a namespace with statements surrounded by comments", () => {
doTest(`
namespace Some {
// test
class Whatever {}
// Some comment.
/* This one too. */
}
`, `
// test
class Whatever {}
// Some comment.
/* This one too. */
`);
});
});
});
2 changes: 1 addition & 1 deletion packages/ts-morph/src/tests/issues/745tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { Project } from "../../Project";

describe("tests for issue #45", () => {
describe("tests for issue #745", () => {
it("should not error when adding a constructor and the class only has an empty comment in the body", () => {
const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("src/MyClass.ts", `class Foo {
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/src/tests/issues/778tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { getInfoFromText } from "../compiler/testHelpers";

describe("tests for issue #654", () => {
describe("tests for issue #778", () => {
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 {
Expand Down

0 comments on commit eba6037

Please sign in to comment.