Help with babel (es6)


#1

I’m coding my first babel es6 package. In just a few lines I’m already stuck. (grin) Here is my code followed by the error.

Edit: If I use console.log instead of just log it activates.

'use babel';

var log = console.log;

export let activate = () =>  {
  log('AnySyn activated');
}
[Enter steps to reproduce below:]

1. Reload with source above

**Atom Version**: 1.0.2
**System**: Ubuntu 12.04.5
**Thrown From**: [anysyn](https://github.com/mark-hahn/anysyn) package, v0.0.1
**User**: root

### Stack Trace

Failed to activate the anysyn package

At Illegal invocation

TypeError: Illegal invocation
at Object.activate (/root/dev/.atom/packages/anysyn/lib/main.js:9:3)
at Package.module.exports.Package.activateNow (/usr/share/atom/resources/app.asar/src/package.js:240:19)
at /usr/share/atom/resources/app.asar/src/package.js:221:30
at Package.module.exports.Package.measure (/usr/share/atom/resources/app.asar/src/package.js:165:15)
at Package.module.exports.Package.activate (/usr/share/atom/resources/app.asar/src/package.js:213:14)
at PackageManager.module.exports.PackageManager.activatePackage (/usr/share/atom/resources/app.asar/src/package-manager.js:452:21)
at /usr/share/atom/resources/app.asar/src/package-manager.js:436:29
at Config.module.exports.Config.transact (/usr/share/atom/resources/app.asar/src/config.js:311:16)
at PackageManager.module.exports.PackageManager.activatePackages (/usr/share/atom/resources/app.asar/src/package-manager.js:431:19)
at PackageManager.module.exports.PackageManager.activate (/usr/share/atom/resources/app.asar/src/package-manager.js:412:46)
at Atom.module.exports.Atom.startEditorWindow (/usr/share/atom/resources/app.asar/src/atom.js:645:21)
at Object. (/usr/share/atom/resources/app.asar/src/window-bootstrap.js:12:8)
at Object. (/usr/share/atom/resources/app.asar/src/window-bootstrap.js:23:4)
at Module._compile (module.js:452:26)
at Object.loadFile [as .js] (/usr/share/atom/resources/app.asar/src/babel.js:162:21)
at Module.load (module.js:347:32)
at Function.Module._load (module.js:302:12)
at Module.require (module.js:357:17)
at require (module.js:376:17)
at setupWindow (file:///usr/share/atom/resources/app.asar/static/index.js:96:23)
at window.onload (file:///usr/share/atom/resources/app.asar/static/index.js:36:7)


### Commands


### Config

```json
{
  "core": {
    "ignoredNames": [
      ".unison*",
      "plexdb",
      "server-js",
      ".git",
      ".hg",
      ".svn",
      ".DS_Store",
      "Thumbs.db",
      ".idea",
      ".live-archive",
      "js",
      "node_modules",
      "nohup.out"
    ],
    "disabledPackages": [
      "atomatigit",
      "select-line",
      "css-snippets",
      "atom-package-manager-commands",
      "package-manager-commands",
      "hex",
      "max-tabs",
      "preview-tabs",
      "scratch",
      "show-in-system",
      "merge-conflicts",
      "proc",
      "sample",
      "scroll-by-function",
      "shadow-test",
      "snippets2",
      "autocomplete-plus",
      "command-logger",
      "error-status",
      "github-issues",
      "language-pegjs",
      "minimap-git-diff",
      "node-ide-bad",
      "pathfinder",
      "regex-railroad-diagram",
      "remote-sync",
      "rest-client",
      "stacktrace",
      "swap-copy-paste",
      "symbols-tree-view",
      "tabs-to-spaces",
      "text-buffer",
      "atom-minimap",
      "assign-align",
      "block-travel",
      "an-evil-package",
      "wheel-zoom",
      "tree-view-git-projects",
      "file-watcher",
      "test-package",
      "selection-count",
      "minimap",
      "hide-tree",
      "enhanced-package-list",
      "autocomplete",
      "coffee-compiler",
      "issues",
      "exception-reporting",
      "layout-manager",
      "link",
      "no-config",
      "node-ide",
      "run-in-terminal",
      "search-multiple-projects",
      "terminal-panel",
      "issues-histogram",
      "coffee-compile",
      "coffee-links",
      "coffee-navigator",
      "spell-check",
      "saveallthetime-atom",
      "atom-terminal",
      "compare-files",
      "gist-it",
      "html-tab",
      "line-ending-converter",
      "code-links",
      "file-mode",
      "language-nginx",
      "recent-finder",
      "incremental-search",
      "archive-view",
      "atom-touch-events",
      "autoflow",
      "autocomplete-snippets",
      "autocomplete-html",
      "autocomplete-css",
      "autocomplete-atom-api",
      "background-tips",
      "backup2gdrive",
      "encoding-selector",
      "fuzzy-finder",
      "grammar-selector",
      "snippets",
      "git-tab-status",
      "markdown-preview-opener",
      "node-debugger",
      "web-browser",
      "browser-plus",
      "coffee-trace",
      "file-types",
      "toggle-invisibles",
      "Sublime-Style-Column-Selection",
      "autohide-tree-view",
      "simple-drag-drop-text",
      "view-tail-large-files",
      "view-tail-large-files",
      "drag-drop-text",
      "cursor-indicator",
      "language-vue-component",
      "select-rectangle",
      "scroll-sync",
      "clip-history",
      "auto-host-markdown-image",
      "Termrk",
      "home-fix",
      "language-generic-config",
      "language-gitignore",
      "markdown-scroll-sync",
      "method-separator",
      "package-list",
      "pane-split-moves-tab",
      "reverse-slashes",
      "search-all-keybindings",
      "sort-lines",
      "untitled-no-more",
      "api-docs",
      "goto",
      "open-in-sourcetree",
      "command-toolbar",
      "ctrl-dir-scroll",
      "autocomplete-ctags",
      "bug-report",
      "line-count",
      "opened-files",
      "nuclide-file-tree",
      "nuclide-quick-open",
      "nuclide-remote-projects",
      "live-archive"
    ],
    "themes": [
      "atom-light-ui",
      "atom-light-syntax"
    ],
    "projectHome": "/root/dev/.atom/packages",
    "audioBeep": false
  }
}

Installed Packages

# User
anysyn, v0.0.1
command-toolbar, v1.0.12
comment-down, v0.1.1
fonts, v0.5.0
grammar-mode, v1.1.0
last-cursor-position, v0.9.0
package-cop, v0.2.10
pretty-json, v0.4.1
remote-edit, v1.8.13
repository-file-actions, v0.6.0
tab-smart-sort, v0.1.4

# Dev
No dev packages

#2

What I would do is

export function activate() {
  console.log('AnySyn activated');
}

But the way you do it should also work as expected


#3

Yes, that works. Actually I had more code and I reduced it to this simple version. My problem relates to scope. I can’t access variables outside of the function.

I thought maybe => could only use the parameters passed in, which would be silly, but this code didn’t work either.

'use babel';

var log = console.log;

export let activate = function() {
  log('AnySyn activated');
}

This code works

'use babel';

var log = console.log;

export let activate = function() {
  console.log('AnySyn activated');
}

If no one here can figure this out, then does anyone know the proper place for me to ask this question? Maybe a babel forum.


#4

Ah, you can’t do

var log = console.log;
log('something');

It will throw TypeError: Illegal invocation. log has to be called on the console, so you can do either plain console.log('something'); or var log = console.log.bind(console);. (Or log.call(console, 'something');, but that would kinda defeat the purpose…


#5

Duh. I knew that. I just assumed it was some technicality of babel.

Thanks mucho.