使用Babel和解构进行ng-annotate错误

Phi*_*hix 16 javascript destructuring angularjs babeljs ng-annotate

使用Babel转换为ES6时出现奇怪的错误,ng-annotate不喜欢解构.我将我的源码复制到在线babel编译器中,它运行正常.ng-annotate在我的gulp管道链中注释掉了错误.删除/* @ngAnnotate */文件中的注释并手动注入也不会改变任何内容.

Gulp部分:

return gulp.src(config.scripts.app)
    .pipe(changed(config.dist + '/scripts'))
    .pipe(plumber())
    .pipe(annotate())

    // Filter out and transpile only .es6.js files
    .pipe(es6)
    .pipe(babel({
        presets: ['es2015'],
        plugins: ['extensible-destructuring'],
        comments: false
    }))
    .pipe(es6.restore)

    .pipe(concat('scripts.js'))
    .pipe(gulp.dest(config.dist + '/scripts'))
Run Code Online (Sandbox Code Playgroud)

有问题的来源:

var [min, max] = values.map(val => +val);
// let/var doesn't make a difference.

ngModelCtrl.$modelValue = [min, max];
Run Code Online (Sandbox Code Playgroud)

错误来自依赖ng-annotate:

Error: StringMap expected string key
    at stringmap.set (/Users/macbookair/Places/app/places/frontend/node_modules/gulp-ng-annotate/node_modules/ng-annotate/node_modules/stringmap/stringmap.js:101:19)
    at Scope.add (/Users/macbookair/Places/app/places/frontend/node_modules/gulp-ng-annotate/node_modules/ng-annotate/build/es5/scope.js:102:17)
    at /Users/macbookair/Places/app/places/frontend/node_modules/gulp-ng-annotate/node_modules/ng-annotate/build/es5/scopetools.js:38:25
    at Array.forEach (native)
    .... more
Run Code Online (Sandbox Code Playgroud)

stringmap.js 有问题的功能:

stringmap.prototype.set = function(key, value) {    
    if (typeof key !== "string") {
        throw new Error("StringMap expected string key");
    }
    if (key === "__proto__") {
        this.hasProto = true;
        this.proto = value;
    } else {
        this.obj[key] = value;
    }
};
Run Code Online (Sandbox Code Playgroud)

注销keyvalue在上面的函数中返回:

undefined { 
    kind: 'var', 
    node: { 
        type: 'ArrayPattern',
        start: 1178,
        end: 1188,
        loc: { start: [Object], end: [Object] },
        range: [ 1178, 1188 ],
        elements: [ [Object], [Object] ] 
    },
    from: 1215 
}
Run Code Online (Sandbox Code Playgroud)

很明显,这个key论点是不确定的,但为什么它甚至关心呢?

Dev*_*der 0

我有类似的问题,我只是简单地将 gulp 管道中的顺序更改为: babel --> annotate。ng-annotate 不支持新的 JS 版本:github.com/olov/ng-annotate/issues/237

最初由 @illagrenan 在上面的评论部分回答