toolness / p5.js-widget

A reusable widget for embedding editable p5 sketches in web pages.
https://toolness.github.io/p5.js-widget/
GNU Lesser General Public License v2.1
162 stars 44 forks source link

Automatically detect use of p5.dom and p5.sound #65

Open toolness opened 8 years ago

toolness commented 8 years ago

I thought it might be useful to statically analyze the user's sketch and determine whether they're using p5.dom and/or p5.sound, and include those libraries in the preview frame if so. This has the benefit of resolving #58 without needing any extra setup or configuration on the part of the author.

escope could make the static analysis task pretty easy. (It could also make implicit-sketch.ts a lot simpler, too.)

Concerns:

Below is an example snippet I wrote while playing around with escope.

Example `escope` snippet ``` js "use strict"; let esprima = require('esprima'); let escope = require('escope'); let ast = esprima.parse(` var a = 1; function boop() { a += b; } boop `); let scopeManager = escope.analyze(ast); function isInScope(ref, scope) { if (scope.set.has(ref.identifier.name)) { return true; } if (!scope.upper) { return false; } return isInScope(ref, scope.upper); } scopeManager.scopes.forEach(scope => { scope.references.forEach(ref => { console.log(ref.identifier.name, isInScope(ref, scope)); }); }); ```