1
1
// LICENSE : MIT
2
2
"use strict" ;
3
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" ;
4
+ import { splitAST as splitSentences , Syntax as SentenceSyntax , SentenceNode } from "sentence-splitter " ;
5
+ const StringSource = require ( "textlint-util-to-string" ) ;
6
+ const { getTokenizer } = require ( "kuromojin" ) ;
7
7
import {
8
8
is助詞Token , is読点Token ,
9
9
concatJoishiTokens ,
10
10
createKeyFromKey ,
11
- restoreToSurfaceFromKey
11
+ restoreToSurfaceFromKey , Token
12
12
} from "./token-utils" ;
13
+ import { TextlintRuleModule } from "@textlint/types" ;
14
+ import { TxtNode } from "@textlint/types/lib/ast-node-types/src" ;
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 : Token [ ] ) {
23
25
// 助詞のみを対象とする
24
26
return tokens . filter ( is助詞Token ) . reduce ( ( keyMap , token ) => {
25
27
// "は:助詞.係助詞" : [token]
@@ -32,7 +34,7 @@ function createSurfaceKeyMap(tokens) {
32
34
} , { } ) ;
33
35
}
34
36
35
- function matchExceptionRule ( tokens ) {
37
+ function matchExceptionRule ( tokens : Token [ ] ) {
36
38
let token = tokens [ 0 ] ;
37
39
// "の" の重なりは例外
38
40
if ( token . pos_detail_1 === "連体化" ) {
@@ -73,20 +75,19 @@ module.exports = function (context, options = {}) {
73
75
const minInterval = options . min_interval || defaultOptions . min_interval ;
74
76
const isStrict = options . strict || defaultOptions . strict ;
75
77
const allow = options . allow || defaultOptions . allow ;
76
- const separatorChars = options . separatorChars || defaultOptions . separatorChars ;
77
78
const { Syntax, report, RuleError} = context ;
78
79
return {
79
80
[ Syntax . Paragraph ] ( node ) {
80
81
if ( helper . isChildNode ( node , [ Syntax . Link , Syntax . Image , Syntax . BlockQuote , Syntax . Emphasis ] ) ) {
81
82
return ;
82
83
}
83
- const isSentenceNode = node => {
84
+ const isSentenceNode = ( node : TxtNode ) : node is SentenceNode => {
84
85
return node . type === SentenceSyntax . Sentence ;
85
86
} ;
86
87
const txtParentNode = splitSentences ( node ) ;
87
88
const sentences = txtParentNode . children . filter ( isSentenceNode ) ;
88
- return getTokenizer ( ) . then ( tokenizer => {
89
- const checkSentence = ( sentence ) => {
89
+ return getTokenizer ( ) . then ( ( tokenizer : any ) => {
90
+ const checkSentence = ( sentence : SentenceNode ) => {
90
91
const sentenceSource = new StringSource ( sentence ) ;
91
92
const text = sentenceSource . toString ( ) ;
92
93
const tokens = tokenizer . tokenizeForSentence ( text ) ;
@@ -115,7 +116,7 @@ module.exports = function (context, options = {}) {
115
116
}
116
117
*/
117
118
Object . keys ( joshiTokenSurfaceKeyMap ) . forEach ( key => {
118
- const tokens = joshiTokenSurfaceKeyMap [ key ] ;
119
+ const tokens : Token [ ] = joshiTokenSurfaceKeyMap [ key ] ;
119
120
const joshiName = restoreToSurfaceFromKey ( key ) ;
120
121
// check allow
121
122
if ( allow . indexOf ( joshiName ) >= 0 ) {
@@ -148,8 +149,8 @@ module.exports = function (context, options = {}) {
148
149
} ) ;
149
150
} ) ;
150
151
} ;
151
- sentences . forEach ( checkSentence ) ;
152
+ sentences . forEach ( node => checkSentence ( node ) )
152
153
} ) ;
153
154
}
154
155
}
155
- } ;
156
+ } as TextlintRuleModule ;
0 commit comments