Skip to content

Commit

Permalink
add rule no Shadda with Madda
Browse files Browse the repository at this point in the history
  • Loading branch information
aborazmeh committed Jul 9, 2024
1 parent f37d6c7 commit 237e996
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Only diacritics will be removed and there likely to be an extra space

أهلا وسهلا بكم

### No Shadda With Madda

Shadda can't be combined with Madda

الآّن والآّن والآّن

## Usage

Via `.textlintrc.json`(Recommended)
Expand All @@ -34,7 +40,8 @@ Via `.textlintrc.json`(Recommended)
{
"rules": {
"arabic-diacritics": {
"remove_loose_diacritics": true
"remove_loose_diacritics": true,
"no_shadda_with_madda": true,
}
}
}
Expand Down Expand Up @@ -67,7 +74,6 @@ Test textlint rule by [textlint-tester](https://github.com/textlint/textlint-tes
- No consecutive five Harakat
- No consecutive three Sokoon
- Twneen on Alef or the letter before
- No Madda with Shadda
- No Tanween *and* Haraka on the same letter
- No Haraka *and* Sukun on the same letter
- No Sukun on the first letter of the word
Expand Down
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { TextlintRuleContext, TextlintRuleModule } from "@textlint/types";

export interface Options {
remove_loose_diacritics?: boolean;
no_shadda_with_madda?: boolean;
}

function noLooseDiacritics(
Expand Down Expand Up @@ -30,14 +31,36 @@ function noLooseDiacritics(
}
}

function noShaddaWithMadda(node: TxtStrNode, text: string, context: Readonly<TextlintRuleContext>) {
const { report, locator, RuleError } = context;
const shadda = "[\u0651\u0AFB\uFC5E-\uFC63\uFCF2-\uFCF4\uFE7C\uFE7D\u11237]";
const alefMadda = "[\u0622\uFE81\uFE82\uFEF5\uFEF6]";
const madda = "\u0653";

const matches = text.matchAll(new RegExp(`(${alefMadda}|${madda})${shadda}|${shadda}${madda}`, "g"));
for (const match of matches) {
const index = match.index ?? 0;
const matchRange = [index, index + match[0].length] as const;
const ruleError = new RuleError("Found Shadda combined with Madda.", {
padding: locator.range(matchRange)
});
report(node, ruleError);
}
}

const report: TextlintRuleModule<Options> = (context, options = {}) => {
const { getSource, Syntax } = context;
return {
[Syntax.Str](node) {
const removeLooseDiacritics = options.remove_loose_diacritics ?? true;
const shaddaWithMaddaOpt = options.no_shadda_with_madda ?? true;

const text = getSource(node); // Get text
noLooseDiacritics(node, text, context, removeLooseDiacritics);

if (shaddaWithMaddaOpt) {
noShaddaWithMadda(node, text, context);
}
}
};
};
Expand Down
22 changes: 21 additions & 1 deletion test/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import rule from "../src/index";
const tester = new TextLintTester();
// ruleName, rule, { valid, invalid }
tester.run("rule", rule, {
valid: ["أهلاً وسهلاً"],
valid: ["أهلاً وسهلاً", "الآن"],
invalid: [
{
text: "أهلا ً وسهلا ً بكم.",
Expand Down Expand Up @@ -39,6 +39,26 @@ tester.run("rule", rule, {
range: [12, 14]
}
]
},
{
text: "الآّن والآّن والآّن",
options: {
no_shadda_with_madda: true
},
errors: [
{
message: "Found Shadda combined with Madda.",
range: [2, 4]
},
{
message: "Found Shadda combined with Madda.",
range: [10, 12]
},
{
message: "Found Shadda combined with Madda.",
range: [18, 20]
}
]
}
]
});

0 comments on commit 237e996

Please sign in to comment.