@kashi-ui/eslint-plugin 0.0.0-stage → 2.29.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.
- package/LICENSE +21 -0
- package/README.md +76 -2
- package/dist/generated/kashi-data.d.ts +8 -0
- package/dist/generated/kashi-data.js +972 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +93 -0
- package/dist/rules/directional-icon.d.ts +10 -0
- package/dist/rules/directional-icon.js +103 -0
- package/dist/rules/html-dir-lang.d.ts +2 -0
- package/dist/rules/html-dir-lang.js +65 -0
- package/dist/rules/kashi-tokens.d.ts +5 -0
- package/dist/rules/kashi-tokens.js +128 -0
- package/dist/rules/no-bidi-control.d.ts +2 -0
- package/dist/rules/no-bidi-control.js +95 -0
- package/dist/rules/no-physical-style.d.ts +2 -0
- package/dist/rules/no-physical-style.js +112 -0
- package/dist/rules/prefer-kashi-validators.d.ts +60 -0
- package/dist/rules/prefer-kashi-validators.js +169 -0
- package/dist/rules/tailwind-logical.d.ts +14 -0
- package/dist/rules/tailwind-logical.js +171 -0
- package/dist/rules/text-rules.d.ts +9 -0
- package/dist/rules/text-rules.js +149 -0
- package/dist/text.d.ts +19 -0
- package/dist/text.js +267 -0
- package/dist/tokens.d.ts +13 -0
- package/dist/tokens.js +66 -0
- package/dist/util.d.ts +47 -0
- package/dist/util.js +67 -0
- package/package.json +71 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Linter, Rule } from "eslint";
|
|
2
|
+
export type { KashiSettings, Locale } from "./util.js";
|
|
3
|
+
export declare const rules: {
|
|
4
|
+
"no-arabic-chars": Rule.RuleModule;
|
|
5
|
+
"persian-digits": Rule.RuleModule;
|
|
6
|
+
zwnj: Rule.RuleModule;
|
|
7
|
+
"no-invalid-zwnj": Rule.RuleModule;
|
|
8
|
+
"punctuation-spacing": Rule.RuleModule;
|
|
9
|
+
"persian-punctuation": Rule.RuleModule;
|
|
10
|
+
"no-kashida": Rule.RuleModule;
|
|
11
|
+
"no-extra-diacritics": Rule.RuleModule;
|
|
12
|
+
"no-bidi-control": Rule.RuleModule;
|
|
13
|
+
"html-dir-lang": Rule.RuleModule;
|
|
14
|
+
"no-physical-style": Rule.RuleModule;
|
|
15
|
+
"tailwind-logical": Rule.RuleModule;
|
|
16
|
+
"directional-icon": Rule.RuleModule;
|
|
17
|
+
"prefer-kashi-validators": Rule.RuleModule;
|
|
18
|
+
"prefer-tokens": Rule.RuleModule;
|
|
19
|
+
"no-unknown-token": Rule.RuleModule;
|
|
20
|
+
};
|
|
21
|
+
export type RuleName = keyof typeof rules;
|
|
22
|
+
/** گروه هر قانون، برای مستندات و پیکربندیها */
|
|
23
|
+
export declare const RULE_GROUPS: Record<RuleName, "text" | "rtl" | "kashi">;
|
|
24
|
+
declare const plugin: {
|
|
25
|
+
meta: {
|
|
26
|
+
name: string;
|
|
27
|
+
version: string;
|
|
28
|
+
namespace: string;
|
|
29
|
+
};
|
|
30
|
+
rules: {
|
|
31
|
+
"no-arabic-chars": Rule.RuleModule;
|
|
32
|
+
"persian-digits": Rule.RuleModule;
|
|
33
|
+
zwnj: Rule.RuleModule;
|
|
34
|
+
"no-invalid-zwnj": Rule.RuleModule;
|
|
35
|
+
"punctuation-spacing": Rule.RuleModule;
|
|
36
|
+
"persian-punctuation": Rule.RuleModule;
|
|
37
|
+
"no-kashida": Rule.RuleModule;
|
|
38
|
+
"no-extra-diacritics": Rule.RuleModule;
|
|
39
|
+
"no-bidi-control": Rule.RuleModule;
|
|
40
|
+
"html-dir-lang": Rule.RuleModule;
|
|
41
|
+
"no-physical-style": Rule.RuleModule;
|
|
42
|
+
"tailwind-logical": Rule.RuleModule;
|
|
43
|
+
"directional-icon": Rule.RuleModule;
|
|
44
|
+
"prefer-kashi-validators": Rule.RuleModule;
|
|
45
|
+
"prefer-tokens": Rule.RuleModule;
|
|
46
|
+
"no-unknown-token": Rule.RuleModule;
|
|
47
|
+
};
|
|
48
|
+
configs: Record<"recommended" | "strict" | "react" | "kashi" | "all", Linter.Config>;
|
|
49
|
+
};
|
|
50
|
+
export declare const configs: Record<"kashi" | "recommended" | "all" | "strict" | "react", Linter.Config<import("@eslint/core", { with: { "resolution-mode": "require" } }).RulesConfig>>;
|
|
51
|
+
export default plugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { noArabicChars, persianDigits, zwnj, noInvalidZwnj, punctuationSpacing, persianPunctuation, noKashida, noExtraDiacritics, } from "./rules/text-rules.js";
|
|
3
|
+
import { noBidiControl } from "./rules/no-bidi-control.js";
|
|
4
|
+
import { htmlDirLang } from "./rules/html-dir-lang.js";
|
|
5
|
+
import { noPhysicalStyle } from "./rules/no-physical-style.js";
|
|
6
|
+
import { tailwindLogical } from "./rules/tailwind-logical.js";
|
|
7
|
+
import { directionalIcon } from "./rules/directional-icon.js";
|
|
8
|
+
import { preferKashiValidators } from "./rules/prefer-kashi-validators.js";
|
|
9
|
+
import { noUnknownToken, preferTokens } from "./rules/kashi-tokens.js";
|
|
10
|
+
const pkg = createRequire(import.meta.url)("../package.json");
|
|
11
|
+
export const rules = {
|
|
12
|
+
"no-arabic-chars": noArabicChars,
|
|
13
|
+
"persian-digits": persianDigits,
|
|
14
|
+
zwnj,
|
|
15
|
+
"no-invalid-zwnj": noInvalidZwnj,
|
|
16
|
+
"punctuation-spacing": punctuationSpacing,
|
|
17
|
+
"persian-punctuation": persianPunctuation,
|
|
18
|
+
"no-kashida": noKashida,
|
|
19
|
+
"no-extra-diacritics": noExtraDiacritics,
|
|
20
|
+
"no-bidi-control": noBidiControl,
|
|
21
|
+
"html-dir-lang": htmlDirLang,
|
|
22
|
+
"no-physical-style": noPhysicalStyle,
|
|
23
|
+
"tailwind-logical": tailwindLogical,
|
|
24
|
+
"directional-icon": directionalIcon,
|
|
25
|
+
"prefer-kashi-validators": preferKashiValidators,
|
|
26
|
+
"prefer-tokens": preferTokens,
|
|
27
|
+
"no-unknown-token": noUnknownToken,
|
|
28
|
+
};
|
|
29
|
+
/** گروه هر قانون، برای مستندات و پیکربندیها */
|
|
30
|
+
export const RULE_GROUPS = {
|
|
31
|
+
"no-arabic-chars": "text",
|
|
32
|
+
"persian-digits": "text",
|
|
33
|
+
zwnj: "text",
|
|
34
|
+
"no-invalid-zwnj": "text",
|
|
35
|
+
"punctuation-spacing": "text",
|
|
36
|
+
"persian-punctuation": "text",
|
|
37
|
+
"no-kashida": "text",
|
|
38
|
+
"no-extra-diacritics": "text",
|
|
39
|
+
"no-bidi-control": "text",
|
|
40
|
+
"html-dir-lang": "rtl",
|
|
41
|
+
"no-physical-style": "rtl",
|
|
42
|
+
"tailwind-logical": "rtl",
|
|
43
|
+
"directional-icon": "rtl",
|
|
44
|
+
"prefer-kashi-validators": "kashi",
|
|
45
|
+
"prefer-tokens": "kashi",
|
|
46
|
+
"no-unknown-token": "kashi",
|
|
47
|
+
};
|
|
48
|
+
const plugin = {
|
|
49
|
+
meta: { name: pkg.name, version: pkg.version, namespace: "kashi" },
|
|
50
|
+
rules,
|
|
51
|
+
configs: {},
|
|
52
|
+
};
|
|
53
|
+
const textRules = {
|
|
54
|
+
"kashi/no-arabic-chars": "warn",
|
|
55
|
+
"kashi/persian-digits": "warn",
|
|
56
|
+
"kashi/zwnj": "warn",
|
|
57
|
+
"kashi/no-invalid-zwnj": "warn",
|
|
58
|
+
"kashi/punctuation-spacing": "warn",
|
|
59
|
+
"kashi/persian-punctuation": "warn",
|
|
60
|
+
"kashi/no-kashida": "warn",
|
|
61
|
+
"kashi/no-extra-diacritics": "warn",
|
|
62
|
+
"kashi/no-bidi-control": "error",
|
|
63
|
+
};
|
|
64
|
+
const recommended = {
|
|
65
|
+
...textRules,
|
|
66
|
+
"kashi/html-dir-lang": "error",
|
|
67
|
+
"kashi/no-physical-style": "warn",
|
|
68
|
+
};
|
|
69
|
+
const react = {
|
|
70
|
+
...recommended,
|
|
71
|
+
"kashi/tailwind-logical": "warn",
|
|
72
|
+
"kashi/directional-icon": "warn",
|
|
73
|
+
};
|
|
74
|
+
const errors = (r) => Object.fromEntries(Object.entries(r).map(([k, v]) => [k, Array.isArray(v) ? ["error", ...v.slice(1)] : "error"]));
|
|
75
|
+
plugin.configs.recommended = { name: "kashi/recommended", plugins: { kashi: plugin }, rules: recommended };
|
|
76
|
+
plugin.configs.react = { name: "kashi/react", plugins: { kashi: plugin }, rules: react };
|
|
77
|
+
plugin.configs.strict = {
|
|
78
|
+
name: "kashi/strict",
|
|
79
|
+
plugins: { kashi: plugin },
|
|
80
|
+
rules: { ...errors(react), "kashi/no-extra-diacritics": ["error", { mode: "all" }] },
|
|
81
|
+
};
|
|
82
|
+
plugin.configs.kashi = {
|
|
83
|
+
name: "kashi/kashi",
|
|
84
|
+
plugins: { kashi: plugin },
|
|
85
|
+
rules: { "kashi/prefer-kashi-validators": "warn", "kashi/prefer-tokens": "warn", "kashi/no-unknown-token": "error" },
|
|
86
|
+
};
|
|
87
|
+
plugin.configs.all = {
|
|
88
|
+
name: "kashi/all",
|
|
89
|
+
plugins: { kashi: plugin },
|
|
90
|
+
rules: Object.fromEntries(Object.keys(rules).map((r) => [`kashi/${r}`, "error"])),
|
|
91
|
+
};
|
|
92
|
+
export const configs = plugin.configs;
|
|
93
|
+
export default plugin;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
/**
|
|
3
|
+
* نام کامپوننت آیکون جهتدار: فلش، شورون، کرت و زاویهٔ چپ/راست در کتابخانههای رایج
|
|
4
|
+
* (کاشی IconArrowLeft، lucide ChevronRight، heroicons ArrowLeftIcon، tabler IconArrowRight،
|
|
5
|
+
* MUI ArrowBack/ArrowForwardIos/NavigateNext، react-icons FaArrowLeft/MdChevronRight…).
|
|
6
|
+
* فلشهای مورب (ArrowUpRight) و دوطرفه (ArrowLeftRight) جهت خواندن ندارند.
|
|
7
|
+
*/
|
|
8
|
+
export declare const DIRECTIONAL_ICON: RegExp;
|
|
9
|
+
export declare function isDirectionalIconName(component: string): boolean;
|
|
10
|
+
export declare const directionalIcon: Rule.RuleModule;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { DOCS, metaMessages, reporter } from "../util.js";
|
|
2
|
+
const MESSAGES = {
|
|
3
|
+
unmirrored: [
|
|
4
|
+
"آیکون جهتدار «{{name}}» با جهت صفحه عوض نمیشود و در یکی از دو جهت برعکس معنا میدهد (فلش «بعدی» به عقب اشاره میکند). قرینهاش کنید: {{hint}}",
|
|
5
|
+
"Directional icon “{{name}}” does not follow the page direction, so it points the wrong way in one of them (a “next” arrow points back). Mirror it: {{hint}}",
|
|
6
|
+
],
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* نام کامپوننت آیکون جهتدار: فلش، شورون، کرت و زاویهٔ چپ/راست در کتابخانههای رایج
|
|
10
|
+
* (کاشی IconArrowLeft، lucide ChevronRight، heroicons ArrowLeftIcon، tabler IconArrowRight،
|
|
11
|
+
* MUI ArrowBack/ArrowForwardIos/NavigateNext، react-icons FaArrowLeft/MdChevronRight…).
|
|
12
|
+
* فلشهای مورب (ArrowUpRight) و دوطرفه (ArrowLeftRight) جهت خواندن ندارند.
|
|
13
|
+
*/
|
|
14
|
+
export const DIRECTIONAL_ICON = /(?:Arrow|Chevrons?|Caret|Angles?|Triangle)[A-Za-z]*?(?:Left|Right)(?:Icon|Outline[d]?|Rounded|Sharp|Solid|Filled|Fill|Bold|Light|Thin|Regular|Duotone|TwoTone|Line|Mini|Micro|Small|Large|Alt|\d+)*$|^(?:Md|Io|Fa|Bi|Ri|Hi|Tb|Lu|Pi|Fi)?(?:ArrowBack|ArrowForward|ArrowBackIos|ArrowBackIosNew|ArrowForwardIos|NavigateNext|NavigateBefore|FirstPage|LastPage)(?:Icon|Outlined|Rounded|Sharp|TwoTone)?$/;
|
|
15
|
+
const NOT_DIRECTIONAL = /(Up|Down)(Left|Right)|LeftRight|RightLeft|Chevrons?UpDown|ArrowsLeftRight/;
|
|
16
|
+
/** نام آیکون در props مثل name="arrow-left" یا icon="chevron-right" */
|
|
17
|
+
const DIRECTIONAL_NAME = /(^|[-_:])(arrow|chevrons?|caret|angles?|triangle)([-_][a-z]+)*?[-_](left|right|back|forward|next|prev|previous)$/i;
|
|
18
|
+
const NOT_DIRECTIONAL_NAME = /(up|down)[-_](left|right)$|left[-_]right|right[-_]left/i;
|
|
19
|
+
export function isDirectionalIconName(component) {
|
|
20
|
+
return DIRECTIONAL_ICON.test(component) && !NOT_DIRECTIONAL.test(component);
|
|
21
|
+
}
|
|
22
|
+
const MIRROR_PROPS = new Set(["mirror", "mirrored", "flip", "flipped", "flipRtl", "rtl", "autoMirror", "data-k-mirror", "data-mirror", "data-flip", "data-rtl-flip"]);
|
|
23
|
+
const MIRROR_CLASS = /(^|\s|:)(rtl:(-?scale-x-|rotate-180|-rotate-180|\[transform)|k-mirror|mirror|flip|rtl-flip|flip-rtl|rtl-mirror|icon-rtl)\b/;
|
|
24
|
+
function jsxName(n) {
|
|
25
|
+
if (!n)
|
|
26
|
+
return "";
|
|
27
|
+
if (n.type === "JSXIdentifier")
|
|
28
|
+
return n.name;
|
|
29
|
+
if (n.type === "JSXMemberExpression")
|
|
30
|
+
return jsxName(n.property);
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
export const directionalIcon = {
|
|
34
|
+
meta: {
|
|
35
|
+
type: "suggestion",
|
|
36
|
+
docs: {
|
|
37
|
+
description: "Require arrow and chevron icons to be mirrored in right-to-left layouts",
|
|
38
|
+
descriptionFa: "آیکون فلش/شورون چپ و راست باید در صفحهٔ راستبهچپ قرینه شود (mirror، data-k-mirror یا کلاس rtl:-scale-x-100).",
|
|
39
|
+
recommended: true,
|
|
40
|
+
url: `${DOCS}#directional-icon`,
|
|
41
|
+
},
|
|
42
|
+
schema: [
|
|
43
|
+
{
|
|
44
|
+
type: "object",
|
|
45
|
+
properties: {
|
|
46
|
+
components: { type: "array", items: { type: "string" }, description: "نام کامپوننتهای جهتدار دیگر (regex)" },
|
|
47
|
+
ignore: { type: "array", items: { type: "string" }, description: "نام کامپوننتهایی که خودشان قرینه میشوند (regex)" },
|
|
48
|
+
mirrorProps: { type: "array", items: { type: "string" } },
|
|
49
|
+
},
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
messages: metaMessages(MESSAGES),
|
|
54
|
+
},
|
|
55
|
+
create(context) {
|
|
56
|
+
const opts = (context.options[0] ?? {});
|
|
57
|
+
const extra = (opts.components ?? []).map((s) => new RegExp(s));
|
|
58
|
+
const ignore = (opts.ignore ?? []).map((s) => new RegExp(s));
|
|
59
|
+
const mirrorProps = new Set([...MIRROR_PROPS, ...(opts.mirrorProps ?? [])]);
|
|
60
|
+
const report = reporter(context, MESSAGES);
|
|
61
|
+
const source = context.sourceCode;
|
|
62
|
+
return {
|
|
63
|
+
JSXOpeningElement(node) {
|
|
64
|
+
const name = jsxName(node.name);
|
|
65
|
+
if (!name || ignore.some((r) => r.test(name)))
|
|
66
|
+
return;
|
|
67
|
+
let directional = isDirectionalIconName(name) || extra.some((r) => r.test(name));
|
|
68
|
+
let label = name;
|
|
69
|
+
if (!directional) {
|
|
70
|
+
// <Icon name="arrow-left" />
|
|
71
|
+
const named = node.attributes.find((a) => a.type === "JSXAttribute" && ["name", "icon", "iconName", "type"].includes(a.name?.name) && a.value?.type === "Literal");
|
|
72
|
+
const v = named ? String(named.value.value) : "";
|
|
73
|
+
if (/^[A-Z]/.test(name) && DIRECTIONAL_NAME.test(v) && !NOT_DIRECTIONAL_NAME.test(v)) {
|
|
74
|
+
directional = true;
|
|
75
|
+
label = `${name} ${named.name.name}="${v}"`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (!directional)
|
|
79
|
+
return;
|
|
80
|
+
for (const a of node.attributes) {
|
|
81
|
+
if (a.type === "JSXSpreadAttribute")
|
|
82
|
+
return; // ممکن است mirror از props بیاید
|
|
83
|
+
const an = a.name?.type === "JSXIdentifier" ? a.name.name : "";
|
|
84
|
+
if (mirrorProps.has(an))
|
|
85
|
+
return;
|
|
86
|
+
if (/^(className|class|style|sx|css)$/.test(an) && a.value) {
|
|
87
|
+
const t = source.getText(a.value);
|
|
88
|
+
if (MIRROR_CLASS.test(t.replace(/["'`{}]/g, " ")) || /scaleX\(\s*-1|rotate\(\s*180|rotateY\(\s*180/.test(t))
|
|
89
|
+
return;
|
|
90
|
+
if (an === "className" && a.value.type === "JSXExpressionContainer" && a.value.expression.type !== "Literal" && a.value.expression.type !== "TemplateLiteral")
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// داخل عنصری با dir ثابت (مثلاً dir="ltr") جهت عوض نمیشود
|
|
95
|
+
for (let p = node.parent?.parent; p; p = p.parent) {
|
|
96
|
+
if (p.type === "JSXElement" && p.openingElement.attributes.some((a) => a.name?.name === "dir"))
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
report({ node: node.name, messageId: "unmirrored", data: { name: label, hint: name.startsWith("Icon") ? "mirror یا className=\"rtl:-scale-x-100\"" : "className=\"rtl:-scale-x-100\"" } });
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { DOCS, metaMessages, reporter, settings } from "../util.js";
|
|
2
|
+
const MESSAGES = {
|
|
3
|
+
missingDir: [
|
|
4
|
+
"<html> بدون dir؛ مرورگر صفحه را چپبهراست میچیند. dir=\"{{dir}}\" بگذارید (نه direction در CSS)",
|
|
5
|
+
"<html> has no dir; the browser lays the page out left-to-right. Add dir=\"{{dir}}\" (not CSS direction)",
|
|
6
|
+
],
|
|
7
|
+
missingLang: [
|
|
8
|
+
"<html> بدون lang؛ صفحهخوان، فونت، خطشکنی و ترجمهٔ خودکار زبان را نمیدانند. lang=\"{{lang}}\" بگذارید",
|
|
9
|
+
"<html> has no lang; screen readers, fonts, hyphenation and translation can't tell the language. Add lang=\"{{lang}}\"",
|
|
10
|
+
],
|
|
11
|
+
mismatch: [
|
|
12
|
+
"lang=\"{{lang}}\" راستبهچپ است ولی dir=\"{{dir}}\"",
|
|
13
|
+
"lang=\"{{lang}}\" is a right-to-left language but dir=\"{{dir}}\"",
|
|
14
|
+
],
|
|
15
|
+
};
|
|
16
|
+
const RTL_LANGS = /^(fa|ar|he|iw|ur|ps|ckb|ku-arab|yi|dv|sd|ug|syr|prs|glk|mzn|lrc|bqi)(-|$)/i;
|
|
17
|
+
export const htmlDirLang = {
|
|
18
|
+
meta: {
|
|
19
|
+
type: "problem",
|
|
20
|
+
fixable: "code",
|
|
21
|
+
docs: {
|
|
22
|
+
description: "Require dir and lang on the root <html> element (Next.js app/layout, Remix root…)",
|
|
23
|
+
descriptionFa: "ریشهٔ <html> (مثل app/layout.tsx در Next.js) باید dir و lang داشته باشد؛ و dir با زبان راستبهچپ جور باشد.",
|
|
24
|
+
recommended: true,
|
|
25
|
+
url: `${DOCS}#html-dir-lang`,
|
|
26
|
+
},
|
|
27
|
+
schema: [
|
|
28
|
+
{
|
|
29
|
+
type: "object",
|
|
30
|
+
properties: { lang: { type: "string" }, dir: { enum: ["rtl", "ltr"] } },
|
|
31
|
+
additionalProperties: false,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
messages: metaMessages(MESSAGES),
|
|
35
|
+
},
|
|
36
|
+
create(context) {
|
|
37
|
+
const opts = (context.options[0] ?? {});
|
|
38
|
+
const dir = opts.dir ?? settings(context).direction ?? "rtl";
|
|
39
|
+
const lang = opts.lang ?? (dir === "rtl" ? "fa" : "en");
|
|
40
|
+
const report = reporter(context, MESSAGES);
|
|
41
|
+
return {
|
|
42
|
+
JSXOpeningElement(node) {
|
|
43
|
+
if (node.name.type !== "JSXIdentifier" || node.name.name !== "html")
|
|
44
|
+
return;
|
|
45
|
+
if (node.attributes.some((a) => a.type === "JSXSpreadAttribute"))
|
|
46
|
+
return;
|
|
47
|
+
const attr = (n) => node.attributes.find((a) => a.name?.name === n);
|
|
48
|
+
const dirAttr = attr("dir"), langAttr = attr("lang");
|
|
49
|
+
const insertAt = node.name.range[1];
|
|
50
|
+
// هر دو در یک اصلاح، چون هر دو همانجا درج میشوند
|
|
51
|
+
const both = `${langAttr ? "" : ` lang="${lang}"`}${dirAttr ? "" : ` dir="${dir}"`}`;
|
|
52
|
+
const fix = (f) => f.insertTextAfterRange([insertAt, insertAt], both);
|
|
53
|
+
if (!langAttr)
|
|
54
|
+
report({ node: node.name, messageId: "missingLang", data: { lang }, fix });
|
|
55
|
+
if (!dirAttr)
|
|
56
|
+
report({ node: node.name, messageId: "missingDir", data: { dir }, fix: langAttr ? fix : null });
|
|
57
|
+
const lv = langAttr?.value?.type === "Literal" ? String(langAttr.value.value) : null;
|
|
58
|
+
const dv = dirAttr?.value?.type === "Literal" ? String(dirAttr.value.value) : null;
|
|
59
|
+
if (lv && dv && RTL_LANGS.test(lv) && dv === "ltr") {
|
|
60
|
+
report({ node: dirAttr, messageId: "mismatch", data: { lang: lv, dir: dv }, fix: (f) => f.replaceText(dirAttr.value, '"rtl"') });
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
/** همهٔ رشتهها و template literalها (شامل className="bg-[var(--k-…)]" و style) */
|
|
3
|
+
export declare const noUnknownToken: Rule.RuleModule;
|
|
4
|
+
/** مقدار ثابت در style درونخطی که توکن کاشی برایش هست */
|
|
5
|
+
export declare const preferTokens: Rule.RuleModule;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { DOCS, metaMessages, reporter } from "../util.js";
|
|
2
|
+
import { closestToken, isColorProperty, isKnownToken, sizeKindOf, tokenForSize, tokensForColor } from "../tokens.js";
|
|
3
|
+
const UNKNOWN = {
|
|
4
|
+
unknown: [
|
|
5
|
+
"توکن «{{name}}» در کاشی نیست؛ مقدار var() خالی میماند و خاصیت بیاثر میشود.{{hint}}",
|
|
6
|
+
"“{{name}}” is not a Kashi token; the var() resolves to nothing and the declaration is dropped.{{hint}}",
|
|
7
|
+
],
|
|
8
|
+
replace: ["{{name}} را {{closest}} کن", "Replace {{name}} with {{closest}}"],
|
|
9
|
+
};
|
|
10
|
+
/** همهٔ رشتهها و template literalها (شامل className="bg-[var(--k-…)]" و style) */
|
|
11
|
+
export const noUnknownToken = {
|
|
12
|
+
meta: {
|
|
13
|
+
type: "problem",
|
|
14
|
+
hasSuggestions: true,
|
|
15
|
+
docs: {
|
|
16
|
+
description: "Disallow var(--k-*) references to tokens that don't exist in Kashi (typos, removed tokens)",
|
|
17
|
+
descriptionFa: "var(--k-…) با نامی که در کاشی نیست (غلط تایپی یا توکن حذفشده)؛ فهرست از خود توکنها و CSS کاشی ساخته میشود.",
|
|
18
|
+
recommended: false,
|
|
19
|
+
url: `${DOCS}#no-unknown-token`,
|
|
20
|
+
},
|
|
21
|
+
schema: [{ type: "object", properties: { allow: { type: "array", items: { type: "string" } } }, additionalProperties: false }],
|
|
22
|
+
messages: metaMessages(UNKNOWN),
|
|
23
|
+
},
|
|
24
|
+
create(context) {
|
|
25
|
+
const allow = (context.options[0]?.allow ?? []).map((s) => new RegExp(s));
|
|
26
|
+
const report = reporter(context, UNKNOWN);
|
|
27
|
+
const text = context.sourceCode.text;
|
|
28
|
+
const check = (start, raw) => {
|
|
29
|
+
for (const m of raw.matchAll(/var\(\s*(--k-[\w-]+)/g)) {
|
|
30
|
+
const name = m[1];
|
|
31
|
+
// نام پویا: `var(--k-chart-${i})` یا "var(--k-text-" + tone + ")"
|
|
32
|
+
const after = raw.slice(m.index + m[0].length);
|
|
33
|
+
if (name.endsWith("-") || after.startsWith("${") || /^["'`]\s*\+/.test(after))
|
|
34
|
+
continue;
|
|
35
|
+
if (isKnownToken(name) || allow.some((r) => r.test(name)))
|
|
36
|
+
continue;
|
|
37
|
+
// متغیری که همین فایل تعریف میکند (مثلاً style={{"--k-my-x": …}}) مال خود کاربر است
|
|
38
|
+
if (new RegExp(`["'\`]?${name.replace(/[-]/g, "\\-")}["'\`]?\\s*:`).test(text))
|
|
39
|
+
continue;
|
|
40
|
+
const at = start + m.index + m[0].indexOf(name);
|
|
41
|
+
const closest = closestToken(name);
|
|
42
|
+
const range = [at, at + name.length];
|
|
43
|
+
report({
|
|
44
|
+
loc: { start: context.sourceCode.getLocFromIndex(range[0]), end: context.sourceCode.getLocFromIndex(range[1]) },
|
|
45
|
+
messageId: "unknown",
|
|
46
|
+
data: { name, hint: closest ? ` ${closest}?` : "" },
|
|
47
|
+
suggest: closest
|
|
48
|
+
? [{ messageId: "replace", data: { name, closest, message: `${name} → ${closest}` }, fix: (f) => f.replaceTextRange(range, closest) }]
|
|
49
|
+
: [],
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
Literal(node) {
|
|
55
|
+
if (typeof node.value === "string" && node.value.includes("--k-"))
|
|
56
|
+
check(node.range[0], text.slice(node.range[0], node.range[1]));
|
|
57
|
+
},
|
|
58
|
+
TemplateElement(node) {
|
|
59
|
+
const raw = text.slice(node.range[0], node.range[1]);
|
|
60
|
+
if (raw.includes("--k-"))
|
|
61
|
+
check(node.range[0], raw);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
const PREFER = {
|
|
67
|
+
color: [
|
|
68
|
+
"رنگ {{found}} همان توکن {{tokens}} کاشی است؛ با var() از توکن استفاده کنید تا با تم تیره و رنگ برند عوض شود",
|
|
69
|
+
"{{found}} is Kashi's {{tokens}}; use the token via var() so it follows dark mode and the brand color",
|
|
70
|
+
],
|
|
71
|
+
size: [
|
|
72
|
+
"{{found}} همان توکن {{tokens}} کاشی است؛ var({{tokens}}) بنویسید تا با تراکم و گردی تم هماهنگ بماند",
|
|
73
|
+
"{{found}} equals Kashi's {{tokens}}; use var({{tokens}}) so it follows the theme's density and radius",
|
|
74
|
+
],
|
|
75
|
+
use: ["جایگزینی با var({{token}})", "Replace with var({{token}})"],
|
|
76
|
+
};
|
|
77
|
+
/** مقدار ثابت در style درونخطی که توکن کاشی برایش هست */
|
|
78
|
+
export const preferTokens = {
|
|
79
|
+
meta: {
|
|
80
|
+
type: "suggestion",
|
|
81
|
+
hasSuggestions: true,
|
|
82
|
+
docs: {
|
|
83
|
+
description: "Prefer Kashi tokens (var(--k-*)) over hard-coded colors and sizes that equal a token, in inline styles",
|
|
84
|
+
descriptionFa: "رنگ hex یا اندازهٔ ثابت در style درونخطی که دقیقاً یک توکن کاشی است ← var(--k-…)، تا با تم تیره، برند و تراکم عوض شود.",
|
|
85
|
+
recommended: false,
|
|
86
|
+
url: `${DOCS}#prefer-tokens`,
|
|
87
|
+
},
|
|
88
|
+
schema: [],
|
|
89
|
+
messages: metaMessages(PREFER),
|
|
90
|
+
},
|
|
91
|
+
create(context) {
|
|
92
|
+
const report = reporter(context, PREFER);
|
|
93
|
+
return {
|
|
94
|
+
"JSXAttribute[name.name='style'] > JSXExpressionContainer > ObjectExpression > Property"(p) {
|
|
95
|
+
if (p.computed || p.value.type !== "Literal" || typeof p.value.value !== "string")
|
|
96
|
+
return;
|
|
97
|
+
const key = p.key.type === "Identifier" ? p.key.name : String(p.key.value);
|
|
98
|
+
const v = p.value.value.trim();
|
|
99
|
+
const raw = context.sourceCode.getText(p.value);
|
|
100
|
+
const q = raw[0];
|
|
101
|
+
if (isColorProperty(key)) {
|
|
102
|
+
for (const m of v.matchAll(/#[0-9a-fA-F]{6}\b|#[0-9a-fA-F]{3}\b/g)) {
|
|
103
|
+
const tokens = tokensForColor(m[0]);
|
|
104
|
+
if (!tokens.length)
|
|
105
|
+
continue;
|
|
106
|
+
report({
|
|
107
|
+
node: p.value,
|
|
108
|
+
messageId: "color",
|
|
109
|
+
data: { found: m[0], tokens: tokens.join("، ") },
|
|
110
|
+
suggest: tokens.map((t) => ({ messageId: "use", data: { token: t, message: `var(${t})` }, fix: (f) => f.replaceText(p.value, q + v.replace(m[0], `var(${t})`) + q) })),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const kind = sizeKindOf(key);
|
|
116
|
+
const token = kind && tokenForSize(kind, v);
|
|
117
|
+
if (token) {
|
|
118
|
+
report({
|
|
119
|
+
node: p.value,
|
|
120
|
+
messageId: "size",
|
|
121
|
+
data: { found: v, tokens: token },
|
|
122
|
+
suggest: [{ messageId: "use", data: { token, message: `var(${token})` }, fix: (f) => f.replaceText(p.value, `${q}var(${token})${q}`) }],
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { findBidiControls } from "@kashi-ui/persian";
|
|
2
|
+
import { DOCS, metaMessages, reporter } from "../util.js";
|
|
3
|
+
const MESSAGES = {
|
|
4
|
+
control: [
|
|
5
|
+
"نویسهٔ نامرئی کنترل جهت {{name}} در کد؛ کد را جور دیگری نشان میدهد که اجرا میشود (Trojan Source). اگر عمدی است escape بنویسید: {{escape}}",
|
|
6
|
+
"Invisible bidi control {{name}} in source; it can make code display differently from how it runs (Trojan Source). If intended, write it escaped: {{escape}}",
|
|
7
|
+
],
|
|
8
|
+
uselessMark: [
|
|
9
|
+
"نشانهٔ نامرئی {{name}} اینجا اثری ندارد (بین دو حرف یا تکراری)؛ معمولاً از کپی جا مانده است",
|
|
10
|
+
"Invisible {{name}} has no effect here (between two letters or repeated); usually left over from copy-paste",
|
|
11
|
+
],
|
|
12
|
+
markOutsideText: ["نشانهٔ نامرئی {{name}} بیرون از متن (در کد یا توضیح)", "Invisible {{name}} outside of text (in code or a comment)"],
|
|
13
|
+
remove: ["حذف {{name}}", "Remove {{name}}"],
|
|
14
|
+
};
|
|
15
|
+
const strong = (c) => !!c && /[A-Za-zؠ-يٮ-ۓەۺ-ۿ-]/.test(c);
|
|
16
|
+
/** آیا index داخل رشته یا متنی است که escape در آن معنا دارد */
|
|
17
|
+
function textContainer(source, index) {
|
|
18
|
+
const node = source.getNodeByRangeIndex(index);
|
|
19
|
+
if (!node)
|
|
20
|
+
return null;
|
|
21
|
+
// /[\u200E\u200F]/ با نویسهٔ خام: دادهٔ تطبیق است؛ escape همان معنا را دارد
|
|
22
|
+
if (node.type === "Literal" && node.regex)
|
|
23
|
+
return { node, escape: "js", regex: true };
|
|
24
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
25
|
+
const inJsxAttr = node.parent?.type === "JSXAttribute";
|
|
26
|
+
return { node, escape: inJsxAttr ? "html" : "js" };
|
|
27
|
+
}
|
|
28
|
+
if (node.type === "TemplateElement") {
|
|
29
|
+
const tagged = node.parent?.parent?.type === "TaggedTemplateExpression";
|
|
30
|
+
return { node, escape: tagged ? null : "js" };
|
|
31
|
+
}
|
|
32
|
+
if (node.type === "JSXText")
|
|
33
|
+
return { node, escape: "html" };
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
export const noBidiControl = {
|
|
37
|
+
meta: {
|
|
38
|
+
type: "problem",
|
|
39
|
+
fixable: "code",
|
|
40
|
+
hasSuggestions: true,
|
|
41
|
+
docs: {
|
|
42
|
+
description: "Disallow raw invisible bidi control characters (Trojan Source) and useless LRM/RLM marks",
|
|
43
|
+
descriptionFa: "نویسههای نامرئی جهتدهی (RLO، LRI، PDF…) در کد خطرناکاند؛ در رشتهها به escape تبدیل میشوند. LRM/RLM بیاثر یا تکراری حذف میشود.",
|
|
44
|
+
recommended: true,
|
|
45
|
+
url: `${DOCS}#no-bidi-control`,
|
|
46
|
+
},
|
|
47
|
+
schema: [],
|
|
48
|
+
messages: metaMessages(MESSAGES),
|
|
49
|
+
},
|
|
50
|
+
create(context) {
|
|
51
|
+
const report = reporter(context, MESSAGES);
|
|
52
|
+
const source = context.sourceCode;
|
|
53
|
+
return {
|
|
54
|
+
Program() {
|
|
55
|
+
const text = source.text;
|
|
56
|
+
const controls = findBidiControls(text);
|
|
57
|
+
if (!controls.length)
|
|
58
|
+
return;
|
|
59
|
+
const comments = source.getAllComments();
|
|
60
|
+
const inComment = (i) => comments.some((c) => c.range[0] <= i && i < c.range[1]);
|
|
61
|
+
for (const c of controls) {
|
|
62
|
+
const loc = { start: source.getLocFromIndex(c.index), end: source.getLocFromIndex(c.index + 1) };
|
|
63
|
+
const range = [c.index, c.index + 1];
|
|
64
|
+
const comment = inComment(c.index);
|
|
65
|
+
const container = comment ? null : textContainer(source, c.index);
|
|
66
|
+
const data = { name: c.name, escape: c.escape };
|
|
67
|
+
if (c.kind !== "mark") {
|
|
68
|
+
const esc = container?.escape === "js" ? c.escape : container?.escape === "html" ? `&#x${c.char.charCodeAt(0).toString(16).toUpperCase()};` : null;
|
|
69
|
+
report({
|
|
70
|
+
loc,
|
|
71
|
+
messageId: "control",
|
|
72
|
+
data: { ...data, escape: esc ?? c.escape },
|
|
73
|
+
...(esc
|
|
74
|
+
? { fix: (f) => f.replaceTextRange(range, esc) }
|
|
75
|
+
: { suggest: [{ messageId: "remove", data: { ...data, message: `Remove ${c.name}` }, fix: (f) => f.removeRange(range) }] }),
|
|
76
|
+
});
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (container?.regex)
|
|
80
|
+
continue;
|
|
81
|
+
if (!container && !comment) {
|
|
82
|
+
report({ loc, messageId: "markOutsideText", data, fix: (f) => f.removeRange(range) });
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const prev = text[c.index - 1], next = text[c.index + 1];
|
|
86
|
+
const duplicate = prev === c.char;
|
|
87
|
+
const lineEnd = comment && (next === undefined || next === "\n" || next === "\r");
|
|
88
|
+
if (duplicate || lineEnd || (strong(prev) && strong(next))) {
|
|
89
|
+
report({ loc, messageId: "uselessMark", data, fix: (f) => f.removeRange(range) });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
};
|