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

Add es6 option #241

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ng-annotate-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ module.exports = function ngAnnotate(src, options) {

const quot = options.single_quotes ? "'" : '"';
const re = (options.regexp ? new RegExp(options.regexp) : /^[a-zA-Z0-9_\$\.\s]+$/);
const es6 = options.es6 ? options.es6 : false;
const rename = new stringmap();
if (options.rename) {
options.rename.forEach(function(value) {
Expand Down Expand Up @@ -1080,6 +1081,7 @@ module.exports = function ngAnnotate(src, options) {
locations: true,
ranges: true,
onComment: comments,
sourceType: es6 ? "module" : "script",
});
stats.parser_parse_t1 = Date.now();
} catch(e) {
Expand Down
8 changes: 6 additions & 2 deletions ng-annotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ const optimist = require("optimist")
describe: "use single quotes (') instead of double quotes (\")",
})
.options("regexp", {
describe: "detect short form myMod.controller(...) iff myMod matches regexp",
describe: "detect short form myMod.controller(...) if myMod matches regexp",
})
.options("es6", {
boolean: true,
describe: "enable es6 compatibility",
})
.options("rename", {
describe: "rename declarations and annotated references\n" +
Expand Down Expand Up @@ -132,7 +136,7 @@ function runAnnotate(err, src) {
config.inFile = filename;
}

["add", "remove", "o", "regexp", "rename", "single_quotes", "plugin", "enable", "stats"].forEach(function(opt) {
["add", "remove", "o", "regexp", "es6", "rename", "single_quotes", "plugin", "enable", "stats"].forEach(function(opt) {
if (opt in argv) {
config[opt] = argv[opt];
}
Expand Down
5 changes: 5 additions & 0 deletions run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ function run(ngAnnotate) {
console.log("testing removing existing $inject annotations");
test(slurp("tests/has_inject_removed.js"), ngAnnotate(slurp("tests/has_inject.js"), {remove: true}).src);

console.log("testing es6");
const original_wrapped = slurp("tests/es6.original.js");
const annotated_wrapper = ngAnnotate(original_wrapped, {add: true, es6: true}).src;
test(slurp("tests/es6.annotated.js"), annotated_wrapper, "es6.original.js");

console.log("testing sourcemaps");
const originalSourcemaps = slurp("tests/sourcemaps.coffee");
const compiledSourcemaps = coffee.compile(originalSourcemaps, { sourceFiles: ["sourcemaps.coffee"], generatedFile: "sourcemaps.js", sourceMap: true });
Expand Down
18 changes: 15 additions & 3 deletions scopetools.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ function createScopes(node, parent) {
} else if (node.type === "VariableDeclaration") {
// Variable declarations names goes in current scope
node.declarations.forEach(function(declarator) {
const name = declarator.id.name;
node.$scope.add(name, node.kind, declarator.id, declarator.range[1]);
// Destructuring es6
if(declarator.id.properties) {
declarator.id.properties.forEach(function(destructuredDeclarator) {
const destructVarName = destructuredDeclarator.value.name;
node.$scope.add(destructVarName, node.kind, destructuredDeclarator, destructuredDeclarator.range[1]);
});
// Normal variable declaration
} else {
const variableName = declarator.id.name;
node.$scope.add(variableName, node.kind, declarator.id, declarator.range[1]);
}
});

} else if (isFunction(node)) {
Expand All @@ -61,8 +70,11 @@ function createScopes(node, parent) {
}
}

let paramName;
node.params.forEach(function(param) {
node.$scope.add(param.name, "param", param, null);
// Args default value (es6)
paramName = param.left ? param.left.name : param.name;
node.$scope.add(paramName, "param", param, null);
});

} else if (isForWithConstLet(node) || isForInOfWithConstLet(node)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/es6.annotated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {'test': 'name'}

import angular from 'angular'

angular.module(module.exports.test, ['deps']).factory('Example', ["CustomService", function (CustomService) {
}])

function es6DefaultArg(firstarg = "defaultValue"){}

const {destruc, turing} = {a:1, b:2}
10 changes: 10 additions & 0 deletions tests/es6.original.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {'test': 'name'}

import angular from 'angular'

angular.module(module.exports.test, ['deps']).factory('Example', function (CustomService) {
})

function es6DefaultArg(firstarg = "defaultValue"){}

const {destruc, turing} = {a:1, b:2}