From 495342fa96b9ca717a83ad2b5c3cb465bd4e7eca Mon Sep 17 00:00:00 2001 From: Konstantin Scheglov Date: Tue, 30 Sep 2025 18:25:35 -0700 Subject: [PATCH] Stop using deprecated analyzer 7.x.y APIs. (#176242) Preparation for landing https://dart-review.googlesource.com/c/sdk/+/443460 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../lib/src/widget_preview/dependency_graph.dart | 11 +++++------ .../src/widget_preview/preview_code_generator.dart | 12 ++++++------ .../flutter_tools/lib/src/widget_preview/utils.dart | 2 +- .../utils/preview_details_matcher.dart | 8 ++++---- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/flutter_tools/lib/src/widget_preview/dependency_graph.dart b/packages/flutter_tools/lib/src/widget_preview/dependency_graph.dart index 1dba4fa28f9..69d8950e664 100644 --- a/packages/flutter_tools/lib/src/widget_preview/dependency_graph.dart +++ b/packages/flutter_tools/lib/src/widget_preview/dependency_graph.dart @@ -11,9 +11,8 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/token.dart'; import 'package:analyzer/dart/ast/visitor.dart'; import 'package:analyzer/dart/constant/value.dart'; -import 'package:analyzer/dart/element/element2.dart'; +import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/diagnostic/diagnostic.dart'; -import 'package:analyzer/error/error.dart'; import 'package:analyzer/source/line_info.dart'; import '../base/logger.dart'; @@ -34,7 +33,7 @@ typedef PreviewDependencyGraph = Map; /// Visitor which detects previews and extracts [PreviewDetails] for later code /// generation. class _PreviewVisitor extends RecursiveAstVisitor { - _PreviewVisitor({required LibraryElement2 lib}) + _PreviewVisitor({required LibraryElement lib}) : packageName = lib.uri.scheme == 'package' ? lib.uri.pathSegments.first : null; late final String? packageName; @@ -208,7 +207,7 @@ final class LibraryPreviewNode { bool get hasErrors => errors.isNotEmpty; /// The set of errors found in this library. - final errors = []; + final errors = []; /// Determines the set of errors found in this library. /// @@ -217,8 +216,8 @@ final class LibraryPreviewNode { errors.clear(); for (final String file in files) { errors.addAll( - ((await context.currentSession.getErrors(file)) as ErrorsResult).errors - .where((AnalysisError error) => error.severity == Severity.error) + ((await context.currentSession.getErrors(file)) as ErrorsResult).diagnostics + .where((error) => error.severity == Severity.error) .toList(), ); } diff --git a/packages/flutter_tools/lib/src/widget_preview/preview_code_generator.dart b/packages/flutter_tools/lib/src/widget_preview/preview_code_generator.dart index baeacbe7633..104306a7744 100644 --- a/packages/flutter_tools/lib/src/widget_preview/preview_code_generator.dart +++ b/packages/flutter_tools/lib/src/widget_preview/preview_code_generator.dart @@ -3,8 +3,8 @@ // found in the LICENSE file. import 'package:analyzer/dart/constant/value.dart'; -import 'package:analyzer/dart/element/element2.dart' as analyzer; -import 'package:analyzer/dart/element/element2.dart'; +import 'package:analyzer/dart/element/element.dart' as analyzer; +import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:built_collection/built_collection.dart'; import 'package:code_builder/code_builder.dart' as cb; @@ -210,7 +210,7 @@ extension on DartObject { DartType(isDartCoreInt: true) => cb.literalNum(toIntValue()!), DartType(isDartCoreString: true) => cb.literalString(toStringValue()!), DartType(isDartCoreNull: true) => cb.literalNull, - InterfaceType(element3: EnumElement()) => _createEnumInstance(this), + InterfaceType(element: EnumElement()) => _createEnumInstance(this), InterfaceType() => _createInstance(type, this), FunctionType() => _createTearoff(toFunctionValue()!), _ => throw UnsupportedError('Unexpected DartObject type: $runtimeType'), @@ -242,10 +242,10 @@ extension on DartObject { final ConstructorInvocation constructorInvocation = object.constructorInvocation!; final ConstructorElement constructor = constructorInvocation.constructor; final cb.Expression type = cb.refer( - dartType.element3.name3!, - _elementToLibraryIdentifier(dartType.element3), + dartType.element.name!, + _elementToLibraryIdentifier(dartType.element), ); - final String? name = constructor.name3 == 'new' ? null : constructor.name3; + final String? name = constructor.name == 'new' ? null : constructor.name; final List positionalArguments = constructorInvocation.positionalArguments .map((e) => e.toExpression()) diff --git a/packages/flutter_tools/lib/src/widget_preview/utils.dart b/packages/flutter_tools/lib/src/widget_preview/utils.dart index e0f6747b454..6b37a8904ca 100644 --- a/packages/flutter_tools/lib/src/widget_preview/utils.dart +++ b/packages/flutter_tools/lib/src/widget_preview/utils.dart @@ -8,7 +8,7 @@ import 'dart:collection'; import 'package:analyzer/dart/analysis/results.dart'; import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/token.dart'; -import 'package:analyzer/dart/element/element2.dart'; +import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/source/source.dart'; import 'package:collection/collection.dart'; diff --git a/packages/flutter_tools/test/commands.shard/hermetic/widget_preview/utils/preview_details_matcher.dart b/packages/flutter_tools/test/commands.shard/hermetic/widget_preview/utils/preview_details_matcher.dart index 700f597aae9..c82329b7c40 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/widget_preview/utils/preview_details_matcher.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/widget_preview/utils/preview_details_matcher.dart @@ -104,24 +104,24 @@ class PreviewMatcher extends Matcher { ); checkPropertyMatch( name: kWrapper, - actual: item.getFirstField(kWrapper)?.toFunctionValue2()?.displayName, + actual: item.getFirstField(kWrapper)?.toFunctionValue()?.displayName, expected: wrapper, ); checkPropertyMatch( name: kTheme, - actual: item.getFirstField(kTheme)?.toFunctionValue2()?.displayName, + actual: item.getFirstField(kTheme)?.toFunctionValue()?.displayName, expected: theme, ); final DartObject? actualBrightness = item.getFirstField(kBrightness); checkPropertyMatch( name: kBrightness, - actual: (actualBrightness?.isNull ?? true) ? null : actualBrightness!.variable2!.toString(), + actual: (actualBrightness?.isNull ?? true) ? null : actualBrightness!.variable!.toString(), expected: brightness, ); checkPropertyMatch( name: kLocalizations, - actual: item.getFirstField(kLocalizations)?.toFunctionValue2()?.displayName, + actual: item.getFirstField(kLocalizations)?.toFunctionValue()?.displayName, expected: localizations, ); return matches;