Skip to content

Commit fedaf6f

Browse files
jugglinmikerwaldron
authored andcommitted
[[FIX]] Allow initializing const bindings to undef
Warning W080 is inappropriate for constant bindings because, unlike `var` and `let` declarations, `const` requires an initializer. Relax the emission of W080 to allow initializing constant bindings with `undefined`. Add unit tests to verify that the warning continues to be emitted for constant bindings declared in ES2015 destructuring patterns.
1 parent e7071e0 commit fedaf6f

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/jshint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4499,7 +4499,7 @@ var JSHINT = (function() {
44994499
var id = state.tokens.prev;
45004500
value = expression(context, 10);
45014501
if (value) {
4502-
if (value.identifier && value.value === "undefined") {
4502+
if (!isConst && value.identifier && value.value === "undefined") {
45034503
warning("W080", id, id.value);
45044504
}
45054505
if (!lone) {

tests/unit/core.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,16 +847,21 @@ exports.testUndefinedAssignment = function (test) {
847847

848848
TestRun(test)
849849
.addError(1, 5, "It's not necessary to initialize 'x' to 'undefined'.")
850-
.addError(2, 7, "It's not necessary to initialize 'y' to 'undefined'.")
851850
.addError(3, 5, "It's not necessary to initialize 'z' to 'undefined'.")
852851
.addError(4, 9, "It's not necessary to initialize 'a' to 'undefined'.")
853-
.addError(6, 9, "It's not necessary to initialize 'c' to 'undefined'.")
854852
.addError(7, 7, "It's not necessary to initialize 'd' to 'undefined'.")
855853
.addError(9, 9, "It's not necessary to initialize 'f' to 'undefined'.")
856-
.addError(10, 11, "It's not necessary to initialize 'g' to 'undefined'.")
857854
.addError(11, 9, "It's not necessary to initialize 'h' to 'undefined'.")
858855
.test(src, {esnext: true});
859856

857+
TestRun(test)
858+
.addError(1, 8, "It's not necessary to initialize 'x' to 'undefined'.")
859+
.addError(2, 8, "It's not necessary to initialize 'y' to 'undefined'.")
860+
.test([
861+
"const {x = undefined} = {};",
862+
"const [y = undefined] = [];",
863+
], {esversion: 6});
864+
860865
test.done();
861866
};
862867

0 commit comments

Comments
 (0)