Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
toolness opened this issue Sep 11, 2016 · 0 comments
Open

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

toolness opened this issue Sep 11, 2016 · 0 comments

Comments

@toolness
Copy link
Owner

toolness commented Sep 11, 2016

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:

  • This will require us to have a representation of the global symbols of p5.dom/p5.sound in the widget source code, and they could go out of date.
  • I'm not sure how much extra weight escope will add to the bundled code.

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

Example escope snippet

"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));
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant