Skip to content

Commit 5c256a2

Browse files
authored
[[FIX]] Tolerate late definition of async function (#3618)
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.
1 parent 61c868c commit 5c256a2

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/jshint.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1850,8 +1850,9 @@ var JSHINT = (function() {
18501850
if (t.reach) {
18511851
return;
18521852
}
1853+
18531854
if (t.id !== "(endline)") {
1854-
if (t.id === "function") {
1855+
if (isFunction(t, i)) {
18551856
if (state.option.latedef === true) {
18561857
warning("W026", t);
18571858
}
@@ -1862,6 +1863,16 @@ var JSHINT = (function() {
18621863
break;
18631864
}
18641865
}
1866+
1867+
function isFunction(t, i) {
1868+
if (t.id === "function") {
1869+
return true;
1870+
}
1871+
if (t.id === "async") {
1872+
t = peek(i);
1873+
return t.id === "function";
1874+
}
1875+
}
18651876
}
18661877

18671878
/**

tests/unit/parser.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -8268,7 +8268,53 @@ exports.unreachable = {
82688268
.test(src);
82698269

82708270
test.done();
8271-
}
8271+
},
8272+
'async function': function (test) {
8273+
var src = [
8274+
"(function() {",
8275+
" return f(4);",
8276+
" async function f(p) {",
8277+
" return p + 1;",
8278+
" }",
8279+
"}());"
8280+
];
8281+
8282+
TestRun(test)
8283+
.test(src, { esversion: 8, latedef: 'nofunc' });
8284+
8285+
test.done();
8286+
},
8287+
"async identifier": function (test) {
8288+
var src = [
8289+
"(function() {",
8290+
" return f(4);",
8291+
" async();",
8292+
"}());"
8293+
];
8294+
8295+
TestRun(test)
8296+
.addError(3, 3, "Unreachable 'async' after 'return'.")
8297+
.test(src, { esversion: 8, latedef: "nofunc" });
8298+
8299+
test.done();
8300+
},
8301+
"async asi": function (test) {
8302+
var src = [
8303+
"(function() {",
8304+
" return f(4);",
8305+
" async",
8306+
" function f(p) {",
8307+
" return p + 1;",
8308+
" }",
8309+
"}());"
8310+
];
8311+
8312+
TestRun(test)
8313+
.addError(3, 3, "Unreachable 'async' after 'return'.")
8314+
.test(src, { esversion: 8, latedef: "nofunc", asi: true, expr: true });
8315+
8316+
test.done();
8317+
},
82728318
};
82738319

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

0 commit comments

Comments
 (0)