Skip to content

Commit

Permalink
feat: #191 - Add SourceFile.getEmitOutput().
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Dec 23, 2017
1 parent 59a254e commit 1707a7d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
12 changes: 12 additions & 0 deletions docs/emitting.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ const sourceFile = ast.getSourceFileOrThrow("MyFile.ts");
sourceFile.emit();
```

Or get its emit output:

```typescript
const emitOutput = sourceFile.getEmitOutput();
emitOutput.getEmitSkipped(); // returns: boolean
for (const outputFile of emitOutput.getOutputFiles()) {
outputFile.getFilePath();
outputFile.getWriteByteOrderMark();
outputFile.getText();
}
```

### Emitting only declaration files (.d.ts)

Specify the `emitOnlyDtsFiles` flag:
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/file/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {callBaseFill} from "./../callBaseFill";
import {TextInsertableNode} from "./../base";
import {Node, Symbol} from "./../common";
import {StatementedNode} from "./../statement";
import {Diagnostic, EmitResult, FormatCodeSettings} from "./../tools";
import {Diagnostic, EmitResult, EmitOutput, FormatCodeSettings} from "./../tools";
import {ImportDeclaration} from "./ImportDeclaration";
import {ExportDeclaration} from "./ExportDeclaration";
import {ExportAssignment} from "./ExportAssignment";
Expand Down Expand Up @@ -554,6 +554,14 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
return this.global.program.emit({ targetSourceFile: this, ...options });
}

/**
* Gets the emit output of this source file.
* @param options - Emit options.
*/
getEmitOutput(options: { emitOnlyDtsFiles?: boolean; } = {}): EmitOutput {
return this.global.languageService.getEmitOutput(this, options.emitOnlyDtsFiles || false);
}

/**
* Formats the source file text using the internal TypeScript formatting API.
*/
Expand Down
3 changes: 1 addition & 2 deletions src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ export class Directory {

private _emitInternal(options: { emitOnlyDtsFiles?: boolean; outDir?: string; declarationDir?: string; } = {})
{
const {languageService} = this.global;
const {emitOnlyDtsFiles = false} = options;
const isJsFile = options.outDir == null ? undefined : /\.js$/i;
const isMapFile = options.outDir == null ? undefined : /\.js\.map$/i;
Expand All @@ -318,7 +317,7 @@ export class Directory {

function *emitDirectory(directory: Directory, outDir?: string, declarationDir?: string): IterableIterator<false | { filePath: string; fileText: string; }> {
for (const sourceFile of directory.getSourceFiles()) {
const output = languageService.getEmitOutput(sourceFile, emitOnlyDtsFiles);
const output = sourceFile.getEmitOutput({ emitOnlyDtsFiles });
if (output.getEmitSkipped()) {
yield false;
return;
Expand Down
25 changes: 25 additions & 0 deletions src/tests/compiler/file/sourceFileTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,31 @@ describe(nameof(SourceFile), () => {
});
});

describe(nameof<SourceFile>(n => n.getEmitOutput), () => {
it("should get the emit output for the source file", () => {
const ast = new TsSimpleAst({ compilerOptions: { noLib: true, outDir: "dist", target: ts.ScriptTarget.ES5 } });
const sourceFile = ast.createSourceFile("file1.ts", "const num1 = 1;");
const result = sourceFile.getEmitOutput();

expect(result.getEmitSkipped()).to.be.false;
expect(result.getOutputFiles().length).to.equal(1);
const outputFile = result.getOutputFiles()[0];
expect(outputFile.getText()).to.equal("var num1 = 1;\n");
expect(outputFile.getFilePath()).to.equal("dist/file1.js");
});

it("should only emit the declaration file when specified", () => {
const ast = new TsSimpleAst({ compilerOptions: { noLib: true, declaration: true, outDir: "dist", target: ts.ScriptTarget.ES5 } });
const sourceFile = ast.createSourceFile("file1.ts", "const num1 = 1;");
const result = sourceFile.getEmitOutput({ emitOnlyDtsFiles: true });

expect(result.getEmitSkipped()).to.be.false;
expect(result.getOutputFiles().length).to.equal(1);
const outputFile = result.getOutputFiles()[0];
expect(outputFile.getFilePath()).to.equal("dist/file1.d.ts");
});
});

describe(nameof<SourceFile>(n => n.fill), () => {
function doTest(startingCode: string, structure: SourceFileSpecificStructure, expectedCode: string) {
const {sourceFile} = getInfoFromText(startingCode);
Expand Down

0 comments on commit 1707a7d

Please sign in to comment.