Skip to content

Commit

Permalink
Add hasData method to no-data rule (#158)
Browse files Browse the repository at this point in the history
See #112
  • Loading branch information
edg2s authored and jdforrester committed Oct 15, 2019
1 parent 693e81f commit c128d08
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
23 changes: 14 additions & 9 deletions docs/no-data.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# no-data

Disallows the .data/removeData methods and $.data/removeData utilies.
Disallows the .data/removeData/hasData methods and $.data/removeData/hasData utilies.

## Rule details

❌ The following patterns are considered errors:
```js
$.data();
$( 'div' ).data();
$div.data();
$( 'div' ).first().data();
$( 'div' ).append( $( 'input' ).data() );
$.removeData();
$( 'div' ).removeData();
$div.removeData();
$.data( elem, 'foo' );
$( 'div' ).data( 'foo', 'bar' );
$div.data( 'foo' );
$( 'div' ).first().data( 'foo', 'bar' );
$( 'div' ).append( $( 'input' ).data( 'foo' ) );
$.removeData( elem, 'foo' );
$( 'div' ).removeData( 'foo' );
$div.removeData( 'foo' );
$.hasData( elem );
```

✔️ The following patterns are not considered errors:
Expand All @@ -26,6 +27,10 @@ removeData();
[].removeData();
div.removeData();
div.removeData;
hasData();
[].hasData();
div.hasData();
div.hasData;
```
## Rule source

Expand Down
2 changes: 1 addition & 1 deletion rules/no-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
const utils = require( './utils.js' );

module.exports = utils.createCollectionOrUtilMethodRule(
[ 'data', 'removeData' ],
[ 'data', 'removeData', 'hasData' ],
( node ) => 'Prefer WeakMap to ' + node.callee.property.name
);
31 changes: 22 additions & 9 deletions tests/no-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const RuleTesterAndDocs = require( '../rule-tester-and-docs' );

const error = 'Prefer WeakMap to data';
const removeError = 'Prefer WeakMap to removeData';
const hasError = 'Prefer WeakMap to hasData';

const ruleTester = new RuleTesterAndDocs();
ruleTester.run( 'no-data', rule, {
Expand All @@ -17,40 +18,52 @@ ruleTester.run( 'no-data', rule, {
'removeData()',
'[].removeData()',
'div.removeData()',
'div.removeData'
'div.removeData',

'hasData()',
'[].hasData()',
'div.hasData()',
'div.hasData'
// TODO: Technically $div.hasData should be allowed as $.hasData
// only exists as a utility, but as we are using createCollectionOrUtilMethodRule
// the non-existant method is disallowed as well.
],
invalid: [
{
code: '$.data()',
code: '$.data(elem, "foo")',
errors: [ { message: error, type: 'CallExpression' } ]
},
{
code: '$("div").data()',
code: '$("div").data("foo", "bar")',
errors: [ { message: error, type: 'CallExpression' } ]
},
{
code: '$div.data()',
code: '$div.data("foo")',
errors: [ { message: error, type: 'CallExpression' } ]
},
{
code: '$("div").first().data()',
code: '$("div").first().data("foo", "bar")',
errors: [ { message: error, type: 'CallExpression' } ]
},
{
code: '$("div").append($("input").data())',
code: '$("div").append($("input").data("foo"))',
errors: [ { message: error, type: 'CallExpression' } ]
},
{
code: '$.removeData()',
code: '$.removeData(elem, "foo")',
errors: [ { message: removeError, type: 'CallExpression' } ]
},
{
code: '$("div").removeData()',
code: '$("div").removeData("foo")',
errors: [ { message: removeError, type: 'CallExpression' } ]
},
{
code: '$div.removeData()',
code: '$div.removeData("foo")',
errors: [ { message: removeError, type: 'CallExpression' } ]
},
{
code: '$.hasData(elem)',
errors: [ { message: hasError, type: 'CallExpression' } ]
}
]
} );

0 comments on commit c128d08

Please sign in to comment.