> ## Documentation Index
> Fetch the complete documentation index at: https://site.aspect.build/llms.txt
> Use this file to discover all available pages before exploring further.

# @aspect_rules_lint//lint:stylelint.bzl

> Bazel rules_lint Stylelint integration: lint_stylelint_aspect to run Stylelint as a Bazel aspect against CSS sources tagged for linting.

<Callout icon="book">
  Documentation for [@aspect\_rules\_lint@v2.1.0](https://registry.bazel.build/modules/aspect_rules_lint/2.1.0) -- <Icon icon="github" iconType="brands" /> [View source](https://github.com/aspect-build/rules_lint/blob/v2.1.0/lint/stylelint.bzl)
</Callout>

Configures [Stylelint](https://stylelint.io/) to run as a Bazel aspect

First, all CSS sources must be the srcs of some Bazel rule.
You can use a `filegroup` with `lint-with-stylelint` in the `tags`:

```python theme={null}
filegroup(
    name = "css",
    srcs = glob(["*.css"]),
    tags = ["lint-with-stylelint"],
)
```

See the `filegroup_tags` and `rule_kinds` attributes below to customize this behavior.

## Usage

Add `stylelint` as a `devDependency` in your `package.json`, and declare a binary target for Bazel to execute it.

For example in `tools/lint/BUILD.bazel`:

```python theme={null}
load("@npm//:stylelint/package_json.bzl", stylelint_bin = "bin")
stylelint_bin.stylelint_binary(name = "stylelint")
```

Then declare the linter aspect, typically in `tools/lint/linters.bzl`:

```python theme={null}
load("@aspect_rules_lint//lint:stylelint.bzl", "lint_stylelint_aspect")
stylelint = lint_stylelint_aspect(
    binary = Label("//tools/lint:stylelint"),
    config = Label("//:stylelintrc"),
)
```

Finally, register the aspect with your linting workflow, such as in `.aspect/cli/config.yaml` for `aspect lint`.

## Function: `stylelint_action`

Spawn stylelint as a Bazel action

### Parameters

<ParamField body="ctx" type="unknown" required>
  an action context OR aspect context
</ParamField>

<ParamField body="executable" type="unknown" required>
  struct with an \_stylelint field
</ParamField>

<ParamField body="srcs" type="unknown" required>
  list of file objects to lint
</ParamField>

<ParamField body="stderr" type="unknown" required>
  output file containing the stderr or --output-file of stylelint
</ParamField>

<ParamField body="exit_code" type="unknown" default={`None`}>
  output file containing the exit code of stylelint.
  If None, then fail the build when stylelint exits non-zero.
  Exit codes may be:
  1 - fatal error
  2 - lint problem
  64 - invalid CLI usage
  78 - invalid configuration file
</ParamField>

<ParamField body="env" type="unknown" default={`{}`}>
  environment variables for stylelint
</ParamField>

<ParamField body="options" type="unknown" default={`[]`}>
  additional command-line arguments
</ParamField>

<ParamField body="format" type="unknown" default={`None`}>
  a formatter to add as a command line argument
</ParamField>

<ParamField body="patch" type="unknown" default={`None`}>
  output file for patch (optional). If provided, uses run\_patcher instead of run\_shell.
</ParamField>

## Function: `lint_stylelint_aspect`

A factory function to create a linter aspect.

### Parameters

<ParamField body="binary" type="unknown" required>
  the stylelint binary, typically a rule like

  ```
  load("@npm//:stylelint/package_json.bzl", stylelint_bin = "bin")
  stylelint_bin.stylelint_binary(name = "stylelint")
  ```
</ParamField>

<ParamField body="config" type="unknown" required>
  label(s) of the stylelint config file
</ParamField>

<ParamField body="rule_kinds" type="unknown" default={`["css_library"]`}>
  which [kinds](https://bazel.build/query/language#kind) of rules should be visited by the aspect
</ParamField>

<ParamField body="filegroup_tags" type="unknown" default={`["lint-with-stylelint"]`}>
  which tags on a `filegroup` indicate that it should be visited by the aspect
</ParamField>
