styled-components / stylelint-processor-styled-components

Lint your styled components with stylelint!

Home Page:https://styled-components.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stylelintrc Property No Unknown Enhancement

opened this issue · comments

Currently we have the following .stylelintrc:

{
  "processors": ["stylelint-processor-styled-components"],
  "plugins": [
    "stylelint-order"
  ],
  "extends": [
    "stylelint-config-recommended",
    "stylelint-config-styled-components"
  ],
  "rules": {
    "order/order": [
      "custom-properties",
      "declarations"
    ],
    "order/properties-alphabetical-order": true,
    "property-no-unknown": [
      true,
      {
        "ignoreProperties": ["last-child", "nth-last-child(1)", "nth-last-child(3)"]
      }
    ]
  }
}

As you can see we have to declare the nth-last-child for each option that we use. Is there a potential that this could be improved so that it just takes nth-last-child and deals with any value at the end?

@KITSDominicWhite Can you explain a little more why you have to declare rules like that? If not, what code snippets will cause what kind of errors.

@chinesedfan Hi Xianming, we are using a chain of pseudoclasses and getting property-no-unknown on them, but it seems like there is some potential bug since it treats pseudoclass as a property and then marks it as unknown. You can find code snippet in the attachment. We also use a method inside of this selector to pass the needed index from props and this might be one of the cases causing some problems in parsing
pseudoclasses

@KITSMarkShulhin Sorry, I didn't reproduce those warnings. Can you paste detailed versions of stylelint related packages? Although you may use regular expressions to filter every nth-last-child.

@chinesedfan I've put our versions in the image below. Great suggestion about regular expressions, thank you!
I will try to setup reproduce case a bit later and will paste it here. Thanks for quick responses ☺️
versions

@chinesedfan I've managed to reproduce the issue.
Turns out that this error occurs in the unit tests when we use css method from styled-components to make selectors that based on other components.
Simple modifier: ':last-child' works like a charm, but when we add the css`` wrapper it doesn't recognize any pseudoclasses. I left the code to reproduce below.

Example component.js

import styled from 'styled-components';

export const TestLi = styled.li`
  color: #000;
`;

export default styled.ul`
  ${TestLi}:first-child {
    color: #fff;
    text-transform: uppercase;
  }
`;

And the test: component.test.js

import React from 'react';
import { css } from 'styled-components';
import StyledUl, { TestLi } from './component';

describe('Testing styled pseudoclasses in jest tests', () => {
  it('should have correct color on first li', () => {
    const sut = shallow(
      <StyledUl>
        <TestLi />
        <TestLi />
      </StyledUl>,
    );
    const expected = '#fff';
    expect(sut).toHaveStyleRule('color', expected, {
      modifier: css`${TestLi}:first-child`,
    });
  });
});

Lint output:

15:41  ✖  Expected first-child to come before -styled-mixin0 - (order/properties-alphabetical-order)
           
15:41  ✖  Unexpected unknown property "first-child" - (property-no-unknown)

@KITSMarkShulhin I suggest you to exclude component.test.js from lint files. Because ${TestLi}:first-child is just a selector, instead of valid css.

@chinesedfan Well, that makes sense. Excluding all the test files should solve our problem, thank you for help :) Maybe we should leave some notation in the docs about this?

@KITSMarkShulhin Yes, it can be mentioned in jest-styled-components or styled-components-website. Feel free to send a PR.