11import { TextlintRuleReporter } from "@textlint/types" ;
22import { createIndex , ItemGroup , Midashi } from "./create-index" ;
33import { SudachiSynonyms } from "sudachi-synonyms-dictionary" ;
4+ import { wrapReportHandler } from "textlint-rule-helper" ;
45
56const TinySegmenter = require ( "tiny-segmenter" ) ;
67const segmenter = new TinySegmenter ( ) ; // インスタンス生成
78
89export interface Options {
10+ /**
11+ * 許可するワードの配列
12+ * ワードは完全一致で比較し、一致した場合は無視されます
13+ */
14+ allows ?: string [ ] ;
915 /**
1016 * 同じ語形の語の中でのアルファベットの表記揺れを許可するかどうか
1117 * trueの場合はカタカナとアルファベットの表記ゆれを許可します
@@ -16,12 +22,14 @@ export interface Options {
1622
1723
1824export const DefaultOptions : Required < Options > = {
25+ allows : [ ] ,
1926 allowAlphabet : true
2027} ;
2128
2229const report : TextlintRuleReporter < Options > = ( context , options = { } ) => {
2330 const allowAlphabet = options . allowAlphabet !== undefined ? options . allowAlphabet : DefaultOptions . allowAlphabet ;
24- const { Syntax, getSource, report, RuleError } = context ;
31+ const allows = options . allows !== undefined ? options . allows : DefaultOptions . allows ;
32+ const { Syntax, getSource, RuleError } = context ;
2533 const usedSudachiSynonyms : Set < SudachiSynonyms > = new Set ( ) ;
2634 const locationMap : Map < SudachiSynonyms , { index : number } > = new Map ( ) ;
2735 const usedItemGroup : Set < ItemGroup > = new Set ( ) ;
@@ -44,36 +52,44 @@ const report: TextlintRuleReporter<Options> = (context, options = {}) => {
4452 } ) ;
4553 } ) ;
4654 } ;
47- return {
48- async [ Syntax . Str ] ( node ) {
49- const { keyItemGroupMap } = await indexPromise ;
50- const text = getSource ( node ) ;
51- const segments : string [ ] = segmenter . segment ( text ) ;
52- let absoluteIndex = node . range [ 0 ] ;
53- segments . forEach ( ( segement ) => {
54- matchSegment ( segement , absoluteIndex , keyItemGroupMap ) ;
55- absoluteIndex += segement . length ;
56- } ) ;
55+ return wrapReportHandler ( context ,
56+ {
57+ ignoreNodeTypes : [ Syntax . BlockQuote , Syntax . CodeBlock , Syntax . Code , Syntax . Html , Syntax . Link , Syntax . Image , Syntax . Comment ]
5758 } ,
58- async [ Syntax . DocumentExit ] ( node ) {
59- await indexPromise ;
60- for ( const itemGroup of usedItemGroup . values ( ) ) {
61- const items = itemGroup . usedItems ( usedSudachiSynonyms , {
62- allowAlphabet
63- } ) ;
64- if ( items . length >= 2 ) {
65- const 同義の見出しList = items . map ( item => item . midashi ) ;
66- // select last used
67- const matchSegment = locationMap . get ( items [ items . length - 1 ] ) ;
68- const index = matchSegment ? matchSegment . index : 0 ;
69- const message = `同義語である「${ 同義の見出しList . join ( "」「" ) } 」が利用されています` ;
70- report ( node , new RuleError ( message , {
71- index
72- } ) ) ;
59+ ( report ) => {
60+ return {
61+ async [ Syntax . Str ] ( node ) {
62+ const { keyItemGroupMap } = await indexPromise ;
63+ const text = getSource ( node ) ;
64+ const segments : string [ ] = segmenter . segment ( text ) ;
65+ let absoluteIndex = node . range [ 0 ] ;
66+ segments . forEach ( ( segement ) => {
67+ matchSegment ( segement , absoluteIndex , keyItemGroupMap ) ;
68+ absoluteIndex += segement . length ;
69+ } ) ;
70+ } ,
71+ async [ Syntax . DocumentExit ] ( node ) {
72+ await indexPromise ;
73+ for ( const itemGroup of usedItemGroup . values ( ) ) {
74+ const items = itemGroup . usedItems ( usedSudachiSynonyms , {
75+ allows,
76+ allowAlphabet
77+ } ) ;
78+ if ( items . length >= 2 ) {
79+ const 同義の見出しList = items . map ( item => item . midashi ) ;
80+ // select last used
81+ const matchSegment = locationMap . get ( items [ items . length - 1 ] ) ;
82+ const index = matchSegment ? matchSegment . index : 0 ;
83+ const message = `同義語である「${ 同義の見出しList . join ( "」「" ) } 」が利用されています` ;
84+ report ( node , new RuleError ( message , {
85+ index
86+ } ) ) ;
87+ }
88+ }
7389 }
74- }
90+ } ;
7591 }
76- } ;
92+ ) ;
7793} ;
7894
7995export default report ;
0 commit comments