Skip to content

Commit

Permalink
refactor(deps): update to setence-splitter@5 (#42)
Browse files Browse the repository at this point in the history
* refactor(deps): update to setence-splitter@5

**BREAKING CHANGES**

> [!WARNING]
> Require Node.js 18+

- Update to sentence-splitter@5
  - https://github.com/textlint-rule/sentence-splitter/releases/tag/v5.0.0

* CI: update node versions

* CI: add .github/release.yml
  • Loading branch information
azu authored Nov 26, 2023
1 parent cb99b2d commit 9102e2c
Show file tree
Hide file tree
Showing 9 changed files with 1,346 additions and 1,453 deletions.
30 changes: 30 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
changelog:
exclude:
labels:
- 'Type: Meta'
- 'Type: Question'
- 'Type: Release'

categories:
- title: Security Fixes
labels: ['Type: Security']
- title: Breaking Changes
labels: ['Type: Breaking Change']
- title: Features
labels: ['Type: Feature']
- title: Bug Fixes
labels: ['Type: Bug']
- title: Documentation
labels: ['Type: Documentation']
- title: Refactoring
labels: ['Type: Refactoring']
- title: Testing
labels: ['Type: Testing']
- title: Maintenance
labels: ['Type: Maintenance']
- title: CI
labels: ['Type: CI']
- title: Dependency Updates
labels: ['Type: Dependencies', "dependencies"]
- title: Other Changes
labels: ['*']
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: [ 16, 18 ]
node-version: [ 18, 20 ]
steps:
- name: checkout
uses: actions/checkout@v2
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
},
"dependencies": {
"@textlint/regexp-string-matcher": "^2.0.2",
"sentence-splitter": "^4.2.0",
"textlint-rule-helper": "^2.3.0",
"textlint-util-to-string": "^3.3.2"
"sentence-splitter": "^5.0.0",
"textlint-rule-helper": "^2.3.1",
"textlint-util-to-string": "^3.3.4"
},
"devDependencies": {
"@textlint/types": "^13.3.0",
"@types/node": "^18.13.0",
"lint-staged": "^13.1.1",
"prettier": "^2.8.4",
"textlint-plugin-html": "^0.3.0",
"textlint-scripts": "^13.3.0",
"@textlint/types": "^13.4.1",
"@types/node": "^20.10.0",
"lint-staged": "^15.1.0",
"prettier": "^3.1.0",
"textlint-plugin-html": "^1.0.0",
"textlint-scripts": "^13.4.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
"typescript": "^5.3.2"
},
"email": "azuciao@gmail.com",
"lint-staged": {
Expand Down
68 changes: 35 additions & 33 deletions src/sentence-length.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TextlintRuleReporter } from "@textlint/types";
import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
import { splitAST, SentenceSplitterSyntax } from "sentence-splitter";
import type { TxtParentNode } from "@textlint/ast-node-types";
import type { TxtParentNodeWithSentenceNodeContent, TxtSentenceNodeChildren } from "sentence-splitter";
import { SentenceSplitterSyntax, splitAST, TxtSentenceNode } from "sentence-splitter";
import { StringSource } from "textlint-util-to-string";
import { RuleHelper } from "textlint-rule-helper";
import { createRegExp } from "@textlint/regexp-string-matcher";
Expand Down Expand Up @@ -47,13 +48,16 @@ const defaultOptions: Required<Options> = {
exclusionPatterns: []
};

