Skip to content

Commit

Permalink
[element model] migrate scope
Browse files Browse the repository at this point in the history
Bug: https://github.com/dart-lang/linter/issues/5099
Change-Id: I85deb22dbe24f024ea69643dc4a3b30348a7fbad
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394880
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Nov 13, 2024
1 parent 99315cb commit d3de4d9
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
3 changes: 3 additions & 0 deletions pkg/analyzer/lib/dart/element/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';

/// Scopes are used to resolve names to elements.
///
Expand All @@ -21,5 +22,7 @@ abstract class Scope {
/// Clients may not extend, implement or mix-in this class.
abstract class ScopeLookupResult {
Element? get getter;
Element2? get getter2;
Element? get setter;
Element2? get setter2;
}
7 changes: 7 additions & 0 deletions pkg/analyzer/lib/src/dart/element/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import 'package:_fe_analyzer_shared/src/scanner/string_canonicalizer.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/scope.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/extensions.dart';
import 'package:analyzer/src/summary2/combinator.dart';
import 'package:analyzer/src/utilities/extensions/collection.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';

/// The scope for the initializers in a constructor.
class ConstructorInitializerScope extends EnclosedScope {
Expand Down Expand Up @@ -774,6 +776,11 @@ class ScopeLookupResultImpl implements ScopeLookupResult {
required this.getter,
required this.setter,
});

@override
Element2? get getter2 => getter?.asElement2;
@override
Element2? get setter2 => setter?.asElement2;
}

class TypeParameterScope extends EnclosedScope {
Expand Down
2 changes: 0 additions & 2 deletions pkg/linter/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
lib/src/ast.dart
lib/src/extensions.dart
lib/src/rules/avoid_renaming_method_parameters.dart
lib/src/rules/avoid_setters_without_getters.dart
lib/src/rules/avoid_void_async.dart
lib/src/rules/deprecated_member_use_from_same_package.dart
Expand All @@ -14,5 +13,4 @@ lib/src/rules/use_build_context_synchronously.dart
lib/src/rules/use_late_for_private_fields_and_variables.dart
lib/src/util/dart_type_utilities.dart
lib/src/util/flutter_utils.dart
lib/src/util/scope.dart
test/rules/use_build_context_synchronously_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class _Visitor extends SimpleAstVisitor<void> {
var result =
resolveNameInScope(name.lexeme, scope, shouldResolveSetter: false);
if (result.isRequestedName) {
var element = result.element2;
var element = result.element;
return element is ClassElement2 ||
element is ExtensionTypeElement2 ||
element is TypeAliasElement2 ||
Expand Down
2 changes: 1 addition & 1 deletion pkg/linter/lib/src/rules/unnecessary_this.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class _Visitor extends SimpleAstVisitor<void> {
// The requested element is inherited, or from an extension.
if (result.isNone) return true;

var resultElement = result.element2;
var resultElement = result.element;

// The result has the matching name, might be shadowing.
// Check that the element is the same.
Expand Down
14 changes: 4 additions & 10 deletions pkg/linter/lib/src/util/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/scope.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/ast/ast.dart';
// ignore: implementation_imports
import 'package:analyzer/src/generated/resolver.dart' show ScopeResolverVisitor;
// ignore: implementation_imports
import 'package:analyzer/src/utilities/extensions/element.dart';

LinterNameInScopeResolutionResult resolveNameInScope(
String id,
Expand All @@ -26,9 +23,9 @@ LinterNameInScopeResolutionResult resolveNameInScope(
}

if (scope != null) {
var ScopeLookupResult(:setter, :getter) = scope.lookup(id);
var requestedElement = shouldResolveSetter ? setter : getter;
var differentElement = shouldResolveSetter ? getter : setter;
var ScopeLookupResult(:setter2, :getter2) = scope.lookup(id);
var requestedElement = shouldResolveSetter ? setter2 : getter2;
var differentElement = shouldResolveSetter ? getter2 : setter2;

if (requestedElement != null) {
return LinterNameInScopeResolutionResult._requestedName(requestedElement);
Expand All @@ -45,7 +42,7 @@ LinterNameInScopeResolutionResult resolveNameInScope(
/// The result of resolving of a basename `id` in a scope.
class LinterNameInScopeResolutionResult {
/// The element with the requested basename, `null` is [isNone].
final Element? element;
final Element2? element;

/// The state of the result.
final _LinterNameInScopeResolutionResultState _state;
Expand All @@ -60,9 +57,6 @@ class LinterNameInScopeResolutionResult {
const LinterNameInScopeResolutionResult._requestedName(this.element)
: _state = _LinterNameInScopeResolutionResultState.requestedName;

/// The element with the requested basename, `null` is [isNone].
Element2? get element2 => element.asElement2;

bool get isDifferentName =>
_state == _LinterNameInScopeResolutionResultState.differentName;

Expand Down
4 changes: 2 additions & 2 deletions pkg/linter/test/scope_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ mixin A<T> {
void _resultDifferent(
AstNode node, String id, bool setter, Element2 element) {
var result = resolveNameInScope(id, node, shouldResolveSetter: setter);
if (!result.isDifferentName || result.element2 != element) {
if (!result.isDifferentName || result.element != element) {
fail('Expected different $element, actual: $result');
}
}
Expand All @@ -900,7 +900,7 @@ mixin A<T> {
void _resultRequested(
AstNode node, String id, bool setter, Element2 element) {
var result = resolveNameInScope(id, node, shouldResolveSetter: setter);
if (!result.isRequestedName || result.element2 != element) {
if (!result.isRequestedName || result.element != element) {
fail('Expected requested $element, actual: $result');
}
}
Expand Down

0 comments on commit d3de4d9

Please sign in to comment.