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:
parent
b119b7744d
commit
d89e5336a6
@ -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(
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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')),
|
||||
|
Loading…
Reference in New Issue
Block a user