textlint-rule-apostrophe
Version:
Textlint rule to check correct apostrophe usage
32 lines • 1.22 kB
JavaScript
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;
while ((match = evilApostropheRegExp.exec(text))) {
const index = match.index;
const matched = match[0];
const replacement = matched.replaceAll(evilApostrophe, goodApostrophe);
const fix = fixer.replaceTextRange([index, index + matched.length], replacement);
const message = `Incorrect usage of an apostrophe: “${matched}”, use “${replacement}” instead`;
report(node, new RuleError(message, {
index,
fix,
}));
}
resolve();
});
},
};
}
const rule = {
linter: reporter,
fixer: reporter,
};
export default rule;
//# sourceMappingURL=index.js.map