aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/parse.js7
-rw-r--r--test/compress/issue-12.js47
-rw-r--r--test/mocha/getter-setter.js89
3 files changed, 143 insertions, 0 deletions
diff --git a/lib/parse.js b/lib/parse.js
index 2218c00c..f1495153 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -1178,6 +1178,13 @@ function parse($TEXT, options) {
break;
}
break;
+ case "operator":
+ if (!is_identifier_string(tok.value)) {
+ throw new JS_Parse_Error("Invalid getter/setter name: " + tok.value,
+ tok.file, tok.line, tok.col, tok.pos);
+ }
+ ret = _make_symbol(AST_SymbolRef);
+ break;
}
next();
return ret;
diff --git a/test/compress/issue-12.js b/test/compress/issue-12.js
index bf87d5c0..e2d8bda7 100644
--- a/test/compress/issue-12.js
+++ b/test/compress/issue-12.js
@@ -9,3 +9,50 @@ keep_name_of_setter: {
input: { a = { set foo () {} } }
expect: { a = { set foo () {} } }
}
+
+setter_with_operator_keys: {
+ input: {
+ var tokenCodes = {
+ get instanceof(){
+ return test0;
+ },
+ set instanceof(value){
+ test0 = value;
+ },
+ set typeof(value){
+ test1 = value;
+ },
+ get typeof(){
+ return test1;
+ },
+ set else(value){
+ test2 = value;
+ },
+ get else(){
+ return test2;
+ }
+ };
+ }
+ expect: {
+ var tokenCodes = {
+ get instanceof(){
+ return test0;
+ },
+ set instanceof(value){
+ test0 = value;
+ },
+ set typeof(value){
+ test1 = value;
+ },
+ get typeof(){
+ return test1;
+ },
+ set else(value){
+ test2 = value;
+ },
+ get else(){
+ return test2;
+ }
+ };
+ }
+} \ No newline at end of file
diff --git a/test/mocha/getter-setter.js b/test/mocha/getter-setter.js
new file mode 100644
index 00000000..641a2026
--- /dev/null
+++ b/test/mocha/getter-setter.js
@@ -0,0 +1,89 @@
+var UglifyJS = require('../../');
+var assert = require("assert");
+
+describe("Getters and setters", function() {
+ it("Should not accept operator symbols as getter/setter name", function() {
+ var illegalOperators = [
+ "++",
+ "--",
+ "+",
+ "-",
+ "!",
+ "~",
+ "&",
+ "|",
+ "^",
+ "*",
+ "/",
+ "%",
+ ">>",
+ "<<",
+ ">>>",
+ "<",
+ ">",
+ "<=",
+ ">=",
+ "==",
+ "===",
+ "!=",
+ "!==",
+ "?",
+ "=",
+ "+=",
+ "-=",
+ "/=",
+ "*=",
+ "%=",
+ ">>=",
+ "<<=",
+ ">>>=",
+ "|=",
+ "^=",
+ "&=",
+ "&&",
+ "||"
+ ];
+ var generator = function() {
+ var results = [];
+
+ for (var i in illegalOperators) {
+ results.push({
+ code: "var obj = { get " + illegalOperators[i] + "() { return test; }};",
+ operator: illegalOperators[i],
+ method: "get"
+ });
+ results.push({
+ code: "var obj = { set " + illegalOperators[i] + "(value) { test = value}};",
+ operator: illegalOperators[i],
+ method: "set"
+ });
+ }
+
+ return results;
+ };
+
+ var testCase = function(data) {
+ return function() {
+ UglifyJS.parse(data.code);
+ };
+ };
+
+ var fail = function(data) {
+ return function (e) {
+ return e instanceof UglifyJS.JS_Parse_Error &&
+ e.message === "Invalid getter/setter name: " + data.operator;
+ };
+ };
+
+ var errorMessage = function(data) {
+ return "Expected but didn't get a syntax error while parsing following line:\n" + data.code;
+ };
+
+ var tests = generator();
+ for (var i = 0; i < tests.length; i++) {
+ var test = tests[i];
+ assert.throws(testCase(test), fail(test), errorMessage(test));
+ }
+ });
+
+});