Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ $ npm install textlint textlint-rule-ginger
$ textlint --rule textlint-rule-ginger text-to-proofread.txt
```

You can set words to skip assertion with the regular expression.

```json
"rule": {
"ginger": {
"skipRegExps": ["[Jj]ava *[Ss]cript"]
}
}
```

## Tests

```
Expand Down
25 changes: 19 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"name": "textlint-rule-ginger",
"version": "2.1.2",
"description":
"textlint rule to check your English grammar with Ginger Proofreading",
"description": "textlint rule to check your English grammar with Ginger Proofreading",
"engines": {
"node": ">=6"
},
Expand All @@ -16,8 +15,16 @@
"precommit": "lint-staged",
"prettier": "prettier --write \"**/*.{js,json,md}\""
},
"files": ["lib", "src"],
"keywords": ["textlint", "rule", "english", "proofreading"],
"files": [
"lib",
"src"
],
"keywords": [
"textlint",
"rule",
"english",
"proofreading"
],
"author": "nodaguti",
"license": "MIT",
"bugs": "https://github.com/textlint-rule/textlint-rule-ginger/issues",
Expand Down Expand Up @@ -46,7 +53,13 @@
"textlint-tester": "^3.0.3"
},
"lint-staged": {
"*.js": ["prettier", "git add"],
"*.json": ["prettier", "git add"]
"*.js": [
"prettier",
"git add"
],
"*.json": [
"prettier",
"git add"
]
}
}
57 changes: 34 additions & 23 deletions src/ginger.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function filterNode({ node, context }) {
return { source, text };
}

function reporter(context) {
function reporter(context, options = {}) {
const opts = Object.assign({ skipRegExps: [] }, options);
const { Syntax, report, RuleError, fixer } = context;

return {
Expand All @@ -55,31 +56,41 @@ function reporter(context) {
return;
}

corrections.forEach((correction) => {
const index = correction.start;
const originalPosition = source.originalPositionFromIndex(index);
const originalRange = [
originalPosition.column,
originalPosition.column + correction.length,
];
corrections
.filter(
(correction) =>
!opts.skipRegExps.some((skipRegExp) =>
RegExp(skipRegExp).test(correction.text),
),
)
.forEach((correction) => {
const index = correction.start;
const originalPosition = source.originalPositionFromIndex(index);
const originalRange = [
originalPosition.column,
originalPosition.column + correction.length,
];

// if range is ignored, skip reporting
if (ignoreNodeManager.isIgnoredRange(originalRange)) {
return;
}
// if range is ignored, skip reporting
if (ignoreNodeManager.isIgnoredRange(originalRange)) {
return;
}

const fix = fixer.replaceTextRange(originalRange, correction.correct);
const message = `${correction.text} -> ${correction.correct}`;
const fix = fixer.replaceTextRange(
originalRange,
correction.correct,
);
const message = `${correction.text} -> ${correction.correct}`;

report(
node,
new RuleError(message, {
line: originalPosition.line - 1,
column: originalPosition.column,
fix,
}),
);
});
report(
node,
new RuleError(message, {
line: originalPosition.line - 1,
column: originalPosition.column,
fix,
}),
);
});
})();
},
};
Expand Down
6 changes: 6 additions & 0 deletions test/ginger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ tester.run('ginger', rule, {
'Hello, world!',
'This sentence contains no mistakes.',
'Misspellings in inline `codee` should be ignored.',
{
text: 'This link does not contain an [errror](index.html).',
options: {
skipRegExps: ['err+or'],
},
},
],
invalid: [
{
Expand Down