1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Improve code fence transform error handling (#12742)

This PR improves the error handling of the code fence removal transform stream by catching errors thrown by the `removeFencedCode` function and passing them to the `end` callback. This appears to resolve a problem where watched builds would blow up whenever a file with fences was reloaded.
This commit is contained in:
Erik Marks 2021-11-19 08:35:04 -08:00 committed by GitHub
parent b119b7744d
commit d89e5336a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View File

@ -41,11 +41,16 @@ class RemoveFencedCodeTransform extends Transform {
// stream, immediately before the "end" event is emitted. // stream, immediately before the "end" event is emitted.
// It applies the transform to the concatenated file contents. // It applies the transform to the concatenated file contents.
_flush(end) { _flush(end) {
const [fileContent, didModify] = removeFencedCode( let fileContent, didModify;
this.filePath, try {
this.buildType, [fileContent, didModify] = removeFencedCode(
Buffer.concat(this._fileBuffers).toString('utf8'), this.filePath,
); this.buildType,
Buffer.concat(this._fileBuffers).toString('utf8'),
);
} catch (error) {
return end(error);
}
const pushAndEnd = () => { const pushAndEnd = () => {
this.push(fileContent); this.push(fileContent);
@ -53,12 +58,11 @@ class RemoveFencedCodeTransform extends Transform {
}; };
if (this.shouldLintTransformedFiles && didModify) { if (this.shouldLintTransformedFiles && didModify) {
lintTransformedFile(fileContent, this.filePath) return lintTransformedFile(fileContent, this.filePath)
.then(pushAndEnd) .then(pushAndEnd)
.catch((error) => end(error)); .catch((error) => end(error));
} else {
pushAndEnd();
} }
return pushAndEnd();
} }
} }

View File

@ -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 () => { it('handles transformed file lint failure', async () => {
lintTransformedFileMock.mockImplementationOnce(() => lintTransformedFileMock.mockImplementationOnce(() =>
Promise.reject(new Error('lint failure')), Promise.reject(new Error('lint failure')),