Skip to content

Commit 77e2245

Browse files
authored
Deferred: Fill in & warn jQuery.Deferred.getStackHook
The API has been replaced by `jQuery.Deferred.getErrorHook`; the legacy name will be removed in jQuery 4.0.0. Fixes gh-483 Closes gh-528
1 parent b27d032 commit 77e2245

File tree

8 files changed

+435
-4
lines changed

8 files changed

+435
-4
lines changed

build/tasks/npmcopy.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const files = {
88

99
"qunit/qunit.js": "qunit/qunit/qunit.js",
1010
"qunit/qunit.css": "qunit/qunit/qunit.css",
11-
"qunit/LICENSE.txt": "qunit/LICENSE.txt"
11+
"qunit/LICENSE.txt": "qunit/LICENSE.txt",
12+
13+
"sinon/sinon.js": "sinon/pkg/sinon.js",
14+
"sinon/LICENSE.txt": "sinon/LICENSE"
1215
};
1316

1417
async function npmcopy() {

eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export default [
9797
Symbol: false,
9898
jQuery: false,
9999
QUnit: false,
100+
sinon: false,
100101
url: false,
101102
expectWarning: false,
102103
expectNoWarning: false,

package-lock.json

+138
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"qunit": "2.21.0",
5858
"rollup": "4.22.4",
5959
"selenium-webdriver": "4.21.0",
60+
"sinon": "9.2.4",
6061
"uglify-js": "3.9.4",
6162
"yargs": "17.7.2"
6263
},

src/jquery/deferred.js

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { migratePatchFunc, migratePatchAndWarnFunc } from "../main.js";
1+
import {
2+
migratePatchFunc,
3+
migratePatchAndWarnFunc,
4+
migrateWarn
5+
} from "../main.js";
6+
import { jQueryVersionSince } from "../compareVersions.js";
27

38
// Support jQuery slim which excludes the deferred module in jQuery 4.0+
49
if ( jQuery.Deferred ) {
510

6-
var oldDeferred = jQuery.Deferred,
11+
var unpatchedGetStackHookValue,
12+
oldDeferred = jQuery.Deferred,
713
tuples = [
814

915
// Action, add listener, callbacks, .then handlers, final state
@@ -63,4 +69,39 @@ migratePatchFunc( jQuery, "Deferred", function( func ) {
6369
// Preserve handler of uncaught exceptions in promise chains
6470
jQuery.Deferred.exceptionHook = oldDeferred.exceptionHook;
6571

72+
// Preserve the optional hook to record the error, if defined
73+
jQuery.Deferred.getErrorHook = oldDeferred.getErrorHook;
74+
75+
// We want to mirror jQuery.Deferred.getErrorHook here, so we cannot use
76+
// existing Migrate utils.
77+
Object.defineProperty( jQuery.Deferred, "getStackHook", {
78+
configurable: true,
79+
enumerable: true,
80+
get: function() {
81+
if ( jQuery.migrateIsPatchEnabled( "deferred-getStackHook" ) ) {
82+
83+
// jQuery 3.x checks `getStackHook` if `getErrorHook` missing;
84+
// don't warn on the getter there.
85+
if ( jQueryVersionSince( "4.0.0" ) ) {
86+
migrateWarn( "deferred-getStackHook",
87+
"jQuery.Deferred.getStackHook is deprecated; " +
88+
"use jQuery.Deferred.getErrorHook" );
89+
}
90+
return jQuery.Deferred.getErrorHook;
91+
} else {
92+
return unpatchedGetStackHookValue;
93+
}
94+
},
95+
set: function( newValue ) {
96+
if ( jQuery.migrateIsPatchEnabled( "deferred-getStackHook" ) ) {
97+
migrateWarn( "deferred-getStackHook",
98+
"jQuery.Deferred.getStackHook is deprecated; " +
99+
"use jQuery.Deferred.getErrorHook" );
100+
jQuery.Deferred.getErrorHook = newValue;
101+
} else {
102+
unpatchedGetStackHookValue = newValue;
103+
}
104+
}
105+
} );
106+
66107
}

test/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<!-- QUnit -->
1111
<link rel="stylesheet" href="../external/qunit/qunit.css" media="screen">
1212
<script src="../external/qunit/qunit.js"></script>
13+
<script src="../external/sinon/sinon.js"></script>
1314

1415
<!-- A promise polyfill -->
1516
<script src="../external/npo/npo.js"></script>

0 commit comments

Comments
 (0)