Skip to content

Commit

Permalink
rule: only fathatan on alef
Browse files Browse the repository at this point in the history
  • Loading branch information
aborazmeh committed Jul 11, 2024
1 parent d52c767 commit 8718e35
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Shadda can't be combined with Sukun

لا يمكن للتنوين أن يأتي في وسًٍط وسٍط وسٌط الكلمة

### Only Fathatan on Alef

تجريباٌ تجريباٍ

## Usage

Via `.textlintrc.json`(Recommended)
Expand All @@ -75,6 +79,7 @@ These are default options, you can change them in your .textlintrc file
"no_shadda_with_madda": true,
"no_shadda_with_sukun": true,
"no_duplicated_diacritics": true,
"only_fathatan_on_alef": true,
}
}
}
Expand Down Expand Up @@ -103,7 +108,6 @@ Test textlint rule by [textlint-tester](https://github.com/textlint/textlint-tes
## TODO

- Normalize diacritics forms before some regexes like for duplicated diacritics, and Tanween related rules
- No diacritics or Tanween on Alef without Hamza except Tanween Al-Nasb
- No consecutive five Harakat
- No consecutive three Sokoon
- Twneen on Alef or the letter before
Expand Down
32 changes: 29 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ export interface Options {
no_shadda_with_madda?: boolean;
no_shadda_with_sukun?: boolean;
no_duplicated_diacritics?: boolean;
no_middle_tanween?: boolean
no_middle_tanween?: boolean;
only_fathatan_on_alef?: boolean;
}

const alefWithFathatan = "\uFD3C\uFD3D";
const fathatan = "\u064B\u08E7\u08F0\uFE70\uFE71";
const dammatan = "\u064C\u08E8\u08F1\uFC5E\uFE72";
const kasratan = "\u064D\u08E9\u08F2\uFC5F\uFE74";

const regex = {
diacritics: "[\u064B-\u0653]",
tanween: "[\u064B-\u064D]",
tanween: `[${alefWithFathatan}${fathatan}${dammatan}${kasratan}]`,
shadda: "[\u0651\u0AFB\uFC5E-\uFC63\uFCF2-\uFCF4\uFE7C\uFE7D\u11237]",
madda: "\u0653",
alefMadda: "[\u0622\uFE81\uFE82\uFEF5\uFEF6]",
sukun: "[\u0652\u07B0\u082C\u08D0\u0AFA\uFE7E\uFE7F\u1123E]"
sukun: "[\u0652\u07B0\u082C\u08D0\u0AFA\uFE7E\uFE7F\u1123E]",
alef: "[\u0627\uFE8D\uFE8E\u1EE00]"
};

function normalize(node: TxtStrNode, text: string, context: Readonly<TextlintRuleContext>) {
Expand Down Expand Up @@ -429,6 +436,20 @@ function noMiddleTanween(node: TxtStrNode, text: string, context: Readonly<Textl
}
}

function onlyFathatanOnAlef(node: TxtStrNode, text: string, context: Readonly<TextlintRuleContext>) {
const { report, locator, RuleError } = context;

const matches = text.matchAll(new RegExp(`${regex.alef}[${dammatan}${kasratan}]`, "ug"));
for (const match of matches) {
const index = match.index ?? 0;
const matchRange = [index, index + match[0].length] as const;
const ruleError = new RuleError("Found Tanween on Alef, only Fathatan can be on Alef.", {
padding: locator.range(matchRange)
});
report(node, ruleError);
}
}

const report: TextlintRuleModule<Options> = (context, options = {}) => {
const { getSource, Syntax } = context;
return {
Expand All @@ -439,6 +460,7 @@ const report: TextlintRuleModule<Options> = (context, options = {}) => {
const shaddaWithSukunOpt = options.no_shadda_with_sukun ?? true;
const duplicatedDiacriticsOpt = options.no_duplicated_diacritics ?? true;
const noMiddleTanweenOpt = options.no_middle_tanween ?? true;
const onlyFathatanOnAlefOpt = options.only_fathatan_on_alef ?? true;

const text = getSource(node); // Get text

Expand All @@ -463,6 +485,10 @@ const report: TextlintRuleModule<Options> = (context, options = {}) => {
if (noMiddleTanweenOpt) {
noMiddleTanween(node, text, context);
}

if (onlyFathatanOnAlefOpt) {
onlyFathatanOnAlef(node, text, context)
}
}
};
};
Expand Down
13 changes: 13 additions & 0 deletions test/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ tester.run("rule", rule, {
range: [5, 6]
}
]
},
{
text: "تجريباٌ تجريباٍ",
errors: [
{
message: "Found Tanween on Alef, only Fathatan can be on Alef.",
range: [5, 7]
},
{
message: "Found Tanween on Alef, only Fathatan can be on Alef.",
range: [13, 15]
}
]
}
]
});

0 comments on commit 8718e35

Please sign in to comment.