Skip to content

Commit fd2f8bd

Browse files
authored
fix(rule): use index-based padding (#23)
fix(rule): use index-based padding
2 parents 9eafd53 + db9b86c commit fd2f8bd

File tree

4 files changed

+81
-689
lines changed

4 files changed

+81
-689
lines changed

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,12 @@
3333
"textlintrule"
3434
],
3535
"devDependencies": {
36-
"espower-babel": "^4.0.0",
37-
"power-assert": "^1.1.0",
38-
"textlint": "^6.0.3",
3936
"textlint-scripts": "^2.1.0"
4037
},
4138
"dependencies": {
4239
"kuromojin": "^1.2.1",
43-
"sentence-splitter": "^2.2.0",
44-
"textlint-rule-helper": "^1.1.4",
45-
"textlint-util-to-string": "^1.1.0"
40+
"sentence-splitter": "^3.0.11",
41+
"textlint-rule-helper": "^2.1.1",
42+
"textlint-util-to-string": "^2.1.1"
4643
}
4744
}

src/no-doubled-joshi.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
"use strict";
33
import {RuleHelper} from "textlint-rule-helper";
44
import {getTokenizer} from "kuromojin";
5-
import {split as splitSentences, Syntax as SentenceSyntax} from "sentence-splitter";
5+
import {splitAST as splitSentences, Syntax as SentenceSyntax} from "sentence-splitter";
66
import StringSource from "textlint-util-to-string";
77
import {
88
is助詞Token, is読点Token,
99
concatJoishiTokens,
1010
createKeyFromKey,
1111
restoreToSurfaceFromKey
1212
} from "./token-utils";
13+
1314
/**
1415
* Create token map object
1516
* {
@@ -30,6 +31,7 @@ function createSurfaceKeyMap(tokens) {
3031
return keyMap;
3132
}, {});
3233
}
34+
3335
function matchExceptionRule(tokens) {
3436
let token = tokens[0];
3537
// "の" の重なりは例外
@@ -46,6 +48,7 @@ function matchExceptionRule(tokens) {
4648
}
4749
return false;
4850
}
51+
4952
/*
5053
default options
5154
*/
@@ -64,7 +67,7 @@ const defaultOptions = {
6467
6568
TODO: need abstraction
6669
*/
67-
module.exports = function(context, options = {}) {
70+
module.exports = function (context, options = {}) {
6871
const helper = new RuleHelper(context);
6972
// 最低間隔値
7073
const minInterval = options.min_interval || defaultOptions.min_interval;
@@ -73,21 +76,20 @@ module.exports = function(context, options = {}) {
7376
const separatorChars = options.separatorChars || defaultOptions.separatorChars;
7477
const {Syntax, report, RuleError} = context;
7578
return {
76-
[Syntax.Paragraph](node){
79+
[Syntax.Paragraph](node) {
7780
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
7881
return;
7982
}
80-
const source = new StringSource(node);
81-
const text = source.toString();
8283
const isSentenceNode = node => {
8384
return node.type === SentenceSyntax.Sentence;
8485
};
85-
const sentences = splitSentences(text, {
86-
separatorChars: separatorChars
87-
}).filter(isSentenceNode);
86+
const txtParentNode = splitSentences(node);
87+
const sentences = txtParentNode.children.filter(isSentenceNode);
8888
return getTokenizer().then(tokenizer => {
8989
const checkSentence = (sentence) => {
90-
const tokens = tokenizer.tokenizeForSentence(sentence.raw);
90+
const sentenceSource = new StringSource(sentence);
91+
const text = sentenceSource.toString();
92+
const tokens = tokenizer.tokenizeForSentence(text);
9193
// 助詞 + 助詞は 一つの助詞として扱う
9294
// https://github.com/textlint-ja/textlint-rule-no-doubled-joshi/issues/15
9395
// 連語(助詞)の対応
@@ -136,15 +138,11 @@ module.exports = function(context, options = {}) {
136138
// 助詞token同士の距離が設定値以下ならエラーを報告する
137139
const differenceIndex = otherPosition - startPosition;
138140
if (differenceIndex <= minInterval) {
139-
const originalIndex = source.originalIndexFromPosition({
140-
line: sentence.loc.start.line,
141-
column: sentence.loc.start.column + (current.word_position - 1)
142-
});
143141
// padding positionを計算する
144-
const padding = {
142+
const originalIndex = sentenceSource.originalIndexFromIndex(current.word_position - 1);
143+
report(sentence, new RuleError(`一文に二回以上利用されている助詞 "${joshiName}" がみつかりました。`, {
145144
index: originalIndex
146-
};
147-
report(node, new RuleError(`一文に二回以上利用されている助詞 "${joshiName}" がみつかりました。`, padding));
145+
}));
148146
}
149147
return current;
150148
});

test/no-doubled-joshi-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ tester.run("no-double-joshi", rule, {
179179
column: 8
180180
}
181181
]
182+
},
183+
//
184+
{
185+
text: `今まで「サイトはNetlify」「スライドはGitLab Pages」といった配信分けをしていたのですが、
186+
「 \`/slides\` にビルドしたスライドを置きたい」という動機のものと、こんな構成を検討しています。
187+
188+
* 最初にtextlintで文法チェック
189+
* ドキュメントを別にビルドしてarticle化
190+
* 複数articleを束ねてFirebaseへデプロイ`,
191+
errors: [
192+
{
193+
message: `一文に二回以上利用されている助詞 "は" がみつかりました。`,
194+
index: 21
195+
}
196+
]
182197
}
183198
]
184199
});

0 commit comments

Comments
 (0)