Skip to content

Commit

Permalink
refactor: #680 - Rename SourceFile#getReferencedFiles() to `getPath…
Browse files Browse the repository at this point in the history
…ReferenceDirectives()`.

BREAKING CHANGE: `SourceFile#getReferencedFiles()` is now `getPathReferenceDirectives()`.

This was done to prevent confusion with upcoming methods in #680. The name was chosen because it is similar to the methods `getTypeReferenceDirectives()` and `getLibReferenceDirectives()`.
  • Loading branch information
dsherret committed Sep 2, 2019
1 parent 8aeef04 commit 578adc7
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
18 changes: 11 additions & 7 deletions docs/details/source-files.md
Expand Up @@ -138,7 +138,7 @@ sourceFile.moveImmediatelySync("NewFile2.ts");
Refresh the source file from the file system:

```ts
import {FileSystemRefreshResult} from "ts-morph";
import { FileSystemRefreshResult } from "ts-morph";

// returns: FileSystemRefreshResult (NoChange, Updated, Deleted)
const result = await sourceFile.refreshFromFileSystem(); // or refreshFromFileSystemSync()
Expand All @@ -151,21 +151,25 @@ If the file was _deleted_: The source file will be removed and all its nodes for

### Remove

Remove a source file from the AST by calling:
Remove a source file from the project by calling:

```ts
project.removeSourceFile(sourceFile); // returns: boolean (if was removed)
sourceFile.forget();
```

Note: This does not delete the file from the file system. To do delete it, call `.delete()`.
Or alternatively:

### Reference comments
```ts
project.removeSourceFile(sourceFile); // returns: boolean (true if was removed)
```

This returns any files that are referenced via :
Note: This does not delete the file from the file system. To do delete it, call `#delete()`.

### Reference comments

```ts
// gets `/// <reference path="..." />` comments
const referencedFiles = sourceFile.getReferencedFiles();
const pathReferenceDirectives = sourceFile.getPathReferenceDirectives();
// gets `/// <reference types="..." />` comments
const typeReferenceDirectives = sourceFile.getTypeReferenceDirectives();
// gets `/// <reference lib="..." />` comments
Expand Down
2 changes: 1 addition & 1 deletion docs/details/variables.md
Expand Up @@ -59,7 +59,7 @@ const declarationKind = variableStatement.getDeclarationKind();
It will return one of the following values:

```ts
import {VariableDeclarationKind} from "ts-morph";
import { VariableDeclarationKind } from "ts-morph";

VariableDeclarationKind.Let;
VariableDeclarationKind.Const;
Expand Down
4 changes: 2 additions & 2 deletions lib/ts-morph.d.ts
Expand Up @@ -477,7 +477,7 @@ export declare class Project {
*/
createSourceFile(filePath: string, sourceFileText?: string | OptionalKind<SourceFileStructure> | WriterFunction, options?: SourceFileCreateOptions): SourceFile;
/**
* Removes a source file from the AST.
* Removes a source file from the project.
* @param sourceFile - Source file to remove.
* @returns True if removed.
*/
Expand Down Expand Up @@ -8199,7 +8199,7 @@ export declare class SourceFile extends SourceFileBase<ts.SourceFile> {
/**
* Gets any `/// <reference path="..." />` comments.
*/
getReferencedFiles(): FileReference[];
getPathReferenceDirectives(): FileReference[];
/**
* Gets any `/// <reference types="..." />` comments.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Project.ts
Expand Up @@ -302,7 +302,7 @@ export class Project {
}

/**
* Removes a source file from the AST.
* Removes a source file from the project.
* @param sourceFile - Source file to remove.
* @returns True if removed.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ast/module/SourceFile.ts
Expand Up @@ -473,7 +473,7 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
/**
* Gets any `/// <reference path="..." />` comments.
*/
getReferencedFiles() {
getPathReferenceDirectives() {
if (this._referencedFiles == null) {
this._referencedFiles = (this.compilerNode.referencedFiles || [])
.map(f => new FileReference(f, this));
Expand Down
8 changes: 4 additions & 4 deletions src/tests/compiler/ast/module/sourceFileTests.ts
Expand Up @@ -1396,19 +1396,19 @@ function myFunction(param: MyClass) {
});
});

describe(nameof<SourceFile>(s => s.getReferencedFiles), () => {
describe(nameof<SourceFile>(s => s.getPathReferenceDirectives), () => {
it("should get when they exist", () => {
const { sourceFile } = getInfoFromText("/// <reference path='file.d.ts' />");
const referencedFiles = sourceFile.getReferencedFiles();
const referencedFiles = sourceFile.getPathReferenceDirectives();
expect(referencedFiles.map(f => f.getFileName())).to.deep.equal(["file.d.ts"]);
});

it("should forget them after a manipulation", () => {
const { sourceFile } = getInfoFromText("/// <reference path='file.d.ts' />");
const referencedFiles = sourceFile.getReferencedFiles();
const referencedFiles = sourceFile.getPathReferenceDirectives();
sourceFile.replaceWithText("");
expect(referencedFiles[0].wasForgotten()).to.be.true;
expect(sourceFile.getReferencedFiles().length).to.equal(0);
expect(sourceFile.getPathReferenceDirectives().length).to.equal(0);
});
});

Expand Down

0 comments on commit 578adc7

Please sign in to comment.