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

refactor(Player): Generate and parse player script's AST #713

Merged
merged 2 commits into from
Aug 1, 2024

Conversation

LuanRT
Copy link
Owner

@LuanRT LuanRT commented Jul 31, 2024

Notes:

  • The Syntax Tree is generated by Jinter (which is built on top of Acorn).
  • While doing this may be slightly slower than using a regular exp, it is much more reliable (plus we already cache the player functions anyway).
  • findFunction expects the functions to be inside a self-invoking function.

Utils#findFunction Options & Usage Example:

export type FindFunctionArgs = {
  /**
   * The name of the function.
   */
  name?: string;

  /**
   * A string that must be included in the function's code for it to be considered.
   */
  includes?: string;

  /**
   * A regular expression that the function's code must match.
   */
  regexp?: RegExp;
};
const source = '(function() {var foo, bar; foo = function() { console.log("foo"); }; bar = function() { console.log("bar"); }; })();';
const result = findFunction(source, { name: 'bar' });
// Or: const result = findFunction(source, { includes: 'console.log("bar")' });
console.log(result);

Output:

{
  start: 69,
  end: 110,
  name: 'bar',
  node: { /**/ },
  result: 'bar = function() { console.log("bar"); };'
}

Closes #711

Notes:
- The Syntax Tree is generated by Jinter (which is built on top of `Acorn`).
- While doing this may be slightly slower than using a regular exp, it is much more reliable (plus we already cache the player functions anyway).
- `findFunction` expects the functions to be inside a self-invoking function.
src/core/Player.ts Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<current version as of July 30th> <Songs stop>
2 participants