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

Feat support acorn options sourceType and ecmaVersion #259

Closed
wants to merge 2 commits 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
5 changes: 5 additions & 0 deletions OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ Use the `--sourcemap` option to generate an inline sourcemap.

Use the `--sourceroot` option to set the sourceRoot property of the generated sourcemap.

Use the `--sourcetype` option to indicate the mode the code should be parsed in.
Can be either "script" or "module". This influences global strict mode and parsing of import and export declarations.

Use the `--ecmaversion` option to indicate the ECMAScript version to parse. Must be either 3, 5, 6 (2015), 7 (2016), or 8 (2017). This influences support for strict mode, the set of reserved words, and support for new syntax features. Default is 7.

Use the `--single_quotes` option to output `'$scope'` instead of `"$scope"`.

Use the `--regexp` option to restrict matching further or to expand matching.
Expand Down
3 changes: 2 additions & 1 deletion ng-annotate-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,8 @@ module.exports = function ngAnnotate(src, options) {
stats.parser_parse_t0 = Date.now();
// acorn
ast = parser(src, {
ecmaVersion: 6,
ecmaVersion: options.ecmaversion || 6,
sourceType: options.sourcetype || 'script',
allowReserved: true,
locations: true,
ranges: true,
Expand Down
10 changes: 9 additions & 1 deletion ng-annotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ const optimist = require("optimist")
.options("sourceroot", {
describe: "set the sourceRoot property of the generated sourcemap"
})
.options("sourcetype", {
describe: "mode the code should be parsed in (\"script\" or \"module\")",
})
.options("ecmaversion", {
describe: "ECMAScript version to parse\n" +
"Must be either 3, 5, 6 (2015), 7 (2016), or 8 (2017).",
})
.options("single_quotes", {
boolean: true,
describe: "use single quotes (') instead of double quotes (\")",
Expand Down Expand Up @@ -132,7 +139,8 @@ function runAnnotate(err, src) {
config.inFile = filename;
}

["add", "remove", "o", "regexp", "rename", "single_quotes", "plugin", "enable", "stats"].forEach(function(opt) {
["add", "remove", "o", "regexp", "rename", "single_quotes", "plugin",
"enable", "stats", "sourcetype", "ecmaversion"].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 @@ -151,6 +151,11 @@ function run(ngAnnotate) {
console.log("testing removing annotations (imported tests)");
test(ngminOriginal, ngAnnotate(ngminAnnotated, {remove: true, regexp: "^myMod"}).src, "ngmin_original.js");

console.log("testing sourcetype 'module'");
const stm = slurp("tests/sourcetype-module.js");
const stmAnnotated = ngAnnotate(stm, { add: true, sourcetype: "module" }).src;
test(slurp("tests/sourcetype-module.annotated.js"), stmAnnotated, "sourcetype-module.annotated.js");

// TODO generic test-runner code for finding and testing all optionals automatically
// optionals angular-dashboard-framework adding annotations
console.log("testing optionals/angular-dashboard-framework.js (adding annotations)");
Expand Down
13 changes: 13 additions & 0 deletions tests/sourcetype-module.annotated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var AppController = (function () {
AppController.$inject = ["$scope"];
function AppController($scope) {
"ngInject";
this.$scope = $scope;
this.title = 'AppController';
}
AppController.prototype.$onInit = function () {
console.log(this.$scope);
};
return AppController;
}());
export { AppController };
12 changes: 12 additions & 0 deletions tests/sourcetype-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var AppController = (function () {
function AppController($scope) {
"ngInject";
this.$scope = $scope;
this.title = 'AppController';
}
AppController.prototype.$onInit = function () {
console.log(this.$scope);
};
return AppController;
}());
export { AppController };