From 5a0bb278582390ce66bfddad87bc73ebf1b2daf7 Mon Sep 17 00:00:00 2001 From: zoe-translates <116055375+zoe-translates@users.noreply.github.com> Date: Mon, 27 Mar 2023 18:20:41 +0800 Subject: [PATCH] Translator tester: Be more vigilant against empty test cases. The test cases in a translator script may be empty, not just with an empty array for `var testCases`, but in the sense of completely missing. In that case, don't pass invalid JSON (all whitespaces or empty string) to `JSON.parse()`, and use an empty array instead. This fixes an annoying `SyntaxError` in the console while loading the file `Ovid.js` in the translators repository. That translator should be fixed, too. --- testTranslators/translatorTester.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/testTranslators/translatorTester.js b/testTranslators/translatorTester.js index 5be6417..a1e309e 100644 --- a/testTranslators/translatorTester.js +++ b/testTranslators/translatorTester.js @@ -224,8 +224,12 @@ var Zotero_TranslatorTester = function(translator, type, debugCallback, translat var testEnd = code.indexOf("/** END TEST CASES **/"); if (testStart !== -1 && testEnd !== -1) { var test = code.substring(testStart + 24, testEnd) + .trim() .replace(/^[\s\r\n]*var testCases = /, '') .replace(/;[\s\r\n]*$/, ''); + if (!test) { + test = "[]"; + } try { var testObject = JSON.parse(test); } catch (e) {