Skip to content

Commit

Permalink
feat: #454 - Wrap NamespaceImport.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 6, 2018
1 parent fc806da commit 001e7d0
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 173 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/dist-scripts
/dist-declarations
/.nyc_output
/npm-debug.log
/npm-debug.log
/yarn-error.log
27 changes: 25 additions & 2 deletions lib/ts-simple-ast.d.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/compiler/ast/CompilerNodeToWrappedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export type CompilerNodeToWrappedType<T extends ts.Node> = T extends ts.ArrayBin
T extends ts.ImportEqualsDeclaration ? compiler.ImportEqualsDeclaration :
T extends ts.ImportSpecifier ? compiler.ImportSpecifier :
T extends ts.NamespaceDeclaration ? compiler.NamespaceDeclaration :
T extends ts.NamespaceImport ? compiler.NamespaceImport :
T extends ts.SourceFile ? compiler.SourceFile :
T extends ts.Block ? compiler.Block :
T extends ts.BreakStatement ? compiler.BreakStatement :
Expand Down
1 change: 1 addition & 0 deletions src/compiler/ast/kindToNodeMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface KindToNodeMappings {
[SyntaxKind.MethodDeclaration]: compiler.MethodDeclaration;
[SyntaxKind.MethodSignature]: compiler.MethodSignature;
[SyntaxKind.ModuleDeclaration]: compiler.NamespaceDeclaration;
[SyntaxKind.NamespaceImport]: compiler.NamespaceImport;
[SyntaxKind.NewExpression]: compiler.NewExpression;
[SyntaxKind.NonNullExpression]: compiler.NonNullExpression;
[SyntaxKind.NotEmittedStatement]: compiler.NotEmittedStatement;
Expand Down
8 changes: 1 addition & 7 deletions src/compiler/ast/module/ExportSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ export class ExportSpecifier extends ExportSpecifierBase<ts.ExportSpecifier> {
if (nameNode.getText() === name)
return this;

const start = nameNode.getStart();
replaceNodeText({
sourceFile: this.sourceFile,
start,
replacingLength: nameNode.getWidth(),
newText: name
});
nameNode.replaceWithText(name);

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ast/module/ImportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class ImportDeclaration extends ImportDeclarationBase<ts.ImportDeclaratio
}

/**
* Gets the namespace import, if it exists.
* Gets the namespace import identifier, if it exists.
*/
getNamespaceImport() {
const importClause = this.getImportClause();
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ast/module/ImportSpecifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { insertIntoParentTextRange, removeCommaSeparatedChild, replaceNodeText, removeChildren } from "../../../manipulation";
import { insertIntoParentTextRange, removeCommaSeparatedChild, removeChildren } from "../../../manipulation";
import { SyntaxKind, ts } from "../../../typescript";
import { StringUtils } from "../../../utils";
import { Node } from "../common";
Expand Down
32 changes: 32 additions & 0 deletions src/compiler/ast/module/NamespaceImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ts } from "../../../typescript";
import { RenameableNode } from "../base";
import { Node } from "../common";

export const NamespaceImportBase = RenameableNode(Node);
export class NamespaceImport extends NamespaceImportBase<ts.NamespaceImport> {
/**
* Sets the name of the namespace import.
*/
setName(name: string) {
const nameNode = this.getNameNode();
if (nameNode.getText() === name)
return this;

nameNode.replaceWithText(name);
return this;
}

/**
* Gets the name of the namespace import.
*/
getName() {
return this.getNameNode().getText();
}

/**
* Gets the namespace import's name node.
*/
getNameNode() {
return this.getNodeFromCompilerNode(this.compilerNode.name);
}
}
1 change: 1 addition & 0 deletions src/compiler/ast/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from "./ImportSpecifier";
export * from "./NamespaceChildableNode";
export * from "./NamespaceDeclaration";
export * from "./NamespaceDeclarationKind";
export * from "./NamespaceImport";
export * from "./SourceFile";
1 change: 1 addition & 0 deletions src/factories/kindToWrapperMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const kindToWrapperMappings: { [key: number]: any } = {
[SyntaxKind.MethodDeclaration]: compiler.MethodDeclaration,
[SyntaxKind.MethodSignature]: compiler.MethodSignature,
[SyntaxKind.ModuleDeclaration]: compiler.NamespaceDeclaration,
[SyntaxKind.NamespaceImport]: compiler.NamespaceImport,
[SyntaxKind.NewExpression]: compiler.NewExpression,
[SyntaxKind.NonNullExpression]: compiler.NonNullExpression,
[SyntaxKind.NotEmittedStatement]: compiler.NotEmittedStatement,
Expand Down
52 changes: 52 additions & 0 deletions src/tests/compiler/module/namespaceImportTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from "chai";
import { NamespaceImport } from "../../../compiler";
import { SyntaxKind } from "../../../typescript";
import { getInfoFromText, getInfoFromTextWithDescendant } from "../testHelpers";

describe(nameof(NamespaceImport), () => {
function getNamespaceImport(text: string) {
return getInfoFromTextWithDescendant<NamespaceImport>(text, SyntaxKind.NamespaceImport);
}

describe(nameof<NamespaceImport>(n => n.setName), () => {
it("should only change what's imported", () => {
const { descendant, sourceFile, project } = getNamespaceImport("import * as ts from './file'; const t = ts;");
const otherSourceFile = project.createSourceFile("file.ts", "export class name {}\nexport class newName {}");
descendant.setName("tsNew");
expect(sourceFile.getText()).to.equal("import * as tsNew from './file'; const t = ts;");
expect(otherSourceFile.getText()).to.equal("export class name {}\nexport class newName {}");
});
});

describe(nameof<NamespaceImport>(n => n.rename), () => {
it("should rename what's imported", () => {
const { descendant, sourceFile, project } = getNamespaceImport("import * as ts from './file'; const t = ts;");
const otherSourceFile = project.createSourceFile("file.ts", "export class name {}\nexport class newName {}");
descendant.rename("tsNew");
expect(sourceFile.getText()).to.equal("import * as tsNew from './file'; const t = tsNew;");
expect(otherSourceFile.getText()).to.equal("export class name {}\nexport class newName {}");
});
});

describe(nameof<NamespaceImport>(n => n.getNameNode), () => {
function doTest(text: string, name: string) {
const { descendant } = getNamespaceImport(text);
expect(descendant.getNameNode().getText()).to.equal(name);
}

it("should get the name", () => {
doTest(`import * as name from "./test";`, "name");
});
});

describe(nameof<NamespaceImport>(n => n.getName), () => {
function doTest(text: string, name: string) {
const { descendant } = getNamespaceImport(text);
expect(descendant.getName()).to.equal(name);
}

it("should get the name", () => {
doTest(`import * as name from "./test";`, "name");
});
});
});
8 changes: 8 additions & 0 deletions src/utils/TypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,14 @@ export class TypeGuards {
return node.getKind() === SyntaxKind.ModuleDeclaration;
}

/**
* Gets if the node is a NamespaceImport.
* @param node - Node to check.
*/
static isNamespaceImport(node: compiler.Node): node is compiler.NamespaceImport {
return node.getKind() === SyntaxKind.NamespaceImport;
}

/**
* Gets if the node is a NeverKeyword.
* @param node - Node to check.
Expand Down

0 comments on commit 001e7d0

Please sign in to comment.