Skip to content

Commit bb9fbce

Browse files
author
Simon Hoss
committed
ds300#29 respect noEmitOnError
1 parent 7aa16d3 commit bb9fbce

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"semi": false
3+
}

index.js

+37-23
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,17 @@ module.exports.transform = function(src, filename, options) {
206206
;({ src, filename, options } = src)
207207
}
208208

209-
if (filename.endsWith('.ts') || filename.endsWith('.tsx')) {
209+
if (filename.endsWith('.ts') || filename.endsWith('.tsx')) {
210+
if (compilerOptions.noEmitOnError) {
211+
const program = ts.createProgram([filename], compilerOptions)
212+
213+
const preErrors = ts
214+
.getPreEmitDiagnostics(program)
215+
.filter(({ category }) => category === ts.DiagnosticCategory.Error)
216+
217+
reportErrors(preErrors)
218+
}
219+
210220
const tsCompileResult = ts.transpileModule(src, {
211221
compilerOptions,
212222
fileName: filename,
@@ -217,28 +227,7 @@ module.exports.transform = function(src, filename, options) {
217227
({ category }) => category === ts.DiagnosticCategory.Error
218228
)
219229

220-
if (errors.length) {
221-
// report first error
222-
const error = errors[0]
223-
const message = ts.flattenDiagnosticMessageText(error.messageText, '\n')
224-
if (error.file) {
225-
let { line, character } = error.file.getLineAndCharacterOfPosition(
226-
error.start
227-
)
228-
if (error.file.fileName === 'module.ts') {
229-
console.error({
230-
error,
231-
filename,
232-
options,
233-
})
234-
}
235-
throw new Error(
236-
`${error.file.fileName} (${line + 1},${character + 1}): ${message}`
237-
)
238-
} else {
239-
throw new Error(message)
240-
}
241-
}
230+
reportErrors(errors)
242231

243232
const babelCompileResult = upstreamTransformer.transform({
244233
src: tsCompileResult.outputText,
@@ -275,3 +264,28 @@ module.exports.transform = function(src, filename, options) {
275264
})
276265
}
277266
}
267+
268+
function reportErrors(errors) {
269+
if (errors.length) {
270+
// report first error
271+
const error = errors[0]
272+
const message = ts.flattenDiagnosticMessageText(error.messageText, '\n')
273+
if (error.file) {
274+
let { line, character } = error.file.getLineAndCharacterOfPosition(
275+
error.start
276+
)
277+
if (error.file.fileName === 'module.ts') {
278+
console.error({
279+
error,
280+
filename,
281+
options,
282+
})
283+
}
284+
throw new Error(
285+
`${error.file.fileName} (${line + 1},${character + 1}): ${message}`
286+
)
287+
} else {
288+
throw new Error(message)
289+
}
290+
}
291+
}

0 commit comments

Comments
 (0)