Skip to content

Commit

Permalink
feat: ImportDeclaration and ExportDeclaration - isModuleSpecifierRela…
Browse files Browse the repository at this point in the history
…tive()
  • Loading branch information
dsherret committed Feb 19, 2018
1 parent f22db6c commit 2ef3064
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/details/exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ exportDeclaration.getModuleSpecifier(); // returns: string | undefined
exportDeclaration.setModuleSpecifier("./new-file");
exportDeclaration.setModuleSpecifier(someSourceFile);
exportDeclaration.hasModuleSpecifier(); // returns: boolean
exportDeclaration.isModuleSpecifierRelative(); // if the module specifier starts with ./, ../, or /
exportDeclaration.getModuleSpecifierSourceFile(); // returns: SourceFile | undefined
```

Expand Down
8 changes: 8 additions & 0 deletions docs/details/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ Get the referenced source file:
const sourceFile = importDeclaration.getModuleSpecifierSourceFile(); // returns: SourceFile | undefined
```

Get if the module specifier is for a relative or non-relative import:

```ts
importDeclaration.isModuleSpecifierRelative();
```

Note: Relative imports are imports that have module specifiers that start with `./`, `../`, or `/`.

### Default import

Get it:
Expand Down
14 changes: 13 additions & 1 deletion src/compiler/file/ExportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ts, SyntaxKind} from "./../../typescript";
import * as errors from "./../../errors";
import {ExportSpecifierStructure} from "./../../structures";
import {insertIntoParentTextRange, verifyAndGetIndex, insertIntoCommaSeparatedNodes} from "./../../manipulation";
import {ArrayUtils, TypeGuards} from "./../../utils";
import {ArrayUtils, TypeGuards, StringUtils} from "./../../utils";
import {Identifier} from "./../common";
import {Statement} from "./../statement";
import {ExportSpecifier} from "./ExportSpecifier";
Expand Down Expand Up @@ -80,6 +80,18 @@ export class ExportDeclaration extends Statement<ts.ExportDeclaration> {
return declarations[0] as SourceFile;
}

/**
* Gets if the module specifier starts with `/`, `./`, or `../`.
*/
isModuleSpecifierRelative() {
const moduleSpecifier = this.getModuleSpecifier();
if (moduleSpecifier == null)
return false;
return StringUtils.startsWith(moduleSpecifier, "/") ||
StringUtils.startsWith(moduleSpecifier, "./") ||
StringUtils.startsWith(moduleSpecifier, "../");
}

/**
* Gets if the module specifier exists
*/
Expand Down
12 changes: 11 additions & 1 deletion src/compiler/file/ImportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ts, SyntaxKind} from "./../../typescript";
import * as errors from "./../../errors";
import {ImportSpecifierStructure} from "./../../structures";
import {insertIntoParentTextRange, verifyAndGetIndex, insertIntoCommaSeparatedNodes, removeChildren} from "./../../manipulation";
import {ArrayUtils, TypeGuards} from "./../../utils";
import {ArrayUtils, TypeGuards, StringUtils} from "./../../utils";
import {Identifier} from "./../common";
import {Statement} from "./../statement";
import {ImportSpecifier} from "./ImportSpecifier";
Expand Down Expand Up @@ -63,6 +63,16 @@ export class ImportDeclaration extends Statement<ts.ImportDeclaration> {
return declarations[0] as SourceFile;
}

/**
* Gets if the module specifier starts with `/`, `./`, or `../`.
*/
isModuleSpecifierRelative() {
const moduleSpecifier = this.getModuleSpecifier();
return StringUtils.startsWith(moduleSpecifier, "/") ||
StringUtils.startsWith(moduleSpecifier, "./") ||
StringUtils.startsWith(moduleSpecifier, "../");
}

/**
* Sets the default import.
* @param text - Text to set as the default import.
Expand Down
27 changes: 27 additions & 0 deletions src/tests/compiler/file/exportDeclarationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,33 @@ describe(nameof(ExportDeclaration), () => {
});
});

describe(nameof<ExportDeclaration>(n => n.isModuleSpecifierRelative), () => {
function doTest(text: string, expected: boolean) {
const {firstChild} = getInfoFromText<ExportDeclaration>(text);
expect(firstChild.isModuleSpecifierRelative()).to.equal(expected);
}

it("should be when using ./", () => {
doTest("export * from './test'", true);
});

it("should be when using ../", () => {
doTest("export * from '../test'", true);
});

it("should be when using /", () => {
doTest("export * from '/test'", true);
});

it("should not be when not", () => {
doTest("export * from 'test'", false);
});

it("should not be when not existing", () => {
doTest("export {test}", false);
});
});

describe(nameof<ExportDeclaration>(n => n.getNamedExports), () => {
function doTest(text: string, expected: { name: string; alias?: string; }[]) {
const {firstChild} = getInfoFromText<ExportDeclaration>(text);
Expand Down
23 changes: 23 additions & 0 deletions src/tests/compiler/file/importDeclarationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ describe(nameof(ImportDeclaration), () => {
});
});

describe(nameof<ImportDeclaration>(n => n.isModuleSpecifierRelative), () => {
function doTest(text: string, expected: boolean) {
const {firstChild} = getInfoFromText<ImportDeclaration>(text);
expect(firstChild.isModuleSpecifierRelative()).to.equal(expected);
}

it("should be when using ./", () => {
doTest("import * as test from './test'", true);
});

it("should be when using ../", () => {
doTest("import * as test from '../test'", true);
});

it("should be when using /", () => {
doTest("import * as test from '/test'", true);
});

it("should not be when not", () => {
doTest("import * as test from 'test'", false);
});
});

describe(nameof<ImportDeclaration>(n => n.setDefaultImport), () => {
function doTest(text: string, newDefaultImport: string, expected: string) {
const {firstChild, sourceFile} = getInfoFromText<ImportDeclaration>(text);
Expand Down

0 comments on commit 2ef3064

Please sign in to comment.