Skip to content
10 changes: 6 additions & 4 deletions src/core/friendly_errors/sketch_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,14 @@ if (typeof IS_MINIFIED !== 'undefined') {
}
}
const keyArray = Object.keys(p5Constructors);
const globalFunctions = ['Renderer', 'Renderer2D', 'RendererGL'];

@aditya-shrivastavv aditya-shrivastavv Mar 6, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. more classes can be added as per need.
  2. I think the name of identifier globalFunctions is not accurate, should it be changed ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, maybe something like classesWithGlobalFunctions would be more accurate?

let functionArray = [];
//get the names of all p5.js functions
for (let i = 0; i < keyArray.length; i++) {
functionArray.push(...Object.keys(p5Constructors[keyArray[i]].prototype));
//get the names of all p5.js functions which are available globally
for (let i = 0; i < globalFunctions.length; i++) {
functionArray.push(...Object.keys(
p5Constructors[globalFunctions[i]].prototype
));
}
functionArray = functionArray.filter(ele => !ele.includes('_'));

//we have p5.js function names with us so we will check
//if they have been declared or not.
Expand Down
46 changes: 45 additions & 1 deletion test/unit/core/error_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ suite('Tests for p5.js sketch_reader', function() {
);

testUnMinified(
'detects reassignment of p5.js function outside setup',
'detects reassignment of p5.js function (text) outside setup',
function() {
return new Promise(function(resolve) {
prepSketchReaderTest(
Expand All @@ -975,6 +975,50 @@ suite('Tests for p5.js sketch_reader', function() {
}
);

testUnMinified(
'detects reassignment of p5.js function (textSize from Typography) outside setup',
function() {
return new Promise(function(resolve) {
prepSketchReaderTest(
['let textSize = 100', 'function setup() {}'],
resolve
);
}).then(function() {
assert.strictEqual(log.length, 1);
assert.match(log[0], /you have used a p5.js reserved function/);
});
}
);

testUnMinified(
'does not detect reassignment of p5.js function (size from TypedDict or Dom) outside setup',
function() {
return new Promise(function(resolve) {
prepSketchReaderTest(
['let size = 100', 'function setup() {}'],
resolve
);
}).then(function() {
assert.strictEqual(log.length, 0);
});
}
);

testUnMinified(
'detects reassignment of p5.js function (point from shape) outside setup',
function() {
return new Promise(function(resolve) {
prepSketchReaderTest(
['let point = 100', 'function setup() {}'],
resolve
);
}).then(function() {
assert.strictEqual(log.length, 1);
assert.match(log[0], /you have used a p5.js reserved function/);
});
}
);

testUnMinified(
'detects reassignment of p5.js functions in declaration lists',
function() {
Expand Down