Skip to content

Commit 182f796

Browse files
committed
refactor: update sentence-splitter and textlint-util-to-string
BREAKING CHANGE: require Node.js 16+
1 parent ead6f37 commit 182f796

File tree

5 files changed

+1846
-2820
lines changed

5 files changed

+1846
-2820
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
## Usage
1010

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

1515
## Options
1616

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

@@ -24,7 +24,7 @@ Configure `"max"` value of the `.textlintrc` file.
2424
{
2525
"rules": {
2626
"max-comma": {
27-
"max" : 4
27+
"max" : 4
2828
}
2929
}
3030
}

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131
},
3232
"homepage": "https://github.com/azu/textlint-rule-max-comma#readme",
3333
"devDependencies": {
34-
"textlint-scripts": "^3.0.0",
35-
"textlint-tester": "^5.3.4"
34+
"textlint-scripts": "^13.3.0",
35+
"textlint-tester": "^13.3.0"
3636
},
3737
"dependencies": {
38-
"sentence-splitter": "^3.2.1",
39-
"textlint-util-to-string": "^3.1.1",
40-
"unist-util-filter": "^2.0.3"
38+
"sentence-splitter": "^4.2.0",
39+
"textlint-util-to-string": "^3.3.2"
4140
}
4241
}

src/textlint-rule-max-comma.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// LICENSE : MIT
22
"use strict";
3-
import filter from "unist-util-filter";
4-
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
3+
import { splitAST, SentenceSplitterSyntax } from "sentence-splitter";
54
import { StringSource } from "textlint-util-to-string"
65

76
function countOfComma(text) {
@@ -18,20 +17,18 @@ export default function (context, options = defaultOptions) {
1817
return {
1918
[Syntax.Paragraph](node) {
2019
const paragraphSentence = splitAST(node)
21-
// Remove Code node for avoiding false-positive in `CODE`
22-
const paragraphSentenceWithoutNode = filter(paragraphSentence, (node) => {
23-
return node.type !== Syntax.Code;
24-
});
25-
if (!paragraphSentenceWithoutNode) {
26-
return;
27-
}
28-
// This `sum(0,1,2,3,4,5,6,7,8,9,10)` is ok
29-
// → This is ok
30-
const sentencesWithoutCode = paragraphSentenceWithoutNode
31-
?.children
32-
?.filter(node => node.type === SentenceSyntax.Sentence) ?? [];
33-
sentencesWithoutCode.forEach(sentence => {
34-
const source = new StringSource(sentence);
20+
const sentences = paragraphSentence.children.filter(node => node.type === SentenceSplitterSyntax.Sentence) ?? [];
21+
sentences.forEach(sentence => {
22+
// Remove Code node for avoiding false-positive in `CODE`
23+
// This `sum(0,1,2,3,4,5,6,7,8,9,10)` is ok
24+
// → This is ok
25+
const source = new StringSource(sentence, {
26+
replacer: ({ node, maskValue }) => {
27+
if (node.type === Syntax.Code) {
28+
return maskValue("_");
29+
}
30+
}
31+
});
3532
const sentenceValue = source.toString();
3633
const count = countOfComma(sentenceValue);
3734
if (count > maxComma) {

test/textlint-rule-max-comma-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import TextLintTester from "textlint-tester";
22
import rule from "../src/textlint-rule-max-comma";
3-
import fs from "fs";
3+
import fs from "node:fs";
44

55
const tester = new TextLintTester();
66
// ruleName, rule, { valid, invalid }

0 commit comments

Comments
 (0)