-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from fabiandev/ts-emit
Transformations with unofficial TypeScript API
- Loading branch information
Showing
47 changed files
with
447 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { TsRuntimeOptions } from '../options/TsRuntimeOptions'; | ||
import { FileResult } from './FileResult'; | ||
import Options from '../options/Options'; | ||
import FileResult from './FileResult'; | ||
|
||
export interface CompilerConfig { | ||
files: string[]; | ||
options: TsRuntimeOptions; | ||
options: Options; | ||
} | ||
|
||
export default CompilerConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { FileResult } from './FileResult'; | ||
import { CompilerConfig } from './CompilerConfig'; | ||
import FileResult from './FileResult'; | ||
import CompilerConfig from './CompilerConfig'; | ||
|
||
export interface CompilerResult { | ||
config: CompilerConfig; | ||
fileResults: FileResult[]; | ||
} | ||
|
||
export default CompilerResult; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { TranspilerOutput } from 'tspoon'; | ||
|
||
export interface FileResult { | ||
transpiler: TranspilerOutput; | ||
file: string; | ||
result: string; | ||
filePath: string; | ||
fileName: string | ||
} | ||
|
||
export default FileResult; |
File renamed without changes.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import * as astUtil from './utils/ast'; | ||
export const ast = astUtil; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/compiler/_archive/visitors/VariableStatementVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as ts from 'typescript'; | ||
import { Visitor, VisitorContext } from 'tspoon'; | ||
import { VariableDeclarationVisitor } from './VariableDeclarationVisitor'; | ||
import * as generate from '../generators'; | ||
import * as utils from '../utils'; | ||
|
||
export class VariableStatementVisitor implements Visitor { | ||
|
||
public filter(node: ts.Node): boolean { | ||
return node.kind === ts.SyntaxKind.VariableDeclaration; | ||
} | ||
|
||
public visit(node: ts.VariableDeclaration, context: VisitorContext, traverse: (...visitors: Visitor[]) => void): void { | ||
traverse(new VariableDeclarationVisitor()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { VariableDeclarationVisitor } from './VariableDeclarationVisitor'; | ||
// export { VariableStatementVisitor } from './VariableStatementVisitor'; |
32 changes: 32 additions & 0 deletions
32
src/compiler/_archive/visitors_new/VariableDeclarationVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as ts from 'typescript/built/local/typescript'; | ||
import { Visitor } from './Visitor'; | ||
import * as generator from '../generator'; | ||
|
||
export class VariableDeclarationVisitor extends Visitor { | ||
|
||
public filter(node: ts.Node): boolean { | ||
return node.kind === ts.SyntaxKind.VariableDeclaration; | ||
} | ||
|
||
public visit(node: ts.VariableDeclaration): ts.Node { | ||
if (!node.type) { | ||
return node; | ||
} | ||
|
||
const nodeName = node.name.getText(); | ||
const def = generator.typeDefinition(node.type, nodeName); | ||
|
||
const val = ts.factory.createCall( | ||
ts.factory.createPropertyAccess(ts.factory.createIdentifier(`_${nodeName}Type`), 'assert'), | ||
[], | ||
[node.initializer], | ||
); | ||
|
||
const check = ts.factory.updateVariableDeclaration(node, node.name, node.type, val); | ||
|
||
const list = ts.factory.createVariableDeclarationList([def, check]); | ||
|
||
return list; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as ts from 'typescript/built/local/typescript'; | ||
|
||
export abstract class Visitor { | ||
|
||
protected abstract filter(node: ts.Node): boolean; | ||
|
||
public abstract visit(node: ts.Node): ts.Node; | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { Transformer } from './transformers/Transformer'; | ||
export { VariableDeclarationTransformer } from './transformers/VariableDeclarationTransformer'; | ||
import * as DEFAULT_TRANSFORMERS from './transformers/default_transformers'; | ||
export { DEFAULT_TRANSFORMERS }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as ts from 'typescript/built/local/typescript'; | ||
|
||
export abstract class Transformer { | ||
|
||
protected visited: ts.Node[] = []; | ||
|
||
protected abstract substitution: ts.SyntaxKind | ts.SyntaxKind[]; | ||
|
||
public abstract onSubstituteNode(context: ts.EmitContext, node: ts.Node): ts.Node; | ||
|
||
public getSubstitutions(): ts.SyntaxKind[] { | ||
return !Array.isArray(this.substitution) ? [this.substitution] : this.substitution; | ||
} | ||
|
||
public getVisited() { | ||
return this.visited; | ||
} | ||
|
||
public process(context: ts.EmitContext, node: ts.Node): ts.Node { | ||
if (this.visited.indexOf(node) !== -1) { | ||
return node; | ||
} | ||
|
||
this.visited.push(node); | ||
|
||
// if (this.getSubstitutions().indexOf(node.kind) === -1) { | ||
// return node; | ||
// } | ||
|
||
return this.onSubstituteNode(context, node); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/compiler/transformers/VariableDeclarationTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as ts from 'typescript/built/local/typescript'; | ||
import { Transformer } from './Transformer'; | ||
import { generator } from '../utils'; | ||
|
||
export class VariableDeclarationTransformer extends Transformer { | ||
|
||
protected substitution = ts.SyntaxKind.VariableDeclarationList; | ||
|
||
public onSubstituteNode(context: ts.EmitContext, node: ts.VariableDeclarationList): ts.Node { | ||
const declarations: ts.VariableDeclaration[] = []; | ||
|
||
for (const declaration of node.declarations) { | ||
declarations.push(...this.processDeclaration(declaration)); | ||
} | ||
|
||
return ts.factory.updateVariableDeclarationList(node, declarations); | ||
} | ||
|
||
private processDeclaration(node: ts.VariableDeclaration): ts.VariableDeclaration[] { | ||
if (!node.type) { | ||
return [node]; | ||
} | ||
|
||
if (node.parent.flags === ts.NodeFlags.Const) { | ||
return this.processConstDeclaration(node); | ||
} | ||
|
||
return this.processLetDeclaration(node); | ||
} | ||
|
||
private processLetDeclaration(node: ts.VariableDeclaration): ts.VariableDeclaration[] { | ||
const nodeName = node.name.getText(); | ||
const typeDefinition = generator.createTypeDefinition(node.type, `_${nodeName}Type`); | ||
|
||
if (!node.initializer) { | ||
return [typeDefinition, node]; | ||
} | ||
|
||
const initializer = generator.createTypeCall(`_${nodeName}Type`, 'assert', [node.initializer]); | ||
const assignment = ts.factory.updateVariableDeclaration(node, node.name, node.type, initializer); | ||
|
||
return [typeDefinition, assignment]; | ||
} | ||
|
||
private processConstDeclaration(node: ts.VariableDeclaration): ts.VariableDeclaration[] { | ||
const nodeName = node.name.getText(); | ||
const typeCalls = generator.createTypeCalls(node.type); | ||
|
||
const initializer = ts.factory.createCall( | ||
ts.factory.createPropertyAccess(typeCalls, 'assert'), [], [node.initializer], | ||
); | ||
|
||
const assignment = ts.factory.updateVariableDeclaration(node, node.name, node.type, initializer); | ||
|
||
return [assignment]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { VariableDeclarationTransformer } from './VariableDeclarationTransformer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import * as astUtil from './utils/ast'; | ||
export const ast = astUtil; | ||
import * as generator from './utils/generator'; | ||
export { generator }; |
Oops, something went wrong.