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>
This commit is contained in:
Brian Wilkerson 2020-02-13 14:52:08 +00:00 committed by commit-bot@chromium.org
parent 1cc9413926
commit 16033f922d
9 changed files with 66 additions and 69 deletions

View file

@ -9,7 +9,6 @@ analyzer:
errors:
# Increase the severity of the unused_import hint.
unused_import: warning
prefer_contains: ignore
# TODO(srawlins): At the time of writing, 2400 violations in lib/. The fix
# is mechanical, via `dartfmt --fix-doc-comments`, but not worth the churn
# today.

View file

@ -154,7 +154,7 @@ PerfArgs parseArgs(List<String> rawArgs) {
for (String pair in args[MAP_OPTION]) {
if (pair is String) {
int index = pair.indexOf(',');
if (index != -1 && pair.indexOf(',', index + 1) == -1) {
if (index != -1 && !pair.contains(',', index + 1)) {
String oldSrcPrefix = _withTrailingSeparator(pair.substring(0, index));
String newSrcPrefix = _withTrailingSeparator(pair.substring(index + 1));
if (Directory(newSrcPrefix).existsSync()) {

View file

@ -74,8 +74,7 @@ class HttpPreviewServer {
List<String> updateValues = request.headers[HttpHeaders.upgradeHeader];
if (request.method == 'GET') {
await _handleGetRequest(request);
} else if (updateValues != null &&
updateValues.indexOf('websocket') >= 0) {
} else if (updateValues != null && updateValues.contains('websocket')) {
// We do not support serving analysis server communications over
// WebSocket connections.
HttpResponse response = request.response;

View file

@ -131,8 +131,7 @@ class HttpAnalysisServer {
List<String> updateValues = request.headers[HttpHeaders.upgradeHeader];
if (request.method == 'GET') {
await _handleGetRequest(request);
} else if (updateValues != null &&
updateValues.indexOf('websocket') >= 0) {
} else if (updateValues != null && updateValues.contains('websocket')) {
// We no longer support serving analysis server communications over
// WebSocket connections.
HttpResponse response = request.response;

View file

@ -2,8 +2,8 @@
// 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 'dart:io';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:path/path.dart' as path;
@ -22,12 +22,6 @@ class LanguageModel {
final Map<int, String> _idx2word;
final int _lookback;
LanguageModel._(
this._interpreter, this._word2idx, this._idx2word, this._lookback);
/// Number of previous tokens to look at during predictions.
int get lookback => _lookback;
/// Load model from directory.
factory LanguageModel.load(String directory) {
// Load model.
@ -55,11 +49,21 @@ class LanguageModel {
return LanguageModel._(interpreter, word2idx, idx2word, lookback);
}
LanguageModel._(
this._interpreter, this._word2idx, this._idx2word, this._lookback);
/// Number of previous tokens to look at during predictions.
int get lookback => _lookback;
/// Tear down the interpreter.
void close() {
_interpreter.delete();
}
bool isNumber(String token) {
return _numeric.hasMatch(token) || token.startsWith('0x');
}
/// Predicts the next token to follow a list of precedent tokens
///
/// Returns a list of tokens, sorted by most probable first.
@ -77,6 +81,16 @@ class LanguageModel {
return _transformOutput(tensorOut.data, tokens);
}
bool _isAlphanumeric(String token) {
// Note that _numeric covers integral and decimal values whereas
// _alphanumeric only matches integral values. Check both.
return _alphanumeric.hasMatch(token) || _numeric.hasMatch(token);
}
bool _isString(String token) {
return token.contains('"') || token.contains("'");
}
/// Transforms tokens to data bytes that can be used as interpreter input.
List<int> _transformInput(List<String> tokens) {
// Replace out of vocabulary tokens.
@ -138,18 +152,4 @@ class LanguageModel {
return Map.fromEntries(scoresAboveThreshold.entries.toList()
..sort((a, b) => b.value.compareTo(a.value)));
}
bool _isAlphanumeric(String token) {
// Note that _numeric covers integral and decimal values whereas
// _alphanumeric only matches integral values. Check both.
return _alphanumeric.hasMatch(token) || _numeric.hasMatch(token);
}
bool _isString(String token) {
return token.indexOf('"') != -1 || token.indexOf("'") != -1;
}
bool isNumber(String token) {
return _numeric.hasMatch(token) || token.startsWith('0x');
}
}

View file

@ -776,7 +776,7 @@ abstract class BaseProcessor {
: (fromDouble ? "'" : '"');
int quoteLength = literal.isMultiline ? 3 : 1;
String lexeme = literal.literal.lexeme;
if (lexeme.indexOf(newQuote) < 0) {
if (!lexeme.contains(newQuote)) {
var changeBuilder = _newDartChangeBuilder();
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
builder.addSimpleReplacement(
@ -801,7 +801,7 @@ abstract class BaseProcessor {
InterpolationElement element = elements[i];
if (element is InterpolationString) {
String lexeme = element.contents.lexeme;
if (lexeme.indexOf(newQuote) >= 0) {
if (lexeme.contains(newQuote)) {
return null;
}
}

View file

@ -23,7 +23,7 @@ class CompletionTestCase extends CompletionDomainHandlerListTokenDetailsTest {
void assertHasCompletion(String completion) {
int expectedOffset = completion.indexOf(CURSOR_MARKER);
if (expectedOffset >= 0) {
if (completion.indexOf(CURSOR_MARKER, expectedOffset + 1) >= 0) {
if (completion.contains(CURSOR_MARKER, expectedOffset + 1)) {
fail(
"Invalid completion, contains multiple cursor positions: '$completion'");
}

View file

@ -30,6 +30,44 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
includedRoot: includedRoot, removeViaComments: removeViaComments);
}
/// Uses the InfoBuilder to build information for a single test file.
///
/// Asserts that [originalContent] is migrated to [migratedContent]. Returns
/// the singular UnitInfo which was built.
Future<UnitInfo> buildInfoForSingleTestFile(String originalContent,
{@required String migratedContent, bool removeViaComments = true}) async {
addTestFile(originalContent);
await buildInfo(removeViaComments: removeViaComments);
// Ignore info for dart:core.
var filteredInfos = [
for (var info in infos) if (!info.path.contains('core.dart')) info
];
expect(filteredInfos, hasLength(1));
UnitInfo unit = filteredInfos[0];
expect(unit.path, testFile);
expect(unit.content, migratedContent);
return unit;
}
/// Uses the InfoBuilder to build information for test files.
///
/// Returns
/// the singular UnitInfo which was built.
Future<List<UnitInfo>> buildInfoForTestFiles(Map<String, String> files,
{String includedRoot}) async {
var testPaths = <String>[];
files.forEach((String path, String content) {
newFile(path, content: content);
testPaths.add(path);
});
await _buildMigrationInfo(testPaths, includedRoot: includedRoot);
// Ignore info for dart:core.
var filteredInfos = [
for (var info in infos) if (!info.path.contains('core.dart')) info
];
return filteredInfos;
}
/// Uses the InfoBuilder to build information for files at [testPaths], which
/// should all share a common parent directory, [includedRoot].
Future<void> _buildMigrationInfo(List<String> testPaths,
@ -61,42 +99,4 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
explainNonNullableTypes: true);
infos = await builder.explainMigration();
}
/// Uses the InfoBuilder to build information for test files.
///
/// Returns
/// the singular UnitInfo which was built.
Future<List<UnitInfo>> buildInfoForTestFiles(Map<String, String> files,
{String includedRoot}) async {
var testPaths = <String>[];
files.forEach((String path, String content) {
newFile(path, content: content);
testPaths.add(path);
});
await _buildMigrationInfo(testPaths, includedRoot: includedRoot);
// Ignore info for dart:core.
var filteredInfos = [
for (var info in infos) if (info.path.indexOf('core.dart') == -1) info
];
return filteredInfos;
}
/// Uses the InfoBuilder to build information for a single test file.
///
/// Asserts that [originalContent] is migrated to [migratedContent]. Returns
/// the singular UnitInfo which was built.
Future<UnitInfo> buildInfoForSingleTestFile(String originalContent,
{@required String migratedContent, bool removeViaComments = true}) async {
addTestFile(originalContent);
await buildInfo(removeViaComments: removeViaComments);
// Ignore info for dart:core.
var filteredInfos = [
for (var info in infos) if (info.path.indexOf('core.dart') == -1) info
];
expect(filteredInfos, hasLength(1));
UnitInfo unit = filteredInfos[0];
expect(unit.path, testFile);
expect(unit.content, migratedContent);
return unit;
}
}

View file

@ -61,7 +61,7 @@ class WidgetDescriptionBase extends AbstractSingleUnitTest {
fail('Not found: $search');
}
if (content.indexOf(search, offset + search.length) != -1) {
if (content.contains(search, offset + search.length)) {
fail('More than one: $search');
}