Skip to content

Commit f964e09

Browse files
committed
WIP All: Support selectively disabling Migrate patches
Fixes gh-449
1 parent 3ed29b0 commit f964e09

15 files changed

+289
-188
lines changed

src/disableWarnings.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// A map from disabled patch codes to `true`. This should really
2+
// be a `Set` but those are unsupported in IE.
3+
export var disabledPatches = Object.create( null );
4+
5+
// Don't apply patches for specified codes. Helpful for code bases
6+
// where some Migrate warnings have been addressed and it's desirable
7+
// to avoid needless patches or false positives.
8+
jQuery.migrateDisablePatches = function() {
9+
var i;
10+
for ( i = 0; i < arguments.length; i++ ) {
11+
disabledPatches[ arguments[ i ] ] = true;
12+
}
13+
};
14+
15+
// Allow enabling patches disabled via `jQuery.migrateDisablePatches`.
16+
// Helpful if you want to disable a patch only for some code that won't
17+
// be updated soon to be able to focus on other warnings - and enable it
18+
// immediately after such a call:
19+
// ```js
20+
// jQuery.migrateDisablePatches( "workaroundA" );
21+
// elem.pluginViolatingWarningA( "pluginMethod" );
22+
// jQuery.migrateEnablePatches( "workaroundA" );
23+
// ```
24+
jQuery.migrateEnablePatches = function() {
25+
var i;
26+
for ( i = 0; i < arguments.length; i++ ) {
27+
delete disabledPatches[ arguments[ i ] ];
28+
}
29+
};

src/jquery/ajax.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ jQuery.ajax = function( ) {
1212

1313
// Be sure we got a jQXHR (e.g., not sync)
1414
if ( jQXHR.promise ) {
15-
migrateWarnFunc( jQXHR, "success", jQXHR.done,
15+
migrateWarnFunc( jQXHR, "success", jQXHR.done, "jqxhr-methods",
1616
"jQXHR.success is deprecated and removed" );
17-
migrateWarnFunc( jQXHR, "error", jQXHR.fail,
17+
migrateWarnFunc( jQXHR, "error", jQXHR.fail, "jqxhr-methods",
1818
"jQXHR.error is deprecated and removed" );
19-
migrateWarnFunc( jQXHR, "complete", jQXHR.always,
19+
migrateWarnFunc( jQXHR, "complete", jQXHR.always, "jqxhr-methods",
2020
"jQXHR.complete is deprecated and removed" );
2121
}
2222

@@ -40,7 +40,7 @@ if ( !jQueryVersionSince( "4.0.0" ) ) {
4040
.indexOf( "application/x-www-form-urlencoded" ) === 0 &&
4141
rjsonp.test( s.data )
4242
) ) {
43-
migrateWarn( "JSON-to-JSONP auto-promotion is deprecated" );
43+
migrateWarn( "jsonp-promotion", "JSON-to-JSONP auto-promotion is deprecated" );
4444
}
4545
} );
4646
}

src/jquery/attributes.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { migrateWarn } from "../main.js";
1+
import { migratePatchFunc, migrateWarn } from "../main.js";
2+
import { disabledPatches } from "../disableWarnings.js";
23

34
var oldRemoveAttr = jQuery.fn.removeAttr,
45
oldToggleClass = jQuery.fn.toggleClass,
@@ -7,24 +8,28 @@ var oldRemoveAttr = jQuery.fn.removeAttr,
78
jQuery.fn.removeAttr = function( name ) {
89
var self = this;
910

10-
jQuery.each( name.match( rmatchNonSpace ), function( _i, attr ) {
11-
if ( jQuery.expr.match.bool.test( attr ) ) {
12-
migrateWarn( "jQuery.fn.removeAttr no longer sets boolean properties: " + attr );
13-
self.prop( attr, false );
14-
}
15-
} );
11+
if ( !disabledPatches[ "removeattr-bool" ] ) {
12+
jQuery.each( name.match( rmatchNonSpace ), function( _i, attr ) {
13+
if ( jQuery.expr.match.bool.test( attr ) ) {
14+
migrateWarn( "removeattr-bool",
15+
"jQuery.fn.removeAttr no longer sets boolean properties: " + attr );
16+
self.prop( attr, false );
17+
}
18+
} );
19+
}
1620

1721
return oldRemoveAttr.apply( this, arguments );
1822
};
1923

