diff --git a/development/build/transforms/remove-fenced-code.js b/development/build/transforms/remove-fenced-code.js index 200233fe8..632660ec2 100644 --- a/development/build/transforms/remove-fenced-code.js +++ b/development/build/transforms/remove-fenced-code.js @@ -41,11 +41,16 @@ class RemoveFencedCodeTransform extends Transform { // stream, immediately before the "end" event is emitted. // It applies the transform to the concatenated file contents. _flush(end) { - const [fileContent, didModify] = removeFencedCode( - this.filePath, - this.buildType, - Buffer.concat(this._fileBuffers).toString('utf8'), - ); + let fileContent, didModify; + try { + [fileContent, didModify] = removeFencedCode( + this.filePath, + this.buildType, + Buffer.concat(this._fileBuffers).toString('utf8'), + ); + } catch (error) { + return end(error); + } const pushAndEnd = () => { this.push(fileContent); @@ -53,12 +58,11 @@ class RemoveFencedCodeTransform extends Transform { }; if (this.shouldLintTransformedFiles && didModify) { - lintTransformedFile(fileContent, this.filePath) + return lintTransformedFile(fileContent, this.filePath) .then(pushAndEnd) .catch((error) => end(error)); - } else { - pushAndEnd(); } + return pushAndEnd(); } } diff --git a/development/build/transforms/remove-fenced-code.test.js b/development/build/transforms/remove-fenced-code.test.js index 5eebc1135..4397b4bf6 100644 --- a/development/build/transforms/remove-fenced-code.test.js +++ b/development/build/transforms/remove-fenced-code.test.js @@ -161,6 +161,28 @@ describe('build/transforms/remove-fenced-code', () => { }); }); + it('handles error during code fence removal or parsing', async () => { + const fileContent = getMinimalFencedCode().concat( + '///: END:ONLY_INCLUDE_IN', + ); + + const stream = createRemoveFencedCodeTransform('main')(mockJsFileName); + + await new Promise((resolve) => { + stream.on('error', (error) => { + expect(error.message).toStrictEqual( + expect.stringContaining( + 'A valid fence consists of two fence lines, but the file contains an uneven number, "3", of fence lines.', + ), + ); + expect(lintTransformedFileMock).toHaveBeenCalledTimes(0); + resolve(); + }); + + stream.end(fileContent); + }); + }); + it('handles transformed file lint failure', async () => { lintTransformedFileMock.mockImplementationOnce(() => Promise.reject(new Error('lint failure')),