Skip to content

Commit

Permalink
refactor: update to sentence-splitter@4 (#40)
Browse files Browse the repository at this point in the history
* refactor: update to sentence-length@4

* CI: update node versions

* fix

* update readme
  • Loading branch information
azu authored Feb 12, 2023
1 parent 9d0bfef commit 7c23ec4
Show file tree
Hide file tree
Showing 6 changed files with 2,121 additions and 3,097 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12, 14]
node-version: [ 16, 18 ]
steps:
- name: checkout
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Add "sentence-length" to your `.textlintrc`.
- The total number of characters allowed on each sentences.
- Sentence.length > 100 and throw Error
- `skipPatterns`: `string[]`
- A strings that match the patterns is uncount of the sentence.
- A strings that match the patterns is uncounted of the sentence.
- Set an array of RegExp-like string.
- See https://github.com/textlint/regexp-string-matcher
- `skipUrlStringLink`: `boolean`
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@
"prepare": "git config --local core.hooksPath .githooks"
},
"dependencies": {
"@textlint/regexp-string-matcher": "^1.1.0",
"sentence-splitter": "^3.2.3",
"textlint-rule-helper": "^2.1.1",
"textlint-util-to-string": "^3.1.1"
"@textlint/regexp-string-matcher": "^2.0.2",
"sentence-splitter": "^4.0.2",
"textlint-rule-helper": "^2.3.0",
"textlint-util-to-string": "^3.2.0"
},
"devDependencies": {
"@textlint/types": "^1.5.5",
"@types/node": "^15.0.2",
"lint-staged": "^11.0.0",
"prettier": "^2.2.1",
"textlint-plugin-html": "^0.2.0",
"textlint-scripts": "^3.0.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
"@textlint/types": "^13.2.0",
"@types/node": "^18.13.0",
"lint-staged": "^13.1.1",
"prettier": "^2.8.4",
"textlint-plugin-html": "^0.3.0",
"textlint-scripts": "^13.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
"email": "[email protected]",
"lint-staged": {
Expand Down
16 changes: 8 additions & 8 deletions src/sentence-length.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
import type { TextlintRuleReporter } from "@textlint/types";
import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
import { splitAST, SentenceSplitterSyntax } from "sentence-splitter";
import { StringSource } from "textlint-util-to-string";
import { RuleHelper } from "textlint-rule-helper";
import { createRegExp } from "@textlint/regexp-string-matcher";
import { TextlintRuleReporter } from "@textlint/types";
import { TxtNode, TxtParentNode } from "@textlint/ast-node-types";

function removeRangeFromString(text: string, regExpStrings: string[]) {
const patterns = regExpStrings.map((pattern) => {
Expand Down Expand Up @@ -74,9 +74,9 @@ const reporter: TextlintRuleReporter<Options> = (context, options = {}) => {
return;
}
// empty break line == split sentence
const paragraph = splitAST(node);
paragraph.children
.filter((sentence) => sentence.type === SentenceSyntax.Sentence)
const sentenceRootNode = splitAST(node);
sentenceRootNode.children
.filter((sentence) => sentence.type === SentenceSplitterSyntax.Sentence)
.forEach((sentence) => {
const filteredSentence = skipUrlStringLink
? {
Expand All @@ -86,8 +86,7 @@ const reporter: TextlintRuleReporter<Options> = (context, options = {}) => {
})
}
: sentence;
// @ts-expect-error: wrong types
const source = new StringSource(filteredSentence);
const source = new StringSource(filteredSentence as TxtParentNode);
const actualText = source.toString();
const sentenceText = removeRangeFromString(actualText, skipPatterns);
// larger than > 100
Expand All @@ -96,6 +95,7 @@ const reporter: TextlintRuleReporter<Options> = (context, options = {}) => {
if (sentenceLength > maxLength) {
const startLine = filteredSentence.loc.start.line;
report(
// @ts-expect-error: It is compatible with textlint node
filteredSentence,
new RuleError(`Line ${startLine} sentence length(${
sentenceLength !== actualTextLength
Expand Down
4 changes: 3 additions & 1 deletion test/sentence-length-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ tester.run("textlint-rule-sentence-length", rule, {
}
},
{
text: `また*CORS policy Invalid*のようなエラーがコンソールに表示されている場合は、[Same Origin Policy][]により\`index.js\`の読み込みが失敗しています。`,
text: `また*CORS policy Invalid*のようなエラーがコンソールに表示されている場合は、[Same Origin Policy][]により\`index.js\`の読み込みが失敗しています。
[Same Origin Policy]: https://example.com/`,
options: {
max: 90
}
Expand Down
Loading

0 comments on commit 7c23ec4

Please sign in to comment.