1
1
// LICENSE : MIT
2
2
"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" ;
7
6
import {
8
7
is助詞Token , is読点Token ,
9
8
concatJoishiTokens ,
10
9
createKeyFromKey ,
11
10
restoreToSurfaceFromKey
12
11
} 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" ;
13
15
14
16
/**
15
17
* Create token map object
@@ -19,7 +21,7 @@ import {
19
21
* @param tokens
20
22
* @returns {* }
21
23
*/
22
- function createSurfaceKeyMap ( tokens ) {
24
+ function createSurfaceKeyMap ( tokens : KuromojiToken [ ] ) : { [ index : string ] : KuromojiToken [ ] } {
23
25
// 助詞のみを対象とする
24
26
return tokens . filter ( is助詞Token ) . reduce ( ( keyMap , token ) => {
25
27
// "は:助詞.係助詞" : [token]
@@ -29,10 +31,10 @@ function createSurfaceKeyMap(tokens) {
29
31
}
30
32
keyMap [ tokenKey ] . push ( token ) ;
31
33
return keyMap ;
32
- } , { } ) ;
34
+ } , { } as { [ index : string ] : KuromojiToken [ ] } ) ;
33
35
}
34
36
35
- function matchExceptionRule ( tokens ) {
37
+ function matchExceptionRule ( tokens : KuromojiToken [ ] ) {
36
38
let token = tokens [ 0 ] ;
37
39
// "の" の重なりは例外
38
40
if ( token . pos_detail_1 === "連体化" ) {
@@ -59,6 +61,14 @@ const defaultOptions = {
59
61
separatorChars : [ "。" , "?" , "!" , "?" , "!" ]
60
62
} ;
61
63
64
+
65
+ export interface Options {
66
+ min_interval ?: number ;
67
+ strict ?: boolean ;
68
+ allow ?: string [ ] ;
69
+ separatorChars ?: string [ ]
70
+ }
71
+
62
72
/*
63
73
1. Paragraph Node -> text
64
74
2. text -> sentences
@@ -67,26 +77,25 @@ const defaultOptions = {
67
77
68
78
TODO: need abstraction
69
79
*/
70
- module . exports = function ( context , options = { } ) {
80
+ const report : TextlintRuleModule < Options > = function ( context , options = { } ) {
71
81
const helper = new RuleHelper ( context ) ;
72
82
// 最低間隔値
73
83
const minInterval = options . min_interval || defaultOptions . min_interval ;
74
84
const isStrict = options . strict || defaultOptions . strict ;
75
85
const allow = options . allow || defaultOptions . allow ;
76
- const separatorChars = options . separatorChars || defaultOptions . separatorChars ;
77
86
const { Syntax, report, RuleError} = context ;
78
87
return {
79
88
[ Syntax . Paragraph ] ( node ) {
80
89
if ( helper . isChildNode ( node , [ Syntax . Link , Syntax . Image , Syntax . BlockQuote , Syntax . Emphasis ] ) ) {
81
90
return ;
82
91
}
83
- const isSentenceNode = node => {
92
+ const isSentenceNode = ( node : TxtNode ) : node is SentenceNode => {
84
93
return node . type === SentenceSyntax . Sentence ;
85
94
} ;
86
95
const txtParentNode = splitSentences ( node ) ;
87
96
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 ) => {
90
99
const sentenceSource = new StringSource ( sentence ) ;
91
100
const text = sentenceSource . toString ( ) ;
92
101
const tokens = tokenizer . tokenizeForSentence ( text ) ;
@@ -115,7 +124,7 @@ module.exports = function (context, options = {}) {
115
124
}
116
125
*/
117
126
Object . keys ( joshiTokenSurfaceKeyMap ) . forEach ( key => {
118
- const tokens = joshiTokenSurfaceKeyMap [ key ] ;
127
+ const tokens : KuromojiToken [ ] = joshiTokenSurfaceKeyMap [ key ] ;
119
128
const joshiName = restoreToSurfaceFromKey ( key ) ;
120
129
// check allow
121
130
if ( allow . indexOf ( joshiName ) >= 0 ) {
@@ -148,8 +157,9 @@ module.exports = function (context, options = {}) {
148
157
} ) ;
149
158
} ) ;
150
159
} ;
151
- sentences . forEach ( checkSentence ) ;
160
+ sentences . forEach ( node => checkSentence ( node ) )
152
161
} ) ;
153
162
}
154
163
}
155
164
} ;
165
+ export default report ;
0 commit comments