Skip to content

Commit

Permalink
refactor: update sentence-splitter and textlint-util-to-string
Browse files Browse the repository at this point in the history
BREAKING CHANGE: require Node.js 16+
  • Loading branch information
azu committed Feb 23, 2023
1 parent ead6f37 commit 182f796
Show file tree
Hide file tree
Showing 5 changed files with 1,846 additions and 2,820 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
## Usage

$ npm install -D textlint textlint-rule-max-comma
$ $(npm bin)/textlint --rule max-comma README.md
$ npx textlint --rule max-comma README.md
# 11:0 error This sentence exceeds the maximum count of comma. Maximum is 4.

## Options

- `max`: maximum number of ","
- `max`: maximum number of `,`
- Default: `4`
- It means that report an error if the sentence include 5 or more `,`

Expand All @@ -24,7 +24,7 @@ Configure `"max"` value of the `.textlintrc` file.
{
"rules": {
"max-comma": {
"max" : 4
"max" : 4
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@
},
"homepage": "https://github.com/azu/textlint-rule-max-comma#readme",
"devDependencies": {
"textlint-scripts": "^3.0.0",
"textlint-tester": "^5.3.4"
"textlint-scripts": "^13.3.0",
"textlint-tester": "^13.3.0"
},
"dependencies": {
"sentence-splitter": "^3.2.1",
"textlint-util-to-string": "^3.1.1",
"unist-util-filter": "^2.0.3"
"sentence-splitter": "^4.2.0",
"textlint-util-to-string": "^3.3.2"
}
}
29 changes: 13 additions & 16 deletions src/textlint-rule-max-comma.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// LICENSE : MIT
"use strict";
import filter from "unist-util-filter";
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
import { splitAST, SentenceSplitterSyntax } from "sentence-splitter";
import { StringSource } from "textlint-util-to-string"

function countOfComma(text) {
Expand All @@ -18,20 +17,18 @@ export default function (context, options = defaultOptions) {
return {
[Syntax.Paragraph](node) {
const paragraphSentence = splitAST(node)
// Remove Code node for avoiding false-positive in `CODE`
const paragraphSentenceWithoutNode = filter(paragraphSentence, (node) => {
return node.type !== Syntax.Code;
});
if (!paragraphSentenceWithoutNode) {
return;
}
// This `sum(0,1,2,3,4,5,6,7,8,9,10)` is ok
// → This is ok
const sentencesWithoutCode = paragraphSentenceWithoutNode
?.children
?.filter(node => node.type === SentenceSyntax.Sentence) ?? [];
sentencesWithoutCode.forEach(sentence => {
const source = new StringSource(sentence);
const sentences = paragraphSentence.children.filter(node => node.type === SentenceSplitterSyntax.Sentence) ?? [];
sentences.forEach(sentence => {
// Remove Code node for avoiding false-positive in `CODE`
// This `sum(0,1,2,3,4,5,6,7,8,9,10)` is ok
// → This is ok
const source = new StringSource(sentence, {
replacer: ({ node, maskValue }) => {
if (node.type === Syntax.Code) {
return maskValue("_");
}
}
});
const sentenceValue = source.toString();
const count = countOfComma(sentenceValue);
if (count > maxComma) {
Expand Down
2 changes: 1 addition & 1 deletion test/textlint-rule-max-comma-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import TextLintTester from "textlint-tester";
import rule from "../src/textlint-rule-max-comma";
import fs from "fs";
import fs from "node:fs";

const tester = new TextLintTester();
// ruleName, rule, { valid, invalid }
Expand Down
Loading

0 comments on commit 182f796

Please sign in to comment.