11// LICENSE : MIT
22"use strict" ;
3- import { RuleHelper } from "textlint-rule-helper" ;
4- import { getTokenizer } from "kuromojin" ;
5- import { splitAST as splitSentences , Syntax as SentenceSyntax } from "sentence-splitter" ;
6- import StringSource from "textlint-util-to-string" ;
3+ import { RuleHelper } from "textlint-rule-helper" ;
4+ import { splitAST as splitSentences , Syntax as SentenceSyntax , SentenceNode } from "sentence-splitter" ;
5+ import { getTokenizer , KuromojiToken } from "kuromojin" ;
76import {
87 is助詞Token , is読点Token ,
98 concatJoishiTokens ,
109 createKeyFromKey ,
1110 restoreToSurfaceFromKey
1211} from "./token-utils" ;
12+ import { TxtNode } from "@textlint/ast-node-types" ;
13+ import { TextlintRuleModule } from "@textlint/types" ;
14+ import { StringSource } from "textlint-util-to-string" ;
1315
1416/**
1517 * Create token map object
@@ -19,7 +21,7 @@ import {
1921 * @param tokens
2022 * @returns {* }
2123 */
22- function createSurfaceKeyMap ( tokens ) {
24+ function createSurfaceKeyMap ( tokens : KuromojiToken [ ] ) : { [ index : string ] : KuromojiToken [ ] } {
2325 // 助詞のみを対象とする
2426 return tokens . filter ( is助詞Token ) . reduce ( ( keyMap , token ) => {
2527 // "は:助詞.係助詞" : [token]
@@ -29,10 +31,10 @@ function createSurfaceKeyMap(tokens) {
2931 }
3032 keyMap [ tokenKey ] . push ( token ) ;
3133 return keyMap ;
32- } , { } ) ;
34+ } , { } as { [ index : string ] : KuromojiToken [ ] } ) ;
3335}
3436
35- function matchExceptionRule ( tokens ) {
37+ function matchExceptionRule ( tokens : KuromojiToken [ ] ) {
3638 let token = tokens [ 0 ] ;
3739 // "の" の重なりは例外
3840 if ( token . pos_detail_1 === "連体化" ) {
@@ -59,6 +61,14 @@ const defaultOptions = {
5961 separatorChars : [ "。" , "?" , "!" , "?" , "!" ]
6062} ;
6163
64+
65+ export interface Options {
66+ min_interval ?: number ;
67+ strict ?: boolean ;
68+ allow ?: string [ ] ;
69+ separatorChars ?: string [ ]
70+ }
71+
6272/*
6373 1. Paragraph Node -> text
6474 2. text -> sentences
@@ -67,26 +77,25 @@ const defaultOptions = {
6777
6878 TODO: need abstraction
6979 */
70- module . exports = function ( context , options = { } ) {
80+ const report : TextlintRuleModule < Options > = function ( context , options = { } ) {
7181 const helper = new RuleHelper ( context ) ;
7282 // 最低間隔値
7383 const minInterval = options . min_interval || defaultOptions . min_interval ;
7484 const isStrict = options . strict || defaultOptions . strict ;
7585 const allow = options . allow || defaultOptions . allow ;
76- const separatorChars = options . separatorChars || defaultOptions . separatorChars ;
7786 const { Syntax, report, RuleError} = context ;
7887 return {
7988 [ Syntax . Paragraph ] ( node ) {
8089 if ( helper . isChildNode ( node , [ Syntax . Link , Syntax . Image , Syntax . BlockQuote , Syntax . Emphasis ] ) ) {
8190 return ;
8291 }
83- const isSentenceNode = node => {
92+ const isSentenceNode = ( node : TxtNode ) : node is SentenceNode => {
8493 return node . type === SentenceSyntax . Sentence ;
8594 } ;
8695 const txtParentNode = splitSentences ( node ) ;
8796 const sentences = txtParentNode . children . filter ( isSentenceNode ) ;
88- return getTokenizer ( ) . then ( tokenizer => {
89- const checkSentence = ( sentence ) => {
97+ return getTokenizer ( ) . then ( ( tokenizer : any ) => {
98+ const checkSentence = ( sentence : SentenceNode ) => {
9099 const sentenceSource = new StringSource ( sentence ) ;
91100 const text = sentenceSource . toString ( ) ;
92101 const tokens = tokenizer . tokenizeForSentence ( text ) ;
@@ -115,7 +124,7 @@ module.exports = function (context, options = {}) {
115124 }
116125 */
117126 Object . keys ( joshiTokenSurfaceKeyMap ) . forEach ( key => {
118- const tokens = joshiTokenSurfaceKeyMap [ key ] ;
127+ const tokens : KuromojiToken [ ] = joshiTokenSurfaceKeyMap [ key ] ;
119128 const joshiName = restoreToSurfaceFromKey ( key ) ;
120129 // check allow
121130 if ( allow . indexOf ( joshiName ) >= 0 ) {
@@ -148,8 +157,9 @@ module.exports = function (context, options = {}) {
148157 } ) ;
149158 } ) ;
150159 } ;
151- sentences . forEach ( checkSentence ) ;
160+ sentences . forEach ( node => checkSentence ( node ) )
152161 } ) ;
153162 }
154163 }
155164} ;
165+ export default report ;
0 commit comments