Skip to content

Commit 10add44

Browse files
authored
fix(deps): update to textlint-util-to-string and use replacer (#49)
1 parent 959aa45 commit 10add44

File tree

3 files changed

+1826
-2071
lines changed

3 files changed

+1826
-2071
lines changed

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"textlintrule"
3535
],
3636
"devDependencies": {
37-
"@textlint/types": "^1.5.4",
38-
"@types/node": "^14.14.41",
37+
"@textlint/types": "^13.2.0",
38+
"@types/node": "^18.13.0",
3939
"husky": "^3.1.0",
4040
"lint-staged": "^10.5.4",
4141
"prettier": "^2.2.1",
@@ -46,8 +46,11 @@
4646
"dependencies": {
4747
"kuromojin": "^3.0.0",
4848
"sentence-splitter": "^3.2.1",
49-
"textlint-rule-helper": "^2.1.1",
50-
"textlint-util-to-string": "^3.0.0"
49+
"textlint-rule-helper": "^2.3.0",
50+
"textlint-util-to-string": "^3.3.0"
51+
},
52+
"resolutions": {
53+
"@textlint/types": "^13.2.0"
5154
},
5255
"prettier": {
5356
"singleQuote": false,

src/no-doubled-joshi.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
restoreToSurfaceFromKey,
1212
is括弧Token,
1313
} from "./token-utils";
14-
import { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
15-
import { TextlintRuleModule } from "@textlint/types";
14+
import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
15+
import type { TextlintRuleModule } from "@textlint/types";
1616
import { StringSource } from "textlint-util-to-string";
1717

1818
/**
@@ -107,31 +107,6 @@ export interface Options {
107107
commaCharacters?: string[];
108108
}
109109

110-
/**
111-
* `obj.method` のCode Nodeのように、区切り文字として意味をもつノードがある場合に、
112-
* このルールでは単純に無視したいので、同じ文字数で意味のない文字列に置き換える
113-
* @param sentenceNode
114-
* @param maskedType
115-
*/
116-
const maskNode = (sentenceNode: TxtParentNode, maskedType: string[]): TxtParentNode => {
117-
// recursive mask
118-
return {
119-
...sentenceNode,
120-
children: sentenceNode.children.map((node) => {
121-
if (maskedType.includes(node.type)) {
122-
return {
123-
...node,
124-
type: node.type,
125-
value: "_".repeat(node.value.length),
126-
};
127-
}
128-
if (node.children) {
129-
return maskNode(node as TxtParentNode, maskedType);
130-
}
131-
return node;
132-
})
133-
}
134-
}
135110
/*
136111
1. Paragraph Node -> text
137112
2. text -> sentences
@@ -159,16 +134,27 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
159134
const isSentenceNode = (node: TxtNode): node is SentenceNode => {
160135
return node.type === SentenceSyntax.Sentence;
161136
};
162-
const txtParentNode = splitSentences(node, {
137+
const txtParentNode = splitSentences(node as TxtParentNode, {
163138
SeparatorParser: {
164139
separatorCharacters,
165140
},
166141
});
167142
const sentences = txtParentNode.children.filter(isSentenceNode);
168143
const checkSentence = async (sentence: SentenceNode) => {
169144
// コードの中身は無視するため、無意味な文字列に置き換える
170-
const maskedSentence = maskNode(sentence, [Syntax.Code]);
171-
const sentenceSource = new StringSource(maskedSentence);
145+
// @ts-expect-error: sentence-splitterが古いので
146+
const sentenceSource = new StringSource(sentence, {
147+
replacer({ node, maskValue }) {
148+
/*
149+
* `obj.method` のCode Nodeのように、区切り文字として意味をもつノードがある場合に、
150+
* このルールでは単純に無視したいので、同じ文字数で意味のない文字列に置き換える
151+
*/
152+
if (node.type === Syntax.Code) {
153+
return maskValue("_");
154+
}
155+
return;
156+
}
157+
});
172158
const text = sentenceSource.toString();
173159
const tokens = await tokenize(text);
174160
// 助詞 + 助詞は 一つの助詞として扱う
@@ -238,6 +224,7 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
238224
// padding positionを計算する
239225
const originalIndex = sentenceSource.originalIndexFromIndex(current.word_position - 1);
240226
report(
227+
// @ts-expect-error: SentenceNodeは独自であるため
241228
sentence,
242229
new RuleError(
243230
`一文に二回以上利用されている助詞 "${joshiName}" がみつかりました。`,

0 commit comments

Comments
 (0)