Как сохранить этот код CSS СУХИМ вместе с удовлетворением нисходящей специфики

Вот мой блок CSS:

:global {
  table, .table {
    border-collapse: collapse;
    border-spacing: 0;
    width: 100%;
    font-size: 0.8em;
    font-family: var(--font-family-roboto);
    font-weight: normal;
    letter-spacing: .25px;

    &.fixed {
      table-layout: fixed;
    }

    &.disabled {
      opacity: 0.5;
    }

    > thead {
      border-bottom: 1px solid var(--grey-light);
    }

    > thead th {
      color: var(--blue);
      text-transform: uppercase;
      font-family: var(--font-family-simplon);
      font-weight: bold;
      letter-spacing: 1px;
      text-align: left;
      line-height: 12px;

      span {
        vertical-align: top;
      }
    }

    > thead th,
    > tfoot td,
    > tbody td {
      padding: 5px;
      word-wrap: break-word;
      vertical-align: middle;
      white-space: pre;

      &:last-child,
      &:first-child {
        padding-left: 20px;
      }
    }

    > tfoot {
      background-color: var(--grey-lighter);
      font-weight: var(--font-weight-bold);
      letter-spacing: 1.25px;
      text-transform: uppercase;
    }

    > caption {
      text-align: left;
      font-size: 1.2em;
      margin-bottom: 15px;
      padding: 0 2px;
    }
  }
}

stylelint жалуется на:

 39:5  ✖  Expected selector ":global table > thead th" to come before selector ":global .table > thead th"                           no-descending-specificity
 41:5  ✖  Expected selector ":global table > tbody td" to come before selector ":global .table > tfoot td"                           no-descending-specificity
 47:7  ✖  Expected selector ":global table > tbody td:last-child" to come before selector ":global .table > tfoot td:last-child"     no-descending-specificity
 48:7  ✖  Expected selector ":global table > thead th:first-child" to come before selector ":global .table > thead th:last-child"    no-descending-specificity
 48:7  ✖  Expected selector ":global table > tfoot td:first-child" to come before selector ":global .table > tfoot td:last-child"    no-descending-specificity
 48:7  ✖  Expected selector ":global table > tfoot td:first-child" to come before selector ":global .table > tbody td:last-child"    no-descending-specificity
 48:7  ✖  Expected selector ":global table > tbody td:first-child" to come before selector ":global .table > tfoot td:last-child"    no-descending-specificity
 48:7  ✖  Expected selector ":global table > tbody td:first-child" to come before selector ":global .table > tbody td:last-child"    no-descending-specificity
 48:7  ✖  Expected selector ":global table > tbody td:first-child" to come before selector ":global .table > tfoot td:first-child"   no-descending-specificity

Кажется, что он хочет, чтобы я разделился table а также .table объявления, но это будет только дублировать стили. Не уверен, как сохранить код СУХОЙ и удовлетворить stylelint. Есть идеи?

Эти ошибки могут быть воспроизведены здесь https://stylelint.io/demo/.

1 ответ

Авторы stylelint подтвердили, что это поведение соответствует назначению:

Я считаю, что правило ведет себя так, как задокументировано. Сравнивает обессиленный CSS, например table > thead th:first-child имеет специфичность 0,1,3, тогда как .table > thead th:last-child имеет специфичность 0,2,2. Таким образом, порядок источника и порядок специфичности не совпадают. У desugared CSS есть встроенные селекторы классов и селекторы элементов. Правило предназначено для предупреждения от них.

Я могу придумать два варианта: либо рефакторинг, чтобы они не переплетались, либо отключение правил для этого блока кода с помощью /* stylelint-disable .. */ Командные комментарии.

Единственное жизнеспособное решение в этом случае - это рефакторинг кода или отключение правила. Я выбираю позже.

Другие вопросы по тегам