eslint-plugin-no-jquery 2.6.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +31 -27
  2. package/README.md.template +5 -2
  3. package/package.json +13 -10
  4. package/src/.eslintrc.js +17 -0
  5. package/src/all-methods.js +2 -1
  6. package/src/index.js +5 -1
  7. package/src/rules/no-ajax-events.js +31 -33
  8. package/src/rules/no-ajax.js +1 -1
  9. package/src/rules/no-and-self.js +1 -3
  10. package/src/rules/no-animate-toggle.js +3 -3
  11. package/src/rules/no-animate.js +28 -30
  12. package/src/rules/no-append-html.js +51 -0
  13. package/src/rules/no-class-state.js +16 -18
  14. package/src/rules/no-class.js +1 -1
  15. package/src/rules/no-constructor-attributes.js +21 -23
  16. package/src/rules/no-data.js +1 -1
  17. package/src/rules/no-deferred.js +2 -2
  18. package/src/rules/no-error.js +1 -3
  19. package/src/rules/no-escape-selector.js +1 -3
  20. package/src/rules/no-event-shorthand.js +7 -7
  21. package/src/rules/no-extend.js +28 -26
  22. package/src/rules/no-fade.js +1 -1
  23. package/src/rules/no-fx-interval.js +13 -15
  24. package/src/rules/no-global-selector.js +39 -41
  25. package/src/rules/no-html.js +2 -1
  26. package/src/rules/no-is-array.js +1 -3
  27. package/src/rules/no-is-function.js +1 -1
  28. package/src/rules/no-jquery-constructor.js +13 -15
  29. package/src/rules/no-load-shorthand.js +17 -19
  30. package/src/rules/no-noop.js +1 -3
  31. package/src/rules/no-now.js +1 -3
  32. package/src/rules/no-on-ready.js +29 -34
  33. package/src/rules/no-other-methods.js +19 -21
  34. package/src/rules/no-other-utils.js +18 -20
  35. package/src/rules/no-parse-html-literal.js +60 -67
  36. package/src/rules/no-parse-json.js +1 -3
  37. package/src/rules/no-parse-xml.js +1 -1
  38. package/src/rules/no-prop.js +1 -1
  39. package/src/rules/no-proxy.js +1 -1
  40. package/src/rules/no-ready-shorthand.js +1 -1
  41. package/src/rules/no-ready.js +9 -11
  42. package/src/rules/no-serialize.js +1 -1
  43. package/src/rules/no-size.js +1 -3
  44. package/src/rules/no-sizzle.js +5 -5
  45. package/src/rules/no-slide.js +1 -1
  46. package/src/rules/no-submit.js +2 -1
  47. package/src/rules/no-unique.js +1 -3
  48. package/src/rules/no-visibility.js +1 -1
  49. package/src/rules/no-wrap.js +1 -1
  50. package/src/rules/variable-pattern.js +6 -5
  51. package/src/utils.js +138 -114
  52. package/Changelog.md +0 -342
@@ -7,8 +7,6 @@ module.exports = utils.createUtilMethodRule(
7
7
  'Prefer `throw` to `$.error`',
8
8
  {
9
9
  fixable: 'code',
10
- fix: function ( node, context, fixer ) {
11
- return fixer.replaceText( node.callee, 'throw new Error' );
12
- }
10
+ fix: ( node, context, fixer ) => fixer.replaceText( node.callee, 'throw new Error' )
13
11
  }
14
12
  );
@@ -7,8 +7,6 @@ module.exports = utils.createUtilMethodRule(
7
7
  'Prefer `CSS.escape` to `$.escapeSelector`',
8
8
  {
9
9
  fixable: 'code',
10
- fix: function ( node, context, fixer ) {
11
- return fixer.replaceText( node.callee, 'CSS.escape' );
12
- }
10
+ fix: ( node, context, fixer ) => fixer.replaceText( node.callee, 'CSS.escape' )
13
11
  }
14
12
  );
@@ -3,12 +3,12 @@
3
3
  const utils = require( '../utils.js' );
4
4
 
5
5
  const ajaxEvents = [
6
- 'ajaxStart',
7
- 'ajaxStop',
8
6
  'ajaxComplete',
9
7
  'ajaxError',
10
- 'ajaxSuccess',
11
- 'ajaxSend'
8
+ 'ajaxSend',
9
+ 'ajaxStart',
10
+ 'ajaxStop',
11
+ 'ajaxSuccess'
12
12
  ];
13
13
 
