11// LICENSE : MIT
22"use strict" ;
33const RuleHelper = require ( "textlint-rule-helper" ) . RuleHelper ;
4+ const emojiRegExp = require ( "emoji-regex" ) ( ) ;
45const japaneseRegExp = / (?: [ 々 〇 〻 \u3400 - \u4DBF \u4E00 - \u9FFF \uF900 - \uFAFF ] | [ \uD840 - \uD87F ] [ \uDC00 - \uDFFF ] | [ ぁ - ん ァ - ヶ ] ) / ;
56const exceptionMarkRegExp = / [ ! ? ! ? \) ) 」 』 ] / ;
67const defaultPeriodMark = / [ 。 \. ] / ;
78const defaultOptions = {
89 // 優先する句点文字
9- periodMark : "。"
10+ periodMark : "。" ,
11+ // 末尾に絵文字を置くことを許可するか
12+ allowEmojiAtEnd : false
1013} ;
1114const reporter = ( context , options = { } ) => {
1215 const { Syntax, RuleError, report, fixer, getSource} = context ;
1316 const helper = new RuleHelper ( context ) ;
1417 const periodMark = options . periodMark || defaultOptions . periodMark ;
18+ const allowEmojiAtEnd = options . allowEmojiAtEnd !== undefined ? options . allowEmojiAtEnd : defaultOptions . allowEmojiAtEnd ;
1519 const ignoredNodeTypes = [ Syntax . ListItem , Syntax . Link , Syntax . Code , Syntax . Image , Syntax . BlockQuote , Syntax . Emphasis ] ;
1620 return {
1721 [ Syntax . Paragraph ] ( node ) {
@@ -27,8 +31,10 @@ const reporter = (context, options = {}) => {
2731 if ( ! japaneseRegExp . test ( lastStrText ) ) {
2832 return ;
2933 }
30- const lastIndex = lastStrText . length - 1 ;
31- const lastChar = lastStrText [ lastIndex ] ;
34+ // サロゲートペアを考慮した文字列長・文字アクセス
35+ const characters = [ ...lastStrText ] ;
36+ const lastIndex = characters . length - 1 ;
37+ const lastChar = characters [ lastIndex ] ;
3238 if ( lastChar === undefined ) {
3339 return ;
3440 }
@@ -48,6 +54,9 @@ const reporter = (context, options = {}) => {
4854 if ( exceptionMarkRegExp . test ( lastChar ) ) {
4955 return ;
5056 }
57+ if ( allowEmojiAtEnd && emojiRegExp . test ( lastChar ) ) {
58+ return ;
59+ }
5160 if ( lastChar === periodMark ) {
5261 return ;
5362 }
@@ -71,4 +80,4 @@ const reporter = (context, options = {}) => {
7180module . exports = {
7281 linter : reporter ,
7382 fixer : reporter
74- } ;
83+ } ;
0 commit comments