Skip to content

Commit

Permalink
feat: Add SourceFile.isInNodeModules()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Nov 12, 2018
1 parent 43c6149 commit 0a1817c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
16 changes: 14 additions & 2 deletions docs/details/source-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,26 @@ sourceFile.getFilePath();
sourceFile.getBaseName();
```

### Check if declaration file
### Other Information

Use:
Check if it's a declaration file:

```ts
sourceFile.isDeclarationFile(); // returns: boolean
```

Check if it was discovered from an external module:

```ts
sourceFile.isFromExternalLibrary();
```

Check if it's a descendant of a `node_modules` directory:

```ts
sourceFile.isInNodeModules();
```

### Save

Save a source file to the file system using one of the following commands:
Expand Down
8 changes: 6 additions & 2 deletions lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7464,9 +7464,13 @@ export declare class SourceFile extends SourceFileBase<ts.SourceFile> {
*/
isDeclarationFile(): boolean;
/**
* Gets if the source file is from an external library.
* Gets if the source file was discovered while loading an external library.
*/
isFromExternalLibrary(): boolean;
/**
* Gets if the source file is a descendant of a node_modules directory.
*/
isInNodeModules(): boolean;
/**
* Gets if this source file has been saved or if the latest changes have been saved.
*/
Expand Down Expand Up @@ -9005,7 +9009,7 @@ export declare class Program {
*/
getEmitModuleResolutionKind(): ModuleResolutionKind;
/**
* Gets if the provided source file is from an external library.
* Gets if the provided source file was discovered while loading an external library.
* @param sourceFile - Source file.
*/
isSourceFileFromExternalLibrary(sourceFile: SourceFile): boolean;
Expand Down
9 changes: 8 additions & 1 deletion src/compiler/ast/module/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
}

/**
* Gets if the source file is from an external library.
* Gets if the source file was discovered while loading an external library.
*/
@Memoize
isFromExternalLibrary() {
Expand All @@ -516,6 +516,13 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
return compilerProgram.isSourceFileFromExternalLibrary(this.compilerNode);
}

/**
* Gets if the source file is a descendant of a node_modules directory.
*/
isInNodeModules() {
return this.getFilePath().indexOf("/node_modules/") >= 0;
}

/**
* Gets if this source file has been saved or if the latest changes have been saved.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/tools/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class Program {
}

/**
* Gets if the provided source file is from an external library.
* Gets if the provided source file was discovered while loading an external library.
* @param sourceFile - Source file.
*/
isSourceFileFromExternalLibrary(sourceFile: SourceFile) {
Expand Down
12 changes: 12 additions & 0 deletions src/tests/compiler/module/sourceFileTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,18 @@ describe(nameof(SourceFile), () => {
}
});

describe(nameof<SourceFile>(n => n.isInNodeModules), () => {
it("should not be when not", () => {
const { sourceFile } = getInfoFromText("", { filePath: "/main.ts" });
expect(sourceFile.isInNodeModules()).to.be.false;
});

it("should be when is", () => {
const { sourceFile } = getInfoFromText("", { filePath: "/node_modules/library/main.d.ts" });
expect(sourceFile.isInNodeModules()).to.be.true;
});
});

describe(nameof<SourceFile>(n => n.getLanguageVariant), () => {
it("should return standard when in a ts file", () => {
const { sourceFile } = getInfoFromText("");
Expand Down

0 comments on commit 0a1817c

Please sign in to comment.