question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion to improve the view parser

See original GitHub issue

The way you parse the views currently is quite buggy, it can fail if comments or parens are present in strings.

A better strategy would be to use a single regex that matches strings, comments and the /l\s*(/, and ignore the strings/comment matches.

Something like this would do:

L_MATCHER = /((?:^|[^\w\d$])l\s*\()|"(?:\\.|[^"\n])*"|'(?:\\.|[^'\n])*'|\/\*[\s\S]*?\*\/|\/\/.*?\n/g

function findL(str, i) {
  var match;
  L_MATCHER.lastIndex = i;
  while (match = L_MATCHER.exec(str)) if(match[1] != null) {
    return match.index + match[1].length;
  }
}

You could also use a similar strategy to match parentheses.

PAREN_MATCHER = /[()]|"(?:\\.|[^"\n])*"|'(?:\\.|[^'\n])*'|\/\*[\s\S]*?\*\/|\/\/.*?\n/g then ignore cases where match[0] is not a parenthesis.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
lhoriecommented, Mar 8, 2018

I’m not sure this approach is worth pursuing further at all. The function call regex/replace will break non-local variable references. Here’s a simple test case:

const { r, l: x, mount, component } = radi;
 
const counter = component({
  view: function() {
    return (
      r('button', {onclick: this.up}, 'Click me ', x(this.counter))
    )
  },
  state: {
    counter: 0
  },
  actions: {
    up() {
      this.counter += 1;
    },
  }
});
 
mount(new counter(), document.body);

So if l is renamed (which may happen, for example, if a bundler or a minifier renames it), things break.

Using any library in a view function will also break:

const { r, l, mount, component } = radi;

function format(date) {return date.toString();}
 
const counter = component({
  view: function() {
    return (
      r('div', format(new Date))
    )
  },
  state: {
    counter: 0
  },
  actions: {
    up() {
      this.counter += 1;
    },
  }
});
 
mount(new counter(), document.body);

My recommendation would be to do the replacements with a custom babel transformation instead of doing it at runtime. Doing it at the JSX level might allow you to drop the l() call altogether if an assumption is made that an interpolation is reactive, but it could just be a macro on l() as well.

0reactions
Marcisbeecommented, May 21, 2018

With merging refactor into master, this is no longer relevant

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing Parser Performance – TypeFox Blog
In this article we showcased 3 ways of improving parsing performance. These are general tips that should be applicable to most top-down parsing...
Read more >
Improving the performance of an ANTLR parser - Strumenta
In this article we offer some suggestions on improving the performance of your ANTLR parser. ... You can also look at the ANTLR...
Read more >
Develop Advanced Security Information Model (ASIM) parsers ...
This article explains how to develop, test, and deploy Microsoft Sentinel Advanced Security Information Model (ASIM) parsers.
Read more >
5 Ways Email Parsing Improves Lead Workflow - LinkedIn
An email parser, together with a webhook, is a great way to improve your workflow for entering leads into your CRM or marketing...
Read more >
In-depth: The Parser Context
Overview. The parser's context is yet another concept. An instance (object) of the context class is created before a non-terminal starts parsing and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found