From 8884eb9fcffb2ff207747bdaf7463791e1fd7af2 Mon Sep 17 00:00:00 2001 From: stvcisco Date: Fri, 27 Mar 2020 08:45:06 +0000 Subject: [PATCH] [[FIX]] TypeError accessing 'value' of undefined Make sure that 'decl.first[0]' is defined before accessing its 'value' attribute. No breaking changes. Fixes #3455 --- src/jshint.js | 2 +- tests/unit/core.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/jshint.js b/src/jshint.js index 66193ba25c..8e235b0ba6 100644 --- a/src/jshint.js +++ b/src/jshint.js @@ -5219,7 +5219,7 @@ var JSHINT = (function() { } nolinebreak(state.tokens.curr); advance(";"); - if (decl) { + if (decl && decl.first && decl.first[0]) { if (decl.value === "const" && !decl.hasInitializer) { warning("E012", decl, decl.first[0].value); } diff --git a/tests/unit/core.js b/tests/unit/core.js index ba126e695b..a02be03154 100644 --- a/tests/unit/core.js +++ b/tests/unit/core.js @@ -2352,6 +2352,20 @@ exports.initializeCStyle = function(test) { test.done(); }; +// regression test for gh-3455 +exports.constWithoutVar = function(test) { + TestRun(test) + .addError(1, 11, "Expected an identifier and instead saw ';'.") + .addError(1, 12, "Expected an identifier and instead saw ')'.") + .addError(1, 12, "Expected ';' and instead saw ''.") + .addError(1, 12, "Unrecoverable syntax error. (100% scanned).") + .test([ + "for (const;)" + ], { esversion: 6 }); + + test.done(); +}; + exports.constWithoutInit = function(test) { TestRun(test, "single binding") .addError(1, 6, "const 'x' is initialized to 'undefined'.")