pxt icon indicating copy to clipboard operation
pxt copied to clipboard

How to define a block that compiles to custom JavaScript?

Open nodingneu opened this issue 4 years ago • 2 comments

I can define blocks like this:

namespace pxsim.myblocks {
  //% block="for $item in $list"
  //% draggableParameters
  export function forEach(list: any[], handler: (item: any) => void): void {
    for (let _item of list) {
      handler(_item);
    }
  }
}

This block transpiles to

myblocks.forEach(null, function (item) {

})

How can I create a custom block that transpiles to

for (let item of list) {
}

nodingneu avatar Oct 30 '20 09:10 nodingneu

Language constructs like for require specific handling in the blocks->TypeScript and TypeScript->Blocks compiler so those blocks are "baked into the compiler". So the answer is no, unless you integrate your changes in pxt itself as a pull request.

pelikhan avatar Nov 06 '20 10:11 pelikhan

It looks like I can edit pxt-blockly instead of a target to do this

nodingneu avatar Nov 06 '20 13:11 nodingneu