Skip to content

Commit

Permalink
fix: Ignore when match is equals to replacement (#19)
Browse files Browse the repository at this point in the history
Allow checking that a word has the correct capitalization.

Closes #16
  • Loading branch information
ggrossetie authored Jan 11, 2021
1 parent 1b0f19f commit 70b545d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ You can configure the rule in your `.textlintrc`:
// Extra words
"words": [
"etc.",
"you can"
"you can",
// Use an array to configure a replacement
["blacklist", "denylist"],
// Ensure that a word has the correct capitalization
["asciidoc", "AsciiDoc"]
],
// Excluded words
"exclude": [
Expand Down
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ function reporter(context, options = {}) {
const [matched, matchedWord] = match;

if (alternative) {
const replacement = matched.replace(
matchedWord,
cloneCase(alternative, matchedWord)
);
const range = [index, index + matched.length];
const fix = fixer.replaceTextRange(range, replacement);
const message = `Avoid using “${word}”, use “${alternative}” instead`;
report(node, new RuleError(message, { index, fix }));
if (alternative !== matchedWord) {
const replacement = matched.replace(
matchedWord,
cloneCase(alternative, matchedWord)
);
const range = [index, index + matched.length];
const fix = fixer.replaceTextRange(range, replacement);
const message = `Avoid using “${matchedWord}”, use “${alternative}” instead`;
report(node, new RuleError(message, { index, fix }));
}
} else {
const message = `Avoid using “${matched.trim()}”`;
report(node, new RuleError(message, { index }));
Expand Down
36 changes: 35 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,40 @@ describe('filterDict', () => {
});
});

tester.run(
'textlint-rule-stop-words',
{
rules: [
{
ruleId: 'stop-words',
rule,
options: {
words: [['Asciidoctor', 'Asciidoctor']],
},
},
],
},
{
valid: [
{
text: 'Asciidoctor is great',
},
],
invalid: [
{
// The capitalization is incorrect
text: 'AsciiDoctor is a fast text processor',
output: 'Asciidoctor is a fast text processor',
errors: [
{
message: 'Avoid using “AsciiDoctor”, use “Asciidoctor” instead',
},
],
},
],
}
);

tester.run('textlint-rule-stop-words', rule, {
valid: [
{
Expand Down Expand Up @@ -263,7 +297,7 @@ tester.run('textlint-rule-stop-words', rule, {
output: 'Use Elm',
errors: [
{
message: 'Avoid using “utilize”, use “use” instead',
message: 'Avoid using “Utilize”, use “use” instead',
},
],
},
Expand Down

0 comments on commit 70b545d

Please sign in to comment.