Skip to content

Magic in the emitter callback? #694

@crcdng

Description

@crcdng

Expected behavior

choo should accept valid JS code in the emitter callback.

Actual behavior

choo throws

"Unexpected token: punc ()) while parsing file: /app/index.js"

when an ES6 arrow function is used the emitter callback.

Steps to reproduce behavior

This is working code (adapted from the intro tutorial, running on glitch)

const choo = require('choo');
const html = require('choo/html');
const main = require('./templates/main.js');

const app = choo();

app.use(function (state, emitter) {
  state.animals = [
    { type: 'lion', x: 200, y: 100 },
    { type: 'crocodile', x: 50, y: 300 }
  ];
  
  emitter.on('addAnimal', function () {
    const obj = { type: 'lion', x: 100, y: 200 };
    state.animals.push(obj);
    emitter.emit('render');
  });
  
});

app.route('/', main);
app.mount('div');

When I replace function ()with () =>

emitter.on('addAnimal', () => {
   const obj = { type: 'lion', x: 100, y: 200 };
   state.animals.push(obj);
   emitter.emit('render');
 });

choo throws the above error. Some kind of magic going on?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions