sweet-js/sweet-core

Directive moves below import statements when compiled

gabejohnson opened this issue · 1 comments

While attempting to reproduce #693 I found some oddities:

import { class } from './es2015-macros';

class Droid {
  constructor(name, color) {
    this.name = name;
    this.color = color;
  }

  rollWithIt(it) {
    return this.name + " is rolling with " + it;
  }
}

results in:

import {class} from "./es2015-macros";
class Droid {
  constructor(name_0, color_1) {
    this.name = name_0;
    this.color = color_1;
  }
  rollWithIt(it_2) {
    return this.name + " is rolling with " + it_2;
  }
}

Note that the class macro doesn't expand.

Another thing I noticed is that:

'lang sweet.js';

import { unwrap, isIdentifier } from './helpers.js' for syntax;

export syntax class = function (ctx) {
  let name = ctx.next().value;
  let bodyCtx = ctx.contextify(ctx.next().value);

  // default constructor if none specified
  let construct = #`function ${name} () {}`;
  let result = #``;
  for (let item of bodyCtx) {
    if (isIdentifier(item) && unwrap(item).value === 'constructor') {
      construct = #`
        function ${name} ${bodyCtx.next().value}
        ${bodyCtx.next().value}
      `;
    } else {
      result = result.concat(#`
        ${name}.prototype.${item} = function
            ${bodyCtx.next().value}
            ${bodyCtx.next().value};
      `);
    }
  }
  return construct.concat(result);
};

results in:

import {unwrap, isIdentifier} from "./helpers.js";
"lang sweet.js";
;
;
export {class_6 as class}

Three things here:

  1. The import statement isn't erased.
  2. The 'lang sweet.js' directive moves below the import statement.
  3. The empty statements don't appear to be removed.

I suspect that the moving directive is part of the problem here, but that's just conjecture.

Fixed by something in the last several PRs.