Repository: jshint/jshint
Pull request #3634 (2022-11-07 00:46:05)

[[FIX]] Correct error message
With the exception of `for..of` and `for..in` heads, constant binding declarations which lack an initializer are syntactically invalid. Despite this, the warning emitted for the construction suggests that the problem lies in runtime behavior. In fact, the constant is not initialized to any value because the code is never evaluated.

Rephrase the warning to more accurately describe the issue.


Patch:
diff --git a/src/jshint.js b/src/jshint.js
index 480a21b8..c02e77b8 100644
--- a/src/jshint.js
+++ b/src/jshint.js
@@ -4499,7 +4499,7 @@ var JSHINT = (function() {
         var id = state.tokens.prev;
         value = expression(context, 10);
         if (value) {
-          if (value.identifier && value.value === "undefined") {
+          if (!isConst && value.identifier && value.value === "undefined") {
             warning("W080", id, id.value);
           }
           if (!lone) {
diff --git a/src/messages.js b/src/messages.js
index 113586b0..4855108f 100644
--- a/src/messages.js
+++ b/src/messages.js
@@ -21,7 +21,7 @@ var errors = {
 
   // Constants
   E011: "'{a}' has already been declared.",
-  E012: "const '{a}' is initialized to 'undefined'.",
+  E012: "Missing initializer for constant '{a}'.",
   E013: "Attempting to override '{a}' which is a constant.",
 
   // Regular expressions


Test patch:
diff --git a/tests/unit/core.js b/tests/unit/core.js
index 139eeb01..47c723ba 100644
--- a/tests/unit/core.js
+++ b/tests/unit/core.js
@@ -847,16 +847,21 @@ exports.testUndefinedAssignment = function (test) {
 
   TestRun(test)
     .addError(1, 5, "It's not necessary to initialize 'x' to 'undefined'.")
-    .addError(2, 7, "It's not necessary to initialize 'y' to 'undefined'.")
     .addError(3, 5, "It's not necessary to initialize 'z' to 'undefined'.")
     .addError(4, 9, "It's not necessary to initialize 'a' to 'undefined'.")
-    .addError(6, 9, "It's not necessary to initialize 'c' to 'undefined'.")
     .addError(7, 7, "It's not necessary to initialize 'd' to 'undefined'.")
     .addError(9, 9, "It's not necessary to initialize 'f' to 'undefined'.")
-    .addError(10, 11, "It's not necessary to initialize 'g' to 'undefined'.")
     .addError(11, 9, "It's not necessary to initialize 'h' to 'undefined'.")
     .test(src, {esnext: true});
 
+  TestRun(test)
+    .addError(1, 8, "It's not necessary to initialize 'x' to 'undefined'.")
+    .addError(2, 8, "It's not necessary to initialize 'y' to 'undefined'.")
+    .test([
+      "const {x = undefined} = {};",
+      "const [y = undefined] = [];",
+    ], {esversion: 6});
+
   test.done();
 };
 
@@ -1165,8 +1170,8 @@ exports.testES6ModulesNamedExportsAffectUnused = function (test) {
   ];
 
   TestRun(test)
-    .addError(19, 14, "const 'c1u' is initialized to 'undefined'.")
-    .addError(19, 19, "const 'c2u' is initialized to 'undefined'.")
+    .addError(19, 14, "Missing initializer for constant 'c1u'.")
+    .addError(19, 19, "Missing initializer for constant 'c2u'.")
     .test(src1, {
       esnext: true,
       unused: true
@@ -2565,7 +2570,7 @@ exports.constWithoutVar = function(test) {
 
 exports.constWithoutInit = function(test) {
   TestRun(test, "single binding")
-    .addError(1, 6, "const 'x' is initialized to 'undefined'.")
+    .addError(1, 6, "Missing initializer for constant 'x'.")
     .test([
       "for (const x; ;) {",
       "  void x;",
@@ -2573,7 +2578,7 @@ exports.constWithoutInit = function(test) {
     ], { esversion: 6 });
 
   TestRun(test, "multiple bindings")
-    .addError(1, 6, "const 'y' is initialized to 'undefined'.")
+    .addError(1, 6, "Missing initializer for constant 'y'.")
     .test([
       "for (const y, z; ;) {",
       "  void (y, z);",
diff --git a/tests/unit/options.js b/tests/unit/options.js
index ca0d5432..defcc396 100644
--- a/tests/unit/options.js
+++ b/tests/unit/options.js
@@ -2488,11 +2488,11 @@ exports.esnext = function (test) {
   ];
 
   TestRun(test)
-    .addError(21, 7, "const 'immutable4' is initialized to 'undefined'.")
+    .addError(21, 7, "Missing initializer for constant 'immutable4'.")
     .test(src, { esnext: true });
 
   TestRun(test)
-    .addError(21, 7, "const 'immutable4' is initialized to 'undefined'.")
+    .addError(21, 7, "Missing initializer for constant 'immutable4'.")
     .test(src, { moz: true });
 
   TestRun(test)