80KiloMett
80KiloMett

Reputation: 55

Atom-Editor: Failed to load init.coffee - ERROR: reserved word 'function'

UPDATE: I found a solution/workaround for the problem. I renamed init.coffee to init.js because Atom also supports JavaScript. I would still like to know the cause of the problem. Is the script below not valid CoffeeScript or am I missing some dependence? I Installed Atom from the official Arch repositories.

For some reason my init.coffee cannot be loaded. The following code is from Atom's "Composed" Commands documentation:

atom.commands.add('atom-text-editor', 'custom:cut-line', function () {
  const editor = this.getModel();
  editor.selectLinesContainingCursors();
  editor.cutSelectedText();
});

Atom throws an error when it starts:

Failed to load /home/myname/.atom/init.coffee
reserved word 'function'

I'm not sure if this is a bug, my fault, or a result of out-of-date documentation. The error message isn't super helpful, since I already that "function" is a reserved word, even though I don't know a lot of Coffee/JavaScript.

I replaced function using () -> {...}, which resulted in the same error except this time for the reserved word const.

Finally, I tried defining a named function which I passed as an argument to atom.commands.add() and got the same error.

I'm on Linux. atom --version returns:

Atom    : 1.46.0
Electron: 4.2.12
Chrome  : 69.0.3497.128
Node    : 10.11.0

Upvotes: 1

Views: 703

Answers (1)

caffeinated.tech
caffeinated.tech

Reputation: 6548

Your solution is the right direction - that code is JavaScript, not CoffeeScript.

It looks like the '"Composed" Commands' documentation you referenced is using both JavaScript and CoffeeScript in their examples.

To convert from JavaScript:

atom.commands.add('atom-text-editor', 'custom:cut-line', function () {
  const editor = this.getModel();
  editor.selectLinesContainingCursors();
  editor.cutSelectedText();
});

to CoffeeScript:

atom.commands.add 'atom-text-editor', 'custom:cut-line',  () ->
  editor = @getModel()
  editor.selectLinesContainingCursors()
  editor.cutSelectedText()
  • When calling a function with arguments, you can leave out the parenthesis ().
  • function is removed in CoffeeScript, just use parenthesis and either a single -> or double arrow =>, where a double arrow is the same as .bind(this), so that would be incorrect here.
  • No const/let/var keywords. Just defined the variable without them.
  • this. can be replaced with @.
  • Braces ({}) wrapping function definitions are optional.
  • No semicolons.

If you want to learn CoffeeScript and help the community, you could fix the documentation yourself by forking, editing and making a pull request of the repository.

Alternatively, you should report this error in the documentations in their repository as an issue.

Upvotes: 1

Related Questions