Skip to content

Commit f1c27f4

Browse files
committed
Improvements, bugfixes and restructuring.
1 parent 723e859 commit f1c27f4

27 files changed

+87
-88
lines changed

src/compiler/Compiler.ts

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import * as ts from 'typescript/built/local/typescript';
4-
import { TsRuntimeOptions } from '../options';
5-
import { CompilerResult } from './CompilerResult';
6-
import { CompilerConfig } from './CompilerConfig';
7-
import { FileResult } from './FileResult';
8-
import { Transformer } from './transformers';
9-
import { Visitor } from './visitors/Visitor';
10-
import * as TRANSFORMERS from './transformers/default_transformers';
11-
import * as VISITORS from './visitors/default_visitors';
4+
import CompilerResult from './CompilerResult';
5+
import CompilerConfig from './CompilerConfig';
6+
import FileResult from './FileResult';
7+
import { Transformer, DEFAULT_TRANSFORMERS } from './transformers';
128

139
export class Compiler {
1410

@@ -19,8 +15,8 @@ export class Compiler {
1915
public process(): Promise<CompilerResult> {
2016
const toTransform: Array<Promise<FileResult>> = [];
2117

22-
const transformers: ts.Transformer[] = Object.keys(TRANSFORMERS).map((key: string) => {
23-
const transformer = new (TRANSFORMERS as any)[key]();
18+
const transformers: ts.Transformer[] = Object.keys(DEFAULT_TRANSFORMERS).map((key: string) => {
19+
const transformer = new (DEFAULT_TRANSFORMERS as any)[key]();
2420

2521
const transform: ts.Transformer = (context) => (f) => {
2622
for (const substitution of transformer.getSubstitutions()) {
@@ -48,33 +44,6 @@ export class Compiler {
4844
});
4945
}
5046

51-
// public processVisitors(): Promise<CompilerResult> {
52-
// const toVisit: Array<Promise<FileResult>> = [];
53-
//
54-
// const visitors: Visitor[] = Object.keys(VISITORS).map((key: string) => {
55-
// return new (VISITORS as any)[key]();
56-
// });
57-
//
58-
// for (const file of this.config.files) {
59-
// toVisit.push(this.visitFile(file, visitors));
60-
// }
61-
//
62-
// return Promise.all(toVisit)
63-
// .then(results => {
64-
// return {
65-
// config: this.config,
66-
// fileResults: results,
67-
// };
68-
// })
69-
// .catch(err => {
70-
// console.error(err);
71-
// });
72-
// }
73-
//
74-
// private visitFile(filePath: string, visitors: Visitor[]): Promise<FileResult> {
75-
//
76-
// }
77-
7847
private transformFile(filePath: string, transformers: ts.Transformer[]): Promise<FileResult> {
7948
return new Promise((resolve, reject) => {
8049
fs.readFile(filePath, this.config.options.encoding, (err, source) => {
@@ -87,7 +56,7 @@ export class Compiler {
8756
const f = ts.createSourceFile(
8857
fileName,
8958
source,
90-
ts.ScriptTarget.Latest,
59+
this.config.options.compilerOptions.target || ts.ScriptTarget.Latest,
9160
true,
9261
ts.ScriptKind.TS,
9362
);
@@ -104,3 +73,5 @@ export class Compiler {
10473
}
10574

10675
}
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export interface FileResult {
33
filePath: string;
44
fileName: string
55
}
6+
7+
export default FileResult;
File renamed without changes.
File renamed without changes.

src/compiler/transformers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export * from './transformers/Transformer';
1+
export { Transformer } from './transformers/Transformer';
2+
export { VariableDeclarationTransformer } from './transformers/VariableDeclarationTransformer';
3+
import * as DEFAULT_TRANSFORMERS from './transformers/default_transformers';
4+
export { DEFAULT_TRANSFORMERS };

src/compiler/transformers/Transformer.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ export abstract class Transformer {
1212
return !Array.isArray(this.substitution) ? [this.substitution] : this.substitution;
1313
}
1414

15-
public process(context: ts.EmitContext, node: ts.Node): ts.Node {
16-
// if (this.visited.indexOf(node.parent) !== -1) {
17-
// return node;
18-
// }
19-
20-
// this.visited.push(node.parent);
15+
public getVisited() {
16+
return this.visited;
17+
}
2118

22-
if (this.getSubstitutions().indexOf(node.kind) === -1) {
19+
public process(context: ts.EmitContext, node: ts.Node): ts.Node {
20+
if (this.visited.indexOf(node) !== -1) {
2321
return node;
2422
}
2523

24+
this.visited.push(node);
25+
26+
// if (this.getSubstitutions().indexOf(node.kind) === -1) {
27+
// return node;
28+
// }
29+
2630
return this.onSubstituteNode(context, node);
2731
}
2832

src/compiler/transformers/VariableDeclarationTransformer.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import * as ts from 'typescript/built/local/typescript';
22
import { Transformer } from './Transformer';
3-
import * as generator from '../generator';
3+
import { generator } from '../utils';
44

55
export class VariableDeclarationTransformer extends Transformer {
66

77
protected substitution = ts.SyntaxKind.VariableDeclarationList;
88

9-
protected visited: ts.Node[] = [];
10-
119
public onSubstituteNode(context: ts.EmitContext, node: ts.VariableDeclarationList): ts.Node {
1210
const declarations: ts.VariableDeclaration[] = [];
1311

@@ -46,12 +44,10 @@ export class VariableDeclarationTransformer extends Transformer {
4644

4745
private processConstDeclaration(node: ts.VariableDeclaration): ts.VariableDeclaration[] {
4846
const nodeName = node.name.getText();
49-
const typeDefinition = generator.createTypeDefinition(node.type, nodeName);
47+
const typeCalls = generator.createTypeCalls(node.type);
5048

5149
const initializer = ts.factory.createCall(
52-
ts.factory.createPropertyAccess(ts.factory.createCall(
53-
ts.factory.createPropertyAccess(ts.factory.createIdentifier('t'), 'number'), [], [],
54-
), 'assert'), [], [node.initializer],
50+
ts.factory.createPropertyAccess(typeCalls, 'assert'), [], [node.initializer],
5551
);
5652

5753
const assignment = ts.factory.updateVariableDeclaration(node, node.name, node.type, initializer);

src/compiler/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import * as generator from './utils/generator';
2+
export { generator };

src/compiler/generator.ts renamed to src/compiler/utils/generator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ export function createTypeCalls(type: ts.TypeNode): ts.CallExpression {
3030
{
3131
const typeRef = type as ts.TypeReferenceNode;
3232
const typeName = typeRef.typeName.getText();
33-
let callExpression: ts.CallExpression;
34-
3533
const args: ts.CallExpression[] = [];
34+
let callExpression: ts.CallExpression;
3635

3736
if (typeRef.typeArguments) {
3837
for (const arg of typeRef.typeArguments) {

src/lib.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { TsRuntimeOptions } from './options/TsRuntimeOptions';
1+
export { Options } from './options/Options';
22
export { DEFAULT_OPTIONS } from './options/default_options';
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as ts from 'typescript';
22

3-
export interface TsRuntimeOptions {
3+
export interface Options {
44
compilerOptions?: ts.CompilerOptions;
55
encoding?: string;
66
basePath?: boolean|string;
77
writePath?: boolean|string;
88
}
99

10-
export default TsRuntimeOptions;
10+
export default Options;

src/options/default_options.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from 'typescript';
2-
import { TsRuntimeOptions } from './TsRuntimeOptions';
2+
import Options from './Options';
33

4-
export const DEFAULT_OPTIONS: TsRuntimeOptions = {
4+
export const DEFAULT_OPTIONS: Options = {
55
basePath: false,
66
compilerOptions: {
77
allowJs: true,
@@ -15,3 +15,5 @@ export const DEFAULT_OPTIONS: TsRuntimeOptions = {
1515
encoding: 'utf8',
1616
writePath: false,
1717
};
18+
19+
export default DEFAULT_OPTIONS;

src/playground/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import * as path from 'path';
2-
import { transform } from '../transform';
3-
import { Writer } from '../writer';
2+
import * as tsr from '../index';
3+
import { Writer, WriterConfig } from '../writer';
44

55
const file = path.join(__dirname, 'test.ts');
66

7-
const writerConfig = {
7+
const writerConfig: WriterConfig = {
88
basePath: __dirname,
99
writePath: __dirname,
1010
};
1111

12-
transform(file)
13-
.then(transformerResult => {
12+
tsr.transform(file).then(transformerResult => {
1413
const writer = new Writer(transformerResult);
1514
writer.writeAll(writerConfig);
1615
});

src/playground/test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const f = () => {
2-
let _xType = t.ref(Function), x = _xType.assert(() => {
3-
const a = t.number().assert(true);
2+
let _xType = t.ref(ComplexType), x = _xType.assert(() => {
3+
const a = t.boolean().assert(true);
44
return a;
55
});
66
const b = t.number().assert(10);
77
let _cType = t.string(), c;
88
let _dType = t.array(t.boolean()), d = _dType.assert([true, false]);
9+
const e = t.array(t.array(t.array(t.number()))).assert([false]);
910
c = 'hi';
11+
let _fType = t.tuple(t.ref(one), t.ref(two)), f;
1012
};

src/playground/test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const f = () => {
22

3-
let x: Function = () => {
3+
let x: ComplexType = () => {
44
const a: boolean = true;
55
return a;
66
};
@@ -9,5 +9,9 @@ const f = () => {
99
let c: string;
1010
let d: boolean[] = [true, false]
1111

12+
const e: Array<Array<Array<number>>> = [false];
13+
1214
c = 'hi';
15+
16+
let f: [one, two];
1317
};

src/transform.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as ts from 'typescript';
22
import { Compiler, CompilerResult } from './compiler';
3-
import { TsRuntimeOptions, DEFAULT_OPTIONS } from './options';
3+
import { Options, DEFAULT_OPTIONS } from './options';
44

5-
export function transform(files: string | string[], options: TsRuntimeOptions = {}): Promise<CompilerResult> {
5+
export function transform(files: string | string[], options: Options = {}): Promise<CompilerResult> {
66
console.log('--> Starting');
77

88
options = getOptions(options);
@@ -16,7 +16,7 @@ export function transform(files: string | string[], options: TsRuntimeOptions =
1616
});
1717
}
1818

19-
function getOptions(options: TsRuntimeOptions = {}): TsRuntimeOptions {
19+
function getOptions(options: Options = {}): Options {
2020
return Object.assign({}, DEFAULT_OPTIONS, options);
2121
}
2222

src/writer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
export * from './writer/Writer';
2-
export * from './writer/WriterResult';
1+
export { Writer } from './writer/Writer';
2+
export { WriterConfig } from './writer/WriterConfig';
3+
export { WriterResult } from './writer/WriterResult';
4+
export { DEFAULT_CONFIG } from './writer/default_config';

src/writer/Writer.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
33
import { CompilerResult, FileResult } from '../compiler';
4-
import { TsRuntimeOptions } from '../options/TsRuntimeOptions';
5-
import { WriterConfig } from './WriterConfig';
6-
import { WriterResult } from './WriterResult';
4+
import Options from '../options/Options';
5+
import WriterConfig from './WriterConfig';
6+
import WriterResult from './WriterResult';
7+
import DEFAULT_CONFIG from './default_config';
78

89
export class Writer {
910

@@ -23,7 +24,7 @@ export class Writer {
2324

2425
public writeFile(fileResult: FileResult, config?: WriterConfig): Promise<WriterResult> {
2526
return new Promise((resolve, reject) => {
26-
const options: WriterConfig | TsRuntimeOptions = config || this.transformerResult.config.options;
27+
const options: WriterConfig = config ? Object.assign({}, DEFAULT_CONFIG, config) : DEFAULT_CONFIG;
2728
const file = fileResult.filePath.replace(new RegExp(`^${options.basePath}`), '');
2829
let location = path.join(options.writePath as string, file);
2930

@@ -53,3 +54,5 @@ export class Writer {
5354
}
5455

5556
}
57+
58+
export default Writer;

src/writer/WriterConfig.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export interface WriterConfig {
22
basePath: string;
33
writePath: string;
4+
encoding?: string;
45
}
6+
7+
export default WriterConfig;

src/writer/WriterResult.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export interface WriterResult {
55
originalPath: string;
66
writePath: string;
77
}
8+
9+
export default WriterResult;

src/writer/default_config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import * as path from 'path';
2-
import { WriterConfig } from './WriterConfig';
1+
import WriterConfig from './WriterConfig';
32

4-
export const config: WriterConfig = {
5-
basePath: path.join(__dirname, 'src'),
6-
writePath: path.join(__dirname, 'dist'),
3+
export const DEFAULT_CONFIG: WriterConfig = {
4+
basePath: __dirname,
5+
writePath: __dirname,
6+
encoding: 'utf8',
77
};
8+
9+
export default DEFAULT_CONFIG;

0 commit comments

Comments
 (0)