Encountering a peculiar issue while transpiling to ES6 using Babel, where ng-annotate
seems to have an aversion to destructuring. I decided to test my code in the online Babel compiler and it worked without any errors. Interestingly, disabling ng-annotate
in my gulp pipeline removed the error altogether. Even manually injecting annotations by removing /* @ngAnnotate */
from the file didn't have any effect.
Here is the relevant section from my Gulp tasks:
return gulp.src(config.scripts.app)
.pipe(changed(config.dist + '/scripts'))
.pipe(plumber())
.pipe(annotate())
// Filtering and transpiling 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'))
Snippet of the source code causing the issue:
var [min, max] = values.map(val => +val);
// Whether using let or var doesn't seem to matter.
ngModelCtrl.$modelValue = [min, max];
The error stems from a dependency within 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
The problematic function in 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;
}
};
Upon logging out key
and value
in the above function, this is returned:
undefined {
kind: 'var',
node: {
type: 'ArrayPattern',
start: 1178,
end: 1188,
loc: { start: [Object], end: [Object] },
range: [ 1178, 1188 ],
elements: [ [Object], [Object] ]
},
from: 1215
}
It's evident that the key
argument is undefined, but the underlying reason for its significance remains elusive.