ts-runtime
Advanced tools
Comparing version 0.1.23 to 0.1.24
@@ -18,3 +18,3 @@ import * as ts from 'typescript'; | ||
private _commonDir; | ||
constructor(sourceFile: ts.SourceFile, options: Options, program: ts.Program, host: ts.CompilerHost, scanner: Scanner, context: ts.TransformationContext, entryFiles: string[], commonDir: string); | ||
constructor(sourceFile: ts.SourceFile, options: Options, program: ts.Program, host: ts.CompilerHost, scanner: Scanner, entryFiles: string[], commonDir: string, context?: ts.TransformationContext); | ||
skip<T extends ts.Node>(node: T, recursive?: boolean, ...exclude: ts.Node[]): T; | ||
@@ -21,0 +21,0 @@ shouldSkip(node: ts.Node): boolean; |
@@ -8,3 +8,3 @@ "use strict"; | ||
class MutationContext { | ||
constructor(sourceFile, options, program, host, scanner, context, entryFiles, commonDir) { | ||
constructor(sourceFile, options, program, host, scanner, entryFiles, commonDir, context) { | ||
this._skipNodes = []; | ||
@@ -55,3 +55,3 @@ this._sourceFile = sourceFile; | ||
isDeclared(node) { | ||
node = util.getIdentifierOfEntityName(node); | ||
node = util.getBaseIdentifierOfEntityName(node); | ||
const typeInfo = this.scanner.getTypeInfo(node); | ||
@@ -67,3 +67,3 @@ const fileName = typeInfo.fileName; | ||
wasDeclared(node) { | ||
node = util.getIdentifierOfEntityName(node); | ||
node = util.getBaseIdentifierOfEntityName(node); | ||
const typeInfo = this.scanner.getTypeInfo(node); | ||
@@ -107,5 +107,8 @@ const fileName = typeInfo.fileName; | ||
next = next.parent; | ||
if (ts.isHeritageClause(node)) { | ||
return false; | ||
} | ||
if (ts.isClassDeclaration(next) || ts.isInterfaceDeclaration(next) || ts.isTypeAliasDeclaration(next)) { | ||
const symbol = this.scanner.getNodeSymbol(next.name); | ||
if (typeInfo.symbol === symbol) { | ||
if (typeInfo.symbol === symbol && next.name.text === util.getIdentifierOfEntityName(node.typeName).text) { | ||
return true; | ||
@@ -112,0 +115,0 @@ } |
@@ -21,3 +21,3 @@ import * as ts from 'typescript'; | ||
private warn(warning); | ||
typeDeclaration(name: string | ts.Identifier | ts.ObjectBindingPattern | ts.ArrayBindingPattern, node: ts.TypeNode): ts.VariableDeclaration; | ||
typeDeclaration(name: string | ts.Identifier, node: ts.TypeNode): ts.VariableDeclaration; | ||
typeAssertion(id: string | ts.Expression, args?: ts.Expression | ts.Expression[]): ts.CallExpression; | ||
@@ -24,0 +24,0 @@ typeReflectionAndAssertion(node: ts.TypeNode, args?: ts.Expression | ts.Expression[]): ts.CallExpression; |
@@ -68,2 +68,3 @@ "use strict"; | ||
return this.undefinedTypeReflection(); | ||
case ts.SyntaxKind.ThisKeyword: | ||
case ts.SyntaxKind.ThisType: | ||
@@ -125,3 +126,3 @@ return this.thisTypeReflection(); | ||
} | ||
typeDeclaration(name, node) { | ||
typeDeclaration(name /* | ts.ObjectBindingPattern | ts.ArrayBindingPattern*/, node) { | ||
return ts.createVariableDeclaration(name, undefined, this.typeReflection(node)); | ||
@@ -128,0 +129,0 @@ } |
import * as ts from 'typescript'; | ||
export interface Options { | ||
[index: string]: any; | ||
libDeclarations?: boolean; | ||
@@ -4,0 +5,0 @@ noAnnotate?: boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ts = require("typescript"); | ||
exports.defaultOptions = { | ||
noAnnotate: false, | ||
compilerOptions: { | ||
moduleResolution: ts.ModuleResolutionKind.NodeJs, | ||
module: ts.ModuleKind.ES2015, | ||
target: ts.ScriptTarget.ES2015, | ||
lib: ["lib.es2015.d.ts"], | ||
strictNullChecks: true, | ||
experimentalDecorators: true, | ||
emitDecoratorMetadata: true, | ||
sourceMap: false, | ||
removeComments: true, | ||
preserveConstEnums: true, | ||
experimentalDecorators: true | ||
}, | ||
@@ -18,0 +9,0 @@ libDeclarations: false, |
{ | ||
"name": "ts-runtime", | ||
"version": "0.1.23", | ||
"version": "0.1.24", | ||
"description": "Runtime type checks for TypeScript", | ||
@@ -16,6 +16,7 @@ "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", | ||
"build": "./node_modules/.bin/tsc --rootDir src --outDir dist --module CommonJS --target es6 --skipLibCheck && cp package.json dist/package.json && cp README.md dist/README.md && cp LICENSE dist/LICENSE", | ||
"build:docs": "node_modules/.bin/rimraf docs/*worker*.js* && node_modules/.bin/webpack", | ||
"build:tsr": "ts-node ./src/bin/index src/index src/lib/index src/bin/index src/bin/process --moduleAlias --compilerOptions '{\"outDir\": \"dist-tsr\", \"strictNullChecks\": false, \"moduleResolution\": \"Node\", \"module\": \"commonjs\", \"target\": \"ES2015\", \"lib\": [\"ESNext\"]}'", | ||
"build:tsr:fast": "ts-node ./src/bin/index src/index src/lib/index src/bin/index src/bin/process --fast --moduleAlias --compilerOptions '{\"outDir\": \"dist-tsr\", \"strictNullChecks\": false, \"moduleResolution\": \"Node\", \"module\": \"commonjs\", \"target\": \"ES2015\", \"lib\": [\"ESNext\"]}'" | ||
"build:docs": "node_modules/.bin/gulp build", | ||
"build:tsr": "ts-node ./src/bin/index src/index src/lib/index src/bin/index src/bin/process --moduleAlias --compilerOptions '{\"outDir\": \"dist-tsr\", \"strictNullChecks\": false, \"moduleResolution\": \"Node\", \"module\": \"commonjs\", \"target\": \"ES2015\", \"lib\": [\"ESNext\"]}'" | ||
}, | ||
@@ -25,6 +26,18 @@ "_moduleAliases": { | ||
}, | ||
"tsr": { | ||
"docs": { | ||
"version": "0.1.23-0" | ||
} | ||
"nyc": { | ||
"extensions": [ | ||
".ts" | ||
], | ||
"include": [ | ||
"src/*.ts", | ||
"src/**/*.ts" | ||
], | ||
"exclude": [ | ||
"**/*.d.ts" | ||
], | ||
"require": [ | ||
"ts-node/register" | ||
], | ||
"sourceMap": true, | ||
"instrument": true | ||
}, | ||
@@ -45,15 +58,24 @@ "dependencies": { | ||
"@types/commander": "^2.3.31", | ||
"@types/expect.js": "^0.3.29", | ||
"@types/lodash.debounce": "^4.0.2", | ||
"@types/node": "^8.0.10", | ||
"@types/mocha": "^2.2.41", | ||
"@types/node": "^8.0.16", | ||
"@types/rimraf": "^0.0.28", | ||
"babel-core": "^6.25.0", | ||
"babel-loader": "^7.1.1", | ||
"babel-preset-es2015": "^6.24.1", | ||
"copy-webpack-plugin": "^4.0.1", | ||
"css-loader": "^0.28.4", | ||
"del": "^3.0.0", | ||
"expect.js": "^0.3.1", | ||
"gulp": "gulpjs/gulp#4.0", | ||
"gulp-clean-css": "^3.7.0", | ||
"gulp-less": "^3.3.2", | ||
"gulp-preprocess": "^2.0.0", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-sourcemaps": "^2.6.0", | ||
"lodash.debounce": "^4.0.8", | ||
"mocha": "^3.4.2", | ||
"module-alias": "^2.0.0", | ||
"monaco-editor": "^0.9.0", | ||
"monaco-typescript": "git+https://git@github.com/Microsoft/monaco-typescript", | ||
"normalize.css": "^7.0.0", | ||
"null-loader": "^0.1.1", | ||
"nyc": "^11.0.3", | ||
"raw-loader": "^0.5.1", | ||
@@ -63,3 +85,3 @@ "source-map-loader": "^0.2.1", | ||
"ts-loader": "^2.2.2", | ||
"uglifyjs-webpack-plugin": "^0.4.6", | ||
"ts-node": "^3.3.0", | ||
"webpack": "^2.2.0", | ||
@@ -66,0 +88,0 @@ "worker-loader": "^0.8.1" |
@@ -5,5 +5,5 @@ # ts-runtime | ||
Please note, that this package is in **beta** and resulting code is **not** intended to be used **for production**. | ||
It is more a **proof of concept** for adding runtime type checks by making use of the recently published TypeScript transformation API and | ||
tests have yet to be written. Feel free to report bugs and to make suggestions. Pull requests are also very welcome. | ||
Please note, that this package is still experimental and resulting code is not intended to be used in production. | ||
It is a proof of concept for adding runtime type checks by making use of the recently published TypeScript transformation API. | ||
Feel free to report bugs and to make suggestions. Pull requests are also very welcome. | ||
@@ -10,0 +10,0 @@ ## Playground |
import { FileReflection } from './host'; | ||
import { Options } from './options'; | ||
export declare function transform(entryFiles: string[], options?: Options): void; | ||
export declare function transform(rootNames: string[], options?: Options): FileReflection[]; | ||
export declare function transformReflection(rootNames: string | string[], reflections: FileReflection[], options?: Options): FileReflection[]; | ||
export declare function getOptions(options?: Options): Options; |
@@ -16,11 +16,8 @@ "use strict"; | ||
let start, elapsed; | ||
function transform(entryFiles, options) { | ||
return transformProgram(entryFiles, options); | ||
function transform(rootNames, options) { | ||
return result(transformProgram(rootNames, options)); | ||
} | ||
exports.transform = transform; | ||
function transformReflection(rootNames, reflections, options) { | ||
const host = transformProgram(rootNames, options, reflections); | ||
if (!host) | ||
return []; | ||
return host.getResult(); | ||
return result(transformProgram(rootNames, options, reflections)); | ||
} | ||
@@ -30,6 +27,12 @@ exports.transformReflection = transformReflection; | ||
const opts = Object.assign({}, options_1.defaultOptions, options); | ||
opts.compilerOptions = Object.assign({}, options_1.defaultOptions.compilerOptions, options && options.compilerOptions || {}); | ||
const compilerOptions = Object.assign(ts.getDefaultCompilerOptions(), options_1.defaultOptions.compilerOptions, options && options.compilerOptions || {}); | ||
opts.compilerOptions = compilerOptions; | ||
return opts; | ||
} | ||
exports.getOptions = getOptions; | ||
function result(host) { | ||
if (!host) | ||
return []; | ||
return host.getResult(); | ||
} | ||
function transformProgram(rootNames, options, reflections) { | ||
@@ -44,8 +47,8 @@ if (!reflections) | ||
emit(bus.events.START); | ||
let tempEntryFiles; | ||
let resolvedEntryFiles; | ||
let commonDir; | ||
let host; | ||
if (!reflections) { | ||
tempEntryFiles = entryFiles.map(f => path.resolve(f)); | ||
commonDir = commondir(tempEntryFiles.map(f => path.dirname(f))); | ||
resolvedEntryFiles = entryFiles.map(f => path.resolve(f)); | ||
commonDir = commondir(resolvedEntryFiles.map(f => path.dirname(f))); | ||
setCompilerOptions(); | ||
@@ -55,4 +58,4 @@ host = ts.createCompilerHost(options.compilerOptions, true); | ||
else { | ||
tempEntryFiles = entryFiles.map(f => path.join(`${path.resolve(path.dirname(f))}`, path.basename(f))); | ||
commonDir = commondir(tempEntryFiles.map(f => path.dirname(f))); | ||
resolvedEntryFiles = entryFiles.map(f => path.join(`${path.resolve(path.dirname(f))}`, path.basename(f))); | ||
commonDir = commondir(resolvedEntryFiles.map(f => path.dirname(f))); | ||
reflections = reflections.map(f => { | ||
@@ -75,3 +78,3 @@ return { | ||
function startTransformation() { | ||
program = ts.createProgram(tempEntryFiles, options.compilerOptions, host); | ||
program = ts.createProgram(resolvedEntryFiles, options.compilerOptions, host); | ||
const { diagnostics, optionsDiagnostics, syntacticDiagnostics } = getDiagnostics(); | ||
@@ -111,3 +114,3 @@ if (!check(diagnostics, options.log) && !options.force) { | ||
host = getHostFromTransformationResult(result); | ||
program = ts.createProgram(tempEntryFiles, options.compilerOptions, host, undefined); | ||
program = ts.createProgram(resolvedEntryFiles, options.compilerOptions, host, undefined); | ||
const { diagnostics } = program.emit(); | ||
@@ -171,3 +174,3 @@ return check(diagnostics, options.log); | ||
currentSourceFile = node; | ||
context = new context_1.MutationContext(node, options, program, host, scanner, transformationContext, tempEntryFiles, commonDir); | ||
context = new context_1.MutationContext(node, options, program, host, scanner, resolvedEntryFiles, commonDir, transformationContext); | ||
} | ||
@@ -232,2 +235,9 @@ } | ||
} | ||
if (!options.compilerOptions.experimentalDecorators) { | ||
const warning = 'Compiler option "experimentalDecorators" was enabled.'; | ||
options.compilerOptions.experimentalDecorators = true; | ||
emit(bus.events.WARN, warning); | ||
if (options.log) | ||
console.warn(warning); | ||
} | ||
} | ||
@@ -234,0 +244,0 @@ function setCompilerOptionsForReflection() { |
@@ -32,2 +32,3 @@ import * as ts from 'typescript'; | ||
export declare function getIdentifierOfEntityName(node: ts.EntityName): ts.Identifier; | ||
export declare function getBaseIdentifierOfEntityName(node: ts.EntityName): ts.Identifier; | ||
export declare function getExtendsClause(node: ts.InterfaceDeclaration | ts.ClassDeclaration): ts.HeritageClause; | ||
@@ -34,0 +35,0 @@ export declare function getImplementsClause(node: ts.InterfaceDeclaration | ts.ClassDeclaration): ts.HeritageClause; |
@@ -201,2 +201,9 @@ "use strict"; | ||
function getIdentifierOfEntityName(node) { | ||
if (ts.isIdentifier(node)) { | ||
return node; | ||
} | ||
return node.right; | ||
} | ||
exports.getIdentifierOfEntityName = getIdentifierOfEntityName; | ||
function getBaseIdentifierOfEntityName(node) { | ||
while (ts.isQualifiedName(node)) { | ||
@@ -207,3 +214,3 @@ node = node.left; | ||
} | ||
exports.getIdentifierOfEntityName = getIdentifierOfEntityName; | ||
exports.getBaseIdentifierOfEntityName = getBaseIdentifierOfEntityName; | ||
function getExtendsClause(node) { | ||
@@ -210,0 +217,0 @@ return node.heritageClauses && node.heritageClauses.find(clause => { |
3
1
205243
31
59
4532