-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (35 loc) · 1015 Bytes
/
index.js
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
const evilApostropheRegExp = /(?:(?<=\s)|[^\d\W])+['‘](?:\w['‘])?\w*/g;
const evilApostrophe = /['‘]/g;
const goodApostrophe = '’';
function reporter(context) {
const { Syntax, RuleError, report, fixer, getSource } = context;
return {
[Syntax.Str](node) {
return new Promise(resolve => {
const text = getSource(node);
let match;
// eslint-disable-next-line no-cond-assign
while ((match = evilApostropheRegExp.exec(text))) {
const index = match.index;
const matched = match[0];
const replacement = matched.replace(evilApostrophe, goodApostrophe);
const range = [index, index + matched.length];
const fix = fixer.replaceTextRange(range, replacement);
const message = `Incorrect usage of an apostrophe: “${matched}”, use “${replacement}” instead`;
report(
node,
new RuleError(message, {
index,
fix,
})
);
}
resolve();
});
},
};
}
module.exports = {
linter: reporter,
fixer: reporter,
};