Skip to content

Commit

Permalink
Version 3.6.0-111.0.dev
Browse files Browse the repository at this point in the history
Merge 3b09a49 into dev
  • Loading branch information
Dart CI committed Aug 2, 2024
2 parents 3fb8298 + 3b09a49 commit ff56a84
Show file tree
Hide file tree
Showing 8 changed files with 72,339 additions and 70,146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/extensions.dart';
import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/dart/element/type_algebra.dart';
import 'package:analyzer/src/dart/resolver/applicable_extensions.dart';
Expand All @@ -25,10 +26,6 @@ import 'package:analyzer/src/workspace/pub.dart';
/// A helper class that produces candidate suggestions for all of the
/// declarations that are in scope at the completion location.
class DeclarationHelper {
/// The regular expression used to detect an unused identifier (a sequence of
/// one or more underscores with no other characters).
static final RegExp UnusedIdentifier = RegExp(r'^_+$');

/// The completion request being processed.
final DartCompletionRequest request;

Expand Down Expand Up @@ -318,6 +315,10 @@ class DeclarationHelper {
continue;
}

if (prefixElement.isWildcardVariable) {
continue;
}

var importedLibrary = element.importedLibrary;
if (importedLibrary == null) {
continue;
Expand Down Expand Up @@ -661,6 +662,9 @@ class DeclarationHelper {
required Namespace namespace,
required String? prefix,
}) {
// Don't suggest declarations in wildcard prefixed namespaces.
if (_isWildcard(prefix)) return;

var importData = ImportData(
libraryUri: library.source.uri,
prefix: prefix,
Expand Down Expand Up @@ -1384,9 +1388,8 @@ class DeclarationHelper {
return true;
}

/// Returns `true` if the [identifier] is composed of one or more underscore
/// characters and nothing else.
bool _isUnused(String identifier) => UnusedIdentifier.hasMatch(identifier);
/// Returns `true` if the [identifier] is a wildcard (a single `_`).
bool _isWildcard(String? identifier) => identifier == '_';

/// Record that the given [operation] should be performed in the second pass.
void _recordOperation(NotImportedOperation operation) {
Expand Down Expand Up @@ -1622,6 +1625,8 @@ class DeclarationHelper {
(mustBeNonVoid && element.returnType is VoidType)) {
return;
}
// Don't suggest wildcard local functions.
if (_isWildcard(element.name)) return;
var matcherScore = state.matcher.score(element.displayName);
if (matcherScore != -1) {
var suggestion = LocalFunctionSuggestion(
Expand Down Expand Up @@ -1695,7 +1700,7 @@ class DeclarationHelper {
/// Adds a suggestion for the parameter represented by the [element].
void _suggestParameter(ParameterElement element) {
if (visibilityTracker.isVisible(element: element, importData: null)) {
if (mustBeConstant || _isUnused(element.name)) {
if (mustBeConstant || _isWildcard(element.name)) {
return;
}
var matcherScore = state.matcher.score(element.displayName);
Expand Down Expand Up @@ -1918,6 +1923,7 @@ class DeclarationHelper {

/// Adds a suggestion for the local variable represented by the [element].
void _suggestVariable(LocalVariableElement element) {
if (element.isWildcardVariable) return;
if (visibilityTracker.isVisible(element: element, importData: null)) {
if (mustBeConstant && !element.isConst) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import '../../../../client/completion_driver_test.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(WildcardFieldTest);
defineReflectiveTests(WildcardForLoopTest);
defineReflectiveTests(WildcardImportPrefixTest);
defineReflectiveTests(WildcardLocalVariableTest);
defineReflectiveTests(WildcardParameterTest);
Expand Down Expand Up @@ -42,7 +43,6 @@ suggestions
''');
}

@FailingTest(reason: "the local '_' is shadowing the field")
Future<void> test_argumentList_withLocal() async {
await computeSuggestions('''
void p(Object o) {}
Expand All @@ -62,15 +62,87 @@ suggestions
}
}

@reflectiveTest
class WildcardForLoopTest extends AbstractCompletionDriverTest {
@override
Set<String> allowedIdentifiers = {'_', '__'};

@override
bool get includeKeywords => false;

Future<void> test_forEach_argumentList() async {
await computeSuggestions('''
void p(Object o) {}
void f() {
for (var _ in []) {
p(^);
}
}
''');
assertResponse('''
suggestions
''');
}

Future<void> test_forEach_argumentList_underscores() async {
await computeSuggestions('''
void p(Object o) {}
void f() {
for (var __ in []) {
p(^);
}
}
''');
assertResponse('''
suggestions
__
kind: localVariable
''');
}

Future<void> test_forParts_argumentList() async {
await computeSuggestions('''
void p(Object o) {}
void f() {
for (var _ = 0; ;) {
p(^);
}
}
''');
assertResponse('''
suggestions
''');
}

Future<void> test_forParts_argumentList_underscores() async {
await computeSuggestions('''
void p(Object o) {}
void f() {
for (var __ = 0; ;) {
p(^);
}
}
''');
assertResponse('''
suggestions
__
kind: localVariable
''');
}
}

@reflectiveTest
class WildcardImportPrefixTest extends AbstractCompletionDriverTest {
@override
Set<String> allowedIdentifiers = {'_', 'isBlank'};
Set<String> allowedIdentifiers = {'_', '__', 'isBlank'};

@override
bool get includeKeywords => false;

@FailingTest(reason: "'_' shouldn't be suggested")
Future<void> test_argumentList() async {
newFile('$testPackageLibPath/ext.dart', '''
extension ES on String {
Expand All @@ -93,6 +165,31 @@ suggestions
''');
}

Future<void> test_argumentList_underscores() async {
newFile('$testPackageLibPath/ext.dart', '''
extension ES on String {
bool get isBlank => false;
}
''');

await computeSuggestions('''
import 'ext.dart' as __;
void p(Object o) {}
void f() {
p(^);
}
''');
assertResponse('''
suggestions
__.ES
kind: extensionInvocation
__
kind: library
''');
}

Future<void> test_stringExtension_argumentList() async {
newFile('$testPackageLibPath/ext.dart', '''
extension ES on String {
Expand Down Expand Up @@ -120,12 +217,11 @@ suggestions
@reflectiveTest
class WildcardLocalVariableTest extends AbstractCompletionDriverTest {
@override
Set<String> allowedIdentifiers = {'_', 'b'};
Set<String> allowedIdentifiers = {'_', '__', 'b'};

@override
bool get includeKeywords => false;

@FailingTest(reason: "'_' shouldn't be suggested")
Future<void> test_argumentList() async {
await computeSuggestions('''
void p(Object o) {}
Expand All @@ -136,17 +232,47 @@ void f() {
}
''');
assertResponse(r'''
suggestions
suggestions
b
kind: localVariable
''');
}

Future<void> test_argumentList_function() async {
await computeSuggestions('''
void p(Object o) {}
void f() {
_() {}
p(^);
}
''');
assertResponse(r'''
suggestions
''');
}

Future<void> test_argumentList_underscores() async {
await computeSuggestions('''
void p(Object o) {}
void f() {
var __ = 0;
p(^);
}
''');
assertResponse(r'''
suggestions
__
kind: localVariable
''');
}
}

@reflectiveTest
class WildcardParameterTest extends AbstractCompletionDriverTest {
@override
Set<String> allowedIdentifiers = {'_', 'b'};
Set<String> allowedIdentifiers = {'_', '__', 'b'};

@override
bool get includeKeywords => false;
Expand All @@ -163,6 +289,21 @@ void f(int _, int b) {
suggestions
b
kind: parameter
''');
}

Future<void> test_argumentList_underscores() async {
await computeSuggestions('''
void p(Object o) {}
void f(int __) {
p(^);
}
''');
assertResponse('''
suggestions
__
kind: parameter
''');
}
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4876,11 +4876,20 @@ class LibraryElementImpl extends LibraryOrAugmentationElementImpl
}

List<CompilationUnitElementImpl> get _partUnits {
return parts
.map((e) => e.uri)
.whereType<DirectiveUriWithUnitImpl>()
.map((e) => e.unit)
.toList();
var result = <CompilationUnitElementImpl>[];

void visitParts(CompilationUnitElementImpl unit) {
for (var part in unit.parts) {
if (part.uri case DirectiveUriWithUnitImpl uri) {
var unit = uri.unit;
result.add(unit);
visitParts(unit);
}
}
}

visitParts(definingCompilationUnit);
return result;
}

@override
Expand Down
Loading

0 comments on commit ff56a84

Please sign in to comment.