sweet-js / sweet-core

Sweeten your JavaScript.
https://www.sweetjs.org
BSD 2-Clause "Simplified" License
4.59k stars 209 forks source link

Add helpers #644

Closed disnet closed 7 years ago

disnet commented 7 years ago

This adds a helper module for macro authors. This will make it easier to change our underlying data model without causing pain for macro authors who use the new helper library.

It should replace the various methods we had on Syntax objects. Use looks something like:

import * as H from 'sweet.js/helpers' for syntax;

syntax m = ctx => {
  let v = ctx.next().value;
  if (H.isIdentifier(v, 'foo') {
    return H.fromString(v, 'bar');
  }
  return H.fromString(v, 'baz');
}
m foo; // expands to 'bar'
disnet commented 7 years ago

The only thing that bugs me is the TypeCodes kludge. When we figure out what to do w/ Syntax I think we should write a token spec and have helpers and internals both depend on it.

Agreed. I hate the kudge too.

On a side note I see you've hopped on the prettier train. I hadn't looked at it closely before. It really opens things up!

Yeah I love it! I'll hook it up to our build process soon.

disnet commented 7 years ago

I've rearranged the tests a little because originally the test-handlers.js file I added wasn't actually being run (forgot to update the files list). Now new tests files will just be picked up automatically.

Let me know if everything looks good.

gabejohnson commented 7 years ago

These won't work w/ MacroContext#name. It's a Syntax object, not RawSyntax. We can either pass the RawSyntax to the MacroContext constructor or wrap it again on the call to name.