Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.

Unable to lint CSS and Styled Components with processor enabled #187

Closed
stramel opened this issue May 21, 2018 · 17 comments
Closed

Unable to lint CSS and Styled Components with processor enabled #187

stramel opened this issue May 21, 2018 · 17 comments

Comments

@stramel
Copy link

stramel commented May 21, 2018

Using v9.2.1 I am able to lint styled-components once again, however I am not able to lint CSS files while the processor is enabled.

@emilgoldsmith
Copy link
Member

This was never possible, you will need to use a different Stylelint config that doesn't include our processor in order to lint CSS files

@stramel
Copy link
Author

stramel commented May 21, 2018

Dang, that makes it difficult for handling stylelint through vscode.

@emilgoldsmith
Copy link
Member

Yeah, it sucks. If you want to there might be ways around it, if we could somehow detect whether a file is purely CSS and not JS, such as possibly look at the extension of a file, or use a library that can look at the statics of the code and check whether it parses correctly as CSS and then in that case not apply our processor to it but just pass it through to Stylelint as is. In that case it could probably work. Feel free to work on it and see if you can make it work

@brandonkal
Copy link

I would love to see this enhancement. Sometimes I just need plain CSS files in addition to styled components.

@emilgoldsmith
Copy link
Member

If you want to try working on it feel free to ask any questions if you encounter any problems

@brandonkal
Copy link

I'm not even sure how I would begin. This is pretty complex because even if this processor could be bypassed when a CSS file is detected there is still the matter of getting stylelint to ignore the "stylelint-config-styled-components" extension. I am not sure if that is possible.

Right now I have it set up this way: most of my plain CSS is in one directory with its own .stylelintrc file.

In the package.json:

"lint:css": "stylelint 'src/**/*.{js,jsx}' && stylelint 'src/**/*.css' --config extras/.stylelintrc"

The main .stylelintrc file in the src root directory:

{
  "processors": ["stylelint-processor-styled-components"],
  "extends": [
    "stylelint-config-standard",
    "stylelint-config-prettier",
    "stylelint-config-styled-components"
  ]
}

And then inside the extras folder:

# This file is used by the stylelint CLI to lint CSS files.
# The .stylelintrc file in the project root directory is configured to lint
# CSS in JS within Styled Components
{
  "extends": [
    "stylelint-config-standard",
    "stylelint-config-prettier",
  ]
}

This works pretty well. Atom-lint will highlight CSS lint errors in the extras folder and JS files in the src directory. For all other CSS files, the package script must be run in the console.

@emilgoldsmith
Copy link
Member