20-
jQuery.fn.toggleClass = function( state ) {
24+
migratePatchFunc( jQuery.fn, "toggleClass", function( state ) {
2125

2226
// Only deprecating no-args or single boolean arg
2327
if ( state !== undefined && typeof state !== "boolean" ) {
28+
2429
return oldToggleClass.apply( this, arguments );
2530
}
2631

27-
migrateWarn( "jQuery.fn.toggleClass( boolean ) is deprecated" );
32+
migrateWarn( "toogleclass-bool", "jQuery.fn.toggleClass( boolean ) is deprecated" );
2833

2934
// Toggle entire class name of each element
3035
return this.each( function() {
@@ -46,4 +51,4 @@ jQuery.fn.toggleClass = function( state ) {
4651
);
4752
}
4853
} );
49-
};
54+
}, "toogleclass-bool" );

src/jquery/core.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { jQueryVersionSince } from "../compareVersions.js";
22
import { migrateWarn, migrateWarnFunc, migrateWarnProp } from "../main.js";
3+
import { disabledPatches } from "../disableWarnings.js";
34

45
var findProp,
56
class2type = {},
@@ -16,23 +17,26 @@ var findProp,
1617
jQuery.fn.init = function( arg1 ) {
1718
var args = Array.prototype.slice.call( arguments );
1819

19-
if ( typeof arg1 === "string" && arg1 === "#" ) {
20+
if ( !disabledPatches[ "code-TODO" ] && typeof arg1 === "string" && arg1 === "#" ) {
2021

21-
// JQuery( "#" ) is a bogus ID selector, but it returned an empty set before jQuery 3.0
22-
migrateWarn( "jQuery( '#' ) is not a valid selector" );
22+
// JQuery( "#" ) is a bogus ID selector, but it returned an empty set
23+
// before jQuery 3.0
24+
migrateWarn( "code-TODO", "jQuery( '#' ) is not a valid selector" );
2325
args[ 0 ] = [];
2426
}
2527

2628
return oldInit.apply( this, args );
2729
};
30+
2831
jQuery.fn.init.prototype = jQuery.fn;
2932

3033
jQuery.find = function( selector ) {
3134
var args = Array.prototype.slice.call( arguments );
3235

3336
// Support: PhantomJS 1.x
3437
// String#match fails to match when used with a //g RegExp, only on some strings
35-
if ( typeof selector === "string" && rattrHashTest.test( selector ) ) {
38+
if ( !disabledPatches[ "selector-hash" ] &&
39+
typeof selector === "string" && rattrHashTest.test( selector ) ) {
3640

3741
// The nonstandard and undocumented unquoted-hash was removed in jQuery 1.12.0
3842
// First see if qS thinks it's a valid selector, if so avoid a false positive
@@ -49,10 +53,12 @@ jQuery.find = function( selector ) {
4953
// Note that there may be false alarms if selector uses jQuery extensions
5054
try {
5155
window.document.querySelector( selector );
52-
migrateWarn( "Attribute selector with '#' must be quoted: " + args[ 0 ] );
56+
migrateWarn( "selector-hash",
57+
"Attribute selector with '#' must be quoted: " + args[ 0 ] );
5358
args[ 0 ] = selector;
5459
} catch ( err2 ) {
55-
migrateWarn( "Attribute selector with '#' was not fixed: " + args[ 0 ] );
60+
migrateWarn( "selector-hash",
61+
"Attribute selector with '#' was not fixed: " + args[ 0 ] );
5662
}
5763
}
5864
}
@@ -70,24 +76,24 @@ for ( findProp in oldFind ) {
7076
// The number of elements contained in the matched element set
7177
migrateWarnFunc( jQuery.fn, "size", function() {
7278
return this.length;
73-
},
79+
}, "size",
7480
"jQuery.fn.size() is deprecated and removed; use the .length property" );
7581

7682
migrateWarnFunc( jQuery, "parseJSON", function() {
7783
return JSON.parse.apply( null, arguments );
78-
},
84+
}, "parse-json",
7985
"jQuery.parseJSON is deprecated; use JSON.parse" );
8086

8187
migrateWarnFunc( jQuery, "holdReady", jQuery.holdReady,
82-
"jQuery.holdReady is deprecated" );
88+
"hold-ready", "jQuery.holdReady is deprecated" );
8389

8490
migrateWarnFunc( jQuery, "unique", jQuery.uniqueSort,
85-
"jQuery.unique is deprecated; use jQuery.uniqueSort" );
91+
"unique-sort", "jQuery.unique is deprecated; use jQuery.uniqueSort" );
8692

