Skip to content

Commit

Permalink
refactor: #619 - Rename StructureTypeGuards methods to remove word …
Browse files Browse the repository at this point in the history
…`Node` and `Declaration` in certain cases.

BREAKING CHANGE: Renamed `StructureTypeGuards` methods to remove word `Node` and `Declaration` in certain cases.
  • Loading branch information
dsherret committed May 18, 2019
1 parent a544e55 commit 92331e1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 58 deletions.
22 changes: 22 additions & 0 deletions breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

View [CHANGELOG.md](CHANGELOG.md) for more detail on releases. This file is only a high level overview of breaking changes.

## Version 3

### Renamed some `StructureTypeGuards` static methods

`StructureTypeGuards` was added in version 2. This class unfortunately had some poorly named methods.

In version 3, some `StructureTypeGuards` static methods have removed the word `Node` and `Declaration` in certain cases.

For example the following:

```ts
if (StructureTypeGuards.isExportableNode(structure))
structure.isExported = false;
```

...is now updated to be...

```ts
if (StructureTypeGuards.isExportable(structure))
structure.isExported = false;
```

## Version 2

### Removed default export
Expand Down
2 changes: 1 addition & 1 deletion docs/manipulation/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ project.createSourceFile("private.ts", classesFileStructure);
function removeExports(structure: Structures) {
forEachStructureChild(structure, removeExports);

if (StructureTypeGuards.isExportableNode(structure))
if (StructureTypeGuards.isExportable(structure))
structure.isExported = false;
}
```
Expand Down
6 changes: 2 additions & 4 deletions docs/manipulation/structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ import { StructureTypeGuards } from "ts-morph";

// ...etc...

if (StructureTypeGuards.isExportableNode(structure))
if (StructureTypeGuards.isExportable(structure))
structure.isExported = false;
```

Note: In ts-morph 3.0, the `isExportableNode` method will be renamed to `isExportable` to reduce confusion.

#### `forEachStructureChild`

Similar to the compiler API's `forEachChild`, there is a `forEachStructureChild` method in ts-morph for navigating over a structure's children.
Expand Down Expand Up @@ -116,5 +114,5 @@ Note that unlike ts-morph's `forEachChild`, this function acts like the `forEach

```ts setup: const structure: SourceFileStructure;
const firstClassDecStructure = forEachStructureChild(structure,
child => StructureTypeGuards.isClassDeclaration(child) ? child : undefined);
child => StructureTypeGuards.isClass(child) ? child : undefined);
```
5 changes: 4 additions & 1 deletion scripts/generation/createStructureTypeGuardsUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ function addNewMethods(typeGuardsClass: ClassDeclaration, structureInfos: Struct
}

function formatName(name: string) {
return name.replace("Structure", "");
name = name.replace("Structure", "").replace(/Node$/, "");
if (name === "ExportDeclaration" || name === "ImportDeclaration" || name === "VariableDeclaration")
return name;
return name.replace(/Declaration$/, "");
}
}
5 changes: 4 additions & 1 deletion src/next-major-deprecations.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Deprecations in the next major

* < TS 3.2 (in order to add BigIntLiteral support)
* Rename ExtendedComment to CommentNode

# Future version

* < TS 3.2 (in order to add BigIntLiteral support)
94 changes: 47 additions & 47 deletions src/structures/utils/StructureTypeGuards.ts

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/tests/structures/utils/structureTypeGuardTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ describe(nameof(StructureTypeGuards), () => {
describe("base structure method tests", () => {
it("should type the type correctly within a guard", () => {
const structure: Structure & { kind: StructureKind; } = { kind: StructureKind.Class };
if (StructureTypeGuards.isAbstractableNode(structure))
if (StructureTypeGuards.isAbstractable(structure))
assert<IsExact<typeof structure, Structure & { kind: StructureKind; } & AbstractableNodeStructure>>(true);
expect(StructureTypeGuards.isAbstractableNode(structure)).to.be.true;
expect(StructureTypeGuards.isAbstractable(structure)).to.be.true;
});
});

describe("top level structure method types", () => {
it("should type the type correctly within a guard", () => {
const structure: Structure & { kind: StructureKind; } = { kind: StructureKind.Class };
if (StructureTypeGuards.isClassDeclaration(structure))
if (StructureTypeGuards.isClass(structure))
assert<IsExact<typeof structure, ClassDeclarationStructure>>(true);
expect(StructureTypeGuards.isClassDeclaration(structure)).to.be.true;
expect(StructureTypeGuards.isClass(structure)).to.be.true;
});
});
});

0 comments on commit 92331e1

Please sign in to comment.