Skip to content

Commit 3c281c3

Browse files
committed
Merge branch 'develop' of github.com:magento-commerce/magento-coding-standard into fix-phpcs-issues
2 parents 20acb9b + 5559687 commit 3c281c3

22 files changed

+384
-74
lines changed

Magento2/Tests/Eslint/AndSelfTest.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
$(document).ready(function () {
2-
'use strict';
1+
define([
2+
'jquery',
3+
], function ($) {
4+
$(function () {
5+
'use strict';
36

4-
$('div').find('p').andSelf().addClass('border');
7+
$('div').find('p').andSelf().addClass('border');
8+
});
59
});
+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
$(document).ready(function () {
2-
'use strict';
1+
define([
2+
'jquery',
3+
], function ($) {
4+
$(function () {
5+
'use strict';
36

4-
$('.btn1').bind('click');
7+
$('.btn1').bind('click');
8+
});
59
});

Magento2/Tests/Eslint/ClickEventShorthandTest.js

-5
This file was deleted.

Magento2/Tests/Eslint/ClickEventShorthandTest.php

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
$(document).ready(function () {
2-
'use strict';
1+
define([
2+
'jquery',
3+
], function ($) {
4+
$(function () {
5+
'use strict';
36

4-
$('table').delegate('td', 'click', function () {
5-
$(this).toggleClass('chosen');
7+
$('table').delegate('td', 'click', function () {
8+
$(this).toggleClass('chosen');
9+
});
610
});
711
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
define([
2+
'jquery',
3+
], function (jQuery) {
4+
$(function () {
5+
'use strict';
6+
7+
jQuery.extend(jQuery.expr[':'], {});
8+
jQuery.extend(jQuery.expr.filters, {});
9+
});
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class DeprecatedExprTest
12+
*
13+
* Test Eslint Rule: jquery-no-deprecated-expr.js
14+
*/
15+
class DeprecatedExprTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute(): void
18+
{
19+
$this->assertFileContainsError(
20+
'DeprecatedExprTest.js',
21+
[
22+
'jQuery.expr[":"] is deprecated; Use jQuery.expr.pseudos instead',
23+
'jQuery.expr.filters is deprecated; Use jQuery.expr.pseudos instead'
24+
]
25+
);
26+
}
27+
}
+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
$(document).ready(function () {
2-
'use strict';
3-
4-
$('#result').load('ajax/test.html');
1+
define([
2+
'jquery',
3+
], function (jQuery) {
4+
jQuery(document).ready(function () {
5+
'use strict';
6+
jQuery('#result').unload(function () {
7+
});
8+
});
59
});
10+

Magento2/Tests/Eslint/EventShorthandTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public function testExecute(): void
1818
{
1919
$this->assertFileContainsError(
2020
'EventShorthandTest.js',
21-
['jQuery.load() was removed, use .on("load", fn) instead']
21+
[
22+
'jQuery.unload() was removed, use .on("unload", fn) instead',
23+
'jQuery.ready(handler) is deprecated and should be replaced with jQuery(handler)'
24+
]
2225
);
2326
}
2427
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
define([
2+
'jquery',
3+
], function ($) {
4+
$(function () {
5+
'use strict';
6+
7+
$('#result').blur();
8+
$.fn.focus(function () {
9+
});
10+
});
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class InputEventShorthandTest
12+
*
13+
* Test Eslint Rule: jquery-no-input-event-shorthand.js
14+
*/
15+
class InputEventShorthandTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute(): void
18+
{
19+
$this->assertFileContainsError(
20+
'InputEventShorthandTest.js',
21+
[
22+
'Instead of .blur(fn) use .on("blur", fn). Instead of .blur() use .trigger("blur")',
23+
'Instead of .focus(fn) use .on("focus", fn). Instead of .focus() use .trigger("focus")',
24+
]
25+
);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
define([
2+
'jquery',
3+
], function ($) {
4+
$(function () {
5+
'use strict';
6+
7+
$.parseJSON('');
8+
$.type({});
9+
$.isArray({});
10+
$.isFunction({});
11+
});
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class MiscDeprecatedFunctionsTest
12+
*
13+
* Test Eslint Rule: jquery-no-misc-deprecated-functions.js
14+
*/
15+
class MiscDeprecatedFunctionsTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute(): void
18+
{
19+
$this->assertFileContainsError(
20+
'MiscDeprecatedFunctionsTest.js',
21+
[
22+
'jQuery.isFunction() is deprecated. In most cases, it can be replaced by [typeof x === "function"]',
23+
'jQuery.type() is deprecated. Replace with an appropriate type check like [typeof x === "function"]',
24+
'jQuery.isArray() is deprecated. Use the native Array.isArray method instead',
25+
'jQuery.parseJSON() is deprecated. To parse JSON strings, use the native JSON.parse method instead'
26+
]
27+
);
28+
}
29+
}

Magento2/Tests/Eslint/SizeTest.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
$(document).ready(function () {
2-
'use strict';
1+
define([
2+
'jquery',
3+
], function ($) {
4+
$(function () {
5+
'use strict';
36

4-
$('div').size();
7+
$('div').size();
8+
});
59
});

Magento2/Tests/Eslint/TrimTest.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
$(document).ready(function () {
2-
'use strict';
1+
define([
2+
'jquery',
3+
], function ($) {
4+
$(function () {
5+
'use strict';
36

4-
$.trim(' hello, how are you? ');
7+
$.trim(' hello, how are you? ');
8+
});
59
});

eslint/.eslintrc-jquery

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
2+
"ignorePatterns": ["**/vendor/**/*.js", "**/node_modules/**/*.js"],
23
"rules": {
34
"jquery-no-andSelf": 2,
45
"jquery-no-bind-unbind": 2,
5-
"jquery-no-click-event-shorthand": 2,
6+
"jquery-no-input-event-shorthand": 2,
67
"jquery-no-delegate-undelegate": 2,
78
"jquery-no-event-shorthand": 2,
89
"jquery-no-size": 2,
9-
"jquery-no-trim": 2
10+
"jquery-no-trim": 2,
11+
"jquery-no-misc-deprecated-functions": 2,
12+
"jquery-no-deprecated-expr": 2
1013
}
1114
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module.exports = {
2+
meta: {
3+
type: 'suggestion',
4+
docs: {
5+
description: 'Disallow the use of deprecated way to add to custom selectors',
6+
category: 'jQuery deprecated functions',
7+
recommended: true,
8+
url: 'https://api.jquery.com/load/'
9+
},
10+
schema: []
11+
},
12+
13+
/**
14+
* Executes the function to check if shorthand methods are used.
15+
*
16+
* @param {Object} context
17+
* @returns {Object}
18+
*/
19+
create: function (context) {
20+
'use strict';
21+
22+
var utils = require('./utils.js');
23+
24+
return {
25+
/**
26+
* Checks for jQuery.expr[':']
27+
*
28+
* @param {Object} node - The node to check.
29+
*/
30+
'MemberExpression[property.value=":"] MemberExpression[property.name="expr"]': function (node) {
31+
if (utils.isjQuery(node)) {
32+
context.report({
33+
node: node,
34+
message: 'jQuery.expr[":"] is deprecated; Use jQuery.expr.pseudos instead'
35+
});
36+
}
37+
},
38+
39+
/**
40+
* Checks for jQuery.expr.filters
41+
*
42+
* @param {Object} node - The node to check.
43+
*/
44+
'MemberExpression[property.name="filters"] MemberExpression[property.name="expr"]': function (node) {
45+
if (utils.isjQuery(node)) {
46+
context.report({
47+
node: node,
48+
message: 'jQuery.expr.filters is deprecated; Use jQuery.expr.pseudos instead'
49+
});
50+
}
51+
}
52+
};
53+
}
54+
};

eslint/rules/jquery-no-event-shorthand.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
meta: {
33
type: 'suggestion',
44
docs: {
5-
description: 'Disallow the use of shorthand methods',
5+
description: 'Disallow the use of shorthand event methods',
66
category: 'jQuery deprecated functions',
77
recommended: true,
88
url: 'https://api.jquery.com/load/'
@@ -11,7 +11,7 @@ module.exports = {
1111
},
1212

1313
/**
14-
* Executes the function to check if shorthand methods are used.
14+
* Executes the function to check if shorthand event methods are used.
1515
*
1616
* @param {Object} context
1717
* @returns {Object}
@@ -23,23 +23,28 @@ module.exports = {
2323

2424
return {
2525
/**
26-
* Checks if shorthand methods are used and reports it.
26+
* Checks if shorthand event methods are used.
2727
*
2828
* @param {Object} node - The node to check.
2929
*/
3030
CallExpression: function (node) {
31-
var names = ['load', 'unload', 'error'],
32-
name;
31+
var namesToMsg = {
32+
'unload': 'jQuery.unload() was removed, use .on("unload", fn) instead.',
33+
'ready': 'jQuery.ready(handler) is deprecated and should be replaced with jQuery(handler)'
34+
},
35+
name,
36+
message;
3337

3438
if (node.callee.type !== 'MemberExpression') {return;}
3539

36-
if (!names.includes(node.callee.property.name)) {return;}
40+
name = node.callee.property.name;
41+
if (!namesToMsg.hasOwnProperty(name)) {return;}
42+
message = namesToMsg[name];
3743

3844
if (utils.isjQuery(node)) {
39-
name = node.callee.property.name;
4045
context.report({
4146
node: node,
42-
message: 'jQuery.' + name + '() was removed, use .on("' + name + '", fn) instead.'
47+
message: message
4348
});
4449
}
4550
}

eslint/rules/jquery-no-click-event-shorthand.js renamed to eslint/rules/jquery-no-input-event-shorthand.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
meta: {
33
type: 'suggestion',
44
docs: {
5-
description: 'Disallow the use of shortcuts to trigger events',
5+
description: 'Disallow the use of shortcuts to input events via keyboard/mouse trigger events',
66
category: 'jQuery deprecated functions',
77
recommended: true,
88
url: 'https://api.jquery.com/bind/'
@@ -42,8 +42,7 @@ module.exports = {
4242
name = node.callee.property.name;
4343
context.report({
4444
node: node,
45-
message:
46-
'Instead of .' + name + '(fn) use .on("' + name + '", fn). Instead of .' + name +
45+
message: 'Instead of .' + name + '(fn) use .on("' + name + '", fn). Instead of .' + name +
4746
'() use .trigger("' + name + '")'
4847
});
4948
}

0 commit comments

Comments
 (0)