From 72f2cde00fc968ce40282e0392d3553963730748 Mon Sep 17 00:00:00 2001 From: GarboMuffin Date: Sun, 14 Aug 2022 19:22:43 -0500 Subject: [PATCH] Use @turbowarp/json to parse non-standards-compliant Scratch 2 projects --- lib/parse.js | 4 +++- package-lock.json | 5 +++++ package.json | 1 + test/unit/parser.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/parse.js b/lib/parse.js index 9111595..dd9c9ce 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,3 +1,5 @@ +const ExtendedJSON = require('@turbowarp/json'); + /** * Converts string from unpack method into a project object. Note: this method * will be expanded greatly in the future in order to support the Scratch 1.4 @@ -16,7 +18,7 @@ module.exports = function (input, callback) { // so remove that specific one before continuing. // SB2 JSONs and SB3 JSONs have different versions of the // character serialized (e.g. \u0008 and \b), strip out both versions - result = JSON.parse(input.replace( + result = ExtendedJSON.parse(input.replace( /(\\+)(b|u0008)/g, (match, backslash, code) => { // If the number is odd, there is an actual backspace. diff --git a/package-lock.json b/package-lock.json index c43f6c9..8fa5d06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1503,6 +1503,11 @@ } } }, + "@turbowarp/json": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@turbowarp/json/-/json-0.1.1.tgz", + "integrity": "sha512-6APLbD+TGNdVKUu2UlWM1cgHbwrStWIwpvDVdiqS/SJ/i0OfjtqeAhnK5EjqNazXGWI+p68ZNxMJe2+UtZZSzA==" + }, "@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", diff --git a/package.json b/package.json index f2a3f63..b17307d 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "semantic-release": "semantic-release" }, "dependencies": { + "@turbowarp/json": "^0.1.1", "ajv": "6.3.0", "jszip": "3.1.5", "pify": "4.0.1" diff --git a/test/unit/parser.js b/test/unit/parser.js index ee36eb9..c2b08c5 100644 --- a/test/unit/parser.js +++ b/test/unit/parser.js @@ -85,3 +85,31 @@ test('backslashes followed by b are not stripped at all', function (t) { t.end(); }); }); + +test('supports non-standard Scratch 2 JSON', (t) => { + const json = ` + { + // This is a line comment + /* This is + a block comment */ + "something": [ + Infinity, + -Infinity, + // Trailing comma should be ignored + NaN, + ] + } + This is some extra garbage at the end that should be ignored. + `; + parse(json, (err, res) => { + t.equal(err, null); + t.same(res, { + something: [ + Infinity, + -Infinity, + NaN + ] + }); + t.end(); + }); +});