jsx-eslint / eslint-plugin-jsx-a11y

Static AST checker for a11y rules on JSX elements.
MIT License
3.41k stars 637 forks source link

Using eslint-plugin-jsx-a11y with styled-components #466

Open fkereki opened 6 years ago

fkereki commented 6 years ago

If you use Styled-Components, eslint-plugin-jsx-a11y cannot detect tags and apply rules, so it's rendered practically useless.

const Styled_Div = styled.div`...`;
const Nice_Button = styled.button`...`;

render() {
    return <Styled_Div... />...<Nice_Button...> ... </Nice_Button>...</Styled_Div>;
}

I have two suggestions:

ljharb commented 6 years ago

I think that "rendered practically useless" is true of just about any static analysis tool for JS, when using a template tag DSL like styled-components.

Regardless, I can see the benefit (even for idiomatic jsx users) of specifying a settings-level map - from 'div' to ['Styled_Div', 'OtherDiv'] etc - that all rules used.

This wouldn't be a trivial amount of work - and would require lots of tests, for every single rule - but I think it'd be useful.

fkereki commented 6 years ago

Wouldn't my second suggestion be simpler to implement?

Best regards, FK

On Sat, Jul 14, 2018 at 7:38 PM Jordan Harband notifications@github.com wrote:

I think that "rendered practically useless" is true of just about any static analysis tool for JS, when using a template tag DSL like styled-components.

Regardless, I can see the benefit (even for idiomatic jsx users) of specifying a settings-level map - from 'div' to ['Styled_Div', 'OtherDiv'] etc - that all rules used.

This wouldn't be a trivial amount of work - and would require lots of tests, for every single rule - but I think it'd be useful.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/evcohen/eslint-plugin-jsx-a11y/issues/466#issuecomment-405054359, or mute the thread https://github.com/notifications/unsubscribe-auth/ACWo31wnKokc2cLZrSuJqAyNLA9iEyPLks5uGnLdgaJpZM4VP9PO .

ljharb commented 6 years ago

It would be much more magical and implicit, which are synonyms for "worse" imo.

fkereki commented 6 years ago

I can understand that - however, as an interim measure, it could do: "better a bird in the hand, than two in the bush"? Thanks for your answers!

jcperez-ch commented 5 years ago

Totally agree with @ljharb that setting up an explicit mapper would be a better idea. In my project we don't use underscores or any kind of inferred tagging, I believe that is up to the developer to construct, however, if we need a universal solution, a mapper (first solution) is the way to go. Question, is this something in progress? I feel like a lint cheater having the need to disable the rule for every Styled component with annotations.

AriTheElk commented 5 years ago

Any update on this? Most of my projects use styled-components pretty heavily and it's a serious bummer missing out on getting a11y hints

eps1lon commented 5 years ago

Looks like #174 could've solved this issue.

brendanmorrell commented 4 years ago

Hey, I made a pretty cool plugin for this (hopefully). It found 6,567 errors in one of my repos that were undiscovered using airbnb/jsx-a11y rules. Please give it a star so we can get more people interested, using it, and contributing to make a more accessible internet for all.

Here’s the github repo it covers all 4 styled components/emotion syntaxes

Existing a11y linting has become significantly less useful (personally, 99% of the components I need linted are styled.). Each scenario below would display the error

Visible, non-interactive elements with click handlers must have at least one keyboard listener.`
const Div = styled.div``;
<Div onClick={() => null}/>
const Div = styled.div.attrs({ onClick: () => null})``;
<Div />
const Div = styled.div.attrs({ onClick: () => null})``;
const StyledDiv = styled(Div)``;
<StyledDiv />
const ButtonAsDiv = styled.button``;
<ButtonAsDiv as="div" onClick={() => null} />

It’s not perfect (no linters are), but it’s super useful, and with a little time and community support, it could be even better.