ts-runtime
Advanced tools
Comparing version 0.1.27 to 0.1.28
@@ -35,3 +35,2 @@ import * as ts from 'typescript'; | ||
getReturnTypeDeclarationName(): string; | ||
getInlineTypeName(node: string | ts.Identifier): string; | ||
getLibDeclarationName(): string; | ||
@@ -41,9 +40,5 @@ getTypeSymbolDeclarationName(node: string | ts.Identifier): string; | ||
getTypeParametersDeclarationName(): string; | ||
getLibTypeParameterSymbolName(): string; | ||
getMembers(node: ts.ClassDeclaration | ts.InterfaceDeclaration): (ts.TypeElement | ts.ClassElement)[]; | ||
getAllMembers(node: ts.ClassDeclaration | ts.InterfaceDeclaration): (ts.TypeElement | ts.ClassElement)[]; | ||
setMerged(symbol: ts.Symbol): Set<ts.Symbol>; | ||
wasMerged(symbol: ts.Symbol): boolean; | ||
setTransformationContext(transformationContext: ts.TransformationContext): void; | ||
setSourceFile(sourceFile: ts.SourceFile): void; | ||
sourceFile: ts.SourceFile; | ||
@@ -50,0 +45,0 @@ transformationContext: ts.TransformationContext; |
108
context.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// import * as path from 'path'; | ||
var ts = require("typescript"); | ||
var util = require("./util"); | ||
var errors_1 = require("./errors"); | ||
var factory_1 = require("./factory"); | ||
@@ -57,3 +57,3 @@ var MutationContext = /** @class */ (function () { | ||
MutationContext.prototype.isImplementationOfOverload = function (node) { | ||
return ts.isFunctionLike(node) && this.checker.isImplementationOfOverload(node); | ||
return !!(ts.isFunctionLike(node) && this.checker.isImplementationOfOverload(node)); | ||
}; | ||
@@ -124,4 +124,4 @@ MutationContext.prototype.isDeclared = function (node) { | ||
}; | ||
// public isSafeAssignment(node: ts.Node, other: ts.Node, strict = false): boolean { | ||
// if (this.options.assertSafe) { | ||
// public isSafeAssignment(node: ts.Node, other: ts.Node, strict = false, force = false): boolean { | ||
// if (this.options.assertSafe && !force) { | ||
// return false; | ||
@@ -156,2 +156,5 @@ // } | ||
var symbol = this.scanner.getNodeSymbol(node.name || node.typeName || node); | ||
if (!symbol) { | ||
return false; | ||
} | ||
var declarations = symbol.getDeclarations(); | ||
@@ -184,7 +187,7 @@ if (!declarations) { | ||
MutationContext.prototype.hasProperty = function (node, name) { | ||
var typeInfo = this.scanner.getTypeInfo(node); | ||
if (!typeInfo || !typeInfo.type) { | ||
var nodeType = this.scanner.getType(node); | ||
if (!nodeType) { | ||
return false; | ||
} | ||
for (var _i = 0, _a = typeInfo.type.getProperties(); _i < _a.length; _i++) { | ||
for (var _i = 0, _a = nodeType.getProperties(); _i < _a.length; _i++) { | ||
var prop = _a[_i]; | ||
@@ -197,8 +200,10 @@ if (prop.name === name) { | ||
}; | ||
// TODO: property names may not be unique | ||
MutationContext.prototype.getPropertyName = function (node, name) { | ||
var typeInfo = this.scanner.getTypeInfo(node); | ||
if (!typeInfo || !typeInfo.type) { | ||
var nodeType = this.scanner.getType(node); | ||
var originalName = name; | ||
if (!nodeType) { | ||
return name; | ||
} | ||
var ids = typeInfo.type.getProperties().map(function (prop) { | ||
var ids = nodeType.getProperties().map(function (prop) { | ||
return prop.name; | ||
@@ -209,2 +214,7 @@ }); | ||
} | ||
// TODO: use a map to ensure unique name | ||
name = name + "_" + originalName; | ||
if (ids && ids.indexOf(name) !== -1) { | ||
return this.getPropertyName(node, name); | ||
} | ||
return name; | ||
@@ -226,6 +236,6 @@ }; | ||
}; | ||
MutationContext.prototype.getInlineTypeName = function (node) { | ||
var name = typeof node === 'string' ? node : node.text; | ||
return this.getIdentifier("" + this.options.declarationPrefix + name + "TypeInline"); | ||
}; | ||
// public getInlineTypeName(node: string | ts.Identifier): string { | ||
// const name = typeof node === 'string' ? node : node.text; | ||
// return this.getIdentifier(`${this.options.declarationPrefix}${name}TypeInline`); | ||
// } | ||
MutationContext.prototype.getLibDeclarationName = function () { | ||
@@ -245,5 +255,5 @@ return this.getIdentifier("" + this.options.libNamespace + this.options.libIdentifier); | ||
}; | ||
MutationContext.prototype.getLibTypeParameterSymbolName = function () { | ||
return 'TypeParametersSymbol'; | ||
}; | ||
// public getLibTypeParameterSymbolName(): string { | ||
// return 'TypeParametersSymbol'; | ||
// } | ||
MutationContext.prototype.getMembers = function (node) { | ||
@@ -270,25 +280,27 @@ var nodeSymbol = this.scanner.getNodeSymbol(node.name); | ||
}; | ||
MutationContext.prototype.getAllMembers = function (node) { | ||
var nodeSymbol = this.scanner.getNodeSymbol(node.name); | ||
var merged = new Set(); | ||
var type; | ||
if (!nodeSymbol) { | ||
type = this.checker.getTypeAtLocation(node); | ||
} | ||
else { | ||
type = this.checker.getDeclaredTypeOfSymbol(nodeSymbol); | ||
} | ||
if (!type) { | ||
return util.arrayFromNodeArray(node.members); | ||
} | ||
(type.getProperties() || []).forEach(function (sym) { | ||
for (var _i = 0, _a = (sym.getDeclarations() || []); _i < _a.length; _i++) { | ||
var typeElement = _a[_i]; | ||
if (ts.isTypeElement(typeElement) || ts.isClassElement(typeElement)) { | ||
merged.add(typeElement); | ||
} | ||
} | ||
}); | ||
return Array.from(merged); | ||
}; | ||
// public getAllMembers(node: ts.ClassDeclaration | ts.InterfaceDeclaration): (ts.TypeElement | ts.ClassElement)[] { | ||
// const nodeSymbol = this.scanner.getNodeSymbol(node.name); | ||
// const merged: Set<ts.TypeElement | ts.ClassElement> = new Set(); | ||
// let type: ts.Type; | ||
// | ||
// if (!nodeSymbol) { | ||
// type = this.checker.getTypeAtLocation(node); | ||
// } else { | ||
// type = this.checker.getDeclaredTypeOfSymbol(nodeSymbol); | ||
// } | ||
// | ||
// if (!type) { | ||
// return util.arrayFromNodeArray(node.members as ts.NodeArray<ts.TypeElement | ts.ClassElement>); | ||
// } | ||
// | ||
// (type.getProperties() || []).forEach(sym => { | ||
// for (let typeElement of (sym.getDeclarations() || [])) { | ||
// if (ts.isTypeElement(typeElement) || ts.isClassElement(typeElement)) { | ||
// merged.add(typeElement); | ||
// } | ||
// } | ||
// }); | ||
// | ||
// return Array.from(merged); | ||
// } | ||
MutationContext.prototype.setMerged = function (symbol) { | ||
@@ -300,9 +312,9 @@ return this._merged.add(symbol); | ||
}; | ||
MutationContext.prototype.setTransformationContext = function (transformationContext) { | ||
this.transformationContext = transformationContext; | ||
}; | ||
MutationContext.prototype.setSourceFile = function (sourceFile) { | ||
this.sourceFile = sourceFile; | ||
}; | ||
Object.defineProperty(MutationContext.prototype, "sourceFile", { | ||
// public setTransformationContext(transformationContext: ts.TransformationContext): void { | ||
// this.transformationContext = transformationContext; | ||
// } | ||
// public setSourceFile(sourceFile: ts.SourceFile): void { | ||
// this.sourceFile = sourceFile; | ||
// } | ||
get: function () { | ||
@@ -312,4 +324,4 @@ return this._sourceFile; | ||
set: function (sourceFile) { | ||
if (ts.isSourceFile(sourceFile)) { | ||
throw new Error("Attemt to set invalid node as SourceFile on MutationContext. Got " + ts.SyntaxKind[sourceFile.kind] + "."); | ||
if (!ts.isSourceFile(sourceFile)) { | ||
throw new errors_1.ProgramError("Attemt to set invalid node as SourceFile on MutationContext."); | ||
} | ||
@@ -316,0 +328,0 @@ this._sourceFile = sourceFile; |
@@ -10,3 +10,3 @@ "use strict"; | ||
this._context = _context; | ||
this._state = new Map(); | ||
this._state = new Map([[FactoryState.None, 1]]); | ||
this._namespace = ''; | ||
@@ -39,2 +39,5 @@ this._lib = 't'; | ||
_this.addState(state); | ||
if (_this._state.size > 1) { | ||
_this.removeState(FactoryState.None); | ||
} | ||
var result = destination.bind(receiver).apply(void 0, args); | ||
@@ -45,2 +48,5 @@ if (_this.match(exports.FactoryRule.Nullable, state)) { | ||
_this.removeState(state); | ||
if (_this._state.size === 0) { | ||
_this.addState(FactoryState.None); | ||
} | ||
return result; | ||
@@ -183,5 +189,4 @@ }; | ||
return this.numericLiteralTypeReflection(node.literal); | ||
case ts.SyntaxKind.ComputedPropertyName: | ||
default: | ||
throw new Error("No literal type reflection for syntax kind '" + ts.SyntaxKind[node.literal.kind] + "' found."); | ||
throw new errors_1.ProgramError("No literal type reflection for syntax kind '" + ts.SyntaxKind[node.literal.kind] + "' found."); | ||
} | ||
@@ -628,3 +633,3 @@ }; | ||
default: | ||
throw new Error("No type element reflection for syntax kind '" + ts.SyntaxKind[node.kind] + "' found."); | ||
throw new errors_1.ProgramError("No type element reflection for syntax kind '" + ts.SyntaxKind[node.kind] + "' found."); | ||
} | ||
@@ -940,3 +945,3 @@ }; | ||
default: | ||
throw new Error("Property name for syntax kind '" + ts.SyntaxKind[node.kind] + "' could not be generated."); | ||
throw new errors_1.ProgramError("Property name for syntax kind '" + ts.SyntaxKind[node.kind] + "' could not be generated."); | ||
} | ||
@@ -954,3 +959,3 @@ }; | ||
default: | ||
throw new Error("Declaration name for syntax kind '" + ts.SyntaxKind[node.kind] + "' could not be generated."); | ||
throw new errors_1.ProgramError("Declaration name for syntax kind '" + ts.SyntaxKind[node.kind] + "' could not be generated."); | ||
} | ||
@@ -957,0 +962,0 @@ }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ts = require("typescript"); | ||
var path = require("path"); | ||
var transform_1 = require("./transform"); | ||
var Host = /** @class */ (function () { | ||
@@ -22,3 +22,5 @@ function Host(files, options, setParentNodes) { | ||
var result = []; | ||
var path = transform_1.getPathModule(); | ||
this.outputs.forEach(function (text, name) { | ||
name = name.split("" + process.cwd() + path.sep).join(''); | ||
result.push({ name: name, text: text }); | ||
@@ -47,8 +49,11 @@ }); | ||
Host.prototype.getDefaultLibFileName = function (options) { | ||
var path = transform_1.getPathModule(); | ||
return path.join(path.resolve(path.dirname(this.defaultLibFileName)), path.basename(this.defaultLibFileName)); | ||
}; | ||
Host.prototype.getDefaultLibLocation = function () { | ||
var path = transform_1.getPathModule(); | ||
return path.resolve(this.defaultLibLocation); | ||
}; | ||
Host.prototype.getCurrentDirectory = function () { | ||
var path = transform_1.getPathModule(); | ||
return path.resolve(this.currentDirectory); | ||
@@ -55,0 +60,0 @@ }; |
@@ -113,3 +113,5 @@ "use strict"; | ||
this.updateConstructor(members, constructor, statements); | ||
members.unshift(this.factory.classTypeParameterSymbolPropertyDeclaration(node.name)); | ||
if (hasTypeParameters) { | ||
members.unshift(this.factory.classTypeParameterSymbolPropertyDeclaration(node.name)); | ||
} | ||
return members; | ||
@@ -116,0 +118,0 @@ }; |
@@ -13,5 +13,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var path = require("path"); | ||
var ts = require("typescript"); | ||
var util = require("../util"); | ||
var transform_1 = require("../transform"); | ||
var Mutator_1 = require("./Mutator"); | ||
@@ -26,2 +26,3 @@ var SourceFileMutator = /** @class */ (function (_super) { | ||
SourceFileMutator.prototype.mutate = function (node) { | ||
var path = transform_1.getPathModule(); | ||
var statements = util.arrayFromNodeArray(node.statements); | ||
@@ -28,0 +29,0 @@ var declarations = []; |
{ | ||
"name": "ts-runtime", | ||
"version": "0.1.27", | ||
"version": "0.1.28", | ||
"description": "Runtime type checks for TypeScript", | ||
@@ -16,4 +16,5 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=test ./node_modules/mocha/bin/_mocha", | ||
"cover": "NODE_ENV=test ./node_modules/.bin/nyc --reporter=text npm test", | ||
"test": "NODE_ENV=test TS_NODE_CACHE=false ./node_modules/mocha/bin/_mocha", | ||
"cover": "NODE_ENV=test TS_NODE_CACHE=false ./node_modules/.bin/nyc npm test", | ||
"coveralls": "./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls", | ||
"build": "./node_modules/.bin/tsc --rootDir src --outDir dist --module CommonJS --target es5 --skipLibCheck && cp package.json dist/package.json && cp README.md dist/README.md && cp LICENSE dist/LICENSE", | ||
@@ -26,19 +27,2 @@ "build:docs": "node_modules/.bin/gulp build", | ||
}, | ||
"nyc": { | ||
"extensions": [ | ||
".ts" | ||
], | ||
"include": [ | ||
"src/*.ts", | ||
"src/**/*.ts" | ||
], | ||
"exclude": [ | ||
"**/*.d.ts" | ||
], | ||
"require": [ | ||
"ts-node/register" | ||
], | ||
"sourceMap": true, | ||
"instrument": true | ||
}, | ||
"dependencies": { | ||
@@ -50,2 +34,3 @@ "chalk": "^2.0.1", | ||
"ora": "^1.2.0", | ||
"path-browserify": "^0.0.0", | ||
"pretty-time": "^0.2.0", | ||
@@ -64,2 +49,3 @@ "regenerator-runtime": "^0.10.5", | ||
"@types/rimraf": "^0.0.28", | ||
"coveralls": "^2.13.1", | ||
"css-loader": "^0.28.4", | ||
@@ -66,0 +52,0 @@ "del": "^3.0.0", |
@@ -0,1 +1,4 @@ | ||
[![Build Status](https://travis-ci.org/fabiandev/ts-runtime.svg?branch=master)](https://travis-ci.org/fabiandev/ts-runtime) | ||
[![Coverage Status](https://coveralls.io/repos/github/fabiandev/ts-runtime/badge.svg?branch=master)](https://coveralls.io/github/fabiandev/ts-runtime?branch=master) | ||
# ts-runtime | ||
@@ -2,0 +5,0 @@ |
@@ -31,2 +31,5 @@ import * as ts from 'typescript'; | ||
getSymbol(type: ts.Type, node: ts.Node): ts.Symbol; | ||
/** | ||
* Take a look at src/transform.ts to see, how to obtain all ambient and external declarations recursively | ||
*/ | ||
getDeclarations(): TsrDeclaration[]; | ||
@@ -33,0 +36,0 @@ getIdentifiers(sf: ts.SourceFile): Set<string>; |
@@ -5,3 +5,3 @@ "use strict"; | ||
var util = require("./util"); | ||
var path = require("path"); | ||
var transform_1 = require("./transform"); | ||
var Scanner = /** @class */ (function () { | ||
@@ -110,2 +110,5 @@ function Scanner(program, options) { | ||
}; | ||
/** | ||
* Take a look at src/transform.ts to see, how to obtain all ambient and external declarations recursively | ||
*/ | ||
Scanner.prototype.getDeclarations = function () { | ||
@@ -124,2 +127,3 @@ return this.declarations; | ||
Scanner.prototype.pathIsExternal = function (fileName) { | ||
var path = transform_1.getPathModule(); | ||
var r = this.program.getCompilerOptions().rootDir; | ||
@@ -126,0 +130,0 @@ var rootDir = !r ? '' : r + path.sep; |
@@ -5,2 +5,3 @@ import { FileReflection } from './host'; | ||
export declare function transformReflection(rootNames: string | string[], reflections: FileReflection[], options?: Options): FileReflection[]; | ||
export declare function getPathModule(): any; | ||
export declare function getOptions(options?: Options): Options; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var path = require("path"); | ||
var format = require("pretty-time"); | ||
@@ -15,2 +14,4 @@ var rimraf = require("rimraf"); | ||
var scanner_1 = require("./scanner"); | ||
var pathReflection = require('path-browserify'); | ||
var path = require('path'); | ||
var start, elapsed; | ||
@@ -22,5 +23,10 @@ function transform(rootNames, options) { | ||
function transformReflection(rootNames, reflections, options) { | ||
path = pathReflection; | ||
return result(transformProgram(rootNames, options, reflections)); | ||
} | ||
exports.transformReflection = transformReflection; | ||
function getPathModule() { | ||
return path; | ||
} | ||
exports.getPathModule = getPathModule; | ||
function getOptions(options) { | ||
@@ -27,0 +33,0 @@ if (options === void 0) { options = {}; } |
@@ -14,3 +14,3 @@ import * as ts from 'typescript'; | ||
export declare function hasFlag(node: ts.Node | ts.Symbol, flag: ts.NodeFlags | ts.SymbolFlags): boolean; | ||
export declare function setParent(node: ts.Node): void; | ||
export declare function setParent(node: ts.Node): ts.Node; | ||
export declare function isAnyKeyword(node: ts.Node): boolean; | ||
@@ -30,3 +30,3 @@ export declare function isSynthesized(node: ts.Node): boolean; | ||
export declare function isBindingName(node: ts.Node): node is ts.Identifier | ts.BindingPattern | ts.ArrayBindingPattern; | ||
export declare function isLiteral(node: ts.Node): node is ts.LiteralTypeNode | ts.NumericLiteral | ts.StringLiteral | ts.KeywordTypeNode; | ||
export declare function isLiteral(node: ts.Node): node is ts.LiteralTypeNode | ts.NumericLiteral | ts.BooleanLiteral | ts.StringLiteral; | ||
export declare function getHash(str: string): number; | ||
@@ -33,0 +33,0 @@ export declare function getHashedDeclarationName(name: string, fileName: string): string; |
13
util.js
@@ -70,3 +70,3 @@ "use strict"; | ||
if (!node) | ||
return; | ||
return node; | ||
ts.forEachChild(node, function (n) { | ||
@@ -76,2 +76,3 @@ n.parent = node; | ||
}); | ||
return node; | ||
} | ||
@@ -86,2 +87,3 @@ exports.setParent = setParent; | ||
} | ||
return false; | ||
} | ||
@@ -253,9 +255,10 @@ exports.isAnyKeyword = isAnyKeyword; | ||
while (ts.isPropertyAccessExpression(node)) { | ||
text += node.name; | ||
text += "." + node.name.text; | ||
node = node.expression; | ||
} | ||
if (text.length > 0) { | ||
return text; | ||
if (node.kind !== ts.SyntaxKind.Identifier) { | ||
throw new errors_1.ProgramError('Can\'t get text of property access expression that contains other expressions than property access expression.'); | ||
} | ||
throw new errors_1.ProgramError('Can\'t get text of property access expression that contains other expressions than property access expression.'); | ||
text = "" + node.text + text; | ||
return text; | ||
} | ||
@@ -262,0 +265,0 @@ exports.getPropertyAccessExpressionTextOrFail = getPropertyAccessExpressionTextOrFail; |
235558
5026
641
10
32
+ Addedpath-browserify@^0.0.0
+ Addedpath-browserify@0.0.0(transitive)