14
14
  const rule = utils.createCollectionMethodRule(
@@ -48,7 +48,7 @@ const rule = utils.createCollectionMethodRule(
48
48
  ].concat( ajaxEvents ),
49
49
  ( node ) => node === true ?
50
50
  'Use the `allowAjaxEvents` option to allow `ajax*` methods. Prefer `.on` or `.trigger`' :
51
- `Prefer .on or .trigger to .${node.callee.property.name}`,
51
+ `Prefer .on or .trigger to .${ node.callee.property.name }`,
52
52
  {
53
53
  fixable: 'code',
54
54
  fix: utils.eventShorthandFixer
@@ -69,10 +69,10 @@ rule.meta.schema = [
69
69
 
70
70
  const parentCreate = rule.create;
71
71
 
72
- rule.create = function ( context ) {
72
+ rule.create = ( context ) => {
73
73
  const rules = parentCreate( context );
74
74
  return {
75
- 'CallExpression:exit': function ( node ) {
75
+ 'CallExpression:exit': ( node ) => {
76
76
  if (
77
77
  node.callee.type === 'MemberExpression' &&
78
78
  context.options[ 0 ] && context.options[ 0 ].allowAjaxEvents
@@ -8,6 +8,7 @@ module.exports = {
8
8
  docs: {
9
9
  description: 'Disallows the ' + utils.jQueryGlobalLink( 'extend' ) + ' utility. Prefer `Object.assign` or the spread operator.'
10
10
  },
11
+ fixable: 'code',
11
12
  schema: [
12
13
  {
13
14
  type: 'object',
@@ -21,32 +22,33 @@ module.exports = {
21
22
  ]
22
23
  },
23
24
 
24
- create: function ( context ) {
25
- return {
26
- 'CallExpression:exit': function ( node ) {
27
- if ( node.callee.type !== 'MemberExpression' ) {
28
- return;
29
- }
30
- const name = node.callee.property.name;
31
- if (
32
- name !== 'extend' ||
25
+ create: ( context ) => ( {
26
+ 'CallExpression:exit': ( node ) => {
27
+ if ( node.callee.type !== 'MemberExpression' ) {
28
+ return;
29
+ }
30
+ const name = node.callee.property.name;
31
+ if (
32
+ name !== 'extend' ||
33
33
  !utils.isjQueryConstructor( context, node.callee.object.name )
34
- ) {
35
- return;
36
- }
37
- const allowDeep = context.options[ 0 ] && context.options[ 0 ].allowDeep;
38
- if (
39
- allowDeep &&
40
- node.arguments[ 0 ] && node.arguments[ 0 ].value === true
41
- ) {
42
- return;
43
- }
44
-
45
- context.report( {
46
- node: node,
47
- message: 'Prefer Object.assign or the spread operator to $.extend'
48
- } );
34
+ ) {
35
+ return;
49
36
  }
50
- };
51
- }
37
+ const allowDeep = context.options[ 0 ] && context.options[ 0 ].allowDeep;
38
+ const isDeep = node.arguments[ 0 ] && node.arguments[ 0 ].value === true;
39
+ if ( allowDeep && isDeep ) {
40
+ return;
41
+ }
42
+
43
+ context.report( {
44
+ node,
45
+ message: 'Prefer Object.assign or the spread operator to $.extend',
46
+ fix: function ( fixer ) {
47
+ if ( !isDeep ) {
48
+ return fixer.replaceText( node.callee, 'Object.assign' );
49
+ }
50
+ }
51
+ } );
52
+ }
53
+ } )
52
54
  };
@@ -6,5 +6,5 @@ module.exports = utils.createCollectionMethodRule(
6
6
  [ 'fadeIn', 'fadeOut', 'fadeTo', 'fadeToggle' ],
7
7
  ( node ) => node === true ?
8
8
  'Prefer CSS transitions' :
9
- `Prefer CSS transitions to .${node.callee.property.name}`
9
+ `Prefer CSS transitions to .${ node.callee.property.name }`
10
10
  );
@@ -11,23 +11,21 @@ module.exports = {
11
11
  schema: []
12
12
  },
13
13
 
14
- create: function ( context ) {
15
- return {
16
- MemberExpression: function ( node ) {
17
- if (
18
- !utils.isjQueryConstructor( context, node.object.name ) ||
14
+ create: ( context ) => ( {
15
+ MemberExpression: ( node ) => {
16
+ if (
17
+ !utils.isjQueryConstructor( context, node.object.name ) ||
19
18
  node.property.name !== 'fx' ||
20
19
  !node.parent.property ||
21
20
  node.parent.property.name !== 'interval'
22
- ) {
23
- return;
24
- }
25
-
26
- context.report( {
27
- node: node,
28
- message: '$.fx.interval is not allowed'
29
- } );
21
+ ) {
22
+ return;
30
23
  }
31
- };
32
- }
24
+
25
+ context.report( {
26
+ node,
27
+ message: '$.fx.interval is not allowed'
28
+ } );
29
+ }
30
+ } )
33
31
  };
@@ -28,61 +28,59 @@ module.exports = {
28
28
  ]
29
29
  },
30
30
 
31
- create: function ( context ) {
32
- return {
33
- 'CallExpression:exit': function ( node ) {
34
- if (
35
- node.callee.type !== 'Identifier' ||
31
+ create: ( context ) => ( {
32
+ 'CallExpression:exit': ( node ) => {
33
+ if (
34
+ node.callee.type !== 'Identifier' ||
36
35
  !utils.isjQueryConstructor( context, node.callee.name ) ||
37
36
  !node.arguments[ 0 ] ||
38
37
  node.arguments[ 0 ].type !== 'Literal'
39
- ) {
40
- return;
41
- }
42
- const value = node.arguments[ 0 ].value;
43
- const allowIds = context.options[ 0 ] && context.options[ 0 ].allowIds;
44
- if (
45
- typeof value !== 'string' ||
38
+ ) {
39
+ return;
40
+ }
41
+ const value = node.arguments[ 0 ].value;
42
+ const allowIds = context.options[ 0 ] && context.options[ 0 ].allowIds;
43
+ if (
44
+ typeof value !== 'string' ||
46
45
  !value ||
47
46
  value === '#' ||
48
47
  ( allowIds && idPattern.test( value.trim() ) )
49
- ) {
50
- return;
51
- }
48
+ ) {
49
+ return;
50
+ }
52
51
 
53
- // Simple HTML check (copied from jQuery)
54
- if (
55
- value[ 0 ] === '<' &&
52
+ // Simple HTML check (copied from jQuery)
53
+ if (
54
+ value[ 0 ] === '<' &&
56
55
  value[ value.length - 1 ] === '>' &&
57
56
  value.length >= 3
58
- ) {
59
- return;
60
- }
61
- if ( rquickExpr.exec( value ) ) {
62
- return;
63
- }
57
+ ) {
58
+ return;
59
+ }
60
+ if ( rquickExpr.exec( value ) ) {
61
+ return;
62
+ }
64
63
 
65
- const selectorContext = node.arguments[ 1 ];
66
- if ( selectorContext ) {
67
- if (
68
- selectorContext.type !== 'Literal' &&
64
+ const selectorContext = node.arguments[ 1 ];
65
+ if ( selectorContext ) {
66
+ if (
67
+ selectorContext.type !== 'Literal' &&
69
68
  !(
70
69
  selectorContext.type === 'Identifier' &&
71
70
  selectorContext.name === 'undefined'
72
71
  )
73
- ) {
74
- return;
75
- }
76
- if ( selectorContext.value === '#' ) {
77
- return;
78
- }
72
+ ) {
73
+ return;
74
+ }
75
+ if ( selectorContext.value === '#' ) {
76
+ return;
79
77
  }
80
-
81
- context.report( {
82
- node: node,
83
- message: 'Avoid queries which search the entire DOM. Keep DOM nodes in memory where possible.'
84
- } );
85
78
  }
86
- };
87
- }
79
+
80
+ context.report( {
81
+ node,
82
+ message: 'Avoid queries which search the entire DOM. Keep DOM nodes in memory where possible.'
83
+ } );
84
+ }
85
+ } )
88
86
  };
@@ -4,5 +4,6 @@ const utils = require( '../utils.js' );
4
4
 
5
5
  module.exports = utils.createCollectionMethodRule(
6
6
  'html',
7
- 'Prefer `Element#innerHTML` to `.html`'
7
+ 'Prefer `Element#innerHTML` to `.html`',
8
+ { getAndSetOptions: true }
8
9
  );
@@ -7,8 +7,6 @@ module.exports = utils.createUtilMethodRule(
7
7
  'Prefer `Array.isArray` to `$.isArray`',
8
8
  {
9
9
  fixable: 'code',
10
- fix: function ( node, context, fixer ) {
11
- return fixer.replaceText( node.callee, 'Array.isArray' );
12
- }
10
+ fix: ( node, context, fixer ) => fixer.replaceText( node.callee, 'Array.isArray' )
13
11
  }
14
12
  );
@@ -7,7 +7,7 @@ module.exports = utils.createUtilMethodRule(
7
7
  'Prefer `typeof` to `$.isFunction`',
8
8
  {
9
9
  fixable: 'code',
10
- fix: function ( node, context, fixer ) {
10
+ fix: ( node, context, fixer ) => {
11
11
  const calleeRange = node.callee.range;
12
12
  return [
13
13
  fixer.replaceTextRange( [ calleeRange[ 0 ], calleeRange[ 1 ] + 1 ], 'typeof ' ),
@@ -11,21 +11,19 @@ module.exports = {
11
11
  schema: []
12
12
  },
13
13
 
14
- create: function ( context ) {
15
- return {
16
- 'CallExpression:exit': function ( node ) {
17
- if (
18
- node.callee.type !== 'Identifier' ||
14
+ create: ( context ) => ( {
15
+ 'CallExpression:exit': ( node ) => {
16
+ if (
17
+ node.callee.type !== 'Identifier' ||
19
18
  !utils.isjQueryConstructor( context, node.callee.name )
20
- ) {
21
- return;
22
- }
23
-
24
- context.report( {
25
- node: node,
26
- message: 'The jQuery constructor is not allowed'
27
- } );
19
+ ) {
20
+ return;
28
21
  }
29
- };
30
- }
22
+
23
+ context.report( {
24
+ node,
25
+ message: 'The jQuery constructor is not allowed'
26
+ } );
27
+ }
28
+ } )
31
29
  };
@@ -12,28 +12,26 @@ module.exports = {
12
12
  schema: []
13
13
  },
14
14
 
15
- create: function ( context ) {
16
- return {
17
- 'CallExpression:exit': function ( node ) {
18
- if ( !(
19
- node.callee.type === 'MemberExpression' &&
15
+ create: ( context ) => ( {
16
+ 'CallExpression:exit': ( node ) => {
17
+ if ( !(
18
+ node.callee.type === 'MemberExpression' &&
20
19
  !utils.isjQueryConstructor( context, node.callee.object.name ) &&
21
20
  node.callee.property.name === 'load' && (
22
- node.arguments.length === 0 ||
21
+ node.arguments.length === 0 ||
23
22
  utils.isFunction( node.arguments[ 0 ] )
24
- )
25
- ) ) {
26
- return;
27
- }
23
+ )
24
+ ) ) {
25
+ return;
26
+ }
28
27
 
29
- if ( utils.isjQuery( context, node.callee ) ) {
30
- context.report( {
31
- node: node,
32
- message: 'Prefer .on or .trigger to .load',
33
- fix: utils.eventShorthandFixer.bind( this, node, context )
34
- } );
35
- }
28
+ if ( utils.isjQuery( context, node.callee ) ) {
29
+ context.report( {
30
+ node,
31
+ message: 'Prefer .on or .trigger to .load',
32
+ fix: utils.eventShorthandFixer.bind( this, node, context )
33
+ } );
36
34
  }
37
- };
38
- }
35
+ }
36
+ } )
39
37
  };
@@ -7,8 +7,6 @@ module.exports = utils.createUtilPropertyRule(
7
7
  'Prefer `function(){}` to `$.noop`',
8
8
  {
9
9
  fixable: 'code',
10
- fix: function ( node, context, fixer ) {
11
- return fixer.replaceText( node, '(function(){})' );
12
- }
10
+ fix: ( node, context, fixer ) => fixer.replaceText( node, '(function(){})' )
13
11
  }
14
12
  );
@@ -7,8 +7,6 @@ module.exports = utils.createUtilMethodRule(
7
7
  'Prefer `Date.now` to `$.now`',
8
8
  {
9
9
  fixable: 'code',
10
- fix: function ( node, context, fixer ) {
11
- return fixer.replaceText( node.callee, 'Date.now' );
12
- }
10
+ fix: ( node, context, fixer ) => fixer.replaceText( node.callee, 'Date.now' )
13
11
  }
14
12
  );
@@ -12,41 +12,36 @@ module.exports = {
12
12
  schema: []
13
13
  },
14
14
 
15
- create: function ( context ) {
16
- return {
17
- 'CallExpression:exit': function ( node ) {
18
- if (
19
- node.callee.type !== 'MemberExpression' ||
15
+ create: ( context ) => ( {
16
+ 'CallExpression:exit': ( node ) => {
17
+ if (
18
+ node.callee.type !== 'MemberExpression' ||
20
19
  node.callee.property.name !== 'on'
21
- ) {
22
- return;
23
- }
24
- const arg = node.arguments[ 0 ];
25
- if ( !arg || arg.value !== 'ready' ) {
26
- return;
27
- }
20
+ ) {
21
+ return;
22
+ }
23
+ const arg = node.arguments[ 0 ];
24
+ if ( !arg || arg.value !== 'ready' ) {
25
+ return;
26
+ }
28
27
 
29
- if ( utils.isjQuery( context, node.callee ) ) {
30
- context.report( {
31
- node: node,
32
- message: '.on("ready") is not allowed',
33
- fix: function ( fixer ) {
34
- if ( node.arguments.length > 1 ) {
35
- return [
36
- fixer.replaceText( node.callee.property, 'ready' ),
37
- fixer.replaceTextRange(
38
- [
39
- node.arguments[ 0 ].range[ 0 ],
40
- node.arguments[ 1 ].range[ 0 ]
41
- ],
42
- ''
43
- )
44
- ];
45
- }
46
- }
47
- } );
48
- }
28
+ if ( utils.isjQuery( context, node.callee ) ) {
29
+ context.report( {
30
+ node,
31
+ message: '.on("ready") is not allowed',
32
+ fix: ( fixer ) => ( node.arguments.length > 1 ) ?
33
+ [
34
+ fixer.replaceText( node.callee.property, 'ready' ),
35
+ fixer.replaceTextRange(
36
+ [
37
+ node.arguments[ 0 ].range[ 0 ],
38
+ node.arguments[ 1 ].range[ 0 ]
39
+ ],
40
+ ''
41
+ )
42
+ ] : null
43
+ } );
49
44
  }
50
- };
51
- }
45
+ }
46
+ } )
52
47
  };
@@ -94,28 +94,26 @@ module.exports = {
94
94
  schema: []
95
95
  },
96
96
 
97
- create: function ( context ) {
98
- return {
99
- 'CallExpression:exit': function ( node ) {
100
- if ( node.callee.type !== 'MemberExpression' ) {
101
- return;
102
- }
103
- const name = node.callee.property.name;
104
- if (
105
- !name ||
97
+ create: ( context ) => ( {
98
+ 'CallExpression:exit': ( node ) => {
99
+ if ( node.callee.type !== 'MemberExpression' ) {
100
+ return;
101
+ }
102
+ const name = node.callee.property.name;
103
+ if (
104
+ !name ||
106
105
  methodsWithRules.includes( name ) ||
107
106
  utils.isjQueryConstructor( context, node.callee.object.name )
108
- ) {
109
- return;
110
- }
111
- if ( utils.isjQuery( context, node.callee ) ) {
112
- context.report( {
113
- node: node,
114
- message: '.{{name}} is not allowed',
115
- data: { name: name }
116
- } );
117
- }
107
+ ) {
108
+ return;
109
+ }
110
+ if ( utils.isjQuery( context, node.callee ) ) {
111
+ context.report( {
112
+ node,
113
+ message: '.{{name}} is not allowed',
114
+ data: { name }
115
+ } );
118
116
  }
119
- };
120
- }
117
+ }
118
+ } )
121
119
  };
@@ -62,27 +62,25 @@ module.exports = {
62
62
  schema: []
63
63
  },
64
64
 
65
- create: function ( context ) {
66
- return {
67
- 'CallExpression:exit': function ( node ) {
68
- if ( node.callee.type !== 'MemberExpression' ) {
69
- return;
70
- }
71
- const name = node.callee.property.name;
72
- if (
73
- !name ||
65
+ create: ( context ) => ( {
66
+ 'CallExpression:exit': ( node ) => {
67
+ if ( node.callee.type !== 'MemberExpression' ) {
68
+ return;
69
+ }
70
+ const name = node.callee.property.name;
71
+ if (
72
+ !name ||
74
73
  utilsWithRules.includes( name ) ||
75
74
  !utils.isjQueryConstructor( context, node.callee.object.name )
76
- ) {
77
- return;
78
- }
79
-
80
- context.report( {
81
- node: node,
82
- message: '$.{{name}} is not allowed',
83
- data: { name: name }
84
- } );
75
+ ) {
76
+ return;
85
77
  }
86
- };
87
- }
78
+
79
+ context.report( {
80
+ node,
81
+ message: '$.{{name}} is not allowed',
82
+ data: { name }
83
+ } );
84
+ }
85
+ } )
88
86
  };