22"use strict" ;
33import { RuleHelper } from "textlint-rule-helper" ;
44import { 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" ;
66import StringSource from "textlint-util-to-string" ;
77import {
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+
3335function 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 } ) ;
0 commit comments