-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
58 lines (51 loc) · 2.65 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* ——————————————————————————————————————————————————————————————————————————— *
* © Julian Cataldo — https://www.juliancataldo.com. *
* See LICENSE in the project root. *
/* —————————————————————————————————————————————————————————————————————————— */
import abbrs from 'case-police/dict/abbreviates.json' assert { type: 'json' };
import brands from 'case-police/dict/brands.json' assert { type: 'json' };
import general from 'case-police/dict/general.json' assert { type: 'json' };
import products from 'case-police/dict/products.json' assert { type: 'json' };
import softwares from 'case-police/dict/softwares.json' assert { type: 'json' };
/* ·········································································· */
import type { Plugin } from 'unified';
import { search } from 'nlcst-search';
import type { Root } from 'nlcst-search';
import { toString } from 'nlcst-to-string';
/* —————————————————————————————————————————————————————————————————————————— */
const url = 'https://github.com/JulianCataldo/retext-case-police';
export type Dict = Record<string, string>;
const casePoliceDicts: Dict = {
...abbrs,
...brands,
...general,
...products,
...softwares,
};
const casePoliceKeys = Object.entries(casePoliceDicts).map(([token]) => token);
export interface Settings {
/** List of words to ignore */
ignore?: string[];
}
const retextCasePolice: Plugin<[Settings] | [], Root> =
(settings = {}) =>
(tree, file) => {
const ignore = settings.ignore ?? [];
search(tree, casePoliceKeys, (nodes) => {
const actual = toString(nodes);
const actualLowerCased = actual.toLowerCase();
const expected = casePoliceDicts[actualLowerCased];
if (!ignore.includes(expected) && actual !== expected) {
const msg = file.message(
`Expected casing of \`${expected}\` instead of \`${actual}\``,
nodes[0],
`retext-case-police:${expected}`,
);
msg.ruleId = 'retext-case-police';
msg.actual = actual;
msg.expected = [expected];
msg.url = url;
}
});
};
export default retextCasePolice;