8793
// Now jQuery.expr.pseudos is the standard incantation
88-
migrateWarnProp( jQuery.expr, "filters", jQuery.expr.pseudos,
94+
migrateWarnProp( jQuery.expr, "filters", jQuery.expr.pseudos, "expr-colon",
8995
"jQuery.expr.filters is deprecated; use jQuery.expr.pseudos" );
90-
migrateWarnProp( jQuery.expr, ":", jQuery.expr.pseudos,
96+
migrateWarnProp( jQuery.expr, ":", jQuery.expr.pseudos, "expr-filters",
9197
"jQuery.expr[':'] is deprecated; use jQuery.expr.pseudos" );
9298

9399
// Prior to jQuery 3.1.1 there were internal refs so we don't warn there
@@ -96,18 +102,18 @@ if ( jQueryVersionSince( "3.1.1" ) ) {
96102
return text == null ?
97103
"" :
98104
( text + "" ).replace( rtrim, "" );
99-
},
105+
}, "trim",
100106
"jQuery.trim is deprecated; use String.prototype.trim" );
101107
}
102108

103109
// Prior to jQuery 3.2 there were internal refs so we don't warn there
104110
if ( jQueryVersionSince( "3.2.0" ) ) {
105111
migrateWarnFunc( jQuery, "nodeName", function( elem, name ) {
106112
return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase();
107-
},
113+
}, "node-name",
108114
"jQuery.nodeName is deprecated" );
109115

110-
migrateWarnFunc( jQuery, "isArray", Array.isArray,
116+
migrateWarnFunc( jQuery, "isArray", Array.isArray, "is-array",
111117
"jQuery.isArray is deprecated; use Array.isArray"
112118
);
113119
}
@@ -126,7 +132,7 @@ if ( jQueryVersionSince( "3.3.0" ) ) {
126132
// ...but misinterprets leading-number strings, e.g. hex literals ("0x...")
127133
// subtraction forces infinities to NaN
128134
!isNaN( obj - parseFloat( obj ) );
129-
},
135+
}, "is-numeric",
130136
"jQuery.isNumeric() is deprecated"
131137
);
132138

@@ -146,19 +152,19 @@ if ( jQueryVersionSince( "3.3.0" ) ) {
146152
return typeof obj === "object" || typeof obj === "function" ?
147153
class2type[ Object.prototype.toString.call( obj ) ] || "object" :
148154
typeof obj;
149-
},
155+
}, "type",
150156
"jQuery.type is deprecated" );
151157

152158
migrateWarnFunc( jQuery, "isFunction",
153159
function( obj ) {
154160
return typeof obj === "function";
155-
},
161+
}, "is-function",
156162
"jQuery.isFunction() is deprecated" );
157163

158164
migrateWarnFunc( jQuery, "isWindow",
159165
function( obj ) {
160166
return obj != null && obj === obj.window;
161-
},
167+
}, "is-window",
162168
"jQuery.isWindow() is deprecated"
163169
);
164170
}

src/jquery/css.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { jQueryVersionSince } from "../compareVersions.js";
2-
import { migrateWarn } from "../main.js";
2+
import { migrateWarn, migratePatchFunc } from "../main.js";
33
import { camelCase } from "../utils.js";
4+
import { disabledPatches } from "../disableWarnings.js";
45

5-
var oldFnCss,
6+
var origFnCss,
67
internalSwapCall = false,
78
ralphaStart = /^[a-z]/,
89

@@ -47,12 +48,12 @@ if ( jQuery.swap ) {
4748
} );
4849
}
4950

50-
jQuery.swap = function( elem, options, callback, args ) {
51+
migratePatchFunc( jQuery, "swap", function( elem, options, callback, args ) {
5152
var ret, name,
5253
old = {};
5354

5455
if ( !internalSwapCall ) {
55-
migrateWarn( "jQuery.swap() is undocumented and deprecated" );
56+
migrateWarn( "swap", "jQuery.swap() is undocumented and deprecated" );
5657
}
5758

5859
// Remember the old values, and insert the new ones
@@ -69,13 +70,13 @@ jQuery.swap = function( elem, options, callback, args ) {
6970
}
7071

7172
return ret;
72-
};
73+
}, "swap" );
7374

