Skip to content

Commit 2e44bc0

Browse files
authored
Use ts.formatDiagnostics over of custom function (#597)
1 parent e2e3eaf commit 2e44bc0

File tree

6 files changed

+107
-118
lines changed

6 files changed

+107
-118
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ _Environment variable denoted in parentheses._
127127
* `-C, --compiler [name]` Specify a custom TypeScript compiler (`TS_NODE_COMPILER`)
128128
* `-D, --ignoreDiagnostics [code]` Ignore TypeScript warnings by diagnostic code (`TS_NODE_IGNORE_DIAGNOSTICS`)
129129
* `-O, --compilerOptions [opts]` JSON object to merge with compiler options (`TS_NODE_COMPILER_OPTIONS`)
130+
* `--pretty` Use pretty diagnostic formatter (`TS_NODE_PRETTY`)
130131
* `--no-cache` Disable the local TypeScript Node cache (`TS_NODE_CACHE`)
131132
* `--skip-project` Skip project config resolution and loading (`TS_NODE_SKIP_PROJECT`)
132133
* `--skip-ignore` Skip ignore checks (`TS_NODE_SKIP_IGNORE`)

package-lock.json

Lines changed: 22 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,15 @@
7070
"semver": "^5.1.0",
7171
"tslint": "^5.0.0",
7272
"tslint-config-standard": "^7.0.0",
73-
"typescript": "^2.8.1"
73+
"typescript": "^2.8.3"
7474
},
7575
"dependencies": {
7676
"arrify": "^1.0.0",
77-
"chalk": "^2.3.0",
7877
"diff": "^3.1.0",
7978
"make-error": "^1.1.1",
8079
"minimist": "^1.2.0",
8180
"mkdirp": "^0.5.1",
82-
"source-map-support": "^0.5.3",
81+
"source-map-support": "^0.5.6",
8382
"yn": "^2.0.0"
8483
}
8584
}

src/bin.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import { inspect } from 'util'
66
import arrify = require('arrify')
77
import Module = require('module')
88
import minimist = require('minimist')
9-
import chalk from 'chalk'
109
import { diffLines } from 'diff'
1110
import { Script } from 'vm'
1211
import { readFileSync, statSync } from 'fs'
13-
import { register, VERSION, DEFAULTS, TSError, parse, printError } from './index'
12+
import { register, VERSION, DEFAULTS, TSError, parse } from './index'
1413

1514
interface Argv {
1615
// Node.js-like options.
@@ -21,6 +20,7 @@ interface Argv {
2120
help?: boolean
2221
version?: boolean
2322
// Register options.
23+
pretty?: boolean
2424
typeCheck?: boolean
2525
transpileOnly?: boolean
2626
cache?: boolean
@@ -38,7 +38,7 @@ interface Argv {
3838
const argv = minimist<Argv>(process.argv.slice(2), {
3939
stopEarly: true,
4040
string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'cacheDirectory', 'ignore'],
41-
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'cache', 'skipProject', 'skipIgnore'],
41+
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'cache', 'pretty', 'skipProject', 'skipIgnore'],
4242
alias: {
4343
eval: ['e'],
4444
print: ['p'],
@@ -86,6 +86,7 @@ Options:
8686
-D, --ignoreDiagnostics [code] Ignore TypeScript warnings by diagnostic code
8787
-O, --compilerOptions [opts] JSON object to merge with compiler options
8888
89+
--pretty Use pretty diagnostic formatter
8990
--no-cache Disable the local TypeScript Node cache
9091
--skip-project Skip reading \`tsconfig.json\`
9192
--skip-ignore Skip \`--ignore\` checks
@@ -101,6 +102,7 @@ const isPrinted = argv.print !== undefined
101102

102103
// Register the TypeScript compiler instance.
103104
const service = register({
105+
pretty: argv.pretty,
104106
typeCheck: argv.typeCheck,
105107
transpileOnly: argv.transpileOnly,
106108
cache: argv.cache,
@@ -175,7 +177,7 @@ function evalAndExit (code: string, isPrinted: boolean) {
175177
result = _eval(code)
176178
} catch (error) {
177179
if (error instanceof TSError) {
178-
console.error(printError(error))
180+
console.error(error.diagnosticText)
179181
process.exit(1)
180182
}
181183

@@ -265,7 +267,7 @@ function startRepl () {
265267

266268
undo()
267269

268-
repl.outputStream.write(`${chalk.bold(name)}\n${comment ? `${comment}\n` : ''}`)
270+
repl.outputStream.write(`${name}\n${comment ? `${comment}\n` : ''}`)
269271
repl.displayPrompt()
270272
}
271273
})
@@ -275,7 +277,7 @@ function startRepl () {
275277
* Eval code from the REPL.
276278
*/
277279
function replEval (code: string, _context: any, _filename: string, callback: (err?: Error, result?: any) => any) {
278-
let err: any
280+
let err: Error | undefined
279281
let result: any
280282

281283
// TODO: Figure out how to handle completion here.
@@ -292,7 +294,8 @@ function replEval (code: string, _context: any, _filename: string, callback: (er
292294
if (Recoverable && isRecoverable(error)) {
293295
err = new Recoverable(error)
294296
} else {
295-
err = printError(error)
297+
console.error(error.diagnosticText)
298+
err = undefined
296299
}
297300
} else {
298301
err = error
@@ -368,18 +371,18 @@ function fileExistsEval (path: string) {
368371
}
369372
}
370373

371-
const RECOVERY_CODES: number[] = [
374+
const RECOVERY_CODES: Set<number> = new Set([
372375
1003, // "Identifier expected."
373376
1005, // "')' expected."
374377
1109, // "Expression expected."
375378
1126, // "Unexpected end of text."
376379
1160, // "Unterminated template literal."
377380
1161 // "Unterminated regular expression literal."
378-
]
381+
])
379382

380383
/**
381384
* Check if a function can recover gracefully.
382385
*/
383386
function isRecoverable (error: TSError) {
384-
return error.diagnostics.every(x => RECOVERY_CODES.indexOf(x.code) > -1)
387+
return error.diagnosticCodes.every(code => RECOVERY_CODES.has(code))
385388
}

src/index.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ describe('ts-node', function () {
122122
}
123123

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

130129
return done()

0 commit comments

Comments
 (0)