sweet-js / sweet-core

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

Sweet.js conflicts with ES6 spreads #715

Open ghost opened 7 years ago

ghost commented 7 years ago

You can try with online editor. I cannot using spreads with Sweet.

  Error: expecting a , punctuator
pointer

This code:

operator $ postfix 1 = (right) => {
  return #`${right}[0][${right}[1]]`;
};

operator $$ prefix 1 = (left) => {
  return #`...${left}`;
};

operator $p left 19 = (object, accessor) => {
  return #`[${object}, ${accessor}]`;
};

let array = [0, 1, 2];
let pointer = array $p 0;
pointer $ = 2;

let v = pointer $;

function acceptPair(array, index){

}

acceptPair( $$ pointer );
gabejohnson commented 7 years ago

I think you have a bug in your definition of $$. It should be

operator $$ prefix 1 = (_, right) => {
  return #`...${right}`;
};
ghost commented 7 years ago
operator $ prefix 1 = (_, right) => {
  return #`${right}[0][${right}[1]]`;
};

operator $$ prefix 1 = (_, right) => {
  return #`...${right}`;
};

operator $p left 19 = (object, accessor) => {
  return #`[${object}, ${accessor}]`;
};

let array = [0, 1, 2];
let pointer = array $p 0;
$ pointer = 2;

let v = $ pointer;

function acceptPair(array, index){

}

acceptPair( $$ pointer );

Anyways

  Error: replacement values for syntax template must not be null or undefined
gabejohnson commented 7 years ago

@capitalknew Sorry, I haven't had coffee yet. Your definition of $$ was correct. I'll take a look in the editor.

ghost commented 7 years ago

This is not coffee script. This is SweetJS and ES6 (spreads usage) example.

gabejohnson commented 7 years ago

This is not coffee script

I know. I was talking about the drink 😄

I think this issue is related to on https://github.com/sweet-js/sweet-core/pull/650#issuecomment-288863584. It's good to have an issue specifically for it though. Thank you.

gabejohnson commented 7 years ago
operator $ prefix 1 = (right) => {
  return #`${right}[0][${right}[1]]`;
};

is broken for a different reason. right is a MacroContext object an can't be interpolated the way you're attempting to. Try

operator $ prefix 1 = (right) => {
  var ptr = right.next().value;
  return #`${ptr}[0][${ptr}[1]]`;
};
ghost commented 7 years ago

Also, try fix "..." operator, it really needs for ES6 today. Error: expecting a , punctuator