Skip to content

Commit 65978c9

Browse files
authored
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`. Closes gh-578 Ref gh-577
1 parent c923c6b commit 65978c9

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/jquery/deferred.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ Object.defineProperty( jQuery.Deferred, "getStackHook", {
8888
},
8989
set: function( newValue ) {
9090
if ( jQuery.migrateIsPatchEnabled( "deferred-getStackHook" ) ) {
91-
migrateWarn( "deferred-getStackHook",
92-
"jQuery.Deferred.getStackHook is deprecated; " +
91+
92+
// Only warn if `getErrorHook` wasn't set to the same value first.
93+
if ( jQuery.Deferred.getErrorHook !== newValue ) {
94+
migrateWarn( "deferred-getStackHook",
95+
"jQuery.Deferred.getStackHook is deprecated; " +
9396
"use jQuery.Deferred.getErrorHook" );
94-
jQuery.Deferred.getErrorHook = newValue;
97+
jQuery.Deferred.getErrorHook = newValue;
98+
}
9599
} else {
96100
unpatchedGetStackHookValue = newValue;
97101
}

test/unit/jquery/deferred.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ QUnit.test( "jQuery.Deferred.getStackHook - getter, no getErrorHook", function(
7777
} );
7878

7979
QUnit.test( "jQuery.Deferred.getStackHook - setter", function( assert ) {
80-
assert.expect( 5 );
80+
assert.expect( 6 );
8181

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

94+
expectNoWarning( assert, "jQuery.Deferred.getStackHook - setter", 1, function() {
95+
var mockFn = function() {};
96+
jQuery.Deferred.getStackHook = jQuery.Deferred.getErrorHook = mockFn;
97+
} );
98+
9499
expectWarning( assert, "asyncHook from jQuery.Deferred.getStackHook reported",
95100
1, function() {
96101
jQuery.Deferred.getStackHook = function() {

warnings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,6 @@ See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58
319319

320320
### \[deferred-getStackHook\] JQMIGRATE: jQuery.Deferred.getStackHook is deprecated; use jQuery.Deferred.getErrorHook
321321

322-
**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
322+
**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.
323323

324-
**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.
324+
**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.

0 commit comments

Comments
 (0)