jashkenas / coffeescript

Unfancy JavaScript
https://coffeescript.org/
MIT License
16.48k stars 1.99k forks source link

That's how it is in CoffeeScript. #5314

Closed Monsupito closed 4 years ago

Monsupito commented 4 years ago

That's how it is in CoffeeScript.

Originally posted by @vendethiel in https://github.com/jashkenas/coffeescript/issues/5313#issuecomment-605660005

Monsupito commented 4 years ago

UP !

Choose one: is this a bug report

Input Code

Discord = require('discord.js')
Logger = require('./Logger')

module.exports = class WebhookPlugin
    constructor: (webhooks) ->
        if !webhooks
            return null

        @webhooks = []

        # Here   \/
        for hook of Object.keys webhooks
            @webhooks.push hook
            webhookCredentials = webhooks[hook].split('/').slice(-2)
            @[hook] =
                url: webhooks[hook]
                id: webhookCredentials[0]
                token: webhookCredentials[1]

            @[hook].client = new (Discord.WebhookClient)(@[hook].id, @[hook].token)

    send: (webhook, content) ->
        if @webhooks and @webhooks.includes(webhook)
            if typeof content == 'object' and !(content instanceof Array)
                content = embeds: [ content ]
            if content
                @[webhook].client.send(content).catch Logger.error

Expected Behavior

The code should be compiled correctly

Current Behavior

On this code the function 'for' is not compiled correctly since the 'of' becomes 'in'

Context

I just wanted to compile my program with this command: "coffee --compile --transpile --map --no-header --output dist/ src/". Everything works fine except that.

Environment

vendethiel commented 4 years ago

Please read the documentation. They are swapped. If you want to iterate over generators, use from.

GeoffreyBooth commented 4 years ago

See https://coffeescript.org/#loops.

CoffeeScript invented for ... of and for ... in before they were adopted in JavaScript, and unfortunately the ECMAScript committee decided to choose different meanings for the keywords than CoffeeScript had chosen. We can't change now because we avoid unnecessary breaking changes. Yes it is confusing; blame TC39.

If you want to iterate over an object, CoffeeScript provides for key, val of obj.