7475
if ( jQueryVersionSince( "3.4.0" ) && typeof Proxy !== "undefined" ) {
7576

7677
jQuery.cssProps = new Proxy( jQuery.cssProps || {}, {
7778
set: function() {
78-
migrateWarn( "jQuery.cssProps is deprecated" );
79+
migrateWarn( "css-props", "jQuery.cssProps is deprecated" );
7980
return Reflect.set.apply( this, arguments );
8081
}
8182
} );
@@ -119,24 +120,29 @@ function isAutoPx( prop ) {
119120
rautoPx.test( prop[ 0 ].toUpperCase() + prop.slice( 1 ) );
120121
}
121122

122-
oldFnCss = jQuery.fn.css;
123+
origFnCss = jQuery.fn.css;
123124

124125
jQuery.fn.css = function( name, value ) {
125126
var camelName,
126127
origThis = this;
127-
if ( name && typeof name === "object" && !Array.isArray( name ) ) {
128-
jQuery.each( name, function( n, v ) {
129-
jQuery.fn.css.call( origThis, n, v );
130-
} );
131-
return this;
132-
}
133-
if ( typeof value === "number" ) {
134-
camelName = camelCase( name );
135-
if ( !isAutoPx( camelName ) && !jQuery.cssNumber[ camelName ] ) {
136-
migrateWarn( "Number-typed values are deprecated for jQuery.fn.css( \"" +
137-
name + "\", value )" );
128+
129+
if ( !disabledPatches[ "css-number" ] ) {
130+
if ( name && typeof name === "object" && !Array.isArray( name ) ) {
131+
jQuery.each( name, function( n, v ) {
132+
jQuery.fn.css.call( origThis, n, v );
133+
} );
134+
return this;
135+
}
136+
137+
if ( typeof value === "number" ) {
138+
camelName = camelCase( name );
139+
if ( !isAutoPx( camelName ) && !jQuery.cssNumber[ camelName ] ) {
140+
migrateWarn( "css-number",
141+
"Number-typed values are deprecated for jQuery.fn.css( \"" +
142+
name + "\", value )" );
143+
}
138144
}
139145
}
140146

141-
return oldFnCss.apply( this, arguments );
147+
return origFnCss.apply( this, arguments );
142148
};

src/jquery/data.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
import { migrateWarn } from "../main.js";
22
import { camelCase } from "../utils.js";
3+
import { disabledPatches } from "../disableWarnings.js";
34

4-
var oldData = jQuery.data;
5+
var origData = jQuery.data;
56

67
jQuery.data = function( elem, name, value ) {
78
var curData, sameKeys, key;
89

910
// Name can be an object, and each entry in the object is meant to be set as data
10-
if ( name && typeof name === "object" && arguments.length === 2 ) {
11-
curData = jQuery.hasData( elem ) && oldData.call( this, elem );
11+
if ( !disabledPatches[ "data-camel-case" ] &&
12+
name && typeof name === "object" && arguments.length === 2 ) {
13+
14+
curData = jQuery.hasData( elem ) && origData.call( this, elem );
1215
sameKeys = {};
1316
for ( key in name ) {
1417
if ( key !== camelCase( key ) ) {
15-
migrateWarn( "jQuery.data() always sets/gets camelCased names: " + key );
18+
migrateWarn( "data-camel-case",
19+
"jQuery.data() always sets/gets camelCased names: " + key );
1620
curData[ key ] = name[ key ];
1721
} else {
1822
sameKeys[ key ] = name[ key ];
1923
}
2024
}
2125

22-
oldData.call( this, elem, sameKeys );
26+
origData.call( this, elem, sameKeys );
2327

2428
return name;
2529
}
2630

2731
// If the name is transformed, look for the un-transformed name in the data object
28-
if ( name && typeof name === "string" && name !== camelCase( name ) ) {
29-
curData = jQuery.hasData( elem ) && oldData.call( this, elem );
32+
if ( !disabledPatches[ "data-camel-case" ] &&
33+
name && typeof name === "string" && name !== camelCase( name ) ) {
34+
35+
curData = jQuery.hasData( elem ) && origData.call( this, elem );
3036
if ( curData && name in curData ) {
31-
migrateWarn( "jQuery.data() always sets/gets camelCased names: " + name );
37+
migrateWarn( "data-camel-case",
38+
"jQuery.data() always sets/gets camelCased names: " + name );
3239
if ( arguments.length > 2 ) {
3340
curData[ name ] = value;
3441
}
3542
return curData[ name ];
3643
}
3744
}
3845

39-
return oldData.apply( this, arguments );
46+
return origData.apply( this, arguments );
4047
};

0 commit comments

Comments
 (0)