fix(eslint-plugin-react-compiler): support v9 context api (#32045)
## Summary This change fixes a gap in the plugin's support of eslint v9. In one place that it's using the `SourceCode` api, it's correctly considering v9's api. But in the other place where `SourceCode` is used, it's only using the legacy api, which was removed in v9.
michael faith committed
Jan 13, 2025 at 12:49 UTC
b3a95caf61bc716fb618997e6e9f3a0c8c9c8374
1 file changed
+5
-6
compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts
+5
-6
@@ -121,7 +121,7 @@ const rule: Rule.RuleModule = {
121
},
122
create(context: Rule.RuleContext) {
123
// Compat with older versions of eslint
124
- const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text;
124
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
125
const filename = context.filename ?? context.getFilename();
126
const userOpts = context.options[0] ?? {};
127
if (
@@ -180,7 +180,7 @@ const rule: Rule.RuleModule = {
180
endLoc = {
181
line: event.fnLoc.start.line,
182
// Babel loc line numbers are 1-indexed
183
- column: sourceCode.split(
183
+ column: sourceCode.text.split(
184
/\r?\n|\r|\n/g,
185
event.fnLoc.start.line,
186
)[event.fnLoc.start.line - 1].length,
@@ -234,7 +234,6 @@ const rule: Rule.RuleModule = {
234
nodeLoc: BabelSourceLocation,
235
suppression: string,
236
): boolean {
237
- const sourceCode = context.getSourceCode();
237
const comments = sourceCode.getAllComments();
238
const flowSuppressionRegex = new RegExp(
239
'\\$FlowFixMe\\[' + suppression + '\\]',
@@ -254,7 +253,7 @@ const rule: Rule.RuleModule = {
253
if (filename.endsWith('.tsx') || filename.endsWith('.ts')) {
254
try {
255
const {parse: babelParse} = require('@babel/parser');
257
- babelAST = babelParse(sourceCode, {
256
+ babelAST = babelParse(sourceCode.text, {
257
filename,
258
sourceType: 'unambiguous',
259
plugins: ['typescript', 'jsx'],
@@ -264,7 +263,7 @@ const rule: Rule.RuleModule = {
263
}
264
} else {
265
try {
267
- babelAST = HermesParser.parse(sourceCode, {
266
+ babelAST = HermesParser.parse(sourceCode.text, {
267
babel: true,
268
enableExperimentalComponentSyntax: true,
269
sourceFilename: filename,
@@ -277,7 +276,7 @@ const rule: Rule.RuleModule = {
276
277
if (babelAST != null) {
278
try {
280
- transformFromAstSync(babelAST, sourceCode, {
279
+ transformFromAstSync(babelAST, sourceCode.text, {
280
filename,
281
highlightCode: false,
282
retainLines: true,