Skip to content

Add fix for $.fn.data calls #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/jquery/data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { migrateWarn } from "../main.js";
import { camelCase } from "../utils.js";

var oldData = jQuery.data;
var oldData = jQuery.data,
oldFnData = jQuery.fn.data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use tabs for indentation, e.g. here:

Suggested change
oldFnData = jQuery.fn.data;
oldFnData = jQuery.fn.data;


jQuery.data = function( elem, name, value ) {
var curData, sameKeys, key;
Expand Down Expand Up @@ -38,3 +39,42 @@ jQuery.data = function( elem, name, value ) {

return oldData.apply( this, arguments );
};

jQuery.fn.extend( {
data: function( key, value ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use tabs for indentation, not spaces. This applies to most of the added lines.

var args, result;
if ( arguments.length === 0 && typeof Proxy !== "undefined" ) {
result = oldFnData.call( this );
if(!result) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if needs to be followed by the space. Please have a look at the GitHub Actions output, it shows you all the various ESLint errors of this PR.

return result;
}
// eslint-disable-next-line no-undef
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it needed when all variables are used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got an eslint error that Proxy is not defined, could it be only for me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this. Instead of silencing the whole rule, please add another override entry to src/.eslintrc.json. There's already one for css.js, you can copy it and just include Proxy as we don't use Reflect here.

return new Proxy( result, {
get: function( target, prop ) {
if (
prop !== camelCase( prop ) &&
target[ prop ] === undefined
) {
migrateWarn(
"jQuery.fn.data() always sets/gets camelCased names: " +
prop
);
return target[ camelCase( prop ) ];
}
return target[ prop ];
}
} );
}
if ( arguments.length > 0 && typeof key === "string" && key !== camelCase( key ) ) {
migrateWarn(
"jQuery.fn.data() always sets/gets camelCased names: " + key
);
args =
arguments.length > 1 ?
[ camelCase( key ), value ] :
[ camelCase( key ) ];
return oldFnData.apply( this, args );
}
return oldFnData.apply( this, arguments );
}
} );