typescript.api
Advanced tools
Comparing version 0.2.0 to 0.2.1
57
index.js
@@ -11,6 +11,5 @@ | ||
var _path = require("path"); | ||
exports.include_lib_declaration = false; | ||
exports.include_node_declaration = false; | ||
exports.allowRemote = false; | ||
exports.debug = false; | ||
exports.compiler; | ||
function check(units) { | ||
@@ -44,3 +43,3 @@ for(var n in units) { | ||
var compiler = new api.Compile.Compiler(logger); | ||
compiler.compile(include_declarations(sourceUnits), function (compiledUnits) { | ||
compiler.compile(sourceUnits, function (compiledUnits) { | ||
if (exports.check(compiledUnits)) { | ||
@@ -98,4 +97,6 @@ exports.run(compiledUnits, null, function (context) { | ||
} | ||
var compiler = new api.Compile.Compiler(logger); | ||
compiler.compile(include_declarations(sourceUnits), callback); | ||
if (!exports.compiler) { | ||
exports.compiler = new api.Compile.Compiler(logger); | ||
} | ||
exports.compiler.compile(sourceUnits, callback); | ||
} | ||
@@ -105,3 +106,7 @@ exports.compile = compile; | ||
var api = load_typescript_api(); | ||
var reflection = api.Reflect.Reflection.create(compiledUnits); | ||
var reflection = new api.Reflect.Reflection(); | ||
for(var n in compiledUnits) { | ||
var script = api.Reflect.Script.create(compiledUnits[n].path, compiledUnits[n].ast); | ||
reflection.scripts.push(script); | ||
} | ||
callback(reflection); | ||
@@ -145,28 +150,8 @@ } | ||
} | ||
function include_declarations(units) { | ||
if (exports.include_lib_declaration) { | ||
var lib_decl = exports.create('lib.d.ts', _fs.readFileSync(_path.join(__dirname, "decl/lib.d.ts"), "utf8")); | ||
units.unshift(lib_decl); | ||
} | ||
if (exports.include_node_declaration) { | ||
var node_decl = exports.create('node.d.ts', _fs.readFileSync(_path.join(__dirname, "decl/node.d.ts"), "utf8")); | ||
units.unshift(node_decl); | ||
} | ||
return units; | ||
} | ||
function api_namespace() { | ||
return load_typescript_api(); | ||
} | ||
exports.api_namespace = api_namespace; | ||
function typescript_namespace() { | ||
return load_typescript(); | ||
} | ||
exports.typescript_namespace = typescript_namespace; | ||
var cache = {}; | ||
var typescript_filename = _path.join(__dirname, "typescript.js"); | ||
var typescript_api_filename = _path.join(__dirname, "typescript.api.js"); | ||
var _cache_typescript_namespace = null; | ||
var _cache_typescript_api_namespace = null; | ||
function load_typescript_api() { | ||
if (_cache_typescript_api_namespace) { | ||
return _cache_typescript_api_namespace; | ||
if (cache.typescript_api) { | ||
return cache.typescript_api; | ||
} | ||
@@ -183,10 +168,10 @@ var sandbox = { | ||
}; | ||
_cache_typescript_api_namespace = load_module(typescript_api_filename, sandbox, [ | ||
cache.typescript_api = load_module(typescript_api_filename, sandbox, [ | ||
"TypeScript" | ||
]).Api; | ||
return _cache_typescript_api_namespace; | ||
return cache.typescript_api; | ||
} | ||
function load_typescript() { | ||
if (_cache_typescript_namespace) { | ||
return _cache_typescript_namespace; | ||
if (cache.typescript) { | ||
return cache.typescript; | ||
} | ||
@@ -196,6 +181,6 @@ var sandbox = { | ||
}; | ||
_cache_typescript_namespace = load_module(typescript_filename, sandbox, [ | ||
cache.typescript = load_module(typescript_filename, sandbox, [ | ||
"TypeScript" | ||
]); | ||
return _cache_typescript_namespace; | ||
return cache.typescript; | ||
} | ||
@@ -207,5 +192,5 @@ function load_module(filename, sandbox, export_type_names) { | ||
} | ||
var script = _vm.createScript(source, "typescript.js"); | ||
var script = _vm.createScript(source, _path.basename(filename)); | ||
script.runInNewContext(sandbox); | ||
return sandbox.exports; | ||
} |
{ | ||
"name": "typescript.api", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "A compiler as a service api enabling nodejs developers to resolve, compile, reflect and run typescript 0.9 source files.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -54,19 +54,19 @@ # typescript.api | ||
typescript.resolve(['./program.ts'], function(units) { | ||
typescript.resolve(['./program.ts'], function(resolved) { | ||
if(!typescript.check(units)) { | ||
if(!typescript.check(resolved)) { | ||
show_diagnostics(units); | ||
show_diagnostics(resolved); | ||
} | ||
else { | ||
typescript.compile(units, function(compilation) { | ||
typescript.compile(resolved, function(compiled) { | ||
if(!typescript.check(compilation)) { | ||
if(!typescript.check(compiled)) { | ||
show_diagnostics (compilation); | ||
show_diagnostics (compiled); | ||
} | ||
else | ||
{ | ||
typescript.run(compilation, null, function(context) { | ||
typescript.run(compiled, null, function(context) { | ||
@@ -100,13 +100,13 @@ // exports are available on the context... | ||
typescript.resolve(["program.ts"], function(units) { | ||
typescript.resolve(["program.ts"], function(resolved) { | ||
for(var n in units) { | ||
for(var n in resolved) { | ||
console.log( units[n].path ); | ||
console.log( resolved[n].path ); | ||
console.log( units[n].content ); | ||
console.log( resolved[n].content ); | ||
for(var m in units[n].references) { | ||
for(var m in resolved[n].references) { | ||
console.log( units[n].references[m] ) | ||
console.log( resolved[n].references[m] ) | ||
@@ -134,11 +134,11 @@ } | ||
typescript.resolve(["program.ts"], function(units) { | ||
typescript.resolve(["program.ts"], function(resolved) { | ||
if(typescript.check (units)) { | ||
if(typescript.check (resolved)) { | ||
typescript.compile(units, function(compilation) { | ||
typescript.compile(resolved, function(compiled) { | ||
if( typescript.check (compilation) ) { | ||
if( typescript.check (compiled) ) { | ||
typescript.run(compilation, null, function(context) { | ||
typescript.run(compiled, null, function(context) { | ||
@@ -169,7 +169,7 @@ }); | ||
var unit = typescript.create("temp.ts", "console.log('hello world');"); | ||
var sourceUnit = typescript.create("temp.ts", "console.log('hello world');"); | ||
typescript.compile([unit], function(compilation) { | ||
typescript.compile([sourceUnit], function(compiled) { | ||
typescript.run(compilation, null, function(context) { | ||
typescript.run(compiled, null, function(context) { | ||
@@ -199,9 +199,9 @@ // will output hello world.. | ||
var unit = typescript.create("temp.ts", "var value:number = 123;"); | ||
var sourceUnit = typescript.create("temp.ts", "var value:number = 123;"); | ||
typescript.compile([unit], function(compilation) { | ||
typescript.compile([sourceUnit], function(compiled) { | ||
for(var n in compilation){ | ||
for(var n in compiled) { | ||
console.log(compilation[n].content); | ||
console.log(compiled[n].content); | ||
} | ||
@@ -229,7 +229,7 @@ }); | ||
typescript.resolve(['program.ts'], function(units){ | ||
typescript.resolve(['program.ts'], function(resolved){ | ||
typescript.compile(units, function(compilation) { | ||
typescript.compile(resolved, function(compiled) { | ||
typescript.reflect(compilation, function(reflection) { | ||
typescript.reflect(compiled, function(reflection) { | ||
@@ -262,7 +262,7 @@ var json = JSON.stringify(reflection, null, ' '); | ||
var unit = typescript.create("temp.ts", "export var value:number = 123;"); | ||
var sourceUnit = typescript.create("temp.ts", "export var value:number = 123;"); | ||
typescript.compile([unit], function(compilation) { | ||
typescript.compile([sourceUnit], function(compiled) { | ||
typescript.run(compilation, null, function(context) { | ||
typescript.run(compiled, null, function(context) { | ||
@@ -269,0 +269,0 @@ console.log(context.value); |
@@ -26,2 +26,4 @@ declare module TypeScript.Api.Units { | ||
public remote: boolean; | ||
public syntaxChecked: boolean; | ||
public typeChecked: boolean; | ||
constructor(path: string, content: string, diagnostics: Diagnostic[], remote: boolean); | ||
@@ -120,4 +122,2 @@ public references(): string[]; | ||
} | ||
} | ||
declare module TypeScript.Api.Resolve { | ||
class Resolver { | ||
@@ -162,3 +162,6 @@ public io: IO.IIO; | ||
public logger: ILogger; | ||
public sourceUnits: Units.SourceUnit[]; | ||
constructor(logger: ILogger); | ||
private isSourceUnitInCache(sourceUnit); | ||
private isSourceUnitUpdated(sourceUnit); | ||
private addSourceUnit(sourceUnit); | ||
@@ -171,24 +174,2 @@ private syntaxCheck(sourceUnit); | ||
} | ||
declare module TypeScript.Api.Ast { | ||
class ASTWalker { | ||
public stack: AST[]; | ||
public callback: (sender: ASTWalker, ast: AST) => void; | ||
public userdata: any; | ||
constructor(); | ||
private walk_varstatement(ast); | ||
private walk_type_ref(ast); | ||
private walk_parameter(ast); | ||
private walk_vardecl(ast); | ||
private walk_funcdecl(ast); | ||
private walk_classdecl(ast); | ||
private walk_interface(ast); | ||
private walk_module(ast); | ||
private walk_import(ast); | ||
private walk_script(ast); | ||
private walk_astlist(ast); | ||
public walk_ast(ast: AST): void; | ||
public walk_ast_array(ast_array: AST[]): void; | ||
public walk(ast: AST, callback: (sender: ASTWalker, ast: AST) => void): void; | ||
} | ||
} | ||
declare module TypeScript.Api.Reflect { | ||
@@ -221,2 +202,3 @@ class Import { | ||
public minChar: number; | ||
private static load_type(result, ast); | ||
static create(ast: Parameter): Parameter; | ||
@@ -244,2 +226,5 @@ } | ||
constructor(); | ||
private static load_comments(result, ast); | ||
private static load_returns(result, ast); | ||
static load_parameters(result: Method, ast: FunctionDeclaration): void; | ||
static create(ast: FunctionDeclaration): Method; | ||
@@ -262,2 +247,4 @@ } | ||
constructor(); | ||
private static load_comments(result, ast); | ||
private static load_type(result, ast); | ||
static create(ast: VariableDeclarator): Variable; | ||
@@ -276,2 +263,6 @@ } | ||
constructor(); | ||
private static load_parameters(result, ast); | ||
private static load_extends(result, ast); | ||
private static load_methods(result, ast); | ||
private static load_variables(result, ast); | ||
static create(ast: InterfaceDeclaration): Interface; | ||
@@ -291,2 +282,7 @@ } | ||
constructor(); | ||
private static load_parameters(result, ast); | ||
private static load_extends(result, ast); | ||
private static load_implements(result, ast); | ||
private static load_methods(result, ast); | ||
private static load_variables(result, ast); | ||
static create(ast: ClassDeclaration): Class; | ||
@@ -307,2 +303,8 @@ } | ||
constructor(); | ||
private static load_imports(result, ast); | ||
private static load_modules(result, ast); | ||
private static load_interfaces(result, ast); | ||
private static load_classes(result, ast); | ||
private static load_methods(result, ast); | ||
private static load_variables(result, ast); | ||
static create(ast: ModuleDeclaration): Module; | ||
@@ -320,2 +322,7 @@ } | ||
constructor(); | ||
private static load_modules(result, ast); | ||
private static load_interfaces(result, ast); | ||
private static load_classes(result, ast); | ||
private static load_methods(result, ast); | ||
private static load_variables(result, ast); | ||
static create(path: string, ast: Script): Script; | ||
@@ -328,4 +335,3 @@ } | ||
constructor(); | ||
static create(compiledUnits: Units.CompiledUnit[]): Reflection; | ||
} | ||
} |
@@ -78,2 +78,4 @@ var TypeScript; | ||
this.remote = remote; | ||
this.syntaxChecked = false; | ||
this.typeChecked = false; | ||
} | ||
@@ -474,11 +476,2 @@ SourceUnit.prototype.references = function () { | ||
Resolve.LoadParameter = LoadParameter; | ||
})(Api.Resolve || (Api.Resolve = {})); | ||
var Resolve = Api.Resolve; | ||
})(TypeScript.Api || (TypeScript.Api = {})); | ||
var Api = TypeScript.Api; | ||
})(TypeScript || (TypeScript = {})); | ||
var TypeScript; | ||
(function (TypeScript) { | ||
(function (Api) { | ||
(function (Resolve) { | ||
var Resolver = (function () { | ||
@@ -521,6 +514,6 @@ function Resolver(io, logger) { | ||
this.load(callback); | ||
} else { | ||
this.units.reverse(); | ||
callback(this.units); | ||
return; | ||
} | ||
this.units.reverse(); | ||
callback(this.units); | ||
}; | ||
@@ -597,2 +590,3 @@ Resolver.prototype.visited = function (parameter) { | ||
this.logger = logger; | ||
this.sourceUnits = []; | ||
var settings = new TypeScript.CompilationSettings(); | ||
@@ -605,2 +599,20 @@ settings.codeGenTarget = TypeScript.LanguageVersion.EcmaScript5; | ||
} | ||
Compiler.prototype.isSourceUnitInCache = function (sourceUnit) { | ||
for(var n in this.sourceUnits) { | ||
if (this.sourceUnits[n].path == sourceUnit.path) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
Compiler.prototype.isSourceUnitUpdated = function (sourceUnit) { | ||
for(var n in this.sourceUnits) { | ||
if (this.sourceUnits[n].path == sourceUnit.path) { | ||
if (this.sourceUnits[n].content != sourceUnit.content) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
Compiler.prototype.addSourceUnit = function (sourceUnit) { | ||
@@ -610,3 +622,28 @@ if (!sourceUnit.hasError()) { | ||
var references = TypeScript.getReferencedFiles(sourceUnit.path, snapshot); | ||
this.compiler.addSourceUnit(sourceUnit.path, snapshot, 0, false, references); | ||
if (!this.isSourceUnitInCache(sourceUnit)) { | ||
sourceUnit.syntaxChecked = false; | ||
sourceUnit.typeChecked = false; | ||
this.compiler.addSourceUnit(sourceUnit.path, snapshot, 0, false, references); | ||
this.sourceUnits.push(sourceUnit); | ||
return; | ||
} | ||
if (this.isSourceUnitUpdated(sourceUnit)) { | ||
sourceUnit.syntaxChecked = false; | ||
sourceUnit.typeChecked = false; | ||
var oldSourceUnit = null; | ||
for(var n in this.sourceUnits) { | ||
if (this.sourceUnits[n].path == sourceUnit.path) { | ||
oldSourceUnit = this.sourceUnits[n]; | ||
} | ||
} | ||
var textSpan = new TypeScript.TextSpan(0, oldSourceUnit.content.length); | ||
var textChange = new TypeScript.TextChangeRange(textSpan, sourceUnit.content.length); | ||
this.compiler.updateSourceUnit(sourceUnit.path, snapshot, 0, false, textChange); | ||
for(var n in this.sourceUnits) { | ||
if (this.sourceUnits[n].path == sourceUnit.path) { | ||
this.sourceUnits[n] = sourceUnit; | ||
} | ||
} | ||
return; | ||
} | ||
} | ||
@@ -645,3 +682,3 @@ }; | ||
if (document) { | ||
var sourceUnit; | ||
var sourceUnit = null; | ||
for(var n in sourceUnits) { | ||
@@ -664,18 +701,32 @@ if (sourceUnits[n].path == emitter_io_map[file]) { | ||
Compiler.prototype.compile = function (sourceUnits, callback) { | ||
this.sourceUnits = this.sourceUnits.filter(function (element, index, array) { | ||
for(var n in sourceUnits) { | ||
if (sourceUnits[n].path == element.path) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
for(var n in sourceUnits) { | ||
this.addSourceUnit(sourceUnits[n]); | ||
} | ||
for(var n in sourceUnits) { | ||
var syntax_diagnostics = this.syntaxCheck(sourceUnits[n]); | ||
for(var m in syntax_diagnostics) { | ||
sourceUnits[n].diagnostics.push(syntax_diagnostics[m]); | ||
for(var n in this.sourceUnits) { | ||
if (!this.sourceUnits[n].syntaxChecked) { | ||
var syntax_diagnostics = this.syntaxCheck(this.sourceUnits[n]); | ||
for(var m in syntax_diagnostics) { | ||
this.sourceUnits[n].diagnostics.push(syntax_diagnostics[m]); | ||
} | ||
this.sourceUnits[n].syntaxChecked = true; | ||
} | ||
} | ||
for(var n in sourceUnits) { | ||
var typecheck_diagnostics = this.typeCheck(sourceUnits[n]); | ||
for(var m in typecheck_diagnostics) { | ||
sourceUnits[n].diagnostics.push(typecheck_diagnostics[m]); | ||
for(var n in this.sourceUnits) { | ||
if (!this.sourceUnits[n].typeChecked) { | ||
var typecheck_diagnostics = this.typeCheck(this.sourceUnits[n]); | ||
for(var m in typecheck_diagnostics) { | ||
this.sourceUnits[n].diagnostics.push(typecheck_diagnostics[m]); | ||
} | ||
this.sourceUnits[n].typeChecked = true; | ||
} | ||
} | ||
var compiledUnits = this.emitUnits(sourceUnits); | ||
var compiledUnits = this.emitUnits(this.sourceUnits); | ||
callback(compiledUnits); | ||
@@ -694,143 +745,2 @@ }; | ||
(function (Api) { | ||
(function (Ast) { | ||
var ASTWalker = (function () { | ||
function ASTWalker() { | ||
this.stack = []; | ||
this.userdata = null; | ||
this.callback = function (sender, ast) { | ||
}; | ||
} | ||
ASTWalker.prototype.walk_varstatement = function (ast) { | ||
if (ast.declaration) { | ||
if (ast.declaration.declarators) { | ||
this.callback(this, ast); | ||
this.stack.push(ast); | ||
this.walk_ast_array(ast.declaration.declarators.members); | ||
this.stack.pop(); | ||
} | ||
} | ||
}; | ||
ASTWalker.prototype.walk_type_ref = function (ast) { | ||
this.callback(this, ast); | ||
}; | ||
ASTWalker.prototype.walk_parameter = function (ast) { | ||
this.callback(this, ast); | ||
if (ast.typeExpr) { | ||
this.stack.push(ast); | ||
this.walk_ast(ast.typeExpr); | ||
this.stack.pop(); | ||
} else { | ||
this.stack.push(ast); | ||
this.walk_ast(ast.id); | ||
this.stack.pop(); | ||
} | ||
}; | ||
ASTWalker.prototype.walk_vardecl = function (ast) { | ||
this.callback(this, ast); | ||
if (ast.typeExpr) { | ||
this.stack.push(ast); | ||
this.walk_ast(ast.typeExpr); | ||
this.stack.pop(); | ||
} else { | ||
this.stack.push(ast); | ||
this.walk_ast(ast.id); | ||
this.stack.pop(); | ||
} | ||
}; | ||
ASTWalker.prototype.walk_funcdecl = function (ast) { | ||
this.callback(this, ast); | ||
this.stack.push(ast); | ||
this.walk_astlist(ast.arguments); | ||
this.stack.pop(); | ||
}; | ||
ASTWalker.prototype.walk_classdecl = function (ast) { | ||
this.callback(this, ast); | ||
this.stack.push(ast); | ||
this.walk_ast(ast.members); | ||
this.stack.pop(); | ||
}; | ||
ASTWalker.prototype.walk_interface = function (ast) { | ||
this.callback(this, ast); | ||
this.stack.push(ast); | ||
this.walk_ast(ast.members); | ||
this.stack.pop(); | ||
}; | ||
ASTWalker.prototype.walk_module = function (ast) { | ||
this.callback(this, ast); | ||
this.stack.push(ast); | ||
this.walk_astlist(ast.members); | ||
this.stack.pop(); | ||
}; | ||
ASTWalker.prototype.walk_import = function (ast) { | ||
this.callback(this, ast); | ||
}; | ||
ASTWalker.prototype.walk_script = function (ast) { | ||
this.callback(this, ast); | ||
this.stack.push(ast); | ||
this.walk_ast_array(ast.moduleElements.members); | ||
this.stack.pop(); | ||
}; | ||
ASTWalker.prototype.walk_astlist = function (ast) { | ||
for(var n in ast.members) { | ||
this.walk_ast(ast.members[n]); | ||
} | ||
}; | ||
ASTWalker.prototype.walk_ast = function (ast) { | ||
switch(ast.nodeType) { | ||
case TypeScript.NodeType.List: | ||
this.walk_astlist(ast); | ||
break; | ||
case TypeScript.NodeType.Script: | ||
this.walk_script(ast); | ||
break; | ||
case TypeScript.NodeType.ModuleDeclaration: | ||
this.walk_module(ast); | ||
break; | ||
case TypeScript.NodeType.InterfaceDeclaration: | ||
this.walk_interface(ast); | ||
break; | ||
case TypeScript.NodeType.VariableDeclarator: | ||
this.walk_vardecl(ast); | ||
break; | ||
case TypeScript.NodeType.VariableStatement: | ||
this.walk_varstatement(ast); | ||
break; | ||
case TypeScript.NodeType.ClassDeclaration: | ||
this.walk_classdecl(ast); | ||
break; | ||
case TypeScript.NodeType.FunctionDeclaration: | ||
this.walk_funcdecl(ast); | ||
break; | ||
case TypeScript.NodeType.Parameter: | ||
this.walk_parameter(ast); | ||
break; | ||
case TypeScript.NodeType.ImportDeclaration: | ||
this.walk_import(ast); | ||
break; | ||
case TypeScript.NodeType.TypeRef: | ||
this.walk_type_ref(ast); | ||
break; | ||
} | ||
}; | ||
ASTWalker.prototype.walk_ast_array = function (ast_array) { | ||
for(var n in ast_array) { | ||
this.walk_ast(ast_array[n]); | ||
} | ||
}; | ||
ASTWalker.prototype.walk = function (ast, callback) { | ||
this.callback = callback; | ||
this.stack = []; | ||
this.walk_ast(ast); | ||
}; | ||
return ASTWalker; | ||
})(); | ||
Ast.ASTWalker = ASTWalker; | ||
})(Api.Ast || (Api.Ast = {})); | ||
var Ast = Api.Ast; | ||
})(TypeScript.Api || (TypeScript.Api = {})); | ||
var Api = TypeScript.Api; | ||
})(TypeScript || (TypeScript = {})); | ||
var TypeScript; | ||
(function (TypeScript) { | ||
(function (Api) { | ||
(function (Reflect) { | ||
@@ -885,9 +795,10 @@ var Import = (function () { | ||
var expression = generic_type.name; | ||
if (expression.nodeType == TypeScript.NodeType.Name) { | ||
walk(expression); | ||
switch(expression.nodeType) { | ||
case TypeScript.NodeType.Name: | ||
walk(expression); | ||
break; | ||
case TypeScript.NodeType.MemberAccessExpression: | ||
walk(expression); | ||
break; | ||
} | ||
if (expression.nodeType == TypeScript.NodeType.MemberAccessExpression) { | ||
walk(expression.operand1); | ||
walk(expression.operand2); | ||
} | ||
break; | ||
@@ -903,4 +814,14 @@ default: | ||
Type.create = function create(ast) { | ||
var create_member_access_expression = function (ast) { | ||
var type = new TypeScript.Api.Reflect.Type(); | ||
type.name = Type.qualifyName(ast); | ||
return type; | ||
}; | ||
var create_named_type = function (namedDeclaraion) { | ||
var type = new TypeScript.Api.Reflect.Type(); | ||
type.name = Type.qualifyName(namedDeclaraion); | ||
return type; | ||
}; | ||
var create_type = function (typeRef) { | ||
var type = new Type(); | ||
var type = new TypeScript.Api.Reflect.Type(); | ||
type.name = Type.qualifyName(typeRef); | ||
@@ -920,3 +841,3 @@ type.arrayCount = typeRef.arrayCount; | ||
var create_generic_type = function (genericType) { | ||
var type = new Type(); | ||
var type = new TypeScript.Api.Reflect.Type(); | ||
type.name = Type.qualifyName(genericType); | ||
@@ -933,2 +854,5 @@ type.limChar = genericType.limChar; | ||
switch(ast.nodeType) { | ||
case TypeScript.NodeType.Name: | ||
type = create_named_type(ast); | ||
break; | ||
case TypeScript.NodeType.GenericType: | ||
@@ -940,2 +864,5 @@ type = create_generic_type(ast); | ||
break; | ||
case TypeScript.NodeType.MemberAccessExpression: | ||
type = create_member_access_expression(ast); | ||
break; | ||
} | ||
@@ -958,2 +885,9 @@ return type; | ||
function Parameter() { } | ||
Parameter.load_type = function load_type(result, ast) { | ||
if (!ast.typeExpr) { | ||
result.type = new TypeScript.Api.Reflect.Type(); | ||
return; | ||
} | ||
result.type = TypeScript.Api.Reflect.Type.create(ast.typeExpr); | ||
}; | ||
Parameter.create = function create(ast) { | ||
@@ -964,5 +898,3 @@ var result = new Parameter(); | ||
result.minChar = ast.minChar; | ||
if (!ast.typeExpr) { | ||
result.type = new TypeScript.Api.Reflect.Type(); | ||
} | ||
Parameter.load_type(result, ast); | ||
return result; | ||
@@ -987,2 +919,23 @@ }; | ||
} | ||
Method.load_comments = function load_comments(result, ast) { | ||
var comments = ast.getDocComments(); | ||
for(var n in comments) { | ||
result.comments.push(comments[n].content); | ||
} | ||
}; | ||
Method.load_returns = function load_returns(result, ast) { | ||
if (ast.returnTypeAnnotation) { | ||
var type_reference = ast.returnTypeAnnotation; | ||
result.returns = TypeScript.Api.Reflect.Type.create(type_reference); | ||
return; | ||
} | ||
result.returns = new TypeScript.Api.Reflect.Type(); | ||
}; | ||
Method.load_parameters = function load_parameters(result, ast) { | ||
for(var n in ast.arguments.members) { | ||
var argument = ast.arguments.members[n]; | ||
var parameter = TypeScript.Api.Reflect.Parameter.create(argument); | ||
result.parameters.push(parameter); | ||
} | ||
}; | ||
Method.create = function create(ast) { | ||
@@ -1002,12 +955,5 @@ var result = new Method(); | ||
result.minChar = ast.minChar; | ||
var comments = ast.getDocComments(); | ||
for(var n in comments) { | ||
result.comments.push(comments[n].content); | ||
} | ||
if (ast.returnTypeAnnotation) { | ||
var type_reference = ast.returnTypeAnnotation; | ||
result.returns = TypeScript.Api.Reflect.Type.create(type_reference); | ||
} else { | ||
result.returns = new TypeScript.Api.Reflect.Type(); | ||
} | ||
Method.load_comments(result, ast); | ||
Method.load_returns(result, ast); | ||
Method.load_parameters(result, ast); | ||
return result; | ||
@@ -1031,2 +977,15 @@ }; | ||
} | ||
Variable.load_comments = function load_comments(result, ast) { | ||
var comments = ast.getDocComments(); | ||
for(var n in comments) { | ||
result.comments.push(comments[n].content); | ||
} | ||
}; | ||
Variable.load_type = function load_type(result, ast) { | ||
if (!ast.typeExpr) { | ||
result.type = new TypeScript.Api.Reflect.Type(); | ||
return; | ||
} | ||
result.type = TypeScript.Api.Reflect.Type.create(ast.typeExpr); | ||
}; | ||
Variable.create = function create(ast) { | ||
@@ -1043,9 +1002,4 @@ var result = new Variable(); | ||
result.minChar = ast.minChar; | ||
var comments = ast.getDocComments(); | ||
for(var n in comments) { | ||
result.comments.push(comments[n].content); | ||
} | ||
if (!ast.typeExpr) { | ||
result.type = new TypeScript.Api.Reflect.Type(); | ||
} | ||
Variable.load_type(result, ast); | ||
Variable.load_comments(result, ast); | ||
return result; | ||
@@ -1072,7 +1026,3 @@ }; | ||
} | ||
Interface.create = function create(ast) { | ||
var result = new Interface(); | ||
result.name = ast.name.text; | ||
result.limChar = ast.limChar; | ||
result.minChar = ast.minChar; | ||
Interface.load_parameters = function load_parameters(result, ast) { | ||
if (ast.typeParameters) { | ||
@@ -1085,10 +1035,40 @@ if (ast.typeParameters.members) { | ||
} | ||
}; | ||
Interface.load_extends = function load_extends(result, ast) { | ||
if (ast.extendsList) { | ||
if (ast.extendsList.members) { | ||
for(var n in ast.extendsList.members) { | ||
var type = TypeScript.Api.Reflect.Type.create(ast.extendsList.members[n]); | ||
result.extends.push(type); | ||
var obj = TypeScript.Api.Reflect.Type.create(ast.extendsList.members[n]); | ||
result.extends.push(obj); | ||
} | ||
} | ||
} | ||
}; | ||
Interface.load_methods = function load_methods(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.FunctionDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Method.create(member); | ||
result.methods.push(obj); | ||
} | ||
} | ||
}; | ||
Interface.load_variables = function load_variables(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.VariableDeclarator) { | ||
var obj = TypeScript.Api.Reflect.Variable.create(member); | ||
result.variables.push(obj); | ||
} | ||
} | ||
}; | ||
Interface.create = function create(ast) { | ||
var result = new Interface(); | ||
result.name = ast.name.text; | ||
result.limChar = ast.limChar; | ||
result.minChar = ast.minChar; | ||
Interface.load_parameters(result, ast); | ||
Interface.load_extends(result, ast); | ||
Interface.load_methods(result, ast); | ||
Interface.load_variables(result, ast); | ||
return result; | ||
@@ -1116,7 +1096,3 @@ }; | ||
} | ||
Class.create = function create(ast) { | ||
var result = new Class(); | ||
result.name = ast.name.text; | ||
result.limChar = ast.limChar; | ||
result.minChar = ast.minChar; | ||
Class.load_parameters = function load_parameters(result, ast) { | ||
if (ast.typeParameters) { | ||
@@ -1129,18 +1105,51 @@ if (ast.typeParameters.members) { | ||
} | ||
}; | ||
Class.load_extends = function load_extends(result, ast) { | ||
if (ast.extendsList) { | ||
if (ast.extendsList.members) { | ||
for(var n in ast.extendsList.members) { | ||
var obj = TypeScript.Api.Reflect.Type.create(ast.extendsList.members[n]); | ||
result.extends.push(obj); | ||
} | ||
} | ||
} | ||
}; | ||
Class.load_implements = function load_implements(result, ast) { | ||
if (ast.implementsList) { | ||
if (ast.implementsList.members) { | ||
for(var n in ast.implementsList.members) { | ||
var type = TypeScript.Api.Reflect.Type.create(ast.implementsList.members[n]); | ||
result.implements.push(type); | ||
var obj = TypeScript.Api.Reflect.Type.create(ast.implementsList.members[n]); | ||
result.implements.push(obj); | ||
} | ||
} | ||
} | ||
if (ast.extendsList) { | ||
if (ast.extendsList.members) { | ||
for(var n in ast.extendsList.members) { | ||
var type = TypeScript.Api.Reflect.Type.create(ast.extendsList.members[n]); | ||
result.extends.push(type); | ||
} | ||
}; | ||
Class.load_methods = function load_methods(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.FunctionDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Method.create(member); | ||
result.methods.push(obj); | ||
} | ||
} | ||
}; | ||
Class.load_variables = function load_variables(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.VariableDeclarator) { | ||
var obj = TypeScript.Api.Reflect.Variable.create(member); | ||
result.variables.push(obj); | ||
} | ||
} | ||
}; | ||
Class.create = function create(ast) { | ||
var result = new Class(); | ||
result.name = ast.name.text; | ||
result.limChar = ast.limChar; | ||
result.minChar = ast.minChar; | ||
Class.load_parameters(result, ast); | ||
Class.load_implements(result, ast); | ||
Class.load_extends(result, ast); | ||
Class.load_methods(result, ast); | ||
Class.load_variables(result, ast); | ||
return result; | ||
@@ -1169,2 +1178,63 @@ }; | ||
} | ||
Module.load_imports = function load_imports(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.ImportDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Import.create(member); | ||
result.imports.push(obj); | ||
} | ||
} | ||
}; | ||
Module.load_modules = function load_modules(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.ModuleDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Module.create(member); | ||
result.modules.push(obj); | ||
} | ||
} | ||
}; | ||
Module.load_interfaces = function load_interfaces(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.InterfaceDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Interface.create(member); | ||
result.interfaces.push(obj); | ||
} | ||
} | ||
}; | ||
Module.load_classes = function load_classes(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.ClassDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Class.create(member); | ||
result.classes.push(obj); | ||
} | ||
} | ||
}; | ||
Module.load_methods = function load_methods(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.FunctionDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Method.create(member); | ||
result.methods.push(obj); | ||
} | ||
} | ||
}; | ||
Module.load_variables = function load_variables(result, ast) { | ||
for(var n in ast.members.members) { | ||
var member = ast.members.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.VariableStatement) { | ||
var statement = member; | ||
if (statement.declaration) { | ||
if (statement.declaration.declarators) { | ||
for(var m in statement.declaration.declarators.members) { | ||
var obj = TypeScript.Api.Reflect.Variable.create(statement.declaration.declarators.members[m]); | ||
result.variables.push(obj); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
Module.create = function create(ast) { | ||
@@ -1175,2 +1245,8 @@ var result = new Module(); | ||
result.minChar = ast.minChar; | ||
Module.load_imports(result, ast); | ||
Module.load_modules(result, ast); | ||
Module.load_interfaces(result, ast); | ||
Module.load_classes(result, ast); | ||
Module.load_methods(result, ast); | ||
Module.load_variables(result, ast); | ||
return result; | ||
@@ -1198,5 +1274,62 @@ }; | ||
} | ||
Script.load_modules = function load_modules(result, ast) { | ||
for(var n in ast.moduleElements.members) { | ||
var member = ast.moduleElements.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.ModuleDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Module.create(member); | ||
result.modules.push(obj); | ||
} | ||
} | ||
}; | ||
Script.load_interfaces = function load_interfaces(result, ast) { | ||
for(var n in ast.moduleElements.members) { | ||
var member = ast.moduleElements.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.InterfaceDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Interface.create(member); | ||
result.interfaces.push(obj); | ||
} | ||
} | ||
}; | ||
Script.load_classes = function load_classes(result, ast) { | ||
for(var n in ast.moduleElements.members) { | ||
var member = ast.moduleElements.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.ClassDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Class.create(member); | ||
result.classes.push(obj); | ||
} | ||
} | ||
}; | ||
Script.load_methods = function load_methods(result, ast) { | ||
for(var n in ast.moduleElements.members) { | ||
var member = ast.moduleElements.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.FunctionDeclaration) { | ||
var obj = TypeScript.Api.Reflect.Method.create(member); | ||
result.methods.push(obj); | ||
} | ||
} | ||
}; | ||
Script.load_variables = function load_variables(result, ast) { | ||
for(var n in ast.moduleElements.members) { | ||
var member = ast.moduleElements.members[n]; | ||
if (member.nodeType == TypeScript.NodeType.VariableStatement) { | ||
var statement = member; | ||
if (statement.declaration) { | ||
if (statement.declaration.declarators) { | ||
for(var m in statement.declaration.declarators.members) { | ||
var obj = TypeScript.Api.Reflect.Variable.create(statement.declaration.declarators.members[m]); | ||
result.variables.push(obj); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
Script.create = function create(path, ast) { | ||
var result = new Script(); | ||
result.path = path; | ||
Script.load_modules(result, ast); | ||
Script.load_interfaces(result, ast); | ||
Script.load_classes(result, ast); | ||
Script.load_methods(result, ast); | ||
Script.load_variables(result, ast); | ||
return result; | ||
@@ -1220,66 +1353,2 @@ }; | ||
} | ||
Reflection.create = function create(compiledUnits) { | ||
var reflection = new TypeScript.Api.Reflect.Reflection(); | ||
var walker = new TypeScript.Api.Ast.ASTWalker(); | ||
walker.userdata = []; | ||
walker.userdata.push(reflection); | ||
for(var n in compiledUnits) { | ||
walker.walk(compiledUnits[n].ast, function (walker, ast) { | ||
if (walker.stack.length < walker.userdata.length - 1) { | ||
do { | ||
walker.userdata.pop(); | ||
} while(walker.stack.length < walker.userdata.length - 1); | ||
} | ||
var parent = walker.userdata[walker.userdata.length - 1]; | ||
switch(ast.nodeType) { | ||
case TypeScript.NodeType.VariableDeclarator: | ||
var variable = Reflect.Variable.create(ast); | ||
parent.variables.push(variable); | ||
walker.userdata.push(variable); | ||
break; | ||
case TypeScript.NodeType.TypeRef: | ||
var type = Reflect.Type.create(ast); | ||
parent.type = type; | ||
walker.userdata.push(type); | ||
break; | ||
case TypeScript.NodeType.Parameter: | ||
var parameter = Reflect.Parameter.create(ast); | ||
parent.parameters.push(parameter); | ||
walker.userdata.push(parameter); | ||
break; | ||
case TypeScript.NodeType.FunctionDeclaration: | ||
var method = Reflect.Method.create(ast); | ||
parent.methods.push(method); | ||
walker.userdata.push(method); | ||
break; | ||
case TypeScript.NodeType.ClassDeclaration: | ||
var _class = Reflect.Class.create(ast); | ||
parent.classes.push(_class); | ||
walker.userdata.push(_class); | ||
break; | ||
case TypeScript.NodeType.InterfaceDeclaration: | ||
var _interface = Reflect.Interface.create(ast); | ||
parent.interfaces.push(_interface); | ||
walker.userdata.push(_interface); | ||
break; | ||
case TypeScript.NodeType.ImportDeclaration: | ||
var _import = Reflect.Import.create(ast); | ||
parent.imports.push(_import); | ||
walker.userdata.push(_import); | ||
break; | ||
case TypeScript.NodeType.ModuleDeclaration: | ||
var _module = Reflect.Module.create(ast); | ||
parent.modules.push(_module); | ||
walker.userdata.push(_module); | ||
break; | ||
case TypeScript.NodeType.Script: | ||
var _script = Reflect.Script.create(compiledUnits[n].path, ast); | ||
parent.scripts.push(_script); | ||
walker.userdata.push(_script); | ||
break; | ||
} | ||
}); | ||
} | ||
return reflection; | ||
}; | ||
return Reflection; | ||
@@ -1286,0 +1355,0 @@ })(); |
2816133
54709