Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

stylelint-processor-styled-components

styled-components870.6kMIT1.10.0

A stylelint processor for styled-components

stylelint, processor, stylelint-processor, styled-components, lint

readme

stylelint-processor-styled-components

Lint your styled components with stylelint!

Build Status Coverage Status Join the community on Spectrum Greenkeeper

Video of project in use

Setup

You need:

(npm install --save-dev \
  stylelint \
  stylelint-processor-styled-components \
  stylelint-config-styled-components \
  stylelint-config-recommended)

Now use those in your .stylelintrc and run stylelint with your JavaScript files!

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

NOTE: The processor works with Flow- and TypeScript-typed files too! (we'll assume TypeScript usage if your files end in .ts or .tsx)

And it also has some options. Their default values are,

{
  "processors": [["stylelint-processor-styled-components", {
    "moduleName": "styled-components",
    "importName": "default",
    "strict": false,
    "ignoreFiles": [],
    "parserPlugins": [
      "jsx",
      ["decorators", { "decoratorsBeforeExport": true }],
      "classProperties",
      "exportExtensions",
      "functionBind",
      "functionSent"
    ]
  }]]
}
  • Combining with moduleName, importName and strict, you can tell the processor what kinds of tagged template literals to lint.
import styled, { css, keyframes } from 'styled-components';

// `importName` from `moduleName`, which means where `styled` comes from
styled(Component)``;
styled('div')``;
styled.div``;

// any other imports from `moduleName` (if `strict` is true, they will not be linted)
css``;
keyframes``;

// special extend calls, which have been deprecated in styled-components v4
Component.extend``;
  • ignoreFiles is passed to micromatch as the second parameter, which means one or more glob patterns for matching.

  • parserPlugins is used to make the processor's parser be able to parse new syntaxes. All available babel parser plugins and related options can be found in Babel's website.

Documentation

Further documentation for this processor lives on the styled-components website!

F.A.Q.

Why does it throw Unexpected token? Even thought the file didn't import styled-components.

You can custom babel plugins by option.parserPlugins now. An API example is our test. But if someone can implement #231, that will be much better.

If your project includes yarn.lock or package-lock.json, an alternative cause can be that babel related dependencies, i.e. @babel/parser and @babel/traverse, are outdated, especially when linting files with new TypeScript syntaxes. You can upgrade them by removing their entries in the lockfile and reinstall dependencies.

Why does it throw unexpected lint errors?

The processor can not always parse interpolations with right things. But you can use interpolation-tagging to help it. If you have ideas to make it more intelligent, feel free to send a PR or share your solution by an new issue.

What's more, if set syntax: css-in-js in stylelint@10, it can extract styles from styled-components without this processor. Even though there are still lots of differences with this processor, we hope this processor's abilities can be migrated to stylelint totally in the future.

I don't want specified tagged template literal to be parsed, i.e. css.

You can set option.strict. More examples are in #258.

License

Licensed under the MIT License, Copyright © 2017 Maximilian Stoiber. See LICENSE.md for more information!

Based on Mapbox' excellent stylelint-processor-markdown, thanks to @davidtheclark!

changelog

1.10.0

  • upgrade @babel/core to ^7.8.3 to support new ECMAScript 2020 features by default, see #290
  • upgrade stylelint to 13 and other dependencies/devDependencies

1.9.0

  • upgrade stylelint to 12

1.8.0

  • support new option ignoreFiles, which allows to skip files to be linted

1.7.0

  • upgrade stylelint to 10, and drop supports for Node < 8 as well

1.6.0

  • avoid block-no-empty error
  • support new option strict, which allows to only parse importName

1.5.2

  • upgrade postcss@7

1.5.1

  • fix column number if it is at the first line of interpolation
  • clean module level variables before each running

1.5.0

  • fix line number is undefined if source is empty
  • support new option parserPlugins, which allows to specify custom babel plugins to parse

1.4.0

  • upgrade babylon parser to @babel/parser
  • able to show parse errors to users
  • support new option importName, which allows to import wrapper components object from other names
  • filter redundant warnings introduced by v1.2.0
  • more intelligent guessing for interpolations

1.3.2

  • added support for optional chaining to our babylon parser
  • various minor development improvements that shouldn't affect the product

1.3.1

  • FIX: Interpolations following multiline comments no longer incorrectly error
  • README UPDATE: Recommend the recommended instead of standard syntax and no longer recommend the syntax config option for stylelint

1.3.0

  • FIX: Moved to using babylon's new parser that handles typescript, this will fix the problems we had with interpolation tags not being useable in Typescript
  • FIX: We now properly handle applying sourcemaps to CSS syntax errors and displaying correct line numbers for errors, Stylelint v9+ is required for this fix though as the biggest part of the fix was on their end
  • Stylelint v9+ was added as a peerdependency due to above reasons, no promises are made with this version if you do not update your Stylelint version

1.2.2

  • FIX: Correctly remove base indentation from injectGlobal when using tabs

1.2.1

  • FIX: Don't throw errors on using processor without filename (with Node.js API / CLI)

1.2.0

  • Substitute multiline interpolations with relevant multiline substitutions so line information about errors should now be correctly displayed

1.1.1

  • FIX: Also catch errors regarding resolving absolute path of file and avoid them bubbling up to Stylelint

1.1.0

  • Add support for Node v4
  • Catch errors to avoid them bubbling up to Stylelint (which broke the Stylelint plugin for VSCode)
  • upgrade typescript-eslint-parser to 9.0.0

1.0.0

0.4.0

  • move typescript to devDependencies
  • upgrade typescript-eslint-parser to 7.0.0
  • support .extend and .attrs Styled Components attributes
  • added shared stylelint config to configure a couple of rules to fit with styled-components’ style of writing CSS

0.3.0

  • fix interpolation following a comment edge case
  • rewrote our indentation handling fixing a lot of indentation rule problems

0.2.2

  • upgrade typescript-eslint-parser to 5.0.0

0.2.1

  • fix edge case in interpolations inside a declaration block

0.2.0

  • parse consecutive template literals for a single property
  • upgrade devDependencies
  • add interpolation linting section to README
  • make styled-mixin interpolation substitutes unique
  • added support for multiple interpolations in a property
  • handles interpolations in one-line css
  • support using stylelint-disable outside of tagged template literals
  • upgrade stylelint to 8.0.0
  • upgrade typescript-eslint-parser to 4.0.0

0.1.2

  • fix: move typescript from dependencies to devdependencies

0.1.1

  • add typescript support
  • add newline between components' styles
  • use unique name for each wrapped selector
  • fix: set stylelint result errored to false if warning does not contain errors

0.1.0

  • initial release

0.0.1 - 0.0.4

  • working draft