Skip to content

Commit

Permalink
[[FIX]] Tolerate late definition of async function (#3618)
Browse files Browse the repository at this point in the history
We used to mark `async function` as unreachable instead of treating it
as any other late function definition. This change makes jshint react to
`async` keyword in the same way as it reacted to `function` keyword.
  • Loading branch information
pirxpilot committed Jun 27, 2022
1 parent 61c868c commit 5c256a2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1850,8 +1850,9 @@ var JSHINT = (function() {
if (t.reach) {
return;
}

if (t.id !== "(endline)") {
if (t.id === "function") {
if (isFunction(t, i)) {
if (state.option.latedef === true) {
warning("W026", t);
}
Expand All @@ -1862,6 +1863,16 @@ var JSHINT = (function() {
break;
}
}

function isFunction(t, i) {
if (t.id === "function") {
return true;
}
if (t.id === "async") {
t = peek(i);
return t.id === "function";
}
}
}

/**
Expand Down
48 changes: 47 additions & 1 deletion tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8268,7 +8268,53 @@ exports.unreachable = {
.test(src);

test.done();
}
},
'async function': function (test) {
var src = [
"(function() {",
" return f(4);",
" async function f(p) {",
" return p + 1;",
" }",
"}());"
];

TestRun(test)
.test(src, { esversion: 8, latedef: 'nofunc' });

test.done();
},
"async identifier": function (test) {
var src = [
"(function() {",
" return f(4);",
" async();",
"}());"
];

TestRun(test)
.addError(3, 3, "Unreachable 'async' after 'return'.")
.test(src, { esversion: 8, latedef: "nofunc" });

test.done();
},
"async asi": function (test) {
var src = [
"(function() {",
" return f(4);",
" async",
" function f(p) {",
" return p + 1;",
" }",
"}());"
];

TestRun(test)
.addError(3, 3, "Unreachable 'async' after 'return'.")
.test(src, { esversion: 8, latedef: "nofunc", asi: true, expr: true });

test.done();
},
};

exports["test for 'break' in switch case + curly braces"] = function (test) {
Expand Down

0 comments on commit 5c256a2

Please sign in to comment.