Oh that's interesting, yeah changing the rule config is probably not something you can get around without something hacky like that. Either you'd have to just let the rules we disable in the styled-components config still be disabled (they aren't generally super crucial?) or do something like the above.

kotarella1110 added a commit to kotarella1110-sandbox/react-boilerplate that referenced this issue Jul 24, 2018
stylelint-processor-styled-components を有効にすると stylelint-config-airbnb が無効になるため、public に .stylelintrc.js を新規作成し対応。
styled-components/stylelint-processor-styled-components#187
fix オプションを有効にするとビルドでループが発生するため、package.json の resolution に stylelint-webpack-plugin を定義。
webpack-contrib/stylelint-webpack-plugin#96
stylelint-config-prettier を有効化。
@techieshark
Copy link

I like the lint script in package.json that @brandonkal shared, as a workaround to this problem. One little update folks might want, because && will not run the second command if the first fails (I'm assuming people actually want to see errors from either, but ultimately return an exit code if either fails):

"lint:css": "E=0; stylelint 'src/**/*.{js,jsx}'||E=$?; stylelint 'src/**/*.css' --config extras/.stylelintrc||E=$?; exit $E"

(Based on this from SO.)

@techieshark
Copy link

techieshark commented Aug 26, 2018

Stylelint can be used in VS Code, on both CSS and JS code, by using an npm script (similar to the above) to lint both .css and .js/.jsx files, and then setting up a VS Code task and problem matcher.

After setting up (description below), from the VS Code command palette, you'll run "run task > vscode:stylelint", and then you should see output like this:

image

In package.json:

"scripts": {
     "lint:css": "E=0; stylelint --config .stylelintrc-jsx.json $OUTFMT './src/**/*.{js,jsx}'||E=$?; stylelint --config .stylelintrc-css.json $OUTFMT './src/**/*.css'||E=$?; exit $E",
    "vscode:stylelint": "OUTFMT='-f compact' npm run lint:css"
}

Set up your CSS-in-JS stylelint config file (I added a -jsx.json to the default name to signify this) and another one for CSS (I appended -css.json on that one).

So .stylelintrc-jsx.json looks something like:

{
  "processors": ["stylelint-processor-styled-components"],
  "extends": [
    "stylelint-config-recommended",
    "stylelint-config-prettier",
    "stylelint-config-styled-components"
  ]
}

And .stylelintrc-css.json:

{
  "extends": [
    "stylelint-config-recommended",
    "stylelint-config-prettier"
  ]
}

Then create a Task for VS Code which calls your npm script.

In .vscode/tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "vscode:stylelint",
            "problemMatcher": {
                "owner": "css",
                "fileLocation": "absolute",
                "pattern": {
                    "regexp": "^(.+):\\sline\\s(\\d+),\\scol\\s(\\d+),\\s(error|warning|info)\\s-\\s(.+)\\s\\((.+)\\)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5,
                    "code": 6
                }
            }
        }
    ]
}

Note that this problem matcher is almost exactly the same as the built-in VS Code "$eslint-compact" matcher, and differs only in that the severity is lowercase in stylelint ("error" in stylelint vs "Error" from eslint). [If someone knows a simpler way to match, please let me know!]

That tells VS Code to match the "compact" stylelint output (-f compact, which you can see by running the task/script vscode:stylelint). An example of that output is like this:

Button.js: line 7, col 16, error - Unexpected missing generic font family (font-family-no-missing-generic-family-keyword)

An improvement on this would be making it run as a background task, perhaps with chokidar to re-run whenever any file changes. Currently you just have to run it manually, but it's still an improvement over just doing it on the command line because you can jump right to the problematic lines by clicking any of the problems shown in the VS Code Problems panel.

@jvega
Copy link

jvega commented Oct 8, 2018

Yeah, it sucks. If you want to there might be ways around it, if we could somehow detect whether a file is purely CSS and not JS, such as possibly look at the extension of a file, or use a library that can look at the statics of the code and check whether it parses correctly as CSS and then in that case not apply our processor to it but just pass it through to Stylelint as is. In that case it could probably work. Feel free to work on it and see if you can make it work

Hi, @emilgoldsmith Do you accept a PR for that?
I need that PR gucong3000/postcss-syntax#36 gets accepted to make PR here
you can check the progress in my fork
BTW issues #163, #206 are related

@emilgoldsmith
Copy link
Member

Yeah sure feel free to submit a PR like that

@schoenwaldnils
Copy link
Contributor

Any updates on this?
It would be nice to have the ignoreFiles option in this processor 🤔
Like this:

processors: [
  {"stylelint-processor-styled-components", {
    "ignoreFiles": [
      "**/*.css",
    ]
  }}
],

@emilgoldsmith
Copy link
Member

Not as far as I know. Feel free to implement it, shouldn't take long

@beausmith
Copy link

Thanks for the sample code solution @brandonkal!

@schoenwaldnils
Copy link
Contributor

I added a PR for the ignoreFiles option #267

@emilgoldsmith I'd be happy if you could review it

@chinesedfan
Copy link
Member

@schoenwaldnils Thank you! v1.8.0 released.

@utrumo
Copy link

utrumo commented Aug 14, 2019

Any updates on this?
It would be nice to have the ignoreFiles option in this processor
Like this:

processors: [
  {"stylelint-processor-styled-components", {
    "ignoreFiles": [
      "**/*.css",
    ]
  }}
],

You must add this to documentation.

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

No branches or pull requests

9 participants