@@ -13,6 +13,12 @@ export interface Options {
1313 * 例) ["ウェブアプリ", "ウェブアプリケーション"]
1414 */
1515 allows ?: string [ ] ;
16+ /**
17+ * 使用を許可する見出し語の配列
18+ * 定義された見出し語以外の同義語をエラーにします
19+ * 例) ["ユーザー"] // => 「ユーザー」だけ許可し「ユーザ」などはエラーにする
20+ */
21+ preferWords ?: string [ ] ;
1622 /**
1723 * 同じ語形の語の中でのアルファベットの表記揺れを許可するかどうか
1824 * trueの場合はカタカナとアルファベットの表記ゆれを許可します
@@ -32,6 +38,7 @@ export interface Options {
3238
3339export const DefaultOptions : Required < Options > = {
3440 allows : [ ] ,
41+ preferWords : [ ] ,
3542 allowAlphabet : true ,
3643 allowNumber : true
3744} ;
@@ -40,7 +47,8 @@ const report: TextlintRuleReporter<Options> = (context, options = {}) => {
4047 const allowAlphabet = options . allowAlphabet ?? DefaultOptions . allowAlphabet ;
4148 const allowNumber = options . allowNumber ?? DefaultOptions . allowNumber ;
4249 const allows = options . allows !== undefined ? options . allows : DefaultOptions . allows ;
43- const { Syntax, getSource, RuleError } = context ;
50+ const preferWords = options . preferWords !== undefined ? options . preferWords : DefaultOptions . preferWords ;
51+ const { Syntax, getSource, RuleError, fixer } = context ;
4452 const usedSudachiSynonyms : Set < SudachiSynonyms > = new Set ( ) ;
4553 const locationMap : Map < SudachiSynonyms , { index : number } > = new Map ( ) ;
4654 const usedItemGroup : Set < ItemGroup > = new Set ( ) ;
@@ -87,7 +95,19 @@ const report: TextlintRuleReporter<Options> = (context, options = {}) => {
8795 allowAlphabet,
8896 allowNumber
8997 } ) ;
90- if ( items . length >= 2 ) {
98+ const preferWord = preferWords . find ( midashi => itemGroup . getItem ( midashi ) )
99+ if ( preferWord ) {
100+ const deniedItems = items . filter ( item => item . midashi !== preferWord )
101+ for ( const item of deniedItems ) {
102+ const index = locationMap . get ( item ) ?. index ?? 0 ;
103+ const deniedWord = item . midashi
104+ const message = `「${ preferWord } 」の同義語である「${ deniedWord } 」が利用されています` ;
105+ report ( node , new RuleError ( message , {
106+ index,
107+ fix : fixer . replaceTextRange ( [ index , index + deniedWord . length ] , preferWord ) ,
108+ } ) ) ;
109+ }
110+ } else if ( items . length >= 2 ) {
91111 const 同義の見出しList = items . map ( item => item . midashi ) ;
92112 // select last used
93113 const matchSegment = locationMap . get ( items [ items . length - 1 ] ) ;
@@ -104,4 +124,7 @@ const report: TextlintRuleReporter<Options> = (context, options = {}) => {
104124 ) ;
105125} ;
106126
107- export default report ;
127+ export default {
128+ linter : report ,
129+ fixer : report ,
130+ } ;
0 commit comments