@@ -12,11 +12,11 @@ function countTen(text) {
1212 */
1313export default function ( context , options = { } ) {
1414 options = ObjectAssign ( { } , defaultOptions , options ) ;
15- var maxLen = options . max ;
15+ const maxLen = options . max ;
1616 const punctuation = / [ 。 ] / ;
1717 let helper = new RuleHelper ( context ) ;
1818 let { Syntax, RuleError, report, getSource} = context ;
19- var currentParagraphTexts = [ ] ;
19+ let currentParagraphTexts = [ ] ;
2020 return {
2121 [ Syntax . Paragraph ] ( ) {
2222 currentParagraphTexts = [ ]
@@ -28,16 +28,37 @@ export default function (context, options = {}) {
2828 }
2929 currentParagraphTexts . push ( node ) ;
3030 } ,
31- [ Syntax . Paragraph + ":exit" ] ( ) {
32- currentParagraphTexts . forEach ( node => {
33- var currentParagraphText = getSource ( node ) ;
34- var sentences = currentParagraphText . split ( punctuation ) ;
35- sentences . forEach ( sentence => {
36- if ( countTen ( sentence ) >= maxLen ) {
37- var ruleError = new context . RuleError ( `一つの文で"、"を${ maxLen } つ以上使用しています` ) ;
38- report ( node , ruleError ) ;
39- }
40- } ) ;
31+ [ Syntax . Paragraph + ":exit" ] ( pNode ) {
32+ let currentTenCount = 0 ;
33+ let text = currentParagraphTexts . map ( strNode => getSource ( strNode ) ) . join ( "\n" ) ;
34+ let characters = text . split ( "" ) ; // ["t","e","x","t"]
35+
36+ let paddingLine = 0 ;
37+ let paddingColumn = 0 ;
38+ characters . forEach ( char => {
39+ if ( char === "、" ) {
40+ currentTenCount ++ ;
41+ }
42+ if ( char === "。" ) {
43+ // reset
44+ currentTenCount = 0 ;
45+ }
46+ // report
47+ if ( currentTenCount >= maxLen ) {
48+ var ruleError = new context . RuleError ( `一つの文で"、"を${ maxLen } つ以上使用しています` , {
49+ line : paddingLine ,
50+ column : paddingColumn
51+ } ) ;
52+ report ( pNode , ruleError ) ;
53+ currentTenCount = 0 ;
54+ }
55+ // calc padding{line,column}
56+ if ( char === "\n" ) {
57+ paddingLine ++ ;
58+ paddingColumn = 0 ;
59+ } else {
60+ paddingColumn ++ ;
61+ }
4162 } ) ;
4263 }
4364 }
0 commit comments