Skip to content

Commit

Permalink
feat: Add ImportEqualsDeclaration.setExternalModuleReference(...).
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Mar 18, 2018
1 parent c6d7c7b commit e3396d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/compiler/file/ImportEqualsDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ export class ImportEqualsDeclaration extends ImportEqualsDeclarationBase<ts.Impo
return this.getNodeFromCompilerNode<ModuleReference>(this.compilerNode.moduleReference);
}

/**
* Sets the external module reference.
* @param externalModuleReference - External module reference as a string.
*/
setExternalModuleReference(externalModuleReference: string): this;
/**
* Sets the external module reference.
* @param sourceFile - Source file to set the external module reference to.
*/
setExternalModuleReference(sourceFile: SourceFile): this;
setExternalModuleReference(textOrSourceFile: string | SourceFile) {
const text = typeof textOrSourceFile === "string" ? textOrSourceFile : this.sourceFile.getRelativePathToSourceFileAsModuleSpecifier(textOrSourceFile);
const moduleReference = this.getModuleReference();
if (TypeGuards.isExternalModuleReference(moduleReference) && moduleReference.getExpression() != null)
moduleReference.getExpressionOrThrow().replaceWithText(writer => writer.quote(text));
else
moduleReference.replaceWithText(writer => writer.write("require(").quote(text).write(")"));
return this;
}

/**
* Gets the source file referenced in the external module reference or throws if it doesn't exist.
*/
Expand Down
26 changes: 26 additions & 0 deletions src/tests/compiler/file/importEqualsDeclarationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ describe(nameof(ImportEqualsDeclaration), () => {
});
});

describe(nameof<ImportEqualsDeclaration>(n => n.setExternalModuleReference), () => {
function doTest(text: string, externalModuleReference: string, expected: string) {
const {firstChild} = getInfoFromText<ImportEqualsDeclaration>(text);
firstChild.setExternalModuleReference(externalModuleReference);
expect(firstChild.getText()).to.equal(expected);
}

it("should set the external module reference when currently a namespace", () => {
doTest("import test = Namespace.Test;", "./test", `import test = require("./test");`);
});

it("should set the external module reference when currently a require with no text", () => {
doTest("import test = require();", "./test", `import test = require("./test");`);
});

it("should set the external module reference when currently a require with other text", () => {
doTest("import test = require('./test2');", "./test", `import test = require("./test");`);
});

it("should set the external module reference when specifying a source file", () => {
const {firstChild, sourceFile} = getInfoFromText<ImportEqualsDeclaration>("import test = require('./test2');");
firstChild.setExternalModuleReference(sourceFile.getDirectory().createSourceFile("test3.ts"));
expect(firstChild.getText()).to.equal(`import test = require("./test3");`);
});
});

describe(nameof<ImportEqualsDeclaration>(d => d.remove), () => {
function doTest(text: string, index: number, expectedText: string) {
const {sourceFile} = getInfoFromText(text);
Expand Down

0 comments on commit e3396d3

Please sign in to comment.