Skip to content

Commit 057b1c6

Browse files
jugglinmikerwaldron
authored andcommitted
[[FIX]] Tolerate keyword in object shorthand
Ensure that JSHint reports a syntax error rather than crashing.
1 parent ecae54a commit 057b1c6

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/jshint.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -4075,8 +4075,10 @@ var JSHINT = (function() {
40754075
warning("W104", state.tokens.next, "object short notation", "6");
40764076
}
40774077
t = expression(context, 10);
4078-
i = t.value;
4079-
saveProperty(props, i, t);
4078+
i = t && t.value;
4079+
if (t) {
4080+
saveProperty(props, i, t);
4081+
}
40804082

40814083
} else if (peek().id !== ":" && (nextVal === "get" || nextVal === "set")) {
40824084
advance(nextVal);

tests/unit/parser.js

+18
Original file line numberDiff line numberDiff line change
@@ -10647,3 +10647,21 @@ exports.loneNullishCoalescing = function (test) {
1064710647

1064810648
test.done();
1064910649
};
10650+
10651+
// gh-3571: "Cannot read properties of undefined at Object.e.nud"
10652+
exports.keywordAsShorthandObjectProperty = function (test) {
10653+
TestRun(test, "as reported")
10654+
.addError(1, 12, "Expected an identifier and instead saw 'do'.")
10655+
.addError(1, 15, "Missing semicolon.")
10656+
.test("const a = {do}", {esversion: 6});
10657+
10658+
TestRun(test, "simplified")
10659+
.addError(1, 7, "Expected an identifier and instead saw 'do'.")
10660+
.test("void {do};", {esversion: 6});
10661+
10662+
TestRun(test, "alternate - penultimate member")
10663+
.addError(1, 7, "Expected an identifier and instead saw 'for'.")
10664+
.test("void {for, baz};", {esversion: 6});
10665+
10666+
test.done();
10667+
};

0 commit comments

Comments
 (0)