2
2
"use strict" ;
3
3
import { RuleHelper } from "textlint-rule-helper" ;
4
4
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" ;
6
6
import StringSource from "textlint-util-to-string" ;
7
7
import {
8
8
is助詞Token , is読点Token ,
9
9
concatJoishiTokens ,
10
10
createKeyFromKey ,
11
11
restoreToSurfaceFromKey
12
12
} from "./token-utils" ;
13
+
13
14
/**
14
15
* Create token map object
15
16
* {
@@ -30,6 +31,7 @@ function createSurfaceKeyMap(tokens) {
30
31
return keyMap ;
31
32
} , { } ) ;
32
33
}
34
+
33
35
function matchExceptionRule ( tokens ) {
34
36
let token = tokens [ 0 ] ;
35
37
// "の" の重なりは例外
@@ -46,6 +48,7 @@ function matchExceptionRule(tokens) {
46
48
}
47
49
return false ;
48
50
}
51
+
49
52
/*
50
53
default options
51
54
*/
@@ -64,7 +67,7 @@ const defaultOptions = {
64
67
65
68
TODO: need abstraction
66
69
*/
67
- module . exports = function ( context , options = { } ) {
70
+ module . exports = function ( context , options = { } ) {
68
71
const helper = new RuleHelper ( context ) ;
69
72
// 最低間隔値
70
73
const minInterval = options . min_interval || defaultOptions . min_interval ;
@@ -73,21 +76,20 @@ module.exports = function(context, options = {}) {
73
76
const separatorChars = options . separatorChars || defaultOptions . separatorChars ;
74
77
const { Syntax, report, RuleError} = context ;
75
78
return {
76
- [ Syntax . Paragraph ] ( node ) {
79
+ [ Syntax . Paragraph ] ( node ) {
77
80
if ( helper . isChildNode ( node , [ Syntax . Link , Syntax . Image , Syntax . BlockQuote , Syntax . Emphasis ] ) ) {
78
81
return ;
79
82
}
80
- const source = new StringSource ( node ) ;
81
- const text = source . toString ( ) ;
82
83
const isSentenceNode = node => {
83
84
return node . type === SentenceSyntax . Sentence ;
84
85
} ;
85
- const sentences = splitSentences ( text , {
86
- separatorChars : separatorChars
87
- } ) . filter ( isSentenceNode ) ;
86
+ const txtParentNode = splitSentences ( node ) ;
87
+ const sentences = txtParentNode . children . filter ( isSentenceNode ) ;
88
88
return getTokenizer ( ) . then ( tokenizer => {
89
89
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 ) ;
91
93
// 助詞 + 助詞は 一つの助詞として扱う
92
94
// https://github.com/textlint-ja/textlint-rule-no-doubled-joshi/issues/15
93
95
// 連語(助詞)の対応
@@ -136,15 +138,11 @@ module.exports = function(context, options = {}) {
136
138
// 助詞token同士の距離が設定値以下ならエラーを報告する
137
139
const differenceIndex = otherPosition - startPosition ;
138
140
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
- } ) ;
143
141
// padding positionを計算する
144
- const padding = {
142
+ const originalIndex = sentenceSource . originalIndexFromIndex ( current . word_position - 1 ) ;
143
+ report ( sentence , new RuleError ( `一文に二回以上利用されている助詞 "${ joshiName } " がみつかりました。` , {
145
144
index : originalIndex
146
- } ;
147
- report ( node , new RuleError ( `一文に二回以上利用されている助詞 "${ joshiName } " がみつかりました。` , padding ) ) ;
145
+ } ) ) ;
148
146
}
149
147
return current ;
150
148
} ) ;
0 commit comments