Skip to content

Commit

Permalink
Use @turbowarp/json to parse non-standards-compliant Scratch 2 projects
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Aug 15, 2022
1 parent 64dfc0c commit 72f2cde
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 28 additions & 0 deletions test/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 72f2cde

Please sign in to comment.