1
1
import type { TextlintRuleModule } from "@textlint/types" ;
2
2
import { matchPatterns } from "@textlint/regexp-string-matcher" ;
3
+ import { StringSource } from "textlint-util-to-string" ;
3
4
4
5
/**
5
6
* テクニカルライティングガイドラインに基づく文書品質改善ルール
@@ -23,7 +24,7 @@ export interface Options {
23
24
}
24
25
25
26
const rule : TextlintRuleModule < Options > = ( context , options = { } ) => {
26
- const { Syntax, report, getSource , locator } = context ;
27
+ const { Syntax, report, locator } = context ;
27
28
const allows = options . allows ?? [ ] ;
28
29
const disableRedundancyGuidance = options . disableRedundancyGuidance ?? false ;
29
30
const disableVoiceGuidance = options . disableVoiceGuidance ?? false ;
@@ -188,8 +189,8 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
188
189
[ Syntax . Document ] ( node ) {
189
190
// 文書全体の分析を実行(enableDocumentAnalysisがtrueの場合)
190
191
if ( enableDocumentAnalysis ) {
191
- const documentText = getSource ( node ) ;
192
- const totalIssues = analyzeDocumentQuality ( documentText ) ;
192
+ const source = new StringSource ( node ) ;
193
+ const totalIssues = analyzeDocumentQuality ( source . toString ( ) ) ;
193
194
194
195
if ( totalIssues > 0 ) {
195
196
report ( node , {
@@ -199,7 +200,8 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
199
200
}
200
201
} ,
201
202
[ Syntax . Paragraph ] ( node ) {
202
- const text = getSource ( node ) ;
203
+ const source = new StringSource ( node ) ;
204
+ const text = source . toString ( ) ;
203
205
204
206
// 許可パターンのチェック
205
207
if ( allows . length > 0 ) {
@@ -222,15 +224,19 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
222
224
const matches = text . matchAll ( pattern ) ;
223
225
for ( const match of matches ) {
224
226
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 ) ;
226
229
227
230
// カテゴリ別のメトリクスを更新
228
231
documentQualityMetrics [ category as keyof typeof documentQualityMetrics ] ++ ;
229
232
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
+ }
234
240
}
235
241
}
236
242
}
@@ -239,15 +245,17 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
239
245
// 文書全体の品質分析関数
240
246
function analyzeDocumentQuality ( text : string ) : number {
241
247
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 )
248
256
] ;
249
257
250
- for ( const { pattern } of allPatterns ) {
258
+ for ( const { pattern } of guidancePatterns ) {
251
259
const matches = text . matchAll ( pattern ) ;
252
260
totalIssues += Array . from ( matches ) . length ;
253
261
}
0 commit comments