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

Easier for JSX users to spot missing ${...} #208

Open
orangemug opened this issue Jul 14, 2021 · 2 comments
Open

Easier for JSX users to spot missing ${...} #208

orangemug opened this issue Jul 14, 2021 · 2 comments
Labels
debugging discussion Discussions and proposals enhancement New feature or request proposal

Comments

@orangemug
Copy link

I've been using htm for a while now (it's awesome btw), every once in a while though I switch back from a project using JSX and end up writing <Avatar /> instead of <${Avatar} /> or <div className={classnames(...)} /> instead of <div className=${classnames(...)} />.

It is sometimes hard to spot why something's failing, because it fails quietly, doesn't throw an error or obvious warning. I believe (and please correct me) the library could check for attributes starting with /^{/ or elements starting with /^[A-Z]/ and warn the user about their potential error.

Because you're very unlikely to have an element with either of the above, it seems like something that could potentially be enabled by default. Or available by default in a developer build.

There is a little demo showing the error at https://github.com/orangemug/htm-suggestion.

Thoughts?

@developit
Copy link
Owner

Hi @orangemug! Sorry for the super slow reply. Here's a Preact plugins that detects these typos and throws an exception right at the callsite:

import { options } from 'preact';

let old = options.vnode;
options.vnode = vnode => {
  let props = vnode.props;
  if (props) for (let i in props) {
    let v = props[i];
    if (i === 'children' || typeof v !== 'string') continue;
    if (v[0] == '{' && v[v.length-1] === '}') throw Error(`Missing $ in prop value: ${i}=\${ }`);
  }
  if (old) old(vnode);
};

@developit developit added discussion Discussions and proposals enhancement New feature or request proposal debugging labels Nov 4, 2021
@orangemug
Copy link
Author

Hey thanks @developit sorry this got lost in the GH notifications. I think this would be really cool to get into something you could import from the skypack (or similar). My go to tool is preact/htm/skypack but the <Avatar /> vs <${Avatar} /> was super frustrating at first it took a long time of to train my fingers to not make mistakes, on occasion my fingers still fail me 😁

If I could do something like the following, it'd be freaking awesome for putting together demos/mockups.

import htm from 'https://cdn.skypack.dev/htm/debug';

Either way, thanks for the plugin I'll try it out tomorrow. Thanks for htm can't stress enough how much it's made my life more pleasant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debugging discussion Discussions and proposals enhancement New feature or request proposal
Projects
None yet
Development

No branches or pull requests

2 participants