Skip to content

Commit

Permalink
refactor: getImport -> getImportDeclaration.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: sourceFile.getImport is now getImportDeclaration for consistency.
  • Loading branch information
dsherret committed Feb 14, 2018
1 parent db9f0fc commit acd9d70
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/details/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Imports of a source file can be retrieved by calling:
// get them all
const imports = sourceFile.getImportDeclarations();
// or get the first one that matches a condition
const importWithDefaultImport = sourceFile.getImport(i => i.getDefaultImport() != null);
const importWithDefaultImport = sourceFile.getImportDeclaration(i => i.getDefaultImport() != null);
```

### Add/Insert
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/file/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,16 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
* Gets the first import declaration that matches a condition, or undefined if it doesn't exist.
* @param condition - Condition to get the import by.
*/
getImport(condition: (importDeclaration: ImportDeclaration) => boolean): ImportDeclaration | undefined {
getImportDeclaration(condition: (importDeclaration: ImportDeclaration) => boolean): ImportDeclaration | undefined {
return ArrayUtils.find(this.getImportDeclarations(), condition);
}

/**
* Gets the first import declaration that matches a condition, or throws if it doesn't exist.
* @param condition - Condition to get the import by.
*/
getImportOrThrow(condition: (importDeclaration: ImportDeclaration) => boolean): ImportDeclaration {
return errors.throwIfNullOrUndefined(this.getImport(condition), "Expected to find an import with the provided condition.");
getImportDeclarationOrThrow(condition: (importDeclaration: ImportDeclaration) => boolean): ImportDeclaration {
return errors.throwIfNullOrUndefined(this.getImportDeclaration(condition), "Expected to find an import with the provided condition.");
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/tests/compiler/file/sourceFileTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,27 @@ describe(nameof(SourceFile), () => {
});
});

describe(nameof<SourceFile>(n => n.getImport), () => {
describe(nameof<SourceFile>(n => n.getImportDeclaration), () => {
it("should get the import declaration", () => {
const {sourceFile} = getInfoFromText("import myImport from 'test'; import {next} from './test';");
expect(sourceFile.getImport(i => i.getDefaultImport() != null)!.getText()).to.equal("import myImport from 'test';");
expect(sourceFile.getImportDeclaration(i => i.getDefaultImport() != null)!.getText()).to.equal("import myImport from 'test';");
});

it("should return undefined when not exists", () => {
const {sourceFile} = getInfoFromText("");
expect(sourceFile.getImportDeclaration(e => false)).to.be.undefined;
});
});

describe(nameof<SourceFile>(n => n.getImportOrThrow), () => {
describe(nameof<SourceFile>(n => n.getImportDeclarationOrThrow), () => {
it("should get the import declaration", () => {
const {sourceFile} = getInfoFromText("import myImport from 'test'; import {next} from './test';");
expect(sourceFile.getImportOrThrow(i => i.getDefaultImport() != null).getText()).to.equal("import myImport from 'test';");
expect(sourceFile.getImportDeclarationOrThrow(i => i.getDefaultImport() != null).getText()).to.equal("import myImport from 'test';");
});

it("should throw when not exists", () => {
const {sourceFile} = getInfoFromText("");
expect(() => sourceFile.getImportOrThrow(e => false)).to.throw();
expect(() => sourceFile.getImportDeclarationOrThrow(e => false)).to.throw();
});
});

Expand Down

0 comments on commit acd9d70

Please sign in to comment.