Comparing version 1.4.3 to 1.5.0
123
dist/_bin.js
@@ -9,3 +9,2 @@ "use strict"; | ||
var chalk = require('chalk'); | ||
var diff_1 = require('diff'); | ||
var vm_1 = require('vm'); | ||
@@ -85,9 +84,2 @@ var index_1 = require('./index'); | ||
} | ||
var _emit = process.emit; | ||
process.emit = function (type, error) { | ||
if (type === 'uncaughtException' && error instanceof index_1.TSError && process.listeners(type).length === 0) { | ||
printAndExit(error); | ||
} | ||
return _emit.apply(this, arguments); | ||
}; | ||
var cwd = process.cwd(); | ||
@@ -98,2 +90,3 @@ var code = argv.eval == null ? argv.print : argv.eval; | ||
var isPrinted = argv.print != null; | ||
var supportsScriptOptions = parseFloat(process.version.substr(1)) >= 1; | ||
var service = index_1.register({ | ||
@@ -113,5 +106,4 @@ fast: argv.fast, | ||
}); | ||
var EVAL_FILENAME = '[eval].ts'; | ||
var EVAL_PATH = path_1.join(cwd, EVAL_FILENAME); | ||
var evalFile = { input: '', output: '', version: 0 }; | ||
var evalId = 0; | ||
var EVAL_PATHS = {}; | ||
for (var _i = 0, _a = arrify(argv.require); _i < _a.length; _i++) { | ||
@@ -144,8 +136,8 @@ var id = _a[_i]; | ||
function evalAndExit(code, isPrinted) { | ||
; | ||
global.__filename = EVAL_FILENAME; | ||
var filename = getEvalFileName(evalId); | ||
var module = new Module(filename); | ||
module.filename = filename; | ||
module.paths = Module._nodeModulePaths(cwd); | ||
global.__filename = filename; | ||
global.__dirname = cwd; | ||
var module = new Module(global.__filename); | ||
module.filename = global.__filename; | ||
module.paths = Module._nodeModulePaths(global.__dirname); | ||
global.exports = module.exports; | ||
@@ -160,3 +152,4 @@ global.module = module; | ||
if (error instanceof index_1.TSError) { | ||
printAndExit(error); | ||
console.error(print(error)); | ||
process.exit(1); | ||
} | ||
@@ -171,36 +164,16 @@ throw error; | ||
function print(error) { | ||
return chalk.bold(chalk.red('⨯') + " Unable to compile TypeScript") + ("\n" + error.diagnostics.join('\n')); | ||
var title = chalk.red('⨯') + " Unable to compile TypeScript"; | ||
return chalk.bold(title) + "\n" + error.diagnostics.map(function (x) { return x.message; }).join('\n'); | ||
} | ||
function printAndExit(error) { | ||
console.error(print(error)); | ||
process.exit(1); | ||
} | ||
function _eval(code, context) { | ||
var undo = evalFile.input; | ||
var isCompletion = !/\n$/.test(code); | ||
evalFile.input += code; | ||
evalFile.version++; | ||
var output; | ||
try { | ||
output = service().compile(evalFile.input, EVAL_PATH); | ||
function _eval(input, context) { | ||
var isCompletion = !/\n$/.test(input); | ||
var path = path_1.join(cwd, getEvalFileName(evalId++)); | ||
var _a = getEvalContent(input), code = _a.code, lineOffset = _a.lineOffset; | ||
var filename = path_1.basename(path); | ||
var output = service().compile(code, path, lineOffset); | ||
var script = vm_1.createScript(output, supportsScriptOptions ? { filename: filename, lineOffset: lineOffset } : filename); | ||
var result = script.runInNewContext(context); | ||
if (!isCompletion) { | ||
EVAL_PATHS[path] = code; | ||
} | ||
catch (error) { | ||
evalFile.input = undo; | ||
throw error; | ||
} | ||
var changes = diff_1.diffLines(evalFile.output, output); | ||
if (isCompletion) { | ||
evalFile.input = undo; | ||
} | ||
else { | ||
evalFile.output = output; | ||
} | ||
var result; | ||
for (var _i = 0, changes_1 = changes; _i < changes_1.length; _i++) { | ||
var change = changes_1[_i]; | ||
if (change.added) { | ||
var script = vm_1.createScript(change.value, EVAL_FILENAME); | ||
result = script.runInNewContext(context); | ||
} | ||
} | ||
return result; | ||
@@ -216,7 +189,2 @@ } | ||
}); | ||
repl.on('reset', function () { | ||
evalFile.input = ''; | ||
evalFile.output = ''; | ||
evalFile.version = 0; | ||
}); | ||
repl.defineCommand('type', { | ||
@@ -226,13 +194,12 @@ help: 'Check the type of a TypeScript identifier', | ||
if (!identifier) { | ||
; | ||
repl.displayPrompt(); | ||
return; | ||
} | ||
var undo = evalFile.input; | ||
evalFile.input += identifier; | ||
evalFile.version++; | ||
var _a = service().getTypeInfo(EVAL_PATH, evalFile.input.length), name = _a.name, comment = _a.comment; | ||
var path = path_1.join(cwd, getEvalFileName(evalId++)); | ||
var _a = getEvalContent(identifier), code = _a.code, lineOffset = _a.lineOffset; | ||
EVAL_PATHS[path] = code; | ||
var _b = service().getTypeInfo(path, code.length), name = _b.name, comment = _b.comment; | ||
delete EVAL_PATHS[path]; | ||
repl.outputStream.write(chalk.bold(name) + "\n" + (comment ? comment + "\n" : '')); | ||
repl.displayPrompt(); | ||
evalFile.input = undo; | ||
} | ||
@@ -253,3 +220,8 @@ }); | ||
if (error instanceof index_1.TSError) { | ||
err = print(error); | ||
if (typeof repl_1.Recoverable === 'function' && isRecoverable(error)) { | ||
err = new repl_1.Recoverable(error); | ||
} | ||
else { | ||
err = print(error); | ||
} | ||
} | ||
@@ -262,8 +234,29 @@ else { | ||
} | ||
function getFileEval(fileName) { | ||
return fileName === EVAL_PATH ? evalFile.input : index_1.getFile(fileName); | ||
function getFileEval(path) { | ||
return EVAL_PATHS.hasOwnProperty(path) ? EVAL_PATHS[path] : index_1.getFile(path); | ||
} | ||
function fileExistsEval(fileName) { | ||
return fileName === EVAL_PATH ? true : index_1.fileExists(fileName); | ||
function fileExistsEval(path) { | ||
return EVAL_PATHS.hasOwnProperty(path) || index_1.fileExists(path); | ||
} | ||
function getEvalContent(input) { | ||
var refs = Object.keys(EVAL_PATHS).map(function (x) { return ("/// <reference path=\"" + x + "\" />\n"); }); | ||
return { | ||
lineOffset: -refs.length, | ||
code: refs.join('') + input | ||
}; | ||
} | ||
function getEvalFileName(index) { | ||
return "[eval " + index + "].ts"; | ||
} | ||
var RECOVERY_CODES = [ | ||
1003, | ||
1005, | ||
1109, | ||
1126, | ||
1160, | ||
1161 | ||
]; | ||
function isRecoverable(error) { | ||
return error.diagnostics.every(function (x) { return RECOVERY_CODES.indexOf(x.code) > -1; }); | ||
} | ||
//# sourceMappingURL=_bin.js.map |
@@ -45,3 +45,3 @@ import { BaseError } from 'make-error'; | ||
cwd: string; | ||
compile(code: string, fileName: string): string; | ||
compile(code: string, fileName: string, lineOffset?: number): string; | ||
getTypeInfo(fileName: string, position: number): TypeInfo; | ||
@@ -54,6 +54,12 @@ } | ||
export declare function getFile(fileName: string): string; | ||
export declare function formatDiagnostics(diagnostics: TS.Diagnostic[], cwd: string, ts: TSCommon, lineOffset: number): TSDiagnostic[]; | ||
export interface TSDiagnostic { | ||
message: string; | ||
code: number; | ||
} | ||
export declare function formatDiagnostic(diagnostic: TS.Diagnostic, cwd: string, ts: TSCommon, lineOffset: number): TSDiagnostic; | ||
export declare class TSError extends BaseError { | ||
diagnostics: TSDiagnostic[]; | ||
name: string; | ||
diagnostics: string[]; | ||
constructor(diagnostics: string[]); | ||
constructor(diagnostics: TSDiagnostic[]); | ||
} |
@@ -77,7 +77,7 @@ "use strict"; | ||
var config = readConfig(compilerOptions, project, cwd, ts); | ||
var configDiagnostics = formatDiagnostics(config.errors, ignoreWarnings, disableWarnings, cwd, ts); | ||
var configDiagnostics = filterDiagnostics(config.errors, ignoreWarnings, disableWarnings); | ||
var cachedir = path_1.join(path_1.resolve(cwd, cacheDirectory), getCompilerDigest({ version: ts.version, fast: fast, ignoreWarnings: ignoreWarnings, disableWarnings: disableWarnings, config: config, compiler: compiler })); | ||
mkdirp.sync(cachedir); | ||
if (configDiagnostics.length) { | ||
throw new TSError(configDiagnostics); | ||
throw new TSError(formatDiagnostics(configDiagnostics, cwd, ts, 0)); | ||
} | ||
@@ -99,3 +99,4 @@ if (config.options.allowJs) { | ||
} | ||
var getOutput = function (code, fileName) { | ||
var getOutput = function (code, fileName, lineOffset) { | ||
if (lineOffset === void 0) { lineOffset = 0; } | ||
var result = ts.transpileModule(code, { | ||
@@ -107,6 +108,6 @@ fileName: fileName, | ||
var diagnosticList = result.diagnostics ? | ||
formatDiagnostics(result.diagnostics, ignoreWarnings, disableWarnings, cwd, ts) : | ||
filterDiagnostics(result.diagnostics, ignoreWarnings, disableWarnings) : | ||
[]; | ||
if (diagnosticList.length) { | ||
throw new TSError(diagnosticList); | ||
throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset)); | ||
} | ||
@@ -149,3 +150,4 @@ return [result.outputText, result.sourceMapText]; | ||
var service_1 = ts.createLanguageService(serviceHost); | ||
getOutput = function (code, fileName) { | ||
getOutput = function (code, fileName, lineOffset) { | ||
if (lineOffset === void 0) { lineOffset = 0; } | ||
var output = service_1.getEmitOutput(fileName); | ||
@@ -155,9 +157,9 @@ var diagnostics = service_1.getCompilerOptionsDiagnostics() | ||
.concat(service_1.getSemanticDiagnostics(fileName)); | ||
var diagnosticList = formatDiagnostics(diagnostics, ignoreWarnings, disableWarnings, cwd, ts); | ||
var diagnosticList = filterDiagnostics(diagnostics, ignoreWarnings, disableWarnings); | ||
if (diagnosticList.length) { | ||
throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset)); | ||
} | ||
if (output.emitSkipped) { | ||
diagnosticList.push(path_1.relative(cwd, fileName) + ": Emit skipped"); | ||
throw new TypeError(path_1.relative(cwd, fileName) + ": Emit skipped"); | ||
} | ||
if (diagnosticList.length) { | ||
throw new TSError(diagnosticList); | ||
} | ||
if (output.outputFiles.length === 0) { | ||
@@ -167,10 +169,11 @@ throw new TypeError('Unable to require `.d.ts` file.\n' + | ||
'Make sure there is a `.js`, `.json` or another executable extension and ' + | ||
("loader (attached before `ts-node`) available alongside `" + fileName + "`.")); | ||
'loader (attached before `ts-node`) available alongside ' + | ||
("`" + path_1.basename(fileName) + "`.")); | ||
} | ||
return [output.outputFiles[1].text, output.outputFiles[0].text]; | ||
}; | ||
compile = readThrough(cachedir, shouldCache, getFile, fileExists, cache, function (code, fileName) { | ||
compile = readThrough(cachedir, shouldCache, getFile, fileExists, cache, function (code, fileName, lineOffset) { | ||
addVersion_1(fileName); | ||
addCache_1(code, fileName); | ||
return getOutput(code, fileName); | ||
return getOutput(code, fileName, lineOffset); | ||
}, getExtension); | ||
@@ -185,3 +188,3 @@ getTypeInfo = function (fileName, position) { | ||
} | ||
return { cwd: cwd, compile: compile, getOutput: getOutput, getTypeInfo: getTypeInfo }; | ||
return { cwd: cwd, compile: compile, getTypeInfo: getTypeInfo }; | ||
} | ||
@@ -243,7 +246,7 @@ function service() { | ||
if (shouldCache === false) { | ||
return function (code, fileName) { | ||
return function (code, fileName, lineOffset) { | ||
var cachePath = path_1.join(cachedir, getCacheName(code, fileName)); | ||
var extension = getExtension(fileName); | ||
var sourceMapPath = "" + cachePath + extension + ".map"; | ||
var out = compile(code, fileName); | ||
var out = compile(code, fileName, lineOffset); | ||
cache.sourceMaps[fileName] = sourceMapPath; | ||
@@ -256,3 +259,3 @@ var output = updateOutput(out[0], fileName, extension, sourceMapPath); | ||
} | ||
return function (code, fileName) { | ||
return function (code, fileName, lineOffset) { | ||
var cachePath = path_1.join(cachedir, getCacheName(code, fileName)); | ||
@@ -266,3 +269,3 @@ var extension = getExtension(fileName); | ||
} | ||
var out = compile(code, fileName); | ||
var out = compile(code, fileName, lineOffset); | ||
var output = updateOutput(out[0], fileName, extension, sourceMapPath); | ||
@@ -324,29 +327,30 @@ var sourceMap = updateSourceMap(out[1], fileName); | ||
exports.getFile = getFile; | ||
function formatDiagnostics(diagnostics, ignore, disable, cwd, ts) { | ||
function filterDiagnostics(diagnostics, ignore, disable) { | ||
if (disable) { | ||
return []; | ||
} | ||
return diagnostics | ||
.filter(function (diagnostic) { | ||
return ignore.indexOf(diagnostic.code) === -1; | ||
}) | ||
.map(function (diagnostic) { | ||
return formatDiagnostic(diagnostic, cwd, ts); | ||
}); | ||
return diagnostics.filter(function (x) { return ignore.indexOf(x.code); }); | ||
} | ||
function formatDiagnostic(diagnostic, cwd, ts) { | ||
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | ||
function formatDiagnostics(diagnostics, cwd, ts, lineOffset) { | ||
return diagnostics.map(function (x) { return formatDiagnostic(x, cwd, ts, lineOffset); }); | ||
} | ||
exports.formatDiagnostics = formatDiagnostics; | ||
function formatDiagnostic(diagnostic, cwd, ts, lineOffset) { | ||
var messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | ||
if (diagnostic.file) { | ||
var path = path_1.relative(cwd, diagnostic.file.fileName); | ||
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; | ||
return path + " (" + (line + 1) + "," + (character + 1) + "): " + message + " (" + diagnostic.code + ")"; | ||
var message = path + " (" + (line + 1 + lineOffset) + "," + (character + 1) + "): " + messageText + " (" + diagnostic.code + ")"; | ||
return { message: message, code: diagnostic.code }; | ||
} | ||
return message + " (" + diagnostic.code + ")"; | ||
return { message: messageText + " (" + diagnostic.code + ")", code: diagnostic.code }; | ||
} | ||
exports.formatDiagnostic = formatDiagnostic; | ||
var TSError = (function (_super) { | ||
__extends(TSError, _super); | ||
function TSError(diagnostics) { | ||
_super.call(this, "\u2A2F Unable to compile TypeScript\n" + diagnostics.join('\n')); | ||
_super.call(this, "\u2A2F Unable to compile TypeScript\n" + diagnostics.map(function (x) { return x.message; }).join('\n')); | ||
this.diagnostics = diagnostics; | ||
this.name = 'TSError'; | ||
this.diagnostics = diagnostics; | ||
this.stack = ''; | ||
} | ||
@@ -353,0 +357,0 @@ return TSError; |
@@ -61,3 +61,3 @@ "use strict"; | ||
child_process_1.exec(BIN_EXEC + " -e \"import * as m from './tests/module';console.log(m.example(123))\"", function (err) { | ||
chai_1.expect(err.message).to.match(new RegExp('\\[eval\\]\\.ts \\(1,59\\): Argument of type \'(?:number|123)\' ' + | ||
chai_1.expect(err.message).to.match(new RegExp('\\[eval [01]\\]\\.ts \\(1,59\\): Argument of type \'(?:number|123)\' ' + | ||
'is not assignable to parameter of type \'string\'\\. \\(2345\\)')); | ||
@@ -64,0 +64,0 @@ return done(); |
{ | ||
"name": "ts-node", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"preferGlobal": true, | ||
@@ -62,3 +62,2 @@ "description": "TypeScript execution environment and REPL for node", | ||
"chalk": "^1.1.1", | ||
"diff": "^3.0.0", | ||
"make-error": "^1.1.1", | ||
@@ -65,0 +64,0 @@ "minimist": "^1.2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115384
10
916
- Removeddiff@^3.0.0
- Removeddiff@3.5.0(transitive)