ts-runtime
Advanced tools
Comparing version 0.1.20 to 0.1.21
@@ -40,2 +40,3 @@ import * as ts from 'typescript'; | ||
getTypeParametersDeclarationName(): string; | ||
getLibTypeParameterSymbolName(): string; | ||
getMembers(node: ts.ClassDeclaration | ts.InterfaceDeclaration): (ts.TypeElement | ts.ClassElement)[]; | ||
@@ -42,0 +43,0 @@ getAllMembers(node: ts.ClassDeclaration | ts.InterfaceDeclaration): (ts.TypeElement | ts.ClassElement)[]; |
@@ -225,2 +225,5 @@ "use strict"; | ||
} | ||
getLibTypeParameterSymbolName() { | ||
return 'TypeParametersSymbol'; | ||
} | ||
getMembers(node) { | ||
@@ -227,0 +230,0 @@ const nodeSymbol = this.scanner.getNodeSymbol(node.name); |
@@ -16,5 +16,7 @@ import * as ts from 'typescript'; | ||
private _load; | ||
private _warnings; | ||
constructor(_context: MutationContext, options: Options, library?: string); | ||
private proxy(target, property, receiver); | ||
typeReflection(node: ts.TypeNode): ts.Expression; | ||
private warn(warning); | ||
typeDeclaration(name: string | ts.Identifier | ts.ObjectBindingPattern | ts.ArrayBindingPattern, node: ts.TypeNode): ts.VariableDeclaration; | ||
@@ -52,8 +54,8 @@ typeAssertion(id: string | ts.Expression, args?: ts.Expression | ts.Expression[]): ts.CallExpression; | ||
classReflection(node: ts.ClassDeclaration, name?: string): ts.Expression; | ||
namedDeclarationsReflections(name: string, declarations: ts.Declaration[]): ts.Expression[]; | ||
namedDeclarationReflection(name: string, declaration: ts.Declaration): ts.Expression; | ||
namedDeclarationsReflections(name: string, declarations: ts.Declaration[], originalName?: string): ts.Expression[]; | ||
namedDeclarationReflection(name: string, declaration: ts.Declaration, originalName?: string): ts.Expression; | ||
enumReflection(node: ts.EnumDeclaration): ts.CallExpression; | ||
enumMemberReflection(node: ts.EnumMember): ts.CallExpression; | ||
variableReflection(node: ts.VariableDeclaration): ts.Expression; | ||
functionReflection(node: FunctionLikeNode): ts.CallExpression; | ||
functionReflection(node: FunctionLikeNode, asCallProperty?: boolean): ts.CallExpression; | ||
returnTypeReflection(node: ts.TypeNode): ts.CallExpression; | ||
@@ -115,2 +117,3 @@ typeParameterReflection(typeParameter: ts.TypeParameterDeclaration, prop?: string): ts.CallExpression; | ||
namespace: string; | ||
readonly warnings: string[]; | ||
} | ||
@@ -117,0 +120,0 @@ export declare enum FactoryState { |
@@ -14,2 +14,3 @@ "use strict"; | ||
this._load = 'ts-runtime/lib'; | ||
this._warnings = []; | ||
this._strictNullChecks = !!options.compilerOptions.strictNullChecks; | ||
@@ -99,6 +100,3 @@ this._namespace = options.libNamespace || this._namespace; | ||
// } | ||
bus.emit(bus.events.WARN, 'Mapped types are not yet supported.'); | ||
if (this.context.options.log) { | ||
console.warn('Mapped types are not yet supported.'); | ||
} | ||
this.warn('Mapped types are not yet supported.'); | ||
return this.anyTypeReflection(); | ||
@@ -109,13 +107,7 @@ case ts.SyntaxKind.IndexedAccessType: | ||
// } | ||
bus.emit(bus.events.WARN, 'Indexed acces types are not yet supported.'); | ||
if (this.context.options.log) { | ||
console.warn('Indexed acces types are not yet supported.'); | ||
} | ||
this.warn('Indexed acces types are not yet supported.'); | ||
return this.anyTypeReflection(); | ||
case ts.SyntaxKind.TypeOperator: | ||
// let a: keyof MyClass; | ||
bus.emit(bus.events.WARN, 'Type operators are not yet supported.'); | ||
if (this.context.options.log) { | ||
console.warn('Type operators are not yet supported.'); | ||
} | ||
this.warn('Type operators are not yet supported.'); | ||
return this.anyTypeReflection(); | ||
@@ -126,2 +118,11 @@ default: | ||
} | ||
warn(warning) { | ||
bus.emit(bus.events.WARN, warning); | ||
if (this.context.options.log && this._warnings.indexOf(warning) === -1) { | ||
this._warnings.push(warning); | ||
console.warn(warning); | ||
return true; | ||
} | ||
return false; | ||
} | ||
typeDeclaration(name, node) { | ||
@@ -227,3 +228,13 @@ return ts.createVariableDeclaration(name, undefined, this.typeReflection(node)); | ||
if (isClassTypeParameter && !this.rule(exports.FactoryRule.NoClassTypeParameterReflection)) { | ||
result = ts.createPropertyAccess(ts.createElementAccess(ts.createThis(), ts.createIdentifier(this.context.getTypeSymbolDeclarationName(parentClass.name.text))), typeNameText); | ||
result = ts.createPropertyAccess(ts.createElementAccess( | ||
// Static members cannot reference class type parameters | ||
// util.isInStaticContext(node) ? | ||
// ts.createElementAccess( | ||
// ts.createIdentifier(parentClass.name.text), | ||
// ts.createPropertyAccess( | ||
// ts.createIdentifier(this.lib), | ||
// ts.createIdentifier(this.context.getLibTypeParameterSymbolName()) | ||
// ) | ||
// ) : | ||
ts.createThis(), ts.createIdentifier(this.context.getTypeSymbolDeclarationName(parentClass.name.text))), typeNameText); | ||
return !keyword ? result : this.libCall(keyword, result); | ||
@@ -269,3 +280,10 @@ } | ||
typeQueryReflection(node) { | ||
return this.libCall('typeOf', ts.createIdentifier(util.getEntityNameText(node.exprName))); | ||
const typeInfo = this.scanner.getTypeInfo(node.exprName); | ||
let text = util.getEntityNameText(node.exprName); | ||
let ref = ts.createIdentifier(text); | ||
if (typeInfo.isTsrDeclaration()) { | ||
text = util.getHashedDeclarationName(text, typeInfo.sourceFile.fileName); | ||
ref = this.asRef(ts.createLiteral(text)); | ||
} | ||
return this.libCall('typeOf', ref); | ||
} | ||
@@ -425,6 +443,6 @@ typeLiteralReflection(node) { | ||
} | ||
namedDeclarationsReflections(name, declarations) { | ||
namedDeclarationsReflections(name, declarations, originalName) { | ||
const expressions = []; | ||
const firstDeclaration = declarations[0]; | ||
const expression = this.namedDeclarationReflection(name, firstDeclaration); | ||
const expression = this.namedDeclarationReflection(name, firstDeclaration, originalName); | ||
if (expression) { | ||
@@ -435,21 +453,22 @@ expressions.push(expression); | ||
} | ||
namedDeclarationReflection(name, declaration) { | ||
namedDeclarationReflection(name, declaration, originalName) { | ||
const typeName = originalName || name; | ||
switch (declaration.kind) { | ||
case ts.SyntaxKind.InterfaceDeclaration: | ||
return this.libCall('declare', [ts.createLiteral(name), this.interfaceReflection(declaration, name)]); | ||
return this.libCall('declare', [ts.createLiteral(name), this.interfaceReflection(declaration, typeName)]); | ||
case ts.SyntaxKind.ClassDeclaration: | ||
return this.libCall('declare', [ts.createLiteral(name), this.classReflection(declaration, name)]); | ||
case ts.SyntaxKind.TypeLiteral: | ||
return this.libCall('declare', [ts.createLiteral(name), this.asType(name, this.typeLiteralReflection(declaration))]); | ||
return this.libCall('declare', [ts.createLiteral(name), this.classReflection(declaration, typeName)]); | ||
case ts.SyntaxKind.TypeLiteral:// remove? | ||
return this.libCall('declare', [ts.createLiteral(name), this.asType(typeName, this.typeLiteralReflection(declaration))]); | ||
case ts.SyntaxKind.EnumDeclaration: | ||
return this.libCall('declare', [ts.createLiteral(name), this.asType(name, this.enumReflection(declaration))]); | ||
case ts.SyntaxKind.EnumMember: | ||
return this.libCall('declare', [ts.createLiteral(name), this.asType(name, this.enumMemberReflection(declaration))]); | ||
return this.libCall('declare', [ts.createLiteral(name), this.asType(typeName, this.enumReflection(declaration))]); | ||
case ts.SyntaxKind.EnumMember:// remove? | ||
return this.libCall('declare', [ts.createLiteral(name), this.asType(typeName, this.enumMemberReflection(declaration))]); | ||
case ts.SyntaxKind.FunctionDeclaration: | ||
return this.libCall('declare', [ts.createLiteral(name), this.asType(name, this.functionReflection(declaration))]); | ||
return this.libCall('declare', [ts.createLiteral(name), this.asType(typeName, this.functionReflection(declaration))]); | ||
case ts.SyntaxKind.VariableDeclaration: | ||
return this.libCall('declare', [ts.createLiteral(name), this.asVar(name, this.variableReflection(declaration))]); | ||
return this.libCall('declare', [ts.createLiteral(name), this.asVar(typeName, this.variableReflection(declaration))]); | ||
case ts.SyntaxKind.TypeAliasDeclaration: | ||
return this.libCall('declare', [ts.createLiteral(name), this.typeAliasReflection(declaration, name)]); | ||
case ts.SyntaxKind.FunctionType: | ||
return this.libCall('declare', [ts.createLiteral(name), this.typeAliasReflection(declaration, typeName)]); | ||
case ts.SyntaxKind.FunctionType:// remove? | ||
return this.libCall('declare', [ts.createLiteral(name), this.functionTypeReflection(declaration)]); | ||
@@ -471,3 +490,3 @@ default: | ||
} | ||
functionReflection(node) { | ||
functionReflection(node, asCallProperty = false) { | ||
const parameters = node.parameters || []; | ||
@@ -1032,2 +1051,5 @@ let args = parameters | ||
} | ||
get warnings() { | ||
return this._warnings; | ||
} | ||
} | ||
@@ -1034,0 +1056,0 @@ exports.Factory = Factory; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const t = require("flow-runtime"); | ||
const map = new Map(); | ||
// const map: Map<string, any> = new Map(); | ||
const voidType = t.void; | ||
const intersect = t.intersect; | ||
const declare = t.declare; | ||
const ref = t.ref; | ||
const decorate = t.decorate; | ||
// const intersect = t.intersect; | ||
// const declare = t.declare; | ||
// const ref = t.ref; | ||
// const decorate = t.decorate; | ||
// t.decorate = (type: any, shouldAssert?: boolean) => { | ||
@@ -11,0 +11,0 @@ // return (input: any, propertyName: any, descriptor: any) => { |
@@ -159,2 +159,3 @@ "use strict"; | ||
} | ||
// TODO: assert parameters and return type is isImplementationOfOverload | ||
mutateMethodDeclaration(node) { | ||
@@ -161,0 +162,0 @@ return this.factory.mutateFunctionBody(node); |
{ | ||
"name": "ts-runtime", | ||
"version": "0.1.20", | ||
"version": "0.1.21", | ||
"description": "Runtime type checks for TypeScript", | ||
@@ -26,3 +26,3 @@ "main": "index.js", | ||
"docs": { | ||
"version": "0.1.20-0" | ||
"version": "0.1.21-0" | ||
} | ||
@@ -29,0 +29,0 @@ }, |
@@ -452,10 +452,8 @@ # ts-runtime | ||
- No reflection of mapped types, indexed access types and type operators yet. | ||
- No support for string enums. | ||
- `readonly` is currently only supported for classes. | ||
- `readonly` is currently only checked for classes. | ||
- Class visibility modifiers are not asserted. | ||
- Class type parameters are only checked when extending, at this time. | ||
- No declaration merging. | ||
- Method overloading is only supported within the same declaration. | ||
- Types with self references and generics are not asserted correctly. | ||
- No class expressions (`const A = class { }`), only class declarations (`class A { }`) can be used. | ||
- `ExpressionWithTypeArguments` can only contain `PropertyAccessExpression`s as expression with an `Identifier` as name. | ||
- `ExpressionWithTypeArguments` can only contain `PropertyAccessExpression`s as expression with an `Identifier` as name, recursively. | ||
- No JSX support. | ||
@@ -462,0 +460,0 @@ |
@@ -6,2 +6,3 @@ import * as ts from 'typescript'; | ||
name: string; | ||
originalName: string; | ||
} | ||
@@ -8,0 +9,0 @@ export declare class Scanner { |
@@ -245,3 +245,3 @@ "use strict"; | ||
if (index === -1) { | ||
this.declarations.unshift({ symbol, name: uid }); | ||
this.declarations.unshift({ symbol, name: uid, originalName: name }); | ||
} | ||
@@ -248,0 +248,0 @@ } |
@@ -144,3 +144,3 @@ "use strict"; | ||
names.push(declarations[i].name); | ||
expressions.unshift(...context.factory.namedDeclarationsReflections(declarations[i].name, declarations[i].symbol.getDeclarations())); | ||
expressions.unshift(...context.factory.namedDeclarationsReflections(declarations[i].name, declarations[i].symbol.getDeclarations(), declarations[i].originalName)); | ||
} | ||
@@ -147,0 +147,0 @@ processed = length; |
@@ -24,2 +24,3 @@ import * as ts from 'typescript'; | ||
export declare function isTypeParameterOfClass(node: ts.TypeReferenceNode): ts.ClassDeclaration; | ||
export declare function isInStaticContext(node: ts.TypeReferenceNode): boolean; | ||
export declare function isSuperStatement(node: ts.Node): boolean; | ||
@@ -26,0 +27,0 @@ export declare function isKind(node: ts.Node, ...kind: ts.SyntaxKind[]): boolean; |
10
util.js
@@ -161,2 +161,12 @@ "use strict"; | ||
exports.isTypeParameterOfClass = isTypeParameterOfClass; | ||
function isInStaticContext(node) { | ||
let current = node; | ||
while (current = current.parent) { | ||
if (isStatic(current)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
exports.isInStaticContext = isInStaticContext; | ||
function isSuperStatement(node) { | ||
@@ -163,0 +173,0 @@ return ts.isExpressionStatement(node) && ts.isCallExpression(node.expression) && |
469999
10720
640