11import type { TextlintRuleModule } from "@textlint/types" ;
22import { matchPatterns } from "@textlint/regexp-string-matcher" ;
3+ import { StringSource } from "textlint-util-to-string" ;
34
45/**
56 * テクニカルライティングガイドラインに基づく文書品質改善ルール
@@ -23,7 +24,7 @@ export interface Options {
2324}
2425
2526const rule : TextlintRuleModule < Options > = ( context , options = { } ) => {
26- const { Syntax, report, getSource , locator } = context ;
27+ const { Syntax, report, locator } = context ;
2728 const allows = options . allows ?? [ ] ;
2829 const disableRedundancyGuidance = options . disableRedundancyGuidance ?? false ;
2930 const disableVoiceGuidance = options . disableVoiceGuidance ?? false ;
@@ -188,8 +189,8 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
188189 [ Syntax . Document ] ( node ) {
189190 // 文書全体の分析を実行(enableDocumentAnalysisがtrueの場合)
190191 if ( enableDocumentAnalysis ) {
191- const documentText = getSource ( node ) ;
192- const totalIssues = analyzeDocumentQuality ( documentText ) ;
192+ const source = new StringSource ( node ) ;
193+ const totalIssues = analyzeDocumentQuality ( source . toString ( ) ) ;
193194
194195 if ( totalIssues > 0 ) {
195196 report ( node , {
@@ -199,7 +200,8 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
199200 }
200201 } ,
201202 [ Syntax . Paragraph ] ( node ) {
202- const text = getSource ( node ) ;
203+ const source = new StringSource ( node ) ;
204+ const text = source . toString ( ) ;
203205
204206 // 許可パターンのチェック
205207 if ( allows . length > 0 ) {
@@ -222,15 +224,19 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
222224 const matches = text . matchAll ( pattern ) ;
223225 for ( const match of matches ) {
224226 const index = match . index ?? 0 ;
225- const matchRange = [ index , index + match [ 0 ] . length ] as const ;
227+ const originalPosition = source . originalIndexFromIndex ( index ) ;
228+ const originalEndPosition = source . originalIndexFromIndex ( index + match [ 0 ] . length ) ;
226229
227230 // カテゴリ別のメトリクスを更新
228231 documentQualityMetrics [ category as keyof typeof documentQualityMetrics ] ++ ;
229232
230- report ( node , {
231- message : message ,
232- padding : locator . range ( matchRange )
233- } ) ;
233+ if ( originalPosition !== undefined && originalEndPosition !== undefined ) {
234+ const matchRange = [ originalPosition , originalEndPosition ] as const ;
235+ report ( node , {
236+ message : message ,
237+ padding : locator . range ( matchRange )
238+ } ) ;
239+ }
234240 }
235241 }
236242 }
@@ -239,15 +245,17 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
239245 // 文書全体の品質分析関数
240246 function analyzeDocumentQuality ( text : string ) : number {
241247 let totalIssues = 0 ;
242- const allPatterns = [
243- ...redundancyGuidance ,
244- ...voiceGuidance ,
245- ...clarityGuidance ,
246- ...consistencyGuidance ,
247- ...structureGuidance
248+
249+ // コードブロックを除外して分析するためのパターンリスト
250+ const guidancePatterns = [
251+ ...( disableRedundancyGuidance ? [ ] : redundancyGuidance ) ,
252+ ...( disableVoiceGuidance ? [ ] : voiceGuidance ) ,
253+ ...( disableClarityGuidance ? [ ] : clarityGuidance ) ,
254+ ...( disableConsistencyGuidance ? [ ] : consistencyGuidance ) ,
255+ ...( disableStructureGuidance ? [ ] : structureGuidance )
248256 ] ;
249257
250- for ( const { pattern } of allPatterns ) {
258+ for ( const { pattern } of guidancePatterns ) {
251259 const matches = text . matchAll ( pattern ) ;
252260 totalIssues += Array . from ( matches ) . length ;
253261 }
0 commit comments