11import { getFixes } from './fix.js' ;
2+ import { setupSourceCodeForFile , SourceCode } from './source_code.js' ;
23
34import type { Fix , FixFn } from './fix.ts' ;
45import type { Node } from './types.ts' ;
@@ -32,11 +33,13 @@ export const diagnostics: DiagnosticReport[] = [];
3233 * @param context - `Context` object
3334 * @param ruleIndex - Index of this rule within `ruleIds` passed from Rust
3435 * @param filePath - Absolute path of file being linted
36+ * @param sourceText - Source text of file being linted
3537 */
3638export let setupContextForFile : (
3739 context : Context ,
3840 ruleIndex : number ,
3941 filePath : string ,
42+ sourceText : string ,
4043) => void ;
4144
4245/**
@@ -65,6 +68,10 @@ export interface InternalContext {
6568 ruleIndex : number ;
6669 // Absolute path of file being linted
6770 filePath : string ;
71+ // `SourceCode` class instance for this rule.
72+ // Rule has single `SourceCode` instance that is updated for each file
73+ // (NOT new `SourceCode` instance for each file).
74+ sourceCode : SourceCode ;
6875 // Options
6976 options : unknown [ ] ;
7077 // `true` if rule can provide fixes (`meta.fixable` in `RuleMeta` is 'code' or 'whitespace')
@@ -89,6 +96,7 @@ export class Context {
8996 this . #internal = {
9097 id : fullRuleName ,
9198 filePath : '' ,
99+ sourceCode : new SourceCode ( ) ,
92100 ruleIndex : - 1 ,
93101 options : [ ] ,
94102 isFixable,
@@ -116,6 +124,11 @@ export class Context {
116124 return getInternal ( this , 'access `context.options`' ) . options ;
117125 }
118126
127+ // Getter for `SourceCode` for file being linted.
128+ get sourceCode ( ) {
129+ return getInternal ( this , 'access `context.sourceCode`' ) . sourceCode ;
130+ }
131+
119132 /**
120133 * Report error.
121134 * @param diagnostic - Diagnostic object
@@ -135,11 +148,12 @@ export class Context {
135148 }
136149
137150 static {
138- setupContextForFile = ( context , ruleIndex , filePath ) => {
151+ setupContextForFile = ( context , ruleIndex , filePath , sourceText ) => {
139152 // TODO: Support `options`
140153 const internal = context . #internal;
141154 internal . ruleIndex = ruleIndex ;
142155 internal . filePath = filePath ;
156+ setupSourceCodeForFile ( internal . sourceCode , sourceText ) ;
143157 } ;
144158
145159 getInternal = ( context , actionDescription ) => {
0 commit comments