diff --git a/src/core/friendly_errors/sketch_reader.js b/src/core/friendly_errors/sketch_reader.js index c9c3159577..e229ad5bfc 100644 --- a/src/core/friendly_errors/sketch_reader.js +++ b/src/core/friendly_errors/sketch_reader.js @@ -319,12 +319,14 @@ if (typeof IS_MINIFIED !== 'undefined') { } } const keyArray = Object.keys(p5Constructors); + const globalFunctions = ['Renderer', 'Renderer2D', 'RendererGL']; 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. diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index e7a69eb6a2..e01ee1256d 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -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( @@ -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() {