Skip to content

Commit 354fd02

Browse files
committed
Deferred: Don't warn on setting getStackHook to the getErrorHook value
Don't warn if `jQuery.Deferred.getStackHook` is set to the value of `jQuery.Deferred.getErrorHook`. This is to facilitate plugins supporting both jQuery <3.7 and older without triggering Migrate warnings and without requiring complex logic parsing `jQuery.fn.jquery`.
1 parent 0ea1015 commit 354fd02

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/jquery/deferred.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ Object.defineProperty( jQuery.Deferred, "getStackHook", {
4848
},
4949
set: function( newValue ) {
5050
if ( jQuery.migrateIsPatchEnabled( "deferred-getStackHook" ) ) {
51-
migrateWarn( "deferred-getStackHook",
52-
"jQuery.Deferred.getStackHook is removed; use jQuery.Deferred.getErrorHook" );
53-
jQuery.Deferred.getErrorHook = newValue;
51+
52+
// Only warn if `getErrorHook` wasn't set to the same value first.
53+
if ( jQuery.Deferred.getErrorHook !== newValue ) {
54+
migrateWarn( "deferred-getStackHook",
55+
"jQuery.Deferred.getStackHook is removed; use jQuery.Deferred.getErrorHook" );
56+
jQuery.Deferred.getErrorHook = newValue;
57+
}
5458
} else {
5559
unpatchedGetStackHookValue = newValue;
5660
}

test/unit/jquery/deferred.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ QUnit.test( "jQuery.Deferred.getStackHook - getter, no getErrorHook", function(
7676
} );
7777

7878
QUnit.test( "jQuery.Deferred.getStackHook - setter", function( assert ) {
79-
assert.expect( 5 );
79+
assert.expect( 6 );
8080

8181
var exceptionHookSpy,
8282
done = assert.async();
@@ -90,6 +90,11 @@ QUnit.test( "jQuery.Deferred.getStackHook - setter", function( assert ) {
9090
"getStackHook mirrors getErrorHook (setter)" );
9191
} );
9292

93+
expectNoMessage( assert, "jQuery.Deferred.getStackHook - setter", 1, function() {
94+
var mockFn = function() {};
95+
jQuery.Deferred.getStackHook = jQuery.Deferred.getErrorHook = mockFn;
96+
} );
97+
9398
expectMessage( assert, "asyncHook from jQuery.Deferred.getStackHook reported",
9499
1, function() {
95100
jQuery.Deferred.getStackHook = function() {

warnings.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ This is _not_ a warning, but a console log message the plugin shows when it firs
161161

162162
### \[jsonp-promotion\] JQMIGRATE: JSON-to-JSONP auto-promotion is removed
163163

164-
**Cause:** `jQuery.ajax` calls with `dataType: 'json'` with a provided callback are automatically converted by jQuery to JSONP requests unless the options also specify `jsonp: false`. Auto-promoting JSON requests to JSONP introduces a security risk as the developer may be unaware they're not just downloading data but executing code from a remote domain. This auto-promoting behavior is deprecated and will be removed in jQuery 4.0.0.
164+
**Cause:** `jQuery.ajax` calls with `dataType: 'json'` with a provided callback are automatically converted by jQuery to JSONP requests unless the options also specify `jsonp: false`. Auto-promoting JSON requests to JSONP introduces a security risk as the developer may be unaware they're not just downloading data but executing code from a remote domain. This auto-promoting behavior has been removed in jQuery 4.0.0.
165165

166166
**Solution:** To trigger a JSONP request, specify the `dataType: "jsonp"` option.
167167

168168
### \[deferred-getStackHook\] JQMIGRATE: jQuery.Deferred.getStackHook is removed; use jQuery.Deferred.getErrorHook
169169

170-
**Cause:** `jQuery.Deferred.getStackHook` was originally created to pass the stack trace from before an async barrier to report when a user error (like calling a non-existing function) causes a promise to be rejected. However, passing a stack trace doesn't take source maps into account, so we started advising to pass the whole error object. To make it clearer, we also renamed the API to `jQuery.Deferred.getErrorHook`. The legacy alias will be removed in jQuery 4.0.0
170+
**Cause:** `jQuery.Deferred.getStackHook` was originally created to pass the stack trace from before an async barrier to report when a user error (like calling a non-existing function) causes a promise to be rejected. However, passing a stack trace doesn't take source maps into account, so we started advising to pass the whole error object. To make it clearer, we also renamed the API to `jQuery.Deferred.getErrorHook`. The legacy alias has been removed in jQuery 4.0.0.
171171

172-
**Solution:** Rename all usage of `jQuery.Deferred.getStackHook` to `jQuery.Deferred.getErrorHook`. If you previously assigned a function returning an error stack to `jQuery.Deferred.getStackHook` or `jQuery.Deferred.getErrorHook`, change it to return a full error object.
172+
**Solution:** Rename all usage of `jQuery.Deferred.getStackHook` to `jQuery.Deferred.getErrorHook`. If you previously assigned a function returning an error stack to `jQuery.Deferred.getStackHook` or `jQuery.Deferred.getErrorHook`, change it to return a full error object. If you aim to still support jQuery <3.7, assign the hook to `jQuery.Deferred.getErrorHook` first and only later to `jQuery.Deferred.getStackHook` to avoid a Migrate warning.
173173

174174

175175
## Deprecated APIs

0 commit comments

Comments
 (0)