From 8e5cecab9ecbca762cf530b26f73638d026ad16b Mon Sep 17 00:00:00 2001 From: Patrick Brosset Date: Mon, 28 Sep 2020 02:12:18 -0700 Subject: [PATCH] Guard against null event.data from worker in the script formatter The FormatterWorkerPool._onWorkerMessage method can call its current task's callback with null as its parameter. This happen when the formatter work doesn't send event data back. When this happens, it creates exceptions in the ScriptFormatter class. So, this change simply guards against this. Bug: 1132284 Change-Id: I9a995a2b81b5e211ae1690caad7bf56a4772c354 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2431404 Reviewed-by: Jose Leal Reviewed-by: Mathias Bynens Commit-Queue: Patrick Brosset --- front_end/formatter/ScriptFormatter.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/front_end/formatter/ScriptFormatter.js b/front_end/formatter/ScriptFormatter.js index d1a15320a4..d6b0619cbb 100644 --- a/front_end/formatter/ScriptFormatter.js +++ b/front_end/formatter/ScriptFormatter.js @@ -91,13 +91,23 @@ export class ScriptFormatter { * @param {function(string, !FormatterSourceMapping)} callback */ constructor(mimeType, content, callback) { - content = content.replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/, ''); + this._mimeType = mimeType; + this._originalContent = content.replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/, ''); this._callback = callback; - this._originalContent = content; - formatterWorkerPool() - .format(mimeType, content, Common.Settings.Settings.instance().moduleSetting('textEditorIndent').get()) - .then(this._didFormatContent.bind(this)); + this._initialize(); + } + + async _initialize() { + const pool = formatterWorkerPool(); + const indent = Common.Settings.Settings.instance().moduleSetting('textEditorIndent').get(); + + const formatResult = await pool.format(this._mimeType, this._originalContent, indent); + if (!formatResult) { + this._callback(this._originalContent, new IdentityFormatterSourceMapping()); + } else { + this._didFormatContent(formatResult); + } } /**