如何校验列表计数器未适配多语言?

解读

在国内大型 Web 产品中,列表计数器(list-style-type 或 counter-* 系列)常被写成 decimal、decimal-leading-zero 甚至 cjk-ideographic,却忽略了阿拉伯语、波斯语、乌尔都语、泰语等 RTL/LTR 混合场景。面试官问“如何校验”,并非让你背诵规则名,而是考察三件事:

  1. 能否识别“未适配”的代码特征(硬编码、未抽变量、未用伪类、未用 dir 属性);
  2. 能否用 Stylelint 的语义化规则 + 自定义插件闭环检测;
  3. 能否把结果与 CI、i18n 平台、设计令牌系统打通,形成“检测-提示-修复-回归”的完整链路。
    回答必须体现“规则设计思路 + 插件代码骨架 + 自动化落地”,否则会被认为只停留在“跑个插件”层面。

知识点

  1. 列表计数器国际化缺陷的三种根因
    • 硬编码 list-style-type: decimal;
    • 未使用 CSS @counter-style 自定义命名;
    • 未与 HTML dir、lang、CSS ::marker 内容联动。
  2. Stylelint 插件体系
    • stylelint.createPlugin(ruleName, ruleFunction);
    • root.walkDecls() 遍历声明;
    • context.fix 提供自动修复;
    • result.warn() 上报错误。
  3. PostCSS Value Parser 与 CSS Tree 解析
    • 识别 counter-reset、counter-increment、content 中的计数器名;
    • 识别 list-style、list-style-type 的取值。
  4. 国内工程配套
    • 设计令牌仓库(如 Ant Design Token)托管各语言计数器名;
    • GitHub Actions / GitLab CI 在 MR 阶段运行 stylelint --custom-syntax postcss-scss;
    • 与语雀/飞书文档对接,把规则报错自动推送给设计师,实现“左移”。

答案

步骤一:建立“不允许硬编码”规则
在 .stylelintrc.js 中关闭原生允许硬编码的规则,并新增自定义规则:

// stylelint-plugin-i18n-counter/index.js
const stylelint = require('stylelint');
const { pluginNamespace } = require('./utils');

const ruleName = 'i18n/no-hardcoded-counter';
const messages = stylelint.utils.ruleMessages(ruleName, {
  rejected: (type) => `列表计数器“${type}”未做多语言适配,请使用设计令牌变量`
});

module.exports = stylelint.createPlugin(ruleName, function(primaryOption, secondaryOption = {}, context) {
  return (root, result) => {
    const validTokens = secondaryOption.tokens || ['--counter-decimal', '--counter-arabic', '--counter-cjk'];
    root.walkDecls(decl => {
      if (decl.prop === 'list-style-type' || decl.prop === 'list-style') {
        const value = decl.value.trim();
        // 过滤掉已允许的变量
        if (value.startsWith('var(')) return;
        // 过滤掉 @counter-style 自定义名
        if (validTokens.includes(value)) return;
        // 其余均视为硬编码
        stylelint.utils.report({
          message: messages.rejected(value),
          node: decl,
          result,
          ruleName,
          line: decl.source.start.line,
          column: decl.source.start.column
        });
        // 自动修复:替换成默认令牌
        if (context.fix) {
          decl.value = 'var(--counter-decimal)';
        }
      }
    });
  };
});

步骤二:在配置里启用规则并注入令牌白名单

// .stylelintrc.js
module.exports = {
  plugins: ['./stylelint-plugin-i18n-counter'],
  rules: {
    'i18n/no-hardcoded-counter': [true, {
      tokens: ['--counter-decimal', '--counter-arabic', '--counter-thai', '--counter-cjk']
    }]
  }
};

步骤三:CI 卡点
在 GitHub Actions 工作流中:

- name: Lint CSS
  run: |
    npx stylelint "src/**/*.{css,scss,less}" --custom-syntax postcss-scss

步骤四:与 i18n 平台联动
把 --counter-* 变量托管到统一令牌服务,设计师新增语言时只需在令牌仓库提 MR,CI 自动回归跑 Stylelint,确保新语言计数器被及时消费,无硬编码回退。

拓展思考

  1. 如何支持动态 lang 属性切换?
    可扩展插件,遍历同一文件中是否配套写了 :lang(ar) { --counter-decimal: arabic-indic; },若无则报 missing-lang-counter。
  2. 如何检测 counter-reset/increment 的命名空间污染?
    在插件里再开一条规则 i18n/no-ambiguous-counter-name,强制计数器名以 i18n- 前缀开头,避免与业务逻辑计数器冲突。
  3. 如何度量修复率?
    在 CI 中把 Stylelint 结果上传至内部质量看板,按团队、按仓库统计“硬编码计数器”缺陷密度,作为技术 KPI 之一,实现“国际化缺陷可量化”。