-
-
Notifications
You must be signed in to change notification settings - Fork 387
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 prefer-dataset
rule
#225
Merged
sindresorhus
merged 13 commits into
sindresorhus:master
from
amedora:add-prefer-dataset-rule
Sep 16, 2019
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
db66368
Add prefer-dataset rule in readme and docs
amedora c5594b2
Add test
amedora d2ce27d
Add to the recommended config
amedora f1e1bf1
Add prefer-dataset rule
amedora 6dc63e3
Merge branch 'master' into add-prefer-dataset-rule
amedora b073fd0
Refactor error reporting
amedora f852ae7
Add more tests
amedora d0251fb
Fix rule
amedora f47bc20
Update prefer-dataset.md
sindresorhus 197d84e
Update readme.md
sindresorhus a9b856e
Update prefer-dataset.js
sindresorhus bb95fd9
Update prefer-dataset.js
sindresorhus f5a2cd5
Update prefer-dataset.js
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Prefer the .dataset property on DOM elements over using setAttribute | ||
|
||
Enforces the use of, for example, `element.dataset.key` over `element.setAttribute('data-key', 'foo')` for DOM elements. | ||
|
||
This rule is fixable. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
element.setAttribute('data-unicorn', '🦄'); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
element.dataset.unicorn = '🦄'; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
const getDocsUrl = require('./utils/get-docs-url'); | ||
|
||
const getMethodName = memberExpression => memberExpression.property.name; | ||
|
||
const getDataAttributeName = arg => { | ||
if (arg.type === 'Literal') { | ||
return (arg.value.match(/^data-(.+)/) || ['', ''])[1]; | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
const parseValueArgument = (context, arg) => { | ||
return context.getSourceCode().getText(arg); | ||
}; | ||
|
||
const dashToCamelCase = str => { | ||
return str.replace(/-([a-z])/g, s => s[1].toUpperCase()); | ||
}; | ||
|
||
const getReplacement = (context, node, memberExpression, propertyName) => { | ||
const objectName = memberExpression.object.name; | ||
const value = parseValueArgument(context, node.arguments[1]); | ||
|
||
propertyName = dashToCamelCase(propertyName); | ||
|
||
if (propertyName.match(/[.:]/)) { | ||
return `${objectName}.dataset['${propertyName}'] = ${value}`; | ||
} | ||
|
||
return `${objectName}.dataset.${propertyName} = ${value}`; | ||
}; | ||
|
||
const isBracketNotation = (context, callee) => { | ||
const bracketOpen = context.getSourceCode().getFirstTokenBetween(callee.object, callee.property, {filter: token => { | ||
return token.value === '['; | ||
}}); | ||
|
||
return bracketOpen !== null && bracketOpen.value === '['; | ||
}; | ||
|
||
const create = context => { | ||
return { | ||
CallExpression(node) { | ||
const {callee} = node; | ||
|
||
if (callee.type !== 'MemberExpression') { | ||
return; | ||
} | ||
|
||
if (getMethodName(callee) !== 'setAttribute') { | ||
return; | ||
} | ||
|
||
if (isBracketNotation(context, callee)) { | ||
return; | ||
} | ||
|
||
const attributeName = getDataAttributeName(node.arguments[0]); | ||
if (attributeName) { | ||
const replacement = getReplacement(context, node, callee, attributeName); | ||
context.report({ | ||
node, | ||
message: 'Prefer `dataset` over `setAttribute`', | ||
fix: fixer => fixer.replaceText(node, replacement) | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocsUrl(__filename) | ||
}, | ||
fixable: 'code' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import test from 'ava'; | ||
import {outdent} from 'outdent'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/prefer-dataset'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const errors = [ | ||
{ | ||
ruleId: 'prefer-dataset', | ||
message: 'Prefer `dataset` over `setAttribute`' | ||
} | ||
]; | ||
|
||
ruleTester.run('prefer-dataset', rule, { | ||
valid: [ | ||
'element.dataset.unicorn = \'🦄\';', | ||
'element.dataset[\'unicorn\'] = \'🦄\';', | ||
'element[setAttribute](\'data-unicorn\', \'🦄\');', | ||
'element.setAttribute(\'foo\', \'bar\');', | ||
'element.setAttribute(foo, bar);', | ||
'element.getAttribute(\'data-unicorn\');' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'element.setAttribute(\'data-unicorn\', \'🦄\');', | ||
errors, | ||
output: 'element.dataset.unicorn = \'🦄\';' | ||
}, | ||
{ | ||
code: 'element.setAttribute(\'data-🦄\', \'🦄\');', | ||
errors, | ||
output: 'element.dataset.🦄 = \'🦄\';' | ||
}, | ||
{ | ||
code: 'element.setAttribute(\'data-foo2\', \'🦄\');', | ||
errors, | ||
output: 'element.dataset.foo2 = \'🦄\';' | ||
}, | ||
{ | ||
code: 'element.setAttribute(\'data-foo:bar\', \'zaz\');', | ||
errors, | ||
output: 'element.dataset[\'foo:bar\'] = \'zaz\';' | ||
}, | ||
{ | ||
code: 'element.setAttribute(\'data-foo.bar\', \'zaz\');', | ||
errors, | ||
output: 'element.dataset[\'foo.bar\'] = \'zaz\';' | ||
}, | ||
{ | ||
code: 'element.setAttribute(\'data-foo-bar\', \'zaz\');', | ||
errors, | ||
output: 'element.dataset.fooBar = \'zaz\';' | ||
}, | ||
{ | ||
code: 'element.setAttribute(\'data-foo\', /* comment */ \'bar\');', | ||
errors, | ||
output: 'element.dataset.foo = \'bar\';' | ||
}, | ||
{ | ||
code: outdent` | ||
element.setAttribute( | ||
\'data-foo\', // comment | ||
\'bar\' // comment | ||
); | ||
`, | ||
errors, | ||
output: 'element.dataset.foo = \'bar\';' | ||
} | ||
amedora marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to delete comments during fixing but it probably doesn't come up that often. @sindresorhus, what's your take on this?
I've done (3) in the past but I think a bunch of different rules current do (1).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MrHen I don't think we should do
1.
, but we need to come up with a guideline that we can apply to all rules. Maybe we could make an utility that just extract all comments to above a node we give it? The comments can require manual formatting and movement. We just shouldn't remove them. Alternatively,3.
if2.
is too complex, but I think we could make something generic that we could apply to many rules. Would you be able to open a new issue about it? We probably need to review all existing rules for this and update the contributing guidelines.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#370