Repository: jshint/jshint
Pull request #3160 (2017-07-16 20:41:16)

[[FIX]] Allow computed method names in obj literal
A quick fix for a bug identified by Test262.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/12415086/badge)](https://coveralls.io/builds/12415086)

Coverage increased (+0.001%) to 97.764% when pulling **e8dd5dd8e673c34218f781b8332ac049fc05bc7a on jugglinmike:computed-method-names** into **1d0486845426191ce2e4a91834c579acb033defa on jshint:master**.

rwaldron: Excellent!

Patch:
diff --git a/src/jshint.js b/src/jshint.js
index 794de19c..52a9f640 100644
--- a/src/jshint.js
+++ b/src/jshint.js
@@ -3274,13 +3274,17 @@ var JSHINT = (function() {
             error("E034");
           }
 
-          i = propertyName();
+          if (state.tokens.next.id === "[") {
+            i = computedPropertyName();
+          } else {
+            i = propertyName();
 
-          // ES6 allows for get() {...} and set() {...} method
-          // definition shorthand syntax, so we don't produce an error
-          // if linting ECMAScript 6 code.
-          if (!i && !state.inES6()) {
-            error("E035");
+            // ES6 allows for get() {...} and set() {...} method
+            // definition shorthand syntax, so we don't produce an error
+            // if linting ECMAScript 6 code.
+            if (!i && !state.inES6()) {
+              error("E035");
+            }
           }
 
           // We don't want to save this getter unless it's an actual getter


Test patch:
diff --git a/tests/test262/expectations.txt b/tests/test262/expectations.txt
index 26a73d02..3fac57d8 100644
--- a/tests/test262/expectations.txt
+++ b/tests/test262/expectations.txt
@@ -221,11 +221,9 @@ built-ins/ArrayIteratorPrototype/next/args-mapped-truncation-before-exhaustion.j
 built-ins/Function/prototype/toString/AsyncFunction.js
 built-ins/Function/prototype/toString/async-function-declaration.js
 built-ins/Function/prototype/toString/async-function-expression.js
-built-ins/Function/prototype/toString/getter-object.js
 built-ins/Function/prototype/toString/line-terminator-normalisation-CR-LF.js
 built-ins/Function/prototype/toString/line-terminator-normalisation-CR.js
 built-ins/Function/prototype/toString/line-terminator-normalisation-LF.js
-built-ins/Function/prototype/toString/setter-object.js
 built-ins/Function/prototype/toString/unicode.js
 built-ins/Array/prototype/every/15.4.4.16-2-17.js
 built-ins/Array/prototype/forEach/15.4.4.18-2-17.js
@@ -405,12 +403,6 @@ language/computed-property-names/class/static/getter-prototype.js
 language/computed-property-names/class/static/method-prototype.js
 language/computed-property-names/class/static/setter-constructor.js
 language/computed-property-names/class/static/setter-prototype.js
-language/computed-property-names/object/accessor/getter-duplicates.js
-language/computed-property-names/object/accessor/getter-super.js
-language/computed-property-names/object/accessor/getter.js
-language/computed-property-names/object/accessor/setter-duplicates.js
-language/computed-property-names/object/accessor/setter-super.js
-language/computed-property-names/object/accessor/setter.js
 language/module-code/early-dup-export-decl.js
 language/module-code/early-dup-export-dflt-id.js
 language/module-code/early-dup-export-id-as.js
@@ -703,11 +695,8 @@ language/expressions/object/dstr-meth-dflt-ary-ptrn-rest-ary-rest.js
 language/expressions/object/dstr-meth-dflt-ary-ptrn-rest-init-id.js
 language/expressions/object/dstr-meth-dflt-ary-ptrn-rest-obj-id.js
 language/expressions/object/dstr-meth-dflt-ary-ptrn-rest-obj-prop-id.js
-language/expressions/object/fn-name-accessor-get.js
-language/expressions/object/fn-name-accessor-set.js
 language/expressions/object/let-non-strict-access.js
 language/expressions/object/let-non-strict-syntax.js
-language/expressions/object/prop-def-id-eval-error.js
 language/expressions/object/scope-gen-meth-param-rest-elem-var-close.js
 language/expressions/object/scope-gen-meth-param-rest-elem-var-open.js
 language/expressions/object/scope-meth-param-rest-elem-var-close.js
@@ -3340,26 +3329,8 @@ language/expressions/super/call-spread-sngl-obj-ident.js
 language/expressions/assignment/dstr-obj-rest-nested-obj-nested-rest.js
 language/expressions/assignment/dstr-obj-rest-nested-obj.js
 language/expressions/assignment/dstr-obj-rest-obj-own-property.js
-language/expressions/object/accessor-name-computed-err-evaluation.js
-language/expressions/object/accessor-name-computed-err-to-prop-key.js
-language/expressions/object/accessor-name-computed-err-unresolvable.js
-language/expressions/object/accessor-name-computed-in.js
 language/expressions/object/accessor-name-computed-yield-expr.js
 language/expressions/object/accessor-name-computed-yield-id.js
-language/expressions/object/accessor-name-computed.js
-language/expressions/object/accessor-name-literal-numeric-binary.js
-language/expressions/object/accessor-name-literal-numeric-exponent.js
-language/expressions/object/accessor-name-literal-numeric-hex.js
-language/expressions/object/accessor-name-literal-numeric-leading-decimal.js
-language/expressions/object/accessor-name-literal-numeric-non-canonical.js
-language/expressions/object/accessor-name-literal-numeric-octal.js
-language/expressions/object/accessor-name-literal-numeric-zero.js
-language/expressions/object/accessor-name-literal-string-char-escape.js
-language/expressions/object/accessor-name-literal-string-double-quote.js
-language/expressions/object/accessor-name-literal-string-empty.js
-language/expressions/object/accessor-name-literal-string-hex-escape.js
-language/expressions/object/accessor-name-literal-string-line-continuation.js
-language/expressions/object/accessor-name-literal-string-single-quote.js
 language/expressions/object/accessor-name-literal-string-unicode-escape.js
 language/expressions/object/dstr-async-gen-meth-ary-init-iter-close.js
 language/expressions/object/dstr-async-gen-meth-ary-init-iter-get-err.js
@@ -4076,7 +4047,6 @@ language/statements/variable/dstr-obj-ptrn-rest-obj-own-property.js
 language/statements/try/dstr-obj-ptrn-rest-nested-obj.js
 language/statements/try/dstr-obj-ptrn-rest-obj-nested-rest.js
 language/statements/try/dstr-obj-ptrn-rest-obj-own-property.js
-language/statements/with/unscopables-inc-dec.js
 language/statements/for-of/dstr-const-obj-ptrn-rest-nested-obj.js
 language/statements/for-of/dstr-const-obj-ptrn-rest-obj-nested-rest.js
 language/statements/for-of/dstr-const-obj-ptrn-rest-obj-own-property.js
diff --git a/tests/unit/parser.js b/tests/unit/parser.js
index 84ad4d79..ef454634 100644
--- a/tests/unit/parser.js
+++ b/tests/unit/parser.js
@@ -5676,9 +5676,13 @@ exports["object ComputedPropertyName"] = function (test) {
     "var c = { [b]: 1 };",
     "var d = { 0: 1 };",
     "var e = { ['s']: 1 };",
+    "var f = { get [0]() {} };",
+    "var g = { set [0](_) {} };",
   ];
 
-  TestRun(test).test(code, { esnext: true });
+  TestRun(test)
+    .addError(19, "Setter is defined without getter.")
+    .test(code, { esnext: true });
 
   TestRun(test)
     .addError(6, "'computed property names' is only available in ES6 (use 'esversion: 6').")
@@ -5691,6 +5695,9 @@ exports["object ComputedPropertyName"] = function (test) {
     .addError(13, "'computed property names' is only available in ES6 (use 'esversion: 6').")
     .addError(15, "'computed property names' is only available in ES6 (use 'esversion: 6').")
     .addError(17, "'computed property names' is only available in ES6 (use 'esversion: 6').")
+    .addError(18, "'computed property names' is only available in ES6 (use 'esversion: 6').")
+    .addError(19, "'computed property names' is only available in ES6 (use 'esversion: 6').")
+    .addError(19, "Setter is defined without getter.")
   .test(code);
 
   test.done();