jashkenas / coffeescript

Unfancy JavaScript
https://coffeescript.org/
MIT License
16.45k stars 1.98k forks source link

Also declare async function, if an await was found in function's body #5464

Closed onepointerT closed 2 months ago

onepointerT commented 2 months ago

Solves the error 'Unexpected reserved word "await"':


      dir = (await opendir(this.path));
             ^^^^^

SyntaxError: Unexpected reserved word
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:152:18)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:298:14)

Node.js v18.19.1````
GeoffreyBooth commented 2 months ago

This needs tests, and you need to commit the compiled output.

onepointerT commented 2 months ago

I'm not from yesterday, I know that and what testing is. I've tested it on the same testing script. The output is omitted, because no further output on that functionality. After using the corrected commit I've got output on a completly other topic, import statements in ES scripts, with "type": "module" in package.json. The problem with the asyncronous await is patched and delivers no further output, I manually controlled the function, it is now an async function, before the commit it was a simple function.

edemaine commented 2 months ago

What's the input that causes the output error you mention?

I'm worried that this code will add async to the outer function in this case (but I didn't test):

->
  -> await undefined
onepointerT commented 2 months ago
export class File extends Path
    constructor: (path) ->
        super path

    readSync: () ->
        fd = await open @path 'r'
        return fd.createReadStream()

    writeSync: (data) ->
        fd = await open @path 'rw'
        return fd.createWriteStream data

Somehow an await undefined sounds a little bit silly, since loops that iterate unless the stack is empty are implemented already. This feature request is required on every async function use inside of a function, except the already included async functionality with For and Of.

What's the input that causes the output error you mention?

I'm worried that this code will add async to the outer function in this case (but I didn't test):

->
  -> await undefined
edemaine commented 2 months ago

Your code [already compiles to](https://coffeescript.org/#try:export%20class%20File%20extends%20Path%0A%20%20%20%20constructor%3A%20(path)%20-%3E%0A%20%20%20%20%20%20%20%20super%20path%0A%20%20%20%20%0A%20%20%20%20readSync%3A%20()%20-%3E%0A%20%20%20%20%20%20%20%20fd%20%3D%20await%20open%20%40path%20'r'%0A%20%20%20%20%20%20%20%20return%20fd.createReadStream()%0A%20%20%20%20%0A%20%20%20%20writeSync%3A%20(data)%20-%3E%0A%20%20%20%20%20%20%20%20fd%20%3D%20await%20open%20%40path%20'rw'%0A%20%20%20%20%20%20%20%20return%20fd.createWriteStream%20data)

export var File = class File extends Path {
  constructor(path) {
    super(path);
  }

  async readSync() {
    var fd;
    fd = (await open(this.path('r')));
    return fd.createReadStream();
  }

  async writeSync(data) {
    var fd;
    fd = (await open(this.path('rw')));
    return fd.createWriteStream(data);
  }

};

It seems that async is already where it needs to be. So I'm not sure what this PR is aiming to fix. It would help if you added a test that is broken with the current CoffeeScript.