Skip to content

Commit

Permalink
Use ts.formatDiagnostics over of custom function (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed May 26, 2018
1 parent e2e3eaf commit 2e44bc0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 118 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -127,6 +127,7 @@ _Environment variable denoted in parentheses._
* `-C, --compiler [name]` Specify a custom TypeScript compiler (`TS_NODE_COMPILER`)
* `-D, --ignoreDiagnostics [code]` Ignore TypeScript warnings by diagnostic code (`TS_NODE_IGNORE_DIAGNOSTICS`)
* `-O, --compilerOptions [opts]` JSON object to merge with compiler options (`TS_NODE_COMPILER_OPTIONS`)
* `--pretty` Use pretty diagnostic formatter (`TS_NODE_PRETTY`)
* `--no-cache` Disable the local TypeScript Node cache (`TS_NODE_CACHE`)
* `--skip-project` Skip project config resolution and loading (`TS_NODE_SKIP_PROJECT`)
* `--skip-ignore` Skip ignore checks (`TS_NODE_SKIP_IGNORE`)
Expand Down
31 changes: 22 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -70,16 +70,15 @@
"semver": "^5.1.0",
"tslint": "^5.0.0",
"tslint-config-standard": "^7.0.0",
"typescript": "^2.8.1"
"typescript": "^2.8.3"
},
"dependencies": {
"arrify": "^1.0.0",
"chalk": "^2.3.0",
"diff": "^3.1.0",
"make-error": "^1.1.1",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"source-map-support": "^0.5.3",
"source-map-support": "^0.5.6",
"yn": "^2.0.0"
}
}
23 changes: 13 additions & 10 deletions src/bin.ts
Expand Up @@ -6,11 +6,10 @@ import { inspect } from 'util'
import arrify = require('arrify')
import Module = require('module')
import minimist = require('minimist')
import chalk from 'chalk'
import { diffLines } from 'diff'
import { Script } from 'vm'
import { readFileSync, statSync } from 'fs'
import { register, VERSION, DEFAULTS, TSError, parse, printError } from './index'
import { register, VERSION, DEFAULTS, TSError, parse } from './index'

interface Argv {
// Node.js-like options.
Expand All @@ -21,6 +20,7 @@ interface Argv {
help?: boolean
version?: boolean
// Register options.
pretty?: boolean
typeCheck?: boolean
transpileOnly?: boolean
cache?: boolean
Expand All @@ -38,7 +38,7 @@ interface Argv {
const argv = minimist<Argv>(process.argv.slice(2), {
stopEarly: true,
string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'cacheDirectory', 'ignore'],
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'cache', 'skipProject', 'skipIgnore'],
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'cache', 'pretty', 'skipProject', 'skipIgnore'],
alias: {
eval: ['e'],
print: ['p'],
Expand Down Expand Up @@ -86,6 +86,7 @@ Options:
-D, --ignoreDiagnostics [code] Ignore TypeScript warnings by diagnostic code
-O, --compilerOptions [opts] JSON object to merge with compiler options
--pretty Use pretty diagnostic formatter
--no-cache Disable the local TypeScript Node cache
--skip-project Skip reading \`tsconfig.json\`
--skip-ignore Skip \`--ignore\` checks
Expand All @@ -101,6 +102,7 @@ const isPrinted = argv.print !== undefined

// Register the TypeScript compiler instance.
const service = register({
pretty: argv.pretty,
typeCheck: argv.typeCheck,
transpileOnly: argv.transpileOnly,
cache: argv.cache,
Expand Down Expand Up @@ -175,7 +177,7 @@ function evalAndExit (code: string, isPrinted: boolean) {
result = _eval(code)
} catch (error) {
if (error instanceof TSError) {
console.error(printError(error))
console.error(error.diagnosticText)
process.exit(1)
}

Expand Down Expand Up @@ -265,7 +267,7 @@ function startRepl () {

undo()

repl.outputStream.write(`${chalk.bold(name)}\n${comment ? `${comment}\n` : ''}`)
repl.outputStream.write(`${name}\n${comment ? `${comment}\n` : ''}`)
repl.displayPrompt()
}
})
Expand All @@ -275,7 +277,7 @@ function startRepl () {
* Eval code from the REPL.
*/
function replEval (code: string, _context: any, _filename: string, callback: (err?: Error, result?: any) => any) {
let err: any
let err: Error | undefined
let result: any

// TODO: Figure out how to handle completion here.
Expand All @@ -292,7 +294,8 @@ function replEval (code: string, _context: any, _filename: string, callback: (er
if (Recoverable && isRecoverable(error)) {
err = new Recoverable(error)
} else {
err = printError(error)
console.error(error.diagnosticText)
err = undefined
}
} else {
err = error
Expand Down Expand Up @@ -368,18 +371,18 @@ function fileExistsEval (path: string) {
}
}

const RECOVERY_CODES: number[] = [
const RECOVERY_CODES: Set<number> = new Set([
1003, // "Identifier expected."
1005, // "')' expected."
1109, // "Expression expected."
1126, // "Unexpected end of text."
1160, // "Unterminated template literal."
1161 // "Unterminated regular expression literal."
]
])

/**
* Check if a function can recover gracefully.
*/
function isRecoverable (error: TSError) {
return error.diagnostics.every(x => RECOVERY_CODES.indexOf(x.code) > -1)
return error.diagnosticCodes.every(code => RECOVERY_CODES.has(code))
}
5 changes: 2 additions & 3 deletions src/index.spec.ts
Expand Up @@ -122,9 +122,8 @@ describe('ts-node', function () {
}

expect(err.message).to.match(new RegExp(
// Node 0.10 can not override the `lineOffset` option.
'\\[eval\\]\\.ts \\(1,59\\): Argument of type \'(?:number|123)\' ' +
'is not assignable to parameter of type \'string\'\\. \\(2345\\)'
'TS2345: Argument of type \'(?:number|123)\' ' +
'is not assignable to parameter of type \'string\'\\.'
))

return done()
Expand Down

0 comments on commit 2e44bc0

Please sign in to comment.