assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

[Feature request] Hook to interact with loaded files content.

Open cainrus opened this issue 2 years ago • 3 comments

which behavior you expected to see?

I want to see additional transformation hook, to interact with loaded assemblyscript code. Hook order must be first, before afterParse hook. Hook must provide at least collection of file content and path. function must return same collection structure sync or async with promise.

Then it will be possible to transpile code with babel, to be able to use some modern stuff like param destruction: https://github.com/babel/babel/issues/14691

source:

type Point = {
  x: number;
  y: number;
}

export function test({x, y}: Point) {
  return x * y;
}

transpiled:

type Point = {
  x: number;
  y: number;
};
export function test(_ref: Point) {
  let x = _ref.x,
      y = _ref.y;
  return x * y;
}

I can implement the hook. What do you think?

cainrus avatar Jun 29 '22 21:06 cainrus

Yes, I guess you can add extra hook. PR are welcomed. But I more prefer to implement such syntax directly in AssemblyScript. Because it will allow people to avoid extra transformation steps and libraries like babel. It may also speed up runtime performance. Array destructing is under development btw: https://github.com/AssemblyScript/assemblyscript/pull/2321

MaxGraey avatar Jun 30 '22 07:06 MaxGraey

Yes, I guess you can add extra hook. PR are welcomed. But I more prefer to implement such syntax directly in AssemblyScript. Because it will allow people to avoid extra transformation steps and libraries like babel. It may also speed up runtime performance. Array destructing is under development btw: #2321

It's good to hear, that destructing is under development 👍 Do I need to add more docs somewhere? I'm using very simple transform class:

const {
  transformAsync,
} = require("@babel/core");

module.exports = class BabelTransform {
  /**
   * @param {{sourceText: string, sourcePath: string}} file
   * @return {Promise<void>}
   */
  async afterRead(file) {
    if (file.sourcePath.startsWith("~lib/")) {
      return;
    }
    file.sourceText = (await transformAsync(file.sourceText, {
      retainLines: true,
      filename: file.sourcePath,
    })).code.replace(/void 0/g, "null");
  }
}

module.exports = BabelTransform;

Right now I'm very excited to work with assemblyscript within watch mode with destruction and maybe more modern stuff I'll try to enable.

cainrus avatar Jun 30 '22 19:06 cainrus

I want a hook to handle generated (wasm) files.

yjhmelody avatar Jul 07 '22 16:07 yjhmelody

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

github-actions[bot] avatar Sep 06 '22 23:09 github-actions[bot]