diff --git a/docs/no-data.md b/docs/no-data.md index af25912f..44646cf8 100644 --- a/docs/no-data.md +++ b/docs/no-data.md @@ -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: @@ -26,6 +27,10 @@ removeData(); [].removeData(); div.removeData(); div.removeData; +hasData(); +[].hasData(); +div.hasData(); +div.hasData; ``` ## Rule source diff --git a/rules/no-data.js b/rules/no-data.js index 4fefc84e..973f4100 100644 --- a/rules/no-data.js +++ b/rules/no-data.js @@ -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 ); diff --git a/tests/no-data.js b/tests/no-data.js index 735468be..b790e7a6 100644 --- a/tests/no-data.js +++ b/tests/no-data.js @@ -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, { @@ -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' } ] } ] } );