如何检测使用 margin-left 而未提供 margin-inline-start?
解读
这道题考察的是“逻辑属性(Logical Properties)”在样式代码中的落地能力。国内大厂(阿里、腾讯、字节、美团)的国际化业务线(Lazada、TikTok、Shopee 等)都要求一套代码同时服务 LTR 与 RTL 场景。
margin-left 属于“物理属性”,在 RTL 下会失效;margin-inline-start 是“逻辑属性”,能随书写方向自动翻转。
Stylelint 作为 CI 卡点工具,必须能在本地保存、MR 流水线、IDE 实时提示三处把“物理属性”拦截下来,否则测试环境 Arabic/Hebrew 语言包一上线就错位,回滚成本极高。
因此,面试官想知道:
- 你是否理解“物理→逻辑”的迁移背景;
- 能否用 Stylelint 的插件机制把规则写死、配死、跑死,让团队零成本遵守;
- 是否具备“渐进升级”思路,避免一次性全量改动导致业务线炸锅。
知识点
- 逻辑属性规范:margin-inline-、margin-block-、padding-inline-、border-inline- 等,浏览器支持度已 95%+(CanIUse 国内镜像数据)。
- Stylelint 插件体系:
- 官方插件列表 stylelint-config-logical 仅覆盖 20+ 规则,未包含 margin-left→margin-inline-start 的强制校验;
- 需要手写 stylelint-plugin-logical-css 或基于 stylelint-use-logical 二次定制。
- PostCSS AST 节点类型:
- decl.prop 获取属性名;
- decl.parent.selector 获取选择器,用于排除 @ckeditor、.legacy-* 等白名单;
- 通过 utils.report() 上报错误,可带自动修复(fix)函数。
- 国内落地配套:
- 与 husky + lint-staged 结合,在 git commit 阶段拦截;
- 在 GitHub Actions / GitLab CI 中追加 stylelint --max-warnings 0,保证 MR 不可合并;
- 提供 npx stylelint --fix 一键批量替换,降低业务线迁移阻力;
- 对 node_modules、dist、_vendor 目录默认忽略,避免第三方库误报。
- 灰度策略:
- 先 warn 后 error,统计 warn 数量,按页面维度逐步清零;
- 配置 allowList: ['.legacy-ltr', '.third-party-*'],保证存量广告代码不降速。
答案
步骤一:安装依赖
npm i -D stylelint stylelint-use-logical postcss postcss-logical
步骤二:在项目根目录新建 stylelint.config.js
module.exports = {
plugins: ['stylelint-use-logical'],
rules: {
'liberty/use-logical': ['always', {
severity: 'error',
except: ['margin-left', 'margin-right'], // 先只处理 margin,后续再扩
}]
}
};
(说明:stylelint-use-logical 已内置 margin-left → margin-inline-start 的自动修复,无需再手写插件。)
步骤三:若官方插件不满足,手写自定义规则 plugins/stylelint-logical-margin.js
const stylelint = require('stylelint');
const { pluginNamespace } = stylelint.utils;
const ruleName = 'plugin/logical-margin';
const messages = stylelint.utils.ruleMessages(ruleName, {
rejected: (prop) => Unexpected physical property "${prop}", use margin-inline-start instead.,
});
module.exports = stylelint.createPlugin(ruleName, (primaryOption, secondaryOption = {}, context) => { return (root, result) => { root.walkDecls((decl) => { if (decl.prop === 'margin-left') { // 白名单过滤 if (secondaryOption.ignoreSelectors && secondaryOption.ignoreSelectors.some((s) => decl.parent.selector.includes(s))) return; // 自动修复 if (context.fix) { decl.prop = 'margin-inline-start'; return; } stylelint.utils.report({ message: messages.rejected(decl.prop), node: decl, result, ruleName, }); } }); }; });
步骤四:在 stylelint.config.js 中注册自定义插件
module.exports = {
plugins: ['./plugins/stylelint-logical-margin'],
rules: {
'plugin/logical-margin': [true, { ignoreSelectors: ['.legacy-ltr'] }],
},
};
步骤五:本地验证
npx stylelint "src/**/*.{css,scss,less}"
出现 error: Unexpected physical property "margin-left", use margin-inline-start instead. 即拦截成功。
步骤六:一键修复
npx stylelint "src/**/*.{css,scss,less}" --fix
所有 margin-left 被替换成 margin-inline-start,git diff 无人工改动,MR 可合并。
拓展思考
- 性能陷阱:
逻辑属性在 Chrome 108+ 已做底层优化,但低版本 WebView(国内安卓 7.x)会触发 reflow 回退。
解决方案:- 用 PostCSS Logical 在编译期把 margin-inline-start 转回 margin-left/margin-right,并插入 dir="rtl" 属性选择器;
- Stylelint 规则加一条限制:禁止在 .webview-old 作用域里使用逻辑属性,避免运行时计算。
- 设计 token 联动:
把 spacing 基线 4px 统一收敛到 CSS 变量 --spacing-start: 4px; 结合 Stylelint 的 declaration-property-value-allowed-list 规则,强制 margin-inline-start 只能使用 var(--spacing-*),防止设计师口头“再左移 1px”导致代码离散。 - 业务线渐进:
先在新模块目录 src/modules/2024-/**/.scss 开启 error,老模块保持 warn;
通过内部数据平台统计 warn 曲线,每月下降 20%,三个月后全量 error;
提供 codemod 脚本,一键把遗留 margin-left: 12px 转成 margin-inline-start: var(--spacing-3)。 - 国际化验收:
在 GitLab CI 里追加一步 RTL 快照测试:- 使用 puppeteer 打开 ?lang=ar 页面,对 body 做 screenshot diff;
- 若 margin-left 物理属性漏网,快照像素差异 > 0.1%,直接 fail MR;
- 形成“Stylelint 静态检查 + 视觉回归”双保险,确保线上零 RTL 错位客诉。