Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ES7 via babel #61

Closed
pago opened this issue Apr 4, 2015 · 11 comments
Closed

Support ES7 via babel #61

pago opened this issue Apr 4, 2015 · 11 comments

Comments

@pago
Copy link

pago commented Apr 4, 2015

I'm currently trying to get Wallaby to work with decorators (new in babel 5.0, stage 1 proposal).

However, since Wallaby tries to read the file even before it runs the preprocessor and does not (yet) support this syntax (even though it handles type annotations), it can not handle the file and throws a syntax error.

Error:

Failed to parse lib/Store.js, SyntaxError: Unexpected character '@' (9:15)

Example:

function Deprecated(target) {
    return target;
}

@Deprecated
class Store {
}

Preprocessor section of wallaby.js:

    preprocessors: {
            '**/*.js': file => {
                if(file.path.match(/jquery|lodash|backbone|velocity|babel/)) {
                    return file.content;
                } else {
                    return babel.transform(file.content, babelOptions);
                }
            }
        },

And finally, the .babelrc:

{
  "stage": 1,
  "loose": true,
  "externalHelpers": true,
  "optional": ["es7.decorators"]
}

Please let me know if you need more information or if there is anything else I can do to help.

@ArtemGovorov
Copy link
Member

You are correct, wallaby supports ES6 and doesn't yet support ES7. It has to instrument code (parse, change and emit) before preprocessors, so has to be able to understand ES7 on all those stages.

While it is technically possible to extend wallaby core to support ES7, before it becomes stable mainstream I'd rather let external tools, such as babel, to provide the support for it in wallaby.

So the good news is that at the moment I'm working on 'compilers' support in wallaby. The concept of compilers is similar to preprocessors, but they will run before wallaby instrumentation. CoffeeScript and TypeScript will be supported via compilers, I will include ES7 via babel compiler as well. This way wallaby core will not have to 'understand' TypeScript/CoffeeScript/ES7 natively, but rather work with (instrument) generated ES5/ES6.

Compilers support is almost ready and will be released some time next week.

@Jabher
Copy link

Jabher commented Apr 5, 2015

+1
Same issue regarding async/await, it's very cool for async testing purposes

e.g.

    it('should get response', async ()=>{
        var resp = await api('users.get', {});
        resp.should.not.be.equal(undefined);
    })

much simplier than

    it('should get response', (done)=>{
        api('users.get', {})
           .then((resp)=>{
              resp.should.not.be.equal(undefined);
              done();
           });
    })

@ArtemGovorov ArtemGovorov changed the title Wallaby tries to read file before transforming Support ES7 via babel Apr 8, 2015
@ArtemGovorov
Copy link
Member

@okonet
Copy link
Contributor

okonet commented Apr 13, 2015

I'm getting something like this:

Failed to run compilers on js/modules/Flux.js, TypeError: unknown: Cannot read property 'start' of null
  at TraversalPath.call (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/path/index.js:457:26)
  at TraversalPath.visit (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/path/index.js:469:10)
  at TraversalContext.visitMultiple (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/context.js:50:16)
  at TraversalContext.visit (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/context.js:71:19)
  at Function.traverse.node (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/index.js:44:17)
  at TraversalPath.visit (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/path/index.js:486:18)
  at TraversalContext.visitSingle (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/context.js:63:41)
  at TraversalContext.visit (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/context.js:73:19)
  at Function.traverse.node (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/index.js:44:17)
  at traverse (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/traversal/index.js:34:14)
  at TransformerPass.transform (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/transformation/transformer-pass.js:70:5)
  at /Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/index.js:544:12
  at arrayEach (/Users/okonet/Projects/adverity/web-app/node_modules/lodash/internal/arrayEach.js:15:9)
  at /Users/okonet/Projects/adverity/web-app/node_modules/lodash/internal/createForEach.js:15:9
  at File.transform (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/index.js:543:5)
  at /Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/index.js:506:13
  at module.exports (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/helpers/parse.js:46:14)
  at File.<anonymous> (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/index.js:505:12)
  at File.parse (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/index.js:460:21)
  at Object.transform (/Users/okonet/Projects/adverity/web-app/node_modules/babel/node_modules/babel-core/lib/babel/transformation/index.js:19:15)
  at Array.forEach (native)
  at emitTwo (events.js:87:13)
  at process.emit (events.js:169:7)
  at handleMessage (child_process.js:306:10)
  at Pipe.channel.onread (child_process.js:334:11)

