Skip to content

Commit

Permalink
[[FIX]] Allow initializing const bindings to undef
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jugglinmike authored and rwaldron committed Nov 10, 2022
1 parent e7071e0 commit fedaf6f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4499,7 +4499,7 @@ var JSHINT = (function() {
var id = state.tokens.prev;
value = expression(context, 10);
if (value) {
if (value.identifier && value.value === "undefined") {

This comment has been minimized.

Copy link
@LolyPopToyou

LolyPopToyou Jul 19, 2023

.if ( ! isConst && значение . идентификатор && значение . значение === "undefined" )

if (!isConst && value.identifier && value.value === "undefined") {
warning("W080", id, id.value);
}
if (!lone) {
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down

0 comments on commit fedaf6f

Please sign in to comment.