Skip to content

Commit

Permalink
fix(deps): update to textlint-util-to-string and use replacer (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
azu authored Feb 11, 2023
1 parent 959aa45 commit 10add44
Show file tree
Hide file tree
Showing 3 changed files with 1,826 additions and 2,071 deletions.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"textlintrule"
],
"devDependencies": {
"@textlint/types": "^1.5.4",
"@types/node": "^14.14.41",
"@textlint/types": "^13.2.0",
"@types/node": "^18.13.0",
"husky": "^3.1.0",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
Expand All @@ -46,8 +46,11 @@
"dependencies": {
"kuromojin": "^3.0.0",
"sentence-splitter": "^3.2.1",
"textlint-rule-helper": "^2.1.1",
"textlint-util-to-string": "^3.0.0"
"textlint-rule-helper": "^2.3.0",
"textlint-util-to-string": "^3.3.0"
},
"resolutions": {
"@textlint/types": "^13.2.0"
},
"prettier": {
"singleQuote": false,
Expand Down
47 changes: 17 additions & 30 deletions src/no-doubled-joshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
restoreToSurfaceFromKey,
is括弧Token,
} from "./token-utils";
import { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
import { TextlintRuleModule } from "@textlint/types";
import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
import type { TextlintRuleModule } from "@textlint/types";
import { StringSource } from "textlint-util-to-string";

/**
Expand Down Expand Up @@ -107,31 +107,6 @@ export interface Options {
commaCharacters?: string[];
}

/**
* `obj.method` のCode Nodeのように、区切り文字として意味をもつノードがある場合に、
* このルールでは単純に無視したいので、同じ文字数で意味のない文字列に置き換える
* @param sentenceNode
* @param maskedType
*/
const maskNode = (sentenceNode: TxtParentNode, maskedType: string[]): TxtParentNode => {
// recursive mask
return {
...sentenceNode,
children: sentenceNode.children.map((node) => {
if (maskedType.includes(node.type)) {
return {
...node,
type: node.type,
value: "_".repeat(node.value.length),
};
}
if (node.children) {
return maskNode(node as TxtParentNode, maskedType);
}
return node;
})
}
}
/*
1. Paragraph Node -> text
2. text -> sentences
Expand Down Expand Up @@ -159,16 +134,27 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
const isSentenceNode = (node: TxtNode): node is SentenceNode => {
return node.type === SentenceSyntax.Sentence;
};
const txtParentNode = splitSentences(node, {
const txtParentNode = splitSentences(node as TxtParentNode, {
SeparatorParser: {
separatorCharacters,
},
});
const sentences = txtParentNode.children.filter(isSentenceNode);
const checkSentence = async (sentence: SentenceNode) => {
// コードの中身は無視するため、無意味な文字列に置き換える
const maskedSentence = maskNode(sentence, [Syntax.Code]);
const sentenceSource = new StringSource(maskedSentence);
// @ts-expect-error: sentence-splitterが古いので
const sentenceSource = new StringSource(sentence, {
replacer({ node, maskValue }) {
/*
* `obj.method` のCode Nodeのように、区切り文字として意味をもつノードがある場合に、
* このルールでは単純に無視したいので、同じ文字数で意味のない文字列に置き換える
*/
if (node.type === Syntax.Code) {
return maskValue("_");
}
return;
}
});
const text = sentenceSource.toString();
const tokens = await tokenize(text);
// 助詞 + 助詞は 一つの助詞として扱う
Expand Down Expand Up @@ -238,6 +224,7 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
// padding positionを計算する
const originalIndex = sentenceSource.originalIndexFromIndex(current.word_position - 1);
report(
// @ts-expect-error: SentenceNodeは独自であるため
sentence,
new RuleError(
`一文に二回以上利用されている助詞 "${joshiName}" がみつかりました。`,
Expand Down
Loading

0 comments on commit 10add44

Please sign in to comment.