Skip to content

Commit ae3d27f

Browse files
authored
Merge pull request #1 from fabiandev/ts-emit
Transformations with unofficial TypeScript API
2 parents 043cec9 + f1c27f4 commit ae3d27f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+447
-84
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"escodegen": "^1.8.1",
1818
"esprima": "^3.1.3",
1919
"flow-runtime": "^0.2.1",
20+
"node-require-fallback": "^0.1.2",
2021
"tcomb": "^3.2.16",
2122
"ts-type-info": "^6.2.2",
2223
"tspoon": "^1.0.254",
23-
"typescript": "^2.1.5"
24+
"typescript": "Microsoft/TypeScript#6c122bcf16c329a21da04d7b5256f20ecc0bbd1d"
2425
}
2526
}

src/compiler/Compiler.ts

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { TranspilerOutput, Visitor, transpile } from 'tspoon';
4-
import { TsRuntimeOptions } from '../options';
5-
import { CompilerResult } from './CompilerResult';
6-
import { CompilerConfig } from './CompilerConfig';
7-
import { FileResult } from './FileResult';
8-
import * as DEFAULT_VISITORS from './visitors/default_visitors';
3+
import * as ts from 'typescript/built/local/typescript';
4+
import CompilerResult from './CompilerResult';
5+
import CompilerConfig from './CompilerConfig';
6+
import FileResult from './FileResult';
7+
import { Transformer, DEFAULT_TRANSFORMERS } from './transformers';
98

109
export class Compiler {
1110

@@ -16,8 +15,21 @@ export class Compiler {
1615
public process(): Promise<CompilerResult> {
1716
const toTransform: Array<Promise<FileResult>> = [];
1817

18+
const transformers: ts.Transformer[] = Object.keys(DEFAULT_TRANSFORMERS).map((key: string) => {
19+
const transformer = new (DEFAULT_TRANSFORMERS as any)[key]();
20+
21+
const transform: ts.Transformer = (context) => (f) => {
22+
for (const substitution of transformer.getSubstitutions()) {
23+
context.enableSubstitution(substitution);
24+
}
25+
context.onSubstituteNode = transformer.process.bind(transformer);
26+
return f;
27+
};
28+
return transform;
29+
});
30+
1931
for (const file of this.config.files) {
20-
toTransform.push(this.transformFile(file));
32+
toTransform.push(this.transformFile(file, transformers));
2133
}
2234

2335
return Promise.all(toTransform)
@@ -26,56 +38,40 @@ export class Compiler {
2638
config: this.config,
2739
fileResults: results,
2840
};
41+
})
42+
.catch(err => {
43+
console.error(err);
2944
});
3045
}
3146

32-
private transformFile(file: string): Promise<FileResult> {
47+
private transformFile(filePath: string, transformers: ts.Transformer[]): Promise<FileResult> {
3348
return new Promise((resolve, reject) => {
34-
fs.readFile(file, this.config.options.encoding, (err, source) => {
49+
fs.readFile(filePath, this.config.options.encoding, (err, source) => {
3550
if (err) {
36-
return reject(`Error reading file ${file}`);
51+
return reject(`Error reading file ${filePath}`);
3752
}
3853

39-
const visitors = Object.keys(DEFAULT_VISITORS).map((key: string) => {
40-
return new (DEFAULT_VISITORS as any)[key]();
41-
});
54+
const fileName = path.basename(filePath);
4255

43-
const transpiler = transpile(source, {
44-
compilerOptions: this.config.options.compilerOptions,
45-
sourceFileName: path.basename(file),
46-
visitors,
47-
});
56+
const f = ts.createSourceFile(
57+
fileName,
58+
source,
59+
this.config.options.compilerOptions.target || ts.ScriptTarget.Latest,
60+
true,
61+
ts.ScriptKind.TS,
62+
);
4863

49-
this.reportFile(transpiler);
64+
const result = ts.emit(f, transformers).result;
5065

5166
resolve({
52-
transpiler,
53-
file,
67+
fileName,
68+
filePath,
69+
result,
5470
});
5571
});
5672
});
5773
}
5874

59-
private reportFile(transpiler: TranspilerOutput): boolean {
60-
if (transpiler.diags) {
61-
for (const d of transpiler.diags) {
62-
const position = d.file.getLineAndCharacterOfPosition(d.start);
63-
64-
const name = d.file.fileName;
65-
const line = position.line + 1;
66-
const character = position.character;
67-
const text = d.messageText;
68-
69-
console.error(`-> ${name}:${line}:${character}:${text}`);
70-
}
71-
}
72-
73-
if (transpiler.halted) {
74-
console.error('Transpiler halted. Exiting now.');
75-
process.exit(1);
76-
}
77-
78-
return !!transpiler.diags;
79-
}
80-
8175
}
76+
77+
export default Compiler;

src/compiler/CompilerConfig.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { TsRuntimeOptions } from '../options/TsRuntimeOptions';
2-
import { FileResult } from './FileResult';
1+
import Options from '../options/Options';
2+
import FileResult from './FileResult';
33

44
export interface CompilerConfig {
55
files: string[];
6-
options: TsRuntimeOptions;
6+
options: Options;
77
}
8+
9+
export default CompilerConfig;

src/compiler/CompilerResult.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { FileResult } from './FileResult';
2-
import { CompilerConfig } from './CompilerConfig';
1+
import FileResult from './FileResult';
2+
import CompilerConfig from './CompilerConfig';
33

44
export interface CompilerResult {
55
config: CompilerConfig;
66
fileResults: FileResult[];
77
}
8+
9+
export default CompilerResult;

src/compiler/FileResult.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { TranspilerOutput } from 'tspoon';
2-
31
export interface FileResult {
4-
transpiler: TranspilerOutput;
5-
file: string;
2+
result: string;
3+
filePath: string;
4+
fileName: string
65
}
6+
7+
export default FileResult;
File renamed without changes.

src/compiler/_archive/generators/class_definition.ts

Whitespace-only changes.

src/compiler/_archive/generators/interface_definition.ts

Whitespace-only changes.

src/compiler/generators/type_definition.ts renamed to src/compiler/_archive/generators/type_definition.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export function typeCalls(type: ts.TypeNode): null | any {
6262
return callExpression;
6363
}
6464
default:
65-
throw new Error('Node Type not supported.');
65+
{
66+
const callExpression = utils.ast.getCallExpression('any');
67+
return callExpression;
68+
}
69+
// throw new Error('Node Type not supported.');
6670
}
6771
}

src/compiler/generators/variable_assignment.ts renamed to src/compiler/_archive/generators/variable_assignment.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as utils from '../utils';
55
export function variableAssignment(name: string, assignment: ts.Expression) {
66
const text = assignment.getText();
77
const callExpression = utils.ast.getCallExpression('assert', `_${name}Type`);
8+
// const transpiled = ts.transpileModule(assignment.getText(), {});
9+
// console.log(transpiled);
810
const toAssert = esprima.parse(assignment.getText()).body.pop();
911

1012
callExpression.arguments.push((toAssert as any).expression);

0 commit comments

Comments
 (0)