DevTools: Improve localization presubmit file parsing error message
Currently, if an unsupported JS feature is used, localization presubmit
checks will fail with the raw error message from esprima, the JS parsing
library. This CL wraps a try-catch block around the parsing call and outputs
a nice error message that specifies the file location and the reason.
Before:
Error: Line 9: Unexpected token =
After:
Error: DevTools localization parser failed:
third_party\blink\renderer\devtools\front_end\security\SecurityPanel.js: Line 9: Unexpected token =
This error is likely due to unsupported JavaScript features. Such features
are not supported by eslint either and will cause presubmit to fail. Please
update the code and use official JavaScript features.
Bug: 941561
Change-Id: If945563f84224232155a7de7cf597c5395a0cbb9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1729729
Reviewed-by: Yang Guo <yangguo@chromium.org>
Commit-Queue: Mandy Chen <mandy.chen@microsoft.com>
Cr-Original-Commit-Position: refs/heads/master@{#697675}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 2b4a1abab0661e491ab275768f8f174cf5686c46
diff --git a/scripts/localization_utils/check_localized_strings.js b/scripts/localization_utils/check_localized_strings.js
index 577915e..ccf3136 100644
--- a/scripts/localization_utils/check_localized_strings.js
+++ b/scripts/localization_utils/check_localized_strings.js
@@ -94,9 +94,20 @@
if (path.basename(filePath) === 'module.json')
return parseLocalizableStringFromModuleJson(fileContent, filePath);
- const ast = esprima.parseModule(fileContent, {loc: true});
- for (const node of ast.body)
+ let ast;
+ try {
+ ast = esprima.parseModule(fileContent, {loc: true});
+ } catch (e) {
+ throw new Error(
+ `DevTools localization parser failed:\n${localizationUtils.getRelativeFilePathFromSrc(filePath)}: ${
+ e.message}` +
+ `\nThis error is likely due to unsupported JavaScript features.` +
+ ` Such features are not supported by eslint either and will cause presubmit to fail.` +
+ ` Please update the code and use official JavaScript features.`);
+ }
+ for (const node of ast.body) {
parseLocalizableStringFromNode(node, filePath);
+ }
}
function parseLocalizableStringFromModuleJson(fileContent, filePath) {