@apostrophecms/stylelint-no-mixed-decls

中文文档
1.2.0发布于 2025/09/16MITCommonJS零依赖近 30 天下载 4.9万

A Stylelint plugin that enforces Sass's mixed-decls rule.

原始使用文档
文档目录8 节
  1. What this plugin does
  2. Installation
  3. Usage
  4. Example: Correct Usage
  5. Example: Incorrect Usage (will report)
  6. Why this matters
  7. Limitations
  8. Please contribute!

@apostrophecms/stylelint-no-mixed-decls

A Stylelint plugin that enforces Sass's mixed-decls rule — requiring declarations and nested rules to be ordered according to Sass's updated behavior.

⚠️ Since Sass 1.77.0+, CSS blocks can no longer freely mix declarations and nested rules.
If you want to declare additional styles after nested rules, those declarations must be placed inside a & { } block.

See: https://sass-lang.com/documentation/breaking-changes/mixed-decls/

What this plugin does

  • Prevents declarations appearing after nested rules within a CSS block unless they're wrapped in a & { } block.
  • Handle @include statements that may contain nested rules.

Installation

npm install @apostrophecms/stylelint-no-mixed-decls --save-dev

Usage

Add it to your Stylelint configuration:

{
  "plugins": [ "@apostrophecms/stylelint-no-mixed-decls" ],
  "rules": {
    "@apostrophecms/stylelint-no-mixed-decls": true
  }
}

You can set a contain-nested option to list mixins that are known to contain nested rules.
This is an answer to the limitations that this plugin cannot analyze mixin definitions from other files.

{
  "plugins": [ "@apostrophecms/stylelint-no-mixed-decls" ],
  "rules": {
    "@apostrophecms/stylelint-no-mixed-decls": [
      true,
      {
        "contain-nested": [ "external-mixin-known-to-contain-nested-rules" ]
      }
    ] 
  }
}

Example: Correct Usage

.foo {
  color: red;

  &--large {
    font-size: 24px;
  }

  & {
    font-weight: bold;
  }
}
@mixin foo {
  display: block;
  clear: both;
}

.foo {
  @include foo;
  @include external-mixin; // not known to contain nested rules
  color: red;
}
@mixin foo {
  display: block;
  clear: both;

  &--large {
    font-size: 24px;
  }
}

.foo {
  @include foo;

  & {
    color: red;
  }
}
@mixin foo {
  & {
    display: block;
    clear: both;
  }
}

.foo {
  font-weight: bold;

  & {
    color: red;
  }

  @include foo;
}

Example: Incorrect Usage (will report)

.foo {
  color: red;

  &--large {
    font-size: 24px;
  }

  font-weight: bold; // ❌ Cannot mix declarations and nested rules. Group them together or wrap declarations in a nested "& { }" block. See https://sass-lang.com/documentation/breaking-changes/mixed-decls/
}
@mixin foo {
  display: block;
  clear: both;

  &--large {
    font-size: 24px;
  }
}

.foo {
  @include foo;
  color: red; // ❌ Cannot mix declarations and nested rules. Group them together or wrap declarations in a nested "& { }" block. See https://sass-lang.com/documentation/breaking-changes/mixed-decls/
}
@mixin foo {
  display: block; // ❌ Cannot mix declarations and nested rules. Group them together or wrap declarations in a nested "& { }" block. See https://sass-lang.com/documentation/breaking-changes/mixed-decls/
  clear: both; // ❌ Cannot mix declarations and nested rules. Group them together or wrap declarations in a nested "& { }" block. See https://sass-lang.com/documentation/breaking-changes/mixed-decls/
}

.foo {
  font-weight: bold;

  & {
    color: red;   
  }

  @include foo;
}
.foo {
  @include external-mixin-known-to-contain-nested-rules;

  color: red; // ❌ Cannot mix declarations and nested rules. Group them together or wrap declarations in a nested "& { }" block. See https://sass-lang.com/documentation/breaking-changes/mixed-decls/
}

Why this matters

This plugin ensures your Sass code adheres to modern CSS nesting behavior, prevents breaking builds on newer Sass versions, and keeps your codebase cleanly structured.

Limitations

This plugin only analyzes mixins defined within the same file being linted. It does not resolve or inspect mixin definitions from other files, even if those files are imported via Sass @use or @import.

As a result:

  • If a mixin with the same name exists in multiple files with different contents (some containing nested rules, others not), this plugin will only be aware of the mixin definitions present in the file currently being linted.

  • It will not detect nested rules within mixins defined elsewhere or included from external files.

Recommendation:

To ensure accurate linting results, define critical mixins with nested rules consistently or colocate them with the code that uses them. Alternatively, wrap declarations following @include statements in a nested & { } block as a safe default when unsure of the mixin's contents.

Use contain-nested option:

This option can be used to list mixins that are known to contain nested rules, so that the plugin can treat them accordingly, even if their definition is not present in the file they are used.

Please contribute!

We welcome contributions! If you find a bug or something missing, please open an issue or submit a pull request.

版本与发布记录

@apostrophecms/stylelint-no-mixed-decls 共发布 3 个版本,首次发布于 2025/05/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.0latest2025/09/161.1 年前
1.1.02025/06/021.4 年前
1.0.02025/05/231.5 年前

关于 @apostrophecms/stylelint-no-mixed-decls 的常见问题

@apostrophecms/stylelint-no-mixed-decls 是什么?

A Stylelint plugin that enforces Sass's mixed-decls rule.

如何安装 @apostrophecms/stylelint-no-mixed-decls?

在项目目录运行 npm install @apostrophecms/stylelint-no-mixed-decls,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:stylelint(>= 16.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@apostrophecms/stylelint-no-mixed-decls 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/apostrophecms__stylelint-no-mixed-decls 是否存在。

@apostrophecms/stylelint-no-mixed-decls 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@apostrophecms/stylelint-no-mixed-decls 可以商用吗?

@apostrophecms/stylelint-no-mixed-decls 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apostrophecms/stylelint-no-mixed-decls 的下载量有多少?

根据 npm 官方下载统计,@apostrophecms/stylelint-no-mixed-decls 在 2026/10/07 – 2026/11/05 这 30 天内被下载 49,420 次,日均约 1,647 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apostrophecms/stylelint-no-mixed-decls 还在维护吗?

最新版本 1.2.0 发布于 2025/09/16(1.1 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/05/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。