@kashi-ui/stylelint-plugin 0.0.0-stage → 2.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ import { KASHI_DATA } from "./kashi-data.js";
2
+ export { KASHI_DATA };
3
+ export declare function isKnownToken(name: string): boolean;
4
+ /** نزدیک‌ترین نام توکن (برای غلط تایپی)، یا undefined */
5
+ export declare function closestToken(name: string): string | undefined;
6
+ /** توکن(های) کاشی برای یک رنگ hex (#abc یا #aabbcc)، یا [] */
7
+ export declare function tokensForColor(hex: string): string[];
8
+ export type SizeKind = "space" | "radius" | "fontSize";
9
+ /** توکن کاشی برای یک اندازه (مثل 1rem یا 16px)، یا undefined */
10
+ export declare function tokenForSize(kind: SizeKind, value: string): string | undefined;
11
+ /** نوع اندازه بر اساس خاصیت CSS (kebab یا camel) */
12
+ export declare function sizeKindOf(prop: string): SizeKind | undefined;
13
+ export declare function isColorProperty(prop: string): boolean;
@@ -0,0 +1,67 @@
1
+ // کپی خودکار از packages/eslint-plugin/src/tokens.ts در build؛ دستی ویرایش نکنید.
2
+ // منطق مشترک قانون‌های توکن (در ESLint و Stylelint همین فایل کپی نمی‌شود؛ stylelint-plugin آن را از این پکیج می‌گیرد)
3
+ import { KASHI_DATA } from "./kashi-data.js";
4
+ export { KASHI_DATA };
5
+ const NAMES = new Set(KASHI_DATA.names);
6
+ export function isKnownToken(name) {
7
+ return NAMES.has(name);
8
+ }
9
+ function distance(a, b) {
10
+ const dp = Array.from({ length: b.length + 1 }, (_, i) => i);
11
+ for (let i = 1; i <= a.length; i++) {
12
+ let prev = dp[0];
13
+ dp[0] = i;
14
+ for (let j = 1; j <= b.length; j++) {
15
+ const tmp = dp[j];
16
+ dp[j] = Math.min(dp[j] + 1, dp[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
17
+ prev = tmp;
18
+ }
19
+ }
20
+ return dp[b.length];
21
+ }
22
+ /** نزدیک‌ترین نام توکن (برای غلط تایپی)، یا undefined */
23
+ export function closestToken(name) {
24
+ let best, bestD = Infinity;
25
+ for (const n of KASHI_DATA.names) {
26
+ const d = distance(name, n);
27
+ if (d < bestD)
28
+ [best, bestD] = [n, d];
29
+ }
30
+ return bestD <= Math.max(2, Math.floor(name.length / 5)) ? best : undefined;
31
+ }
32
+ /** توکن(های) کاشی برای یک رنگ hex (#abc یا #aabbcc)، یا [] */
33
+ export function tokensForColor(hex) {
34
+ let h = hex.toLowerCase();
35
+ if (/^#[0-9a-f]{3}$/.test(h))
36
+ h = "#" + [...h.slice(1)].map((c) => c + c).join("");
37
+ // سفید و سیاه خالص عمداً ثابت‌اند (متن روی عکس، چاپ، سایه در color-mix) و نباید با تم عوض شوند
38
+ if (h === "#ffffff" || h === "#000000")
39
+ return [];
40
+ return KASHI_DATA.colors[h] ?? [];
41
+ }
42
+ /** توکن کاشی برای یک اندازه (مثل 1rem یا 16px)، یا undefined */
43
+ export function tokenForSize(kind, value) {
44
+ const m = /^(-?[\d.]+)(px|rem)$/.exec(value.trim());
45
+ if (!m || Number(m[1]) <= 0)
46
+ return undefined;
47
+ // کمتر از ۴px (۰٫۲۵rem) خط مو و تنظیم ریز است، نه فاصله‌گذاری طراحی
48
+ if (kind === "space" && (m[2] === "px" ? Number(m[1]) < 4 : Number(m[1]) < 0.25))
49
+ return undefined;
50
+ return KASHI_DATA[kind][`${+Number(m[1]).toFixed(4)}${m[2]}`];
51
+ }
52
+ /** نوع اندازه بر اساس خاصیت CSS (kebab یا camel) */
53
+ export function sizeKindOf(prop) {
54
+ const p = prop.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase());
55
+ // inset/top/left مختصات جای‌گذاری‌اند، نه فاصله‌گذاری؛ توکن فاصله برایشان معنا ندارد
56
+ if (/^(margin|padding|gap|row-gap|column-gap|scroll-margin|scroll-padding)(-|$)/.test(p))
57
+ return "space";
58
+ if (/radius$/.test(p))
59
+ return "radius";
60
+ if (p === "font-size")
61
+ return "fontSize";
62
+ return undefined;
63
+ }
64
+ export function isColorProperty(prop) {
65
+ const p = prop.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase());
66
+ return /^(color|background|background-color|border(-[a-z-]+)?-color|border(-(top|right|bottom|left|inline|block)(-start|-end)?)?|outline(-color)?|fill|stroke|caret-color|accent-color|text-decoration-color|column-rule-color|box-shadow|text-shadow|stop-color|flood-color|lighting-color|text-emphasis-color)$/.test(p);
67
+ }
@@ -0,0 +1,6 @@
1
+ /** همهٔ قانون‌ها؛ در پیکربندی Stylelint: plugins: ["@kashi-ui/stylelint-plugin"] */
2
+ declare const plugins: import("stylelint").Plugin[];
3
+ /** گروه هر قانون، برای مستندات و تست هم‌خوانی */
4
+ export declare const RULE_GROUPS: Record<string, "rtl" | "text" | "kashi">;
5
+ export declare const ruleNames: string[];
6
+ export default plugins;
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ // @kashi-ui/stylelint-plugin — قانون‌های RTL و فارسی کاشی برای Stylelint 16 تا 18
2
+ import { useLogicalProperties, useLogicalValues, noPhysicalTransform, noRootDirection, tailwindLogical } from "./rules/rtl.js";
3
+ import { persianText, persianTypography, useTokens, noUnknownToken } from "./rules/other.js";
4
+ /** همهٔ قانون‌ها؛ در پیکربندی Stylelint: plugins: ["@kashi-ui/stylelint-plugin"] */
5
+ const plugins = [
6
+ useLogicalProperties,
7
+ useLogicalValues,
8
+ noPhysicalTransform,
9
+ noRootDirection,
10
+ tailwindLogical,
11
+ persianText,
12
+ persianTypography,
13
+ useTokens,
14
+ noUnknownToken,
15
+ ];
16
+ /** گروه هر قانون، برای مستندات و تست هم‌خوانی */
17
+ export const RULE_GROUPS = {
18
+ "kashi/use-logical-properties": "rtl",
19
+ "kashi/use-logical-values": "rtl",
20
+ "kashi/no-physical-transform": "rtl",
21
+ "kashi/no-root-direction": "rtl",
22
+ "kashi/tailwind-logical": "rtl",
23
+ "kashi/persian-text": "text",
24
+ "kashi/persian-typography": "text",
25
+ "kashi/use-tokens": "kashi",
26
+ "kashi/no-unknown-token": "kashi",
27
+ };
28
+ export const ruleNames = plugins.map((p) => p.ruleName);
29
+ export default plugins;
@@ -0,0 +1,16 @@
1
+ export declare const rules: {
2
+ readonly "kashi/use-tokens": readonly [true, {
3
+ readonly severity: "warning";
4
+ }];
5
+ readonly "kashi/no-unknown-token": true;
6
+ };
7
+ declare const _default: {
8
+ plugins: import("stylelint").Plugin[];
9
+ rules: {
10
+ readonly "kashi/use-tokens": readonly [true, {
11
+ readonly severity: "warning";
12
+ }];
13
+ readonly "kashi/no-unknown-token": true;
14
+ };
15
+ };
16
+ export default _default;
package/dist/kashi.js ADDED
@@ -0,0 +1,7 @@
1
+ // برای پروژه‌هایی که از کاشی استفاده می‌کنند، کنار config یا strict: extends: [".../config", ".../kashi"]
2
+ import plugins from "./index.js";
3
+ export const rules = {
4
+ "kashi/use-tokens": [true, { severity: "warning" }],
5
+ "kashi/no-unknown-token": true,
6
+ };
7
+ export default { plugins, rules };
@@ -0,0 +1,4 @@
1
+ export declare const persianText: import("stylelint").Plugin;
2
+ export declare const persianTypography: import("stylelint").Plugin;
3
+ export declare const useTokens: import("stylelint").Plugin;
4
+ export declare const noUnknownToken: import("stylelint").Plugin;
@@ -0,0 +1,199 @@
1
+ import valueParser from "postcss-value-parser";
2
+ import { lintPersianText } from "@kashi-ui/persian";
3
+ import { createRule, valueOffset, hasFixedDirection, siblingDecls, inLtrContext } from "../util.js";
4
+ import { closestToken, isColorProperty, isKnownToken, sizeKindOf, tokenForSize, tokensForColor } from "../generated/tokens.js";
5
+ const LABEL = {
6
+ "arabic-char": "حرف عربی / Arabic letter",
7
+ digit: "رقم / digit",
8
+ "missing-zwnj": "نیم‌فاصله / ZWNJ",
9
+ zwsp: "ZWSP",
10
+ "invalid-zwnj": "نیم‌فاصلهٔ بی‌جا / misplaced ZWNJ",
11
+ "space-before-punctuation": "فاصلهٔ علامت / punctuation spacing",
12
+ "missing-space-after-punctuation": "فاصلهٔ علامت / punctuation spacing",
13
+ "latin-punctuation": "علامت لاتین / Latin punctuation",
14
+ kashida: "کشیده / kashida",
15
+ diacritic: "اعراب / diacritic",
16
+ "bidi-mark": "LRM/RLM",
17
+ };
18
+ export const persianText = createRule({
19
+ name: "persian-text",
20
+ description: "Check Persian text in content and quotes strings (Arabic letters, digits, ZWNJ, punctuation)",
21
+ descriptionFa: "متن فارسی رشته‌های content و quotes: همان بررسی‌های قانون‌های متنی ESLint («ي/ك»، ارقام، نیم‌فاصله، علامت‌ها، کشیده، اعراب)؛ با اصلاح خودکار.",
22
+ fixable: true,
23
+ messages: {
24
+ issue: ["{{label}}: «{{found}}» ← «{{replacement}}»", "{{label}}: “{{found}}” → “{{replacement}}”"],
25
+ },
26
+ optionSchema: { digits: [(v) => v === "fa" || v === "latin" || v === false] },
27
+ run(root, { options, report, ignored }) {
28
+ root.walkDecls(/^(content|quotes|--.*)$/i, (decl) => {
29
+ if (ignored(decl))
30
+ return;
31
+ const base = valueOffset(decl);
32
+ const fixes = [];
33
+ valueParser(decl.value).walk((n) => {
34
+ if (n.type !== "string")
35
+ return;
36
+ const issues = lintPersianText(n.value, { digits: options.digits ?? "fa" });
37
+ if (issues.length)
38
+ fixes.push({ node: n, issues });
39
+ });
40
+ let applied = false;
41
+ const applyAll = () => {
42
+ if (applied)
43
+ return;
44
+ applied = true;
45
+ const parsed = valueParser(decl.value);
46
+ parsed.walk((n) => {
47
+ if (n.type !== "string")
48
+ return;
49
+ // تا پایدار شدن: «سلام ,» ← «سلام ،» ← «سلام،»
50
+ let out = n.value;
51
+ for (let round = 0; round < 5; round++) {
52
+ const issues = lintPersianText(out, { digits: options.digits ?? "fa" });
53
+ if (!issues.length)
54
+ break;
55
+ let last = Infinity;
56
+ for (const i of [...issues].reverse()) {
57
+ if (i.end > last)
58
+ continue; // هم‌پوشانی: دور بعد
59
+ out = out.slice(0, i.start) + i.replacement + out.slice(i.end);
60
+ last = i.start;
61
+ }
62
+ }
63
+ n.value = out;
64
+ });
65
+ decl.value = parsed.toString();
66
+ };
67
+ for (const { node, issues } of fixes) {
68
+ for (const i of issues) {
69
+ const index = base + node.sourceIndex + 1 + i.start;
70
+ report({
71
+ node: decl,
72
+ messageId: "issue",
73
+ data: { label: LABEL[i.type] ?? i.type, found: i.found.replace(/‌/g, "[ZWNJ]"), replacement: i.replacement.replace(/‌/g, "[ZWNJ]") },
74
+ index,
75
+ endIndex: Math.max(index + 1, base + node.sourceIndex + 1 + i.end),
76
+ fix: applyAll,
77
+ });
78
+ }
79
+ }
80
+ });
81
+ },
82
+ });
83
+ /** قاعده‌ای که متنش لاتین یا رقم است: فونت mono، حروف بزرگ لاتین، یا direction ثابت */
84
+ function latinOnly(decl) {
85
+ if (hasFixedDirection(decl))
86
+ return true;
87
+ return siblingDecls(decl).some((d) => (/^font(-family)?$/i.test(d.prop) && /mono|monospace|courier|consolas/i.test(d.value)) || (/^text-transform$/i.test(d.prop) && /uppercase|capitalize/i.test(d.value)));
88
+ }
89
+ export const persianTypography = createRule({
90
+ name: "persian-typography",
91
+ description: "Flag positive letter-spacing and italics, which break or fake-slant connected Persian script",
92
+ descriptionFa: "letter-spacing مثبت بین حروف پیوستهٔ فارسی فاصله می‌اندازد و فارسی ایتالیک ندارد (مرورگر حروف را کج می‌کند)؛ مگر در selector لاتین/کد، فونت mono یا direction ثابت. فشرده‌سازی کم (منفی) اتصال را نمی‌شکند و فقط با negativeLetterSpacing: true گزارش می‌شود.",
93
+ fixable: false,
94
+ messages: {
95
+ letterSpacing: [
96
+ "letter-spacing: {{value}} بین حروف پیوستهٔ فارسی فاصله می‌اندازد؛ برای متن فارسی حذفش کنید یا فقط روی :lang(en) بگذارید",
97
+ "letter-spacing: {{value}} pulls apart the joined letters of Persian; remove it for Persian text or scope it to :lang(en)",
98
+ ],
99
+ italic: [
100
+ "فارسی ایتالیک ندارد؛ font-style: {{value}} حروف را مصنوعی کج می‌کند. برای تأکید از وزن (font-weight) یا رنگ استفاده کنید",
101
+ "Persian has no italics; font-style: {{value}} fake-slants the letters. Use font-weight or color for emphasis",
102
+ ],
103
+ },
104
+ optionSchema: { negativeLetterSpacing: [(v) => typeof v === "boolean"] },
105
+ run(root, { options, report, ignored }) {
106
+ root.walkDecls(/^(letter-spacing|font-style)$/i, (decl) => {
107
+ if (ignored(decl) || inLtrContext(decl) || latinOnly(decl))
108
+ return;
109
+ const v = decl.value.trim().toLowerCase();
110
+ if (/^letter-spacing$/i.test(decl.prop)) {
111
+ if (/^(normal|inherit|initial|unset|revert|revert-layer|0([a-z%]*)?|0?\.0+[a-z]*)$/.test(v) || /var\(/.test(v))
112
+ return;
113
+ if (v.startsWith("-") && !options.negativeLetterSpacing)
114
+ return;
115
+ report({ node: decl, messageId: "letterSpacing", data: { value: decl.value }, word: decl.prop });
116
+ }
117
+ else if (/^(italic|oblique)/.test(v)) {
118
+ report({ node: decl, messageId: "italic", data: { value: decl.value }, word: decl.value });
119
+ }
120
+ });
121
+ },
122
+ });
123
+ /** متغیرهای --k-* که خود این stylesheet تعریف می‌کند */
124
+ function definedHere(root) {
125
+ const out = new Set();
126
+ root.walkDecls(/^--k-/, (d) => void out.add(d.prop));
127
+ return out;
128
+ }
129
+ export const useTokens = createRule({
130
+ name: "use-tokens",
131
+ description: "Prefer Kashi tokens over hard-coded colors and sizes that equal a token",
132
+ descriptionFa: "رنگ hex یا اندازهٔ ثابتی که دقیقاً یک توکن کاشی است (#0fa89b، 1rem در padding، 12px در border-radius) ← var(--k-…)، تا با تم تیره، برند و تراکم عوض شود.",
133
+ fixable: false,
134
+ messages: {
135
+ color: ["{{found}} همان توکن {{tokens}} کاشی است؛ var() بنویسید تا با تم تیره و برند عوض شود", "{{found}} is Kashi's {{tokens}}; use var() so it follows dark mode and the brand"],
136
+ size: ["{{found}} همان توکن {{tokens}} کاشی است؛ var({{tokens}}) بنویسید", "{{found}} equals Kashi's {{tokens}}; use var({{tokens}})"],
137
+ },
138
+ run(root, { report, ignored }) {
139
+ root.walkDecls((decl) => {
140
+ if (decl.prop.startsWith("--") || ignored(decl))
141
+ return;
142
+ const base = valueOffset(decl);
143
+ const color = isColorProperty(decl.prop.toLowerCase());
144
+ const kind = sizeKindOf(decl.prop.toLowerCase());
145
+ if (!color && !kind)
146
+ return;
147
+ valueParser(decl.value).walk((n) => {
148
+ // var() فقط پیش‌فرض است؛ calc و color-mix محاسبه‌اند، نه مقدار طراحی
149
+ if (n.type === "function" && /^(var|calc|min|max|clamp|color-mix)$/i.test(n.value))
150
+ return false;
151
+ if (n.type !== "word")
152
+ return;
153
+ if (color && /^#[0-9a-f]{3}([0-9a-f]{3})?$/i.test(n.value)) {
154
+ const tokens = tokensForColor(n.value);
155
+ if (tokens.length)
156
+ report({ node: decl, messageId: "color", data: { found: n.value, tokens: tokens.join("، ") }, index: base + n.sourceIndex, endIndex: base + n.sourceIndex + n.value.length });
157
+ }
158
+ else if (kind) {
159
+ const token = tokenForSize(kind, n.value);
160
+ if (token)
161
+ report({ node: decl, messageId: "size", data: { found: n.value, tokens: token }, index: base + n.sourceIndex, endIndex: base + n.sourceIndex + n.value.length });
162
+ }
163
+ return undefined;
164
+ });
165
+ });
166
+ },
167
+ });
168
+ export const noUnknownToken = createRule({
169
+ name: "no-unknown-token",
170
+ description: "Disallow var(--k-*) references to tokens that don't exist in Kashi",
171
+ descriptionFa: "var(--k-…) با نامی که در کاشی نیست (غلط تایپی یا توکن حذف‌شده)؛ متغیری که خود فایل تعریف می‌کند مجاز است.",
172
+ fixable: false,
173
+ messages: {
174
+ unknown: ["توکن «{{name}}» در کاشی نیست؛ var() خالی می‌ماند.{{hint}}", "“{{name}}” is not a Kashi token; the var() resolves to nothing.{{hint}}"],
175
+ },
176
+ optionSchema: { allow: [(v) => typeof v === "string"] },
177
+ run(root, { options, report, ignored }) {
178
+ const own = definedHere(root);
179
+ const allow = (options.allow ?? []).map((s) => new RegExp(s));
180
+ const check = (node, value, base) => {
181
+ for (const m of value.matchAll(/var\(\s*(--k-[\w-]+)/g)) {
182
+ const name = m[1];
183
+ if (isKnownToken(name) || own.has(name) || allow.some((r) => r.test(name)))
184
+ continue;
185
+ const closest = closestToken(name);
186
+ const index = base + m.index + m[0].indexOf(name);
187
+ report({ node, messageId: "unknown", data: { name, hint: closest ? ` ${closest}?` : "" }, index, endIndex: index + name.length });
188
+ }
189
+ };
190
+ root.walkDecls((decl) => {
191
+ if (!ignored(decl))
192
+ check(decl, decl.value, valueOffset(decl));
193
+ });
194
+ root.walkAtRules((at) => {
195
+ if (!ignored(at))
196
+ check(at, at.params, `@${at.name}${at.raws.afterName ?? " "}`.length);
197
+ });
198
+ },
199
+ });
@@ -0,0 +1,7 @@
1
+ import { siblingDecls } from "../util.js";
2
+ export declare const useLogicalProperties: import("stylelint").Plugin;
3
+ export declare const useLogicalValues: import("stylelint").Plugin;
4
+ export declare const noPhysicalTransform: import("stylelint").Plugin;
5
+ export declare const noRootDirection: import("stylelint").Plugin;
6
+ export declare const tailwindLogical: import("stylelint").Plugin;
7
+ export { siblingDecls };
@@ -0,0 +1,295 @@
1
+ import valueParser from "postcss-value-parser";
2
+ import { logicalProperty, logicalValue, logicalTailwindClass, splitTailwindClass } from "@kashi-ui/persian";
3
+ import { createRule, isDirectionAware, namesPhysicalSide, isCentering, inKeyframes, siblingDecls, valueOffset, tailwindVersion, hasFixedDirection, stripDirection, inLtrContext } from "../util.js";
4
+ const fn = (v) => /^(?:var|env|calc|min|max|clamp)\(/i.test(v);
5
+ /** مقدارهای فاصله‌دار سطح اول (بدون شکستن داخل پرانتز) */
6
+ function topLevelValues(value) {
7
+ const parsed = valueParser(value);
8
+ return parsed.nodes.filter((n) => n.type !== "space" && n.type !== "comment").map((n) => valueParser.stringify(n));
9
+ }
10
+ const SHORTHAND_4 = /^(margin|padding|inset|scroll-margin|scroll-padding|border-width|border-style|border-color)$/i;
11
+ const blockInline = (prop) => {
12
+ const m = /^(border)-(width|style|color)$/i.exec(prop);
13
+ return m ? [`border-block-${m[2]}`, `border-inline-${m[2]}`] : [`${prop}-block`, `${prop}-inline`];
14
+ };
15
+ export const useLogicalProperties = createRule({
16
+ name: "use-logical-properties",
17
+ description: "Use logical properties (margin-inline-start, inset-inline-end…) instead of physical left/right ones",
18
+ descriptionFa: "خاصیت فیزیکی (margin-left، padding-right، left، border-left، border-top-left-radius…) و میان‌برهای چهارمقداری نامتقارن (margin: 0 1rem 0 2rem) ← معادل منطقی؛ با اصلاح خودکار.",
19
+ fixable: true,
20
+ messages: {
21
+ property: ["«{{prop}}» فیزیکی است؛ «{{logical}}» بنویسید", "“{{prop}}” is physical; use “{{logical}}”"],
22
+ shorthand: [
23
+ "«{{prop}}: {{value}}» چپ و راست متفاوت دارد؛ با {{block}} و {{inline}} بنویسید",
24
+ "“{{prop}}: {{value}}” has different left and right values; use {{block}} and {{inline}}",
25
+ ],
26
+ radius: ["گوشه‌های چپ و راست border-radius متفاوت‌اند؛ گوشه‌های منطقی (border-start-start-radius…) بنویسید", "border-radius has different left and right corners; use logical corners (border-start-start-radius…)"],
27
+ },
28
+ optionSchema: {
29
+ ignoreProperties: [(v) => typeof v === "string"],
30
+ ignoreLocalVariables: [(v) => typeof v === "boolean"],
31
+ },
32
+ run(root, { options, report, ignored }) {
33
+ const dir = options.direction ?? "rtl";
34
+ const ignoreProps = new Set((options.ignoreProperties ?? []).map((p) => p.toLowerCase()));
35
+ const ignoreLocal = options.ignoreLocalVariables ?? true;
36
+ const side = (physical) => ((physical === "left") === (dir === "ltr") ? "start" : "end");
37
+ root.walkDecls((decl) => {
38
+ const prop = decl.prop.toLowerCase();
39
+ if (prop.startsWith("--") || ignoreProps.has(prop) || ignored(decl) || isDirectionAware(decl))
40
+ return;
41
+ // direction ثابت در همین عنصر margin/padding/border را ثابت می‌کند؛ left/right اما با جهت قاب والد سنجیده می‌شوند
42
+ if ((hasFixedDirection(decl) || inLtrContext(decl)) && !/^(left|right|inset)/.test(prop))
43
+ return;
44
+ const logical = logicalProperty(prop, dir);
45
+ if (logical) {
46
+ const physicalSide = /left/.test(prop) ? "left" : "right";
47
+ if (namesPhysicalSide(decl, physicalSide))
48
+ return;
49
+ if (/^(left|right)$/.test(prop)) {
50
+ if (isCentering(decl))
51
+ return;
52
+ // left: auto قاعدهٔ فیزیکی دیگری را خنثی می‌کند؛ نسخهٔ منطقی‌اش آن را خنثی نمی‌کند
53
+ if (/^auto$/i.test(decl.value.trim()))
54
+ return;
55
+ if (ignoreLocal && /var\(\s*--(?!k-)/.test(decl.value))
56
+ return;
57
+ }
58
+ report({ node: decl, messageId: "property", data: { prop: decl.prop, logical }, word: decl.prop, fix: () => void (decl.prop = logical) });
59
+ return;
60
+ }
61
+ if (SHORTHAND_4.test(prop)) {
62
+ const v = topLevelValues(decl.value);
63
+ if (v.length !== 4 || v[1] === v[3] || namesPhysicalSide(decl))
64
+ return;
65
+ if (prop === "inset" && isCentering(decl))
66
+ return;
67
+ // بخش افقی را declaration بعدی (padding-inline: …) بازنویسی می‌کند؛ سمت فیزیکی اثری ندارد
68
+ const after = siblingDecls(decl).slice(siblingDecls(decl).indexOf(decl) + 1);
69
+ if (after.some((d) => d.prop.toLowerCase() === blockInline(prop)[1]))
70
+ return;
71
+ const [block, inline] = blockInline(prop);
72
+ const [top, right, bottom, left] = v;
73
+ const start = side("left") === "start" ? left : right;
74
+ const end = side("left") === "start" ? right : left;
75
+ const blockValue = top === bottom ? top : `${top} ${bottom}`;
76
+ report({
77
+ node: decl,
78
+ messageId: "shorthand",
79
+ data: { prop: decl.prop, value: decl.value, block, inline },
80
+ word: decl.prop,
81
+ fix: () => {
82
+ decl.cloneBefore({ prop: block, value: blockValue });
83
+ decl.prop = inline;
84
+ decl.value = `${start} ${end}`;
85
+ },
86
+ });
87
+ return;
88
+ }
89
+ if (prop === "border-radius") {
90
+ if (decl.value.includes("/")) {
91
+ const [h] = decl.value.split("/");
92
+ const hv = topLevelValues(h);
93
+ if (hv.length === 4 && (hv[0] !== hv[1] || hv[2] !== hv[3]) && !namesPhysicalSide(decl))
94
+ report({ node: decl, messageId: "radius", word: decl.prop });
95
+ return;
96
+ }
97
+ const v = topLevelValues(decl.value);
98
+ let [tl, tr, br, bl] = v;
99
+ if (v.length === 3)
100
+ bl = tr;
101
+ if (v.length < 3 || (tl === tr && br === bl) || namesPhysicalSide(decl))
102
+ return;
103
+ // top-left: start-? ؛ در rtl چپ «پایان» است
104
+ const corner = (block, phys) => `border-${block}-${side(phys)}-radius`;
105
+ report({
106
+ node: decl,
107
+ messageId: "radius",
108
+ word: decl.prop,
109
+ fix: () => {
110
+ const pairs = [
111
+ [corner("start", "left"), tl],
112
+ [corner("start", "right"), tr],
113
+ [corner("end", "right"), br],
114
+ [corner("end", "left"), bl],
115
+ ];
116
+ pairs.sort((a, b) => a[0].localeCompare(b[0]));
117
+ for (const [p, val] of pairs)
118
+ decl.cloneBefore({ prop: p, value: val });
119
+ decl.remove();
120
+ },
121
+ });
122
+ }
123
+ });
124
+ },
125
+ });
126
+ export const useLogicalValues = createRule({
127
+ name: "use-logical-values",
128
+ description: "Use logical values for text-align, float and clear (start/end, inline-start/inline-end)",
129
+ descriptionFa: "text-align: left/right ← start/end، و float/clear: left/right ← inline-start/inline-end؛ با اصلاح خودکار.",
130
+ fixable: true,
131
+ messages: {
132
+ value: ["«{{prop}}: {{value}}» فیزیکی است؛ «{{logical}}» بنویسید", "“{{prop}}: {{value}}” is physical; use “{{logical}}”"],
133
+ },
134
+ run(root, { options, report, ignored }) {
135
+ const dir = options.direction ?? "rtl";
136
+ root.walkDecls(/^(text-align|text-align-last|float|clear)$/i, (decl) => {
137
+ if (ignored(decl) || isDirectionAware(decl) || hasFixedDirection(decl) || inLtrContext(decl))
138
+ return;
139
+ const value = decl.value.replace(/\s*!important\s*$/i, "");
140
+ const logical = logicalValue(decl.prop, value, dir);
141
+ if (!logical || namesPhysicalSide(decl, value.trim().toLowerCase()))
142
+ return;
143
+ report({
144
+ node: decl,
145
+ messageId: "value",
146
+ data: { prop: decl.prop, value: value.trim(), logical },
147
+ index: valueOffset(decl),
148
+ endIndex: valueOffset(decl) + value.length,
149
+ fix: () => void (decl.value = decl.value.replace(/left|right/i, logical)),
150
+ });
151
+ });
152
+ },
153
+ });
154
+ export const noPhysicalTransform = createRule({
155
+ name: "no-physical-transform",
156
+ description: "Flag horizontal translations that don't flip for right-to-left layouts",
157
+ descriptionFa: "translateX و translate افقی در صفحهٔ راست‌به‌چپ به سمت اشتباه حرکت می‌کنند؛ مگر در :dir()، وسط‌چینی left: 50%، یا با متغیر.",
158
+ fixable: false,
159
+ messages: {
160
+ translate: [
161
+ "«{{value}}» در جهت مخالف به سمت اشتباه حرکت می‌کند؛ برای :dir(rtl) قرینه‌اش را بگذارید یا مقدار را از متغیری بگیرید که با جهت عوض می‌شود",
162
+ "“{{value}}” moves the wrong way when direction flips; mirror it under :dir(rtl) or take the distance from a direction-aware variable",
163
+ ],
164
+ },
165
+ run(root, { report, ignored }) {
166
+ // انیمیشن‌هایی که در :dir() برعکس یا عوض می‌شوند (مثل skeleton کاشی)
167
+ let animationHandled = false;
168
+ root.walkDecls(/^(animation|animation-name|animation-direction)$/i, (d) => {
169
+ if (isDirectionAware(d))
170
+ animationHandled = true;
171
+ });
172
+ // ‎±50%‎ نصف پهنای خود عنصر است (وسط‌چینی دور یک نقطه)، نه حرکت به یک سمت
173
+ const nonZero = (v) => !/^[-+]?0(\.0*)?([a-z%]*)$/i.test(v.trim()) && !/var\(/.test(v) && !/^[-+]?50%$/.test(v.trim());
174
+ const xOf = (d) => {
175
+ if (/^translate$/i.test(d.prop))
176
+ return topLevelValues(d.value)[0];
177
+ const m = /translate(?:x|3d)?\(\s*([^,\s)]+)/i.exec(d.value);
178
+ return m?.[1];
179
+ };
180
+ // قاعده‌هایی که نسخهٔ جهت‌دارشان ([dir="ltr"] .x، .x:dir(rtl)) جای دیگری از همین فایل آمده
181
+ const mirrored = new Set();
182
+ root.walkRules((r) => {
183
+ if (/:dir\(|\[dir\b|\.(rtl|ltr)\b/.test(r.selector))
184
+ for (const sel of r.selectors)
185
+ mirrored.add(stripDirection(sel));
186
+ });
187
+ root.walkDecls(/^(transform|translate)$/i, (decl) => {
188
+ if (ignored(decl) || isDirectionAware(decl) || namesPhysicalSide(decl) || isCentering(decl))
189
+ return;
190
+ const rule = decl.parent?.type === "rule" ? decl.parent : undefined;
191
+ if (rule && rule.selectors.every((sel) => mirrored.has(stripDirection(sel))))
192
+ return;
193
+ const frames = inKeyframes(decl);
194
+ if (frames && animationHandled)
195
+ return;
196
+ if (frames) {
197
+ // لرزش (shake) به هر دو سمت می‌رود و جهت ندارد
198
+ const signs = new Set();
199
+ frames.walkDecls(/^(transform|translate)$/i, (d) => {
200
+ const x = xOf(d);
201
+ if (x && nonZero(x))
202
+ signs.add(x.trim().startsWith("-") ? "-" : "+");
203
+ });
204
+ if (signs.size === 2)
205
+ return;
206
+ }
207
+ if (/^translate$/i.test(decl.prop)) {
208
+ const [x] = topLevelValues(decl.value);
209
+ if (x && nonZero(x) && !/^none$/i.test(x))
210
+ report({ node: decl, messageId: "translate", data: { value: `translate: ${decl.value}` }, word: decl.value });
211
+ return;
212
+ }
213
+ valueParser(decl.value).walk((n) => {
214
+ if (n.type !== "function")
215
+ return;
216
+ const name = n.value.toLowerCase();
217
+ const args = n.nodes.filter((a) => a.type !== "div" && a.type !== "space").map((a) => valueParser.stringify(a));
218
+ if ((name === "translatex" || name === "translate" || name === "translate3d") && args[0] && nonZero(args[0])) {
219
+ const text = valueParser.stringify(n);
220
+ report({ node: decl, messageId: "translate", data: { value: text }, index: valueOffset(decl) + n.sourceIndex, endIndex: valueOffset(decl) + n.sourceIndex + text.length });
221
+ }
222
+ });
223
+ });
224
+ },
225
+ });
226
+ export const noRootDirection = createRule({
227
+ name: "no-root-direction",
228
+ description: "Disallow setting the page direction with CSS (direction on html/body/:root); use the dir attribute",
229
+ descriptionFa: "direction: rtl روی html، body یا :root؛ جهت صفحه را با <html dir=\"rtl\"> بدهید تا صفحه‌خوان، ترتیب Tab، اسکرول‌بار و input ها هم بدانند. direction: ltr روی یک جزء (کد، شماره) مجاز است.",
230
+ fixable: false,
231
+ messages: {
232
+ root: [
233
+ "جهت صفحه با CSS («{{selector}} { direction: {{value}} }»)؛ <html dir=\"{{value}}\"> بنویسید. CSS برای مرورگر و صفحه‌خوان جهت سند را عوض نمی‌کند",
234
+ "Page direction set in CSS (“{{selector}} { direction: {{value}} }”); use <html dir=\"{{value}}\">. CSS doesn't change the document direction for the browser and assistive tech",
235
+ ],
236
+ },
237
+ run(root, { report, ignored }) {
238
+ root.walkDecls(/^direction$/i, (decl) => {
239
+ if (ignored(decl) || !/^(rtl|ltr)$/i.test(decl.value.trim()))
240
+ return;
241
+ const rule = decl.parent;
242
+ if (rule?.type !== "rule")
243
+ return;
244
+ const roots = rule.selectors.filter((s) => /^\s*(html|body|:root|\*)\s*$/i.test(s));
245
+ if (roots.length)
246
+ report({ node: decl, messageId: "root", data: { selector: roots.join(", "), value: decl.value.trim() }, word: decl.prop });
247
+ });
248
+ },
249
+ });
250
+ export const tailwindLogical = createRule({
251
+ name: "tailwind-logical",
252
+ description: "Use logical Tailwind classes in @apply",
253
+ descriptionFa: "@apply با کلاس فیزیکی Tailwind (ml-4، text-right، rounded-l…) ← کلاس منطقی؛ با اصلاح خودکار.",
254
+ fixable: true,
255
+ messages: {
256
+ physical: ["@apply «{{found}}» فیزیکی است؛ «{{replacement}}» بنویسید", "@apply “{{found}}” is physical; use “{{replacement}}”"],
257
+ },
258
+ optionSchema: {
259
+ tailwindVersion: [(v) => typeof v === "string" || typeof v === "number"],
260
+ prefix: [(v) => typeof v === "string"],
261
+ },
262
+ run(root, { options, report, ignored }) {
263
+ const dir = options.direction ?? "rtl";
264
+ const version = tailwindVersion(options.tailwindVersion);
265
+ const modern = !version || version[0] > 4 || (version[0] === 4 && version[1] >= 2);
266
+ root.walkAtRules(/^apply$/i, (at) => {
267
+ if (ignored(at) || isDirectionAware(at))
268
+ return;
269
+ const classes = at.params.split(/\s+/).filter(Boolean);
270
+ if (classes.some((c) => /(^|:)(rtl|ltr):/.test(c)))
271
+ return;
272
+ const centered = classes.some((c) => /^-translate-x-1\/2$/.test(splitTailwindClass(c).negative ? "-" + splitTailwindClass(c).base : c));
273
+ let offset = 0;
274
+ for (const cls of classes) {
275
+ const at0 = at.params.indexOf(cls, offset);
276
+ offset = at0 + cls.length;
277
+ if (centered && /^(left|right)-1\/2$/.test(splitTailwindClass(cls).base))
278
+ continue;
279
+ const rep = logicalTailwindClass(cls, { dir, inset: modern ? "inset-s" : "start", prefix: options.prefix ?? "" });
280
+ if (!rep)
281
+ continue;
282
+ const index = `@${at.name}${at.raws.afterName ?? " "}`.length + at0;
283
+ report({
284
+ node: at,
285
+ messageId: "physical",
286
+ data: { found: cls, replacement: rep },
287
+ index,
288
+ endIndex: index + cls.length,
289
+ fix: () => void (at.params = at.params.split(/(\s+)/).map((p) => (p === cls ? rep : p)).join("")),
290
+ });
291
+ }
292
+ });
293
+ },
294
+ });
295
+ export { siblingDecls };