From bb9fbce938dde009e98f5c3e22bdd8e29de9b7c4 Mon Sep 17 00:00:00 2001 From: Simon Hoss Date: Sat, 28 Jul 2018 22:26:55 +0200 Subject: [PATCH 1/2] #29 respect noEmitOnError --- .prettierrc | 3 +++ index.js | 60 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..c58969d --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "semi": false +} \ No newline at end of file diff --git a/index.js b/index.js index 64c6ca4..38b8623 100644 --- a/index.js +++ b/index.js @@ -206,7 +206,17 @@ module.exports.transform = function(src, filename, options) { ;({ src, filename, options } = src) } - if (filename.endsWith('.ts') || filename.endsWith('.tsx')) { + if (filename.endsWith('.ts') || filename.endsWith('.tsx')) { + if (compilerOptions.noEmitOnError) { + const program = ts.createProgram([filename], compilerOptions) + + const preErrors = ts + .getPreEmitDiagnostics(program) + .filter(({ category }) => category === ts.DiagnosticCategory.Error) + + reportErrors(preErrors) + } + const tsCompileResult = ts.transpileModule(src, { compilerOptions, fileName: filename, @@ -217,28 +227,7 @@ module.exports.transform = function(src, filename, options) { ({ category }) => category === ts.DiagnosticCategory.Error ) - if (errors.length) { - // report first error - const error = errors[0] - const message = ts.flattenDiagnosticMessageText(error.messageText, '\n') - if (error.file) { - let { line, character } = error.file.getLineAndCharacterOfPosition( - error.start - ) - if (error.file.fileName === 'module.ts') { - console.error({ - error, - filename, - options, - }) - } - throw new Error( - `${error.file.fileName} (${line + 1},${character + 1}): ${message}` - ) - } else { - throw new Error(message) - } - } + reportErrors(errors) const babelCompileResult = upstreamTransformer.transform({ src: tsCompileResult.outputText, @@ -275,3 +264,28 @@ module.exports.transform = function(src, filename, options) { }) } } + +function reportErrors(errors) { + if (errors.length) { + // report first error + const error = errors[0] + const message = ts.flattenDiagnosticMessageText(error.messageText, '\n') + if (error.file) { + let { line, character } = error.file.getLineAndCharacterOfPosition( + error.start + ) + if (error.file.fileName === 'module.ts') { + console.error({ + error, + filename, + options, + }) + } + throw new Error( + `${error.file.fileName} (${line + 1},${character + 1}): ${message}` + ) + } else { + throw new Error(message) + } + } +} \ No newline at end of file From 2e3d890d0360f26f9f7714f00628c17d7fbfe16c Mon Sep 17 00:00:00 2001 From: Simon Hoss Date: Sat, 28 Jul 2018 22:36:54 +0200 Subject: [PATCH 2/2] fix lint --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 38b8623..2228e70 100644 --- a/index.js +++ b/index.js @@ -206,7 +206,7 @@ module.exports.transform = function(src, filename, options) { ;({ src, filename, options } = src) } - if (filename.endsWith('.ts') || filename.endsWith('.tsx')) { + if (filename.endsWith('.ts') || filename.endsWith('.tsx')) { if (compilerOptions.noEmitOnError) { const program = ts.createProgram([filename], compilerOptions) @@ -214,7 +214,7 @@ module.exports.transform = function(src, filename, options) { .getPreEmitDiagnostics(program) .filter(({ category }) => category === ts.DiagnosticCategory.Error) - reportErrors(preErrors) + reportErrors(preErrors, filename, options) } const tsCompileResult = ts.transpileModule(src, { @@ -227,7 +227,7 @@ module.exports.transform = function(src, filename, options) { ({ category }) => category === ts.DiagnosticCategory.Error ) - reportErrors(errors) + reportErrors(errors, filename, options) const babelCompileResult = upstreamTransformer.transform({ src: tsCompileResult.outputText, @@ -265,7 +265,7 @@ module.exports.transform = function(src, filename, options) { } } -function reportErrors(errors) { +function reportErrors(errors, filename, options) { if (errors.length) { // report first error const error = errors[0] @@ -288,4 +288,4 @@ function reportErrors(errors) { throw new Error(message) } } -} \ No newline at end of file +}