const isSentenceNode = (node: TxtParentNodeWithSentenceNodeContent): node is TxtSentenceNode => {
return node.type === SentenceSplitterSyntax.Sentence;
};
const reporter: TextlintRuleReporter<Options> = (context, options = {}) => {
const maxLength = options.max ?? defaultOptions.max;
const skipPatterns = options.skipPatterns ?? options.exclusionPatterns ?? defaultOptions.skipPatterns;
const skipUrlStringLink = options.skipUrlStringLink ?? defaultOptions.skipUrlStringLink;
const helper = new RuleHelper(context);
const { Syntax, RuleError, report } = context;
const isUrlStringLink = (node: TxtNode | TxtParentNode): boolean => {
const isUrlStringLink = (node: TxtSentenceNodeChildren): boolean => {
if (node.type !== Syntax.Link) {
return false;
}
Expand All @@ -79,37 +83,35 @@ const reporter: TextlintRuleReporter<Options> = (context, options = {}) => {
}
// empty break line == split sentence
const sentenceRootNode = splitAST(node);
sentenceRootNode.children
.filter((sentence) => sentence.type === SentenceSplitterSyntax.Sentence)
.forEach((sentence) => {
const filteredSentence = skipUrlStringLink
? {
...sentence,
children: sentence.children.filter((sentenceChildNode: TxtNode | TxtParentNode) => {
return !isUrlStringLink(sentenceChildNode);
})
}
: sentence;
const source = new StringSource(filteredSentence);
const actualText = source.toString();
const sentenceText = removeRangeFromString(actualText, skipPatterns);
// larger than > 100
const actualTextLength = actualText.length;
const sentenceLength = sentenceText.length;
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
? `${sentenceLength}, original:${actualTextLength}`
: sentenceLength
}) exceeds the maximum sentence length of ${maxLength}.
sentenceRootNode.children.filter(isSentenceNode).forEach((sentence) => {
const filteredSentence = skipUrlStringLink
? {
...sentence,
children: sentence.children.filter((sentenceChildNode) => {
return !isUrlStringLink(sentenceChildNode);
})
}
: sentence;
const source = new StringSource(filteredSentence);
const actualText = source.toString();
const sentenceText = removeRangeFromString(actualText, skipPatterns);
// larger than > 100
const actualTextLength = actualText.length;
const sentenceLength = sentenceText.length;
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
? `${sentenceLength}, original:${actualTextLength}`
: sentenceLength
}) exceeds the maximum sentence length of ${maxLength}.
Over ${sentenceLength - maxLength} characters.`)
);
}
});
);
}
});
}
};
};
Expand Down
54 changes: 54 additions & 0 deletions test/sentence-length-html-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import TextLintTester from "textlint-tester";
import rule from "../src/sentence-length";

const tester = new TextLintTester();

(async function testHTML() {
const htmlPlugin = (await import("textlint-plugin-html")).default;
tester.run(
"textlint-rule-sentence-length:plugin",
{
plugins: [
{
pluginId: "html",
plugin: htmlPlugin
}
],
rules: [
{
ruleId: "textlint-rule-sentence-length",
rule: rule,
options: {
max: 15
}
}
]
},
{
valid: [
{
text: "<p>this is a test.</p>",
ext: ".html"
},
{
text: "<p>TEST is <a href='https://example.com'>https://example.com</a></p>",
ext: ".html"
}
],
invalid: [
{
text: "<p>this is a test for textlint-rule-sentence-length with plugin</p>",
ext: ".html",
errors: [
{
message: `Line 1 sentence length(60) exceeds the maximum sentence length of 15.
Over 45 characters.`,
line: 1,
column: 4
}
]
}
]
}
);
})();
49 changes: 0 additions & 49 deletions test/sentence-length-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import TextLintTester from "textlint-tester";
import rule from "../src/sentence-length";
// @ts-expect-error: no types
import htmlPlugin from "textlint-plugin-html";

const tester = new TextLintTester();

Expand Down Expand Up @@ -254,50 +252,3 @@ Over 18 characters.`
}
]
});

tester.run(
"textlint-rule-sentence-length:plugin",
{
plugins: [
{
pluginId: "html",
plugin: htmlPlugin
}
],
rules: [
{
ruleId: "textlint-rule-sentence-length",
rule: rule,
options: {
max: 15
}
}
]
},
{
valid: [
{
text: "<p>this is a test.</p>",
ext: ".html"
},
{
text: "<p>TEST is <a href='https://example.com'>https://example.com</a></p>",
ext: ".html"
}
],
invalid: [
{
text: "<p>this is a test for textlint-rule-sentence-length with plugin</p>",
ext: ".html",
errors: [
{
message: `Line 1 sentence length(60) exceeds the maximum sentence length of 15.
Over 45 characters.`,
line: 1,
column: 4
}
]
}
]
}
);
10 changes: 0 additions & 10 deletions test/tsconfig.json

This file was deleted.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"noFallthroughCasesInSwitch": true
},
"include": [
"src/**/*"
"src/**/*",
"test/**/*"
],
"exclude": [
".git",
Expand Down
Loading

0 comments on commit 9102e2c

Please sign in to comment.