@ArtemGovorov
Copy link
Member

@okonet I'm trying to reproduce the issue, is js/modules/Flux.js this file /~https://github.com/facebook/flux/blob/master/dist/Flux.js? Probably something else as I can't reproduce it on this file. Could you please share the file where the issue is reproduced?

@okonet
Copy link
Contributor

okonet commented Apr 13, 2015

Og sorry it was a bit misleading...

Actually it has nothing to do with this particular file. I've seen the same error for other files in the project but when I've tried to localize the issue and ignored them...

I'm still not sure what's the problem but it might be webpack post processor (which I use) or something else.

Any way you could gide me?

Here is the config:

var wallabyWebpack = require('wallaby-webpack');
var babel = require('babel');
var webpack = require('webpack');

var wallabyPostprocessor = wallabyWebpack({
      plugins: [
        new webpack.DefinePlugin({
          _PRODUCTION_: true,
        })
      ]
    }
);

module.exports = function (wallaby) {
  return {
    // set `load: false` to all of source files and tests processed by webpack
    // (except external files),
    // as they should not be loaded in browser,
    // their wrapped versions will be loaded instead
    files: [
      { pattern: 'node_modules/chai/chai.js', instrument: false},
      { pattern: 'js/**/*.js*', load: false },
      { pattern: 'js/**/__tests__/*_spec.*', ignore: true }
    ],

    tests: [
      { pattern: 'js/**/__tests__/*_spec.*', load: false }
    ],

    compilers: {
      'js/**/*.js*': wallaby.compilers.babel({
        babel: babel,
        // other babel options
        stage: 0    // https://babeljs.io/docs/usage/experimental/
      }),

      'js/**/*.coffee': wallaby.compilers.coffeeScript({
        // CoffeeScript compiler specific options
      })
    },

    testFramework: "mocha@2.0.1",

    postprocessor: wallabyPostprocessor,

    debug: true,

    bootstrap: function () {
      var mocha = wallaby.testFramework;
      mocha.ui('bdd');
      var expect = chai.expect;
      var should = chai.should();
      // required to trigger tests loading
      window.__moduleBundler.loadTests();
    }
  };
};

I'm using babel@5.1.4

@okonet
Copy link
Contributor

okonet commented Apr 13, 2015

And here is the source of Flux.js

import Flummox from 'flummox';
import NavigationActions from './Navigation/actions/NavigationActions.js';
import NavigationStore from './Navigation/stores/NavigationStore.js';
import EntitiesActions from './EntitySelector/actions/EntitiesActions.js';
import EntitiesStore from './EntitySelector/stores/EntitiesStore.js';

export default class Flux extends Flummox {

    constructor() {
        super();

        this.createActions('navigation', NavigationActions);
        this.createStore('navigation', NavigationStore, this);

        this.createActions('entities', EntitiesActions);
        this.createStore('entities', EntitiesStore, this);
    }

}

@ArtemGovorov
Copy link
Member

Your configuration looks good. I have created the repo /~https://github.com/wallabyjs/wallaby-webpack-es7 with your file structure and your config file. On a couple of simple files it works fine. Could you please have a look and perhaps try to add something that I'm missing to break it?

@okonet
Copy link
Contributor

okonet commented Apr 14, 2015

Ok I've isolated the case. It's related to React and PhantomJS (Function.bind).

BTW I wasn't aware wallaby uses phantomjs...

Related: duojs/test#55

I'll commit failing test and fix to the repository.

@ArtemGovorov
Copy link
Member

Cool, thanks! Yes, it's a known issue, I have described it in the blog post about using React with wallaby.js - http://dm.gl/2015/03/11/wallaby-react/:

phantomPolyfill.js implements Function.prototype.bind that is used in React but not supported by PhantomJs (that is used by wallaby.js to run tests in browser environment).

Is it an issue for you that wallaby.js is using PhantomJs? We are planning to support more browser engines - #29, but it won't be 'headless'.

@okonet
Copy link
Contributor

okonet commented Apr 14, 2015

No, it's not an issue actually. I'm using karma now. But was looking into jsdom lately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants