-
Notifications
You must be signed in to change notification settings - Fork 57
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 require-autocomplete lint rule #251
Conversation
d14ca8f
to
0ffd508
Compare
cb0f08a
to
9852a93
Compare
'url', | ||
'week', | ||
]; | ||
const autocompleteElementNames = ['input', 'TextField']; |
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.
If we have other components that some libraries may want the rule applied to, I think we could make this a setting.
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.
We probably should make it a setting, for example on web
I can think of the SimpleDateField component that could benefit from it 🤔
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.
Ha, I was looking into whether we have the autocomplete-valid
rule enabled and it seems like we apply it on the <Select>
component too! We should probably do the same here.
@@ -45,6 +45,8 @@ module.exports = { | |||
'@shopify/react-no-multiple-render-methods': 'off', | |||
// Prefer all non-React-specific members be marked private in React class components. | |||
'@shopify/react-prefer-private-members': 'off', | |||
// Require input elements to have autocomplete values | |||
'@shopify/react-require-autocomplete': 'off', |
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.
Does this make the rule disabled by default, allowing apps to opt in on their own time?
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.
Correct.
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.
Super cool, good work! Your commit still mentions it‘s a WIP you may want to amend that 😜
'url', | ||
'week', | ||
]; | ||
const autocompleteElementNames = ['input', 'TextField']; |
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.
We probably should make it a setting, for example on web
I can think of the SimpleDateField component that could benefit from it 🤔
return <TextField type="text" {...fieldProps} />; | ||
}`, | ||
}, | ||
], |
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 think we could add two more test cases:
- for an
input
type on whichautoComplete
is not valid (e.g.<input type="checkbox" />
) - a test that ensures we don’t warn about components other than
input
andTextField
@@ -26,6 +26,7 @@ module.exports = { | |||
'@shopify/react-initialize-state': 'error', | |||
'@shopify/react-no-multiple-render-methods': 'error', | |||
'@shopify/react-prefer-private-members': 'error', | |||
'@shopify/react-require-autocomplete': 'error', |
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’m guessing you thought this through already but… could there be any case where we wouldn’t want an autoComplete
on one of these input types? Asking because maybe it should simply be a warn
.
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.
eslint when ran via sewing-kit runs with maxWarnings=0
so errors and warnings are functionally identical - any of either will cause CI to fail. We tend to not use warnings at all because you can't tell the difference between a warning that is left that way for a reason and a warning that is an oversight that should be fixed. Using an eslint-disable-next-line
comment is a clearer way to say "this is a violation but we know better"
}), | ||
}; | ||
|
||
function findAttrValue(attributes, name, context) { |
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.
Interesting, I’d recommend using a module like jsx-ast-utils so we don’t have to implement these utils ourselves?
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.
Great work! Thank you for providing tests and the detailed documentation of this new rule.
I tested this against Shopify/web
and because the rule is set to error
for react
it is indeed on by default.
There were 54 errors and they seem to be legitimate.
error: Input elements of type 'text' must have an autoComplete attribute (@shopify/react-require-autocomplete) at app/components/SkeletonIndex/SkeletonIndex.tsx:34:15:
32 | <SkeletonTabs />
33 | <Card.Section>
> 34 | <TextField
| ^
35 | connectedLeft={filterButton}
36 | label=""
37 | labelHidden
error: Input elements of type 'text' must have an autoComplete attribute (@shopify/react-require-autocomplete) at app/components/SsnField/SsnField.tsx:41:5:
39 |
40 | return (
> 41 | <TextField
| ^
42 | value={format(value)}
43 | label={label}
44 | helpText={helpText}
I think minor is safe since this is a new feature but not a breaking change.
9852a93
to
e605405
Compare
Thanks @vsumner and @Tom-Bonnike! I've update the PR with your feedback and added a changelog entry.
'@shopify/react-require-autocomplete': [
'error',
{
inputComponents: ['TextField'],
},
], Let me know if I am missing anything else! |
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.
Code is mostly looking good. Added a few questions inline, mainly around capitalisation of attributes - i thought they'd always be lowercase.
@@ -0,0 +1,31 @@ | |||
# Require inputs have have autocomplete attributes when necessary | |||
|
|||
The HTML `autoComplete` helps users to complete filling in forms by using data stored in the browser. This is particularly useful for people with motor disabilities or cognitive impairment who may have difficulties filling out forms online. |
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.
The HTML `autoComplete` helps users to complete filling in forms by using data stored in the browser. This is particularly useful for people with motor disabilities or cognitive impairment who may have difficulties filling out forms online. | |
The HTML `autocomplete` attribute helps users to complete filling in forms by using data stored in the browser. This is particularly useful for people with motor disabilities or cognitive impairment who may have difficulties filling out forms online. |
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'm using autoComplete
with an uppercase c
here because the rule is targeted towards React, and that the supported attribute has the uppercase letter: https://reactjs.org/docs/dom-elements.html#all-supported-html-attributes.
Do you think I should change the value here in the docs?
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've adjusted the readme a bit. I start with autocomplete
and then state that in React the attribute is written in camelCase. Then I use autoComplete
from that point on.
@@ -45,6 +45,8 @@ module.exports = { | |||
'@shopify/react-no-multiple-render-methods': 'off', | |||
// Prefer all non-React-specific members be marked private in React class components. | |||
'@shopify/react-prefer-private-members': 'off', | |||
// Require input elements to have autocomplete values | |||
'@shopify/react-require-autocomplete': 'off', |
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.
Correct.
return; | ||
} | ||
|
||
const hasAutoCompleteProp = hasProp(node.attributes, 'autoComplete'); |
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.
Should attributes be lowercase rather than camel case? autocomplete
rather than autoComplete
?
@@ -147,6 +147,7 @@ module.exports = { | |||
polarisComponentFromJSX, | |||
getRootObject, | |||
docsUrl, | |||
findDefinition, |
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.
Where is this used, can it be removed? It does not seem to be a top-level function in this file so iI'd be suprised if this even works.
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 was using this function in a previous version of the lint rule and forgot to remove this after the refactor. Good catch!
e605405
to
1303d63
Compare
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.
Nice! Thanks for doing this, and thanks for teaching me autoComplete
is one of those react uses different conventions edge-cases.
One nitpick about changelog formatting to fixup then you can go squash and merge!
packages/eslint-plugin/CHANGELOG.md
Outdated
@@ -7,6 +7,8 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). | |||
|
|||
<!-- ## Unreleased --> |
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.
Uncomment the heading, and add a subheading now there is changelog content for a new release
<!-- ## Unreleased --> | |
## Unreleased | |
### Changed |
1303d63
to
8556bfc
Compare
Description
Adds an eslint rule that tries to ensure we include an
autoComplete
attribute on input. Part of the Autocomplete project.Valid input example:
Invalid input example:
This rule catches most issues, but there are some circumstances where props passed into an
input
orTextField
may be defined in a different module (passed as props to a parent for example) making it difficult to know for sure if anautoComplete
value was passed or not. In these cases we do not report an error.Type of change
Checklist