Skip to content
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

Add removeAttr to no-attr rule #133

Merged
merged 2 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion docs/no-attr.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# no-attr

Disallows the .attr method and $.attr utility.
Disallows the .attr/removeAttr methods and $.attr/removeAttr utilies.

## Rule details

Expand All @@ -13,6 +13,8 @@ $( 'div' ).first().attr();
$( 'div' ).append( $( 'input' ).attr() );
$( 'div' ).attr( 'name' );
$( 'div' ).attr( 'name', 'random' );
$.removeAttr();
$( 'div' ).removeAttr( 'name' );
```

✔️ The following patterns are not considered errors:
Expand All @@ -21,4 +23,6 @@ attr();
[].attr();
div.attr();
div.attr;
removeAttr();
div.removeAttr;
```
9 changes: 7 additions & 2 deletions rules/no-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
const utils = require( './utils.js' );

module.exports = utils.createCollectionOrUtilMethodRule(
'attr',
( node ) => 'Prefer Element#' + ( node.arguments.length === 2 ? 'set' : 'get' ) + 'Attribute to attr'
[ 'attr', 'removeAttr' ],
( node ) => 'Prefer Element#' +
(
node.callee.property.name === 'removeAttr' ? 'remove' :
node.arguments.length === 2 ? 'set' : 'get'
) +
'Attribute to ' + node.callee.property.name
);
11 changes: 10 additions & 1 deletion tests/no-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const RuleTesterAndDocs = require( '../rule-tester-and-docs' );

const getError = 'Prefer Element#getAttribute to attr';
const setError = 'Prefer Element#setAttribute to attr';
const removeError = 'Prefer Element#removeAttribute to removeAttr';

const ruleTester = new RuleTesterAndDocs();
ruleTester.run( 'no-attr', rule, {
valid: [ 'attr()', '[].attr()', 'div.attr()', 'div.attr' ],
valid: [ 'attr()', '[].attr()', 'div.attr()', 'div.attr', 'removeAttr()', 'div.removeAttr' ],
invalid: [
{
code: '$.attr()',
Expand Down Expand Up @@ -37,6 +38,14 @@ ruleTester.run( 'no-attr', rule, {
{
code: '$("div").attr("name", "random")',
errors: [ { message: setError, type: 'CallExpression' } ]
},
{
code: '$.removeAttr()',
errors: [ { message: removeError, type: 'CallExpression' } ]
},
{
code: '$("div").removeAttr("name")',
errors: [ { message: removeError, type: 'CallExpression' } ]
}
]
} );