Skip to content

Commit 16033f9

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Enable prefer_contains in analysis_server
Change-Id: Idac9828eb452f685452a33e6946ece2ba0d197f4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135583 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
1 parent 1cc9413 commit 16033f9

9 files changed

Lines changed: 66 additions & 69 deletions

File tree

pkg/analysis_server/analysis_options.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ analyzer:
99
errors:
1010
# Increase the severity of the unused_import hint.
1111
unused_import: warning
12-
prefer_contains: ignore
1312
# TODO(srawlins): At the time of writing, 2400 violations in lib/. The fix
1413
# is mechanical, via `dartfmt --fix-doc-comments`, but not worth the churn
1514
# today.

pkg/analysis_server/benchmark/integration/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ PerfArgs parseArgs(List<String> rawArgs) {
154154
for (String pair in args[MAP_OPTION]) {
155155
if (pair is String) {
156156
int index = pair.indexOf(',');
157-
if (index != -1 && pair.indexOf(',', index + 1) == -1) {
157+
if (index != -1 && !pair.contains(',', index + 1)) {
158158
String oldSrcPrefix = _withTrailingSeparator(pair.substring(0, index));
159159
String newSrcPrefix = _withTrailingSeparator(pair.substring(index + 1));
160160
if (Directory(newSrcPrefix).existsSync()) {

pkg/analysis_server/lib/src/edit/preview/http_preview_server.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ class HttpPreviewServer {
7474
List<String> updateValues = request.headers[HttpHeaders.upgradeHeader];
7575
if (request.method == 'GET') {
7676
await _handleGetRequest(request);
77-
} else if (updateValues != null &&
78-
updateValues.indexOf('websocket') >= 0) {
77+
} else if (updateValues != null && updateValues.contains('websocket')) {
7978
// We do not support serving analysis server communications over
8079
// WebSocket connections.
8180
HttpResponse response = request.response;

pkg/analysis_server/lib/src/server/http_server.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ class HttpAnalysisServer {
131131
List<String> updateValues = request.headers[HttpHeaders.upgradeHeader];
132132
if (request.method == 'GET') {
133133
await _handleGetRequest(request);
134-
} else if (updateValues != null &&
135-
updateValues.indexOf('websocket') >= 0) {
134+
} else if (updateValues != null && updateValues.contains('websocket')) {
136135
// We no longer support serving analysis server communications over
137136
// WebSocket connections.
138137
HttpResponse response = request.response;

pkg/analysis_server/lib/src/services/completion/dart/language_model.dart

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:io';
65
import 'dart:convert';
6+
import 'dart:io';
77
import 'dart:typed_data';
88

99
import 'package:path/path.dart' as path;
@@ -22,12 +22,6 @@ class LanguageModel {
2222
final Map<int, String> _idx2word;
2323
final int _lookback;
2424

25-
LanguageModel._(
26-
this._interpreter, this._word2idx, this._idx2word, this._lookback);
27-
28-
/// Number of previous tokens to look at during predictions.
29-
int get lookback => _lookback;
30-
3125
/// Load model from directory.
3226
factory LanguageModel.load(String directory) {
3327
// Load model.
@@ -55,11 +49,21 @@ class LanguageModel {
5549
return LanguageModel._(interpreter, word2idx, idx2word, lookback);
5650
}
5751

52+
LanguageModel._(
53+
this._interpreter, this._word2idx, this._idx2word, this._lookback);
54+
55+
/// Number of previous tokens to look at during predictions.
56+
int get lookback => _lookback;
57+
5858
/// Tear down the interpreter.
5959
void close() {
6060
_interpreter.delete();
6161
}
6262

63+
bool isNumber(String token) {
64+
return _numeric.hasMatch(token) || token.startsWith('0x');
65+
}
66+
6367
/// Predicts the next token to follow a list of precedent tokens
6468
///
6569
/// Returns a list of tokens, sorted by most probable first.
@@ -77,6 +81,16 @@ class LanguageModel {
7781
return _transformOutput(tensorOut.data, tokens);
7882
}
7983

84+
bool _isAlphanumeric(String token) {
85+
// Note that _numeric covers integral and decimal values whereas
86+
// _alphanumeric only matches integral values. Check both.
87+
return _alphanumeric.hasMatch(token) || _numeric.hasMatch(token);
88+
}
89+
90+
bool _isString(String token) {
91+
return token.contains('"') || token.contains("'");
92+
}
93+
8094
/// Transforms tokens to data bytes that can be used as interpreter input.
8195
List<int> _transformInput(List<String> tokens) {
8296
// Replace out of vocabulary tokens.
@@ -138,18 +152,4 @@ class LanguageModel {
138152
return Map.fromEntries(scoresAboveThreshold.entries.toList()
139153
..sort((a, b) => b.value.compareTo(a.value)));
140154
}
141-
142-
bool _isAlphanumeric(String token) {
143-
// Note that _numeric covers integral and decimal values whereas
144-
// _alphanumeric only matches integral values. Check both.
145-
return _alphanumeric.hasMatch(token) || _numeric.hasMatch(token);
146-
}
147-
148-
bool _isString(String token) {
149-
return token.indexOf('"') != -1 || token.indexOf("'") != -1;
150-
}
151-
152-
bool isNumber(String token) {
153-
return _numeric.hasMatch(token) || token.startsWith('0x');
154-
}
155155
}

pkg/analysis_server/lib/src/services/correction/base_processor.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ abstract class BaseProcessor {
776776
: (fromDouble ? "'" : '"');
777777
int quoteLength = literal.isMultiline ? 3 : 1;
778778
String lexeme = literal.literal.lexeme;
779-
if (lexeme.indexOf(newQuote) < 0) {
779+
if (!lexeme.contains(newQuote)) {
780780
var changeBuilder = _newDartChangeBuilder();
781781
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
782782
builder.addSimpleReplacement(
@@ -801,7 +801,7 @@ abstract class BaseProcessor {
801801
InterpolationElement element = elements[i];
802802
if (element is InterpolationString) {
803803
String lexeme = element.contents.lexeme;
804-
if (lexeme.indexOf(newQuote) >= 0) {
804+
if (lexeme.contains(newQuote)) {
805805
return null;
806806
}
807807
}

pkg/analysis_server/test/completion_test_support.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CompletionTestCase extends CompletionDomainHandlerListTokenDetailsTest {
2323
void assertHasCompletion(String completion) {
2424
int expectedOffset = completion.indexOf(CURSOR_MARKER);
2525
if (expectedOffset >= 0) {
26-
if (completion.indexOf(CURSOR_MARKER, expectedOffset + 1) >= 0) {
26+
if (completion.contains(CURSOR_MARKER, expectedOffset + 1)) {
2727
fail(
2828
"Invalid completion, contains multiple cursor positions: '$completion'");
2929
}

pkg/analysis_server/test/src/edit/nnbd_migration/nnbd_migration_test_base.dart

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,44 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
3030
includedRoot: includedRoot, removeViaComments: removeViaComments);
3131
}
3232

33+
/// Uses the InfoBuilder to build information for a single test file.
34+
///
35+
/// Asserts that [originalContent] is migrated to [migratedContent]. Returns
36+
/// the singular UnitInfo which was built.
37+
Future<UnitInfo> buildInfoForSingleTestFile(String originalContent,
38+
{@required String migratedContent, bool removeViaComments = true}) async {
39+
addTestFile(originalContent);
40+
await buildInfo(removeViaComments: removeViaComments);
41+
// Ignore info for dart:core.
42+
var filteredInfos = [
43+
for (var info in infos) if (!info.path.contains('core.dart')) info
44+
];
45+
expect(filteredInfos, hasLength(1));
46+
UnitInfo unit = filteredInfos[0];
47+
expect(unit.path, testFile);
48+
expect(unit.content, migratedContent);
49+
return unit;
50+
}
51+
52+
/// Uses the InfoBuilder to build information for test files.
53+
///
54+
/// Returns
55+
/// the singular UnitInfo which was built.
56+
Future<List<UnitInfo>> buildInfoForTestFiles(Map<String, String> files,
57+
{String includedRoot}) async {
58+
var testPaths = <String>[];
59+
files.forEach((String path, String content) {
60+
newFile(path, content: content);
61+
testPaths.add(path);
62+
});
63+
await _buildMigrationInfo(testPaths, includedRoot: includedRoot);
64+
// Ignore info for dart:core.
65+
var filteredInfos = [
66+
for (var info in infos) if (!info.path.contains('core.dart')) info
67+
];
68+
return filteredInfos;
69+
}
70+
3371
/// Uses the InfoBuilder to build information for files at [testPaths], which
3472
/// should all share a common parent directory, [includedRoot].
3573
Future<void> _buildMigrationInfo(List<String> testPaths,
@@ -61,42 +99,4 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
6199
explainNonNullableTypes: true);
62100
infos = await builder.explainMigration();
63101
}
64-
65-
/// Uses the InfoBuilder to build information for test files.
66-
///
67-
/// Returns
68-
/// the singular UnitInfo which was built.
69-
Future<List<UnitInfo>> buildInfoForTestFiles(Map<String, String> files,
70-
{String includedRoot}) async {
71-
var testPaths = <String>[];
72-
files.forEach((String path, String content) {
73-
newFile(path, content: content);
74-
testPaths.add(path);
75-
});
76-
await _buildMigrationInfo(testPaths, includedRoot: includedRoot);
77-
// Ignore info for dart:core.
78-
var filteredInfos = [
79-
for (var info in infos) if (info.path.indexOf('core.dart') == -1) info
80-
];
81-
return filteredInfos;
82-
}
83-
84-
/// Uses the InfoBuilder to build information for a single test file.
85-
///
86-
/// Asserts that [originalContent] is migrated to [migratedContent]. Returns
87-
/// the singular UnitInfo which was built.
88-
Future<UnitInfo> buildInfoForSingleTestFile(String originalContent,
89-
{@required String migratedContent, bool removeViaComments = true}) async {
90-
addTestFile(originalContent);
91-
await buildInfo(removeViaComments: removeViaComments);
92-
// Ignore info for dart:core.
93-
var filteredInfos = [
94-
for (var info in infos) if (info.path.indexOf('core.dart') == -1) info
95-
];
96-
expect(filteredInfos, hasLength(1));
97-
UnitInfo unit = filteredInfos[0];
98-
expect(unit.path, testFile);
99-
expect(unit.content, migratedContent);
100-
return unit;
101-
}
102102
}

pkg/analysis_server/test/src/services/flutter/widget_description.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class WidgetDescriptionBase extends AbstractSingleUnitTest {
6161
fail('Not found: $search');
6262
}
6363

64-
if (content.indexOf(search, offset + search.length) != -1) {
64+
if (content.contains(search, offset + search.length)) {
6565
fail('More than one: $search');
6666
}
6767

0 commit comments

Comments
 (0)