Skip to content

Commit

Permalink
feat: Add Directory.forget(). Will deprecate Directory.remove() in ne…
Browse files Browse the repository at this point in the history
…xt major.
  • Loading branch information
dsherret committed Apr 21, 2018
1 parent 2299531 commit f584d20
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions docs/navigation/directories.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ directory.deleteImmediatelySync();

This isn't recommended though because it could possibly leave the file system in a halfway state if your code errors before it's done.

### Removing
### Forgetting

Removes from main project object without deleting it:
Forgets the directory from main project object without deleting it:

```ts
directory.remove();
directory.forget();
```

Note that after doing this, the directory object and all its descendant source files and directories will not be available. If you want to use them again,
Expand Down
2 changes: 1 addition & 1 deletion src/factories/DirectoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class DirectoryCache {
if (!isRootDir)
this.addToDirectoriesByDirPath(newDirectory);

if (!newDirectory._hasLoadedParent())
if (!this.has(parentDirPath))
this.orphanDirs.set(path, newDirectory);

this.directoriesByPath.set(path, newDirectory);
Expand Down
26 changes: 18 additions & 8 deletions src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ export class Directory {
for (const dir of this.getDirectories())
dir.delete();
fileSystemWrapper.queueDelete(path);
this.remove();
this.forget();
}

/**
Expand All @@ -438,7 +438,7 @@ export class Directory {
async deleteImmediately() {
const {fileSystemWrapper} = this.global;
const path = this.getPath();
this.remove();
this.forget();
await fileSystemWrapper.deleteImmediately(path);
}

Expand All @@ -453,24 +453,34 @@ export class Directory {
}

/**
* Removes the directory and all its descendants from the AST.
* Forgets the directory and all its descendants from the Project.
*
* Note: Does not delete the directory from the file system.
*/
remove() {
if (this._wasRemoved())
forget() {
if (this._wasForgotten())
return;

for (const sourceFile of this.getSourceFiles())
sourceFile.forget();

for (const dir of this.getDirectories())
dir.remove();
dir.forget();

this.global.compilerFactory.removeDirectoryFromCache(this);
this._global = undefined;
}

/**
* Removes the directory and all its descendants from the AST.
*
* Note: Does not delete the directory from the file system.
* @deprecated - Use `.forget()`.
*/
remove() {
this.forget();
}

/**
* Asynchronously saves the directory and all the unsaved source files to the disk.
*/
Expand Down Expand Up @@ -562,7 +572,7 @@ export class Directory {
}

/** @internal */
_wasRemoved() {
_wasForgotten() {
return this._global == null;
}

Expand All @@ -572,7 +582,7 @@ export class Directory {
}

private throwIfDeletedOrRemoved() {
if (this._wasRemoved())
if (this._wasForgotten())
throw new errors.InvalidOperationError("Cannot use a directory that was deleted or removed.");
}

Expand Down
1 change: 1 addition & 0 deletions src/next-major-deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Remove:
* `isEqualsExport` on `ExportAssignmentStructure`.
* `Identifier.getDefinitionReferencingNodes`
* `getRelativePathToSourceFile` and `getRelativePathToSourceFileAsModuleSpecifier`
* `Directory.remove`
24 changes: 12 additions & 12 deletions src/tests/fileSystem/directoryTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ describe(nameof(Directory), () => {

directory.delete();
expect(fileSystem.getDeleteLog()).to.deep.equal([]);
expect(directory._wasRemoved()).to.be.true;
expect(childDir._wasRemoved()).to.be.true;
expect(directory._wasForgotten()).to.be.true;
expect(childDir._wasForgotten()).to.be.true;
expect(sourceFile.wasForgotten()).to.be.true;
expect(otherSourceFile.wasForgotten()).to.be.false;
project.saveSync();
Expand Down Expand Up @@ -722,8 +722,8 @@ describe(nameof(Directory), () => {
const otherSourceFile = project.createSourceFile("otherFile.ts");

await directory.deleteImmediately();
expect(directory._wasRemoved()).to.be.true;
expect(childDir._wasRemoved()).to.be.true;
expect(directory._wasForgotten()).to.be.true;
expect(childDir._wasForgotten()).to.be.true;
expect(sourceFile.wasForgotten()).to.be.true;
expect(otherSourceFile.wasForgotten()).to.be.false;
expect(fileSystem.getDeleteLog()).to.deep.equal([{ path: "/dir" }]);
Expand All @@ -740,29 +740,29 @@ describe(nameof(Directory), () => {
const otherSourceFile = project.createSourceFile("otherFile.ts");

directory.deleteImmediatelySync();
expect(directory._wasRemoved()).to.be.true;
expect(childDir._wasRemoved()).to.be.true;
expect(directory._wasForgotten()).to.be.true;
expect(childDir._wasForgotten()).to.be.true;
expect(sourceFile.wasForgotten()).to.be.true;
expect(otherSourceFile.wasForgotten()).to.be.false;
expect(fileSystem.getDeleteLog()).to.deep.equal([{ path: "/dir" }]);
});
});

describe(nameof<Directory>(d => d.remove), () => {
it("should remove the file and all its descendants", () => {
describe(nameof<Directory>(d => d.forget), () => {
it("should forget the directory and all its descendants", () => {
const project = getProject();
const directory = project.createDirectory("dir");
const childDir = directory.createDirectory("childDir");
const sourceFile = directory.createSourceFile("file.ts");
const otherSourceFile = project.createSourceFile("otherFile.ts");

directory.remove();
expect(directory._wasRemoved()).to.be.true;
directory.forget();
expect(directory._wasForgotten()).to.be.true;
expect(() => directory.getPath()).to.throw();
expect(childDir._wasRemoved()).to.be.true;
expect(childDir._wasForgotten()).to.be.true;
expect(sourceFile.wasForgotten()).to.be.true;
expect(otherSourceFile.wasForgotten()).to.be.false;
expect(() => directory.remove()).to.not.throw();
expect(() => directory.forget()).to.not.throw();
});
});

Expand Down

0 comments on commit f584d20

Please sign in to comment.