From 4c92a413a37300018da662858ff32c627d1729e1 Mon Sep 17 00:00:00 2001 From: michael faith Date: Sat, 11 Jan 2025 14:49:44 -0600 Subject: [PATCH] fix(eslint-plugin-react-compiler): support v9 context api 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. --- .../src/rules/ReactCompilerRule.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts b/compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts index b9a1ffa440c4..3f778deee47b 100644 --- a/compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts +++ b/compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts @@ -121,7 +121,7 @@ const rule: Rule.RuleModule = { }, create(context: Rule.RuleContext) { // Compat with older versions of eslint - const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text; + const sourceCode = context.sourceCode ?? context.getSourceCode(); const filename = context.filename ?? context.getFilename(); const userOpts = context.options[0] ?? {}; if ( @@ -180,7 +180,7 @@ const rule: Rule.RuleModule = { endLoc = { line: event.fnLoc.start.line, // Babel loc line numbers are 1-indexed - column: sourceCode.split( + column: sourceCode.text.split( /\r?\n|\r|\n/g, event.fnLoc.start.line, )[event.fnLoc.start.line - 1].length, @@ -234,7 +234,6 @@ const rule: Rule.RuleModule = { nodeLoc: BabelSourceLocation, suppression: string, ): boolean { - const sourceCode = context.getSourceCode(); const comments = sourceCode.getAllComments(); const flowSuppressionRegex = new RegExp( '\\$FlowFixMe\\[' + suppression + '\\]', @@ -254,7 +253,7 @@ const rule: Rule.RuleModule = { if (filename.endsWith('.tsx') || filename.endsWith('.ts')) { try { const {parse: babelParse} = require('@babel/parser'); - babelAST = babelParse(sourceCode, { + babelAST = babelParse(sourceCode.text, { filename, sourceType: 'unambiguous', plugins: ['typescript', 'jsx'], @@ -264,7 +263,7 @@ const rule: Rule.RuleModule = { } } else { try { - babelAST = HermesParser.parse(sourceCode, { + babelAST = HermesParser.parse(sourceCode.text, { babel: true, enableExperimentalComponentSyntax: true, sourceFilename: filename, @@ -277,7 +276,7 @@ const rule: Rule.RuleModule = { if (babelAST != null) { try { - transformFromAstSync(babelAST, sourceCode, { + transformFromAstSync(babelAST, sourceCode.text, { filename, highlightCode: false, retainLines: true,