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

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

Open
fkereki opened this issue Jul 14, 2018 · 8 comments
Open

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

fkereki opened this issue Jul 14, 2018 · 8 comments

Comments

@fkereki
Copy link

fkereki commented Jul 14, 2018

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:

  • add some definitions for equivalences in the configuration file:
equivalences = {
    'Styled_Div' : 'div',
    'Nice_Button' : 'button'
}
  • alternatively, if the name of an tag ends with underscore and a HTML tag (as in my examples) just assume the tag: Styled_Div would be processed as a div, and Nice_Button as a button.
@ljharb
Copy link
Member

ljharb commented Jul 14, 2018

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
Copy link
Author

fkereki commented Jul 14, 2018 via email

@ljharb
Copy link
Member

ljharb commented Jul 14, 2018

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

@fkereki
Copy link
Author

fkereki commented Jul 15, 2018

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
Copy link

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.

@ljharb ljharb changed the title Using eslint-plugin-jsx-a11y Using eslint-plugin-jsx-a11y with styled-components Nov 19, 2018
@AriTheElk
Copy link

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
Copy link
Contributor

eps1lon commented Feb 8, 2019

Looks like #174 could've solved this issue.

@brendanmorrell
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants