From 16033f922d4bd400aa34ec86ab741bda1b3c1cac Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Thu, 13 Feb 2020 14:52:08 +0000 Subject: [PATCH] Enable prefer_contains in analysis_server Change-Id: Idac9828eb452f685452a33e6946ece2ba0d197f4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135583 Reviewed-by: Konstantin Shcheglov Commit-Queue: Brian Wilkerson --- pkg/analysis_server/analysis_options.yaml | 1 - .../benchmark/integration/main.dart | 2 +- .../src/edit/preview/http_preview_server.dart | 3 +- .../lib/src/server/http_server.dart | 3 +- .../completion/dart/language_model.dart | 42 +++++----- .../services/correction/base_processor.dart | 4 +- .../test/completion_test_support.dart | 2 +- .../nnbd_migration_test_base.dart | 76 +++++++++---------- .../services/flutter/widget_description.dart | 2 +- 9 files changed, 66 insertions(+), 69 deletions(-) diff --git a/pkg/analysis_server/analysis_options.yaml b/pkg/analysis_server/analysis_options.yaml index 44b31e69a8b..a58b511e7ee 100644 --- a/pkg/analysis_server/analysis_options.yaml +++ b/pkg/analysis_server/analysis_options.yaml @@ -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. diff --git a/pkg/analysis_server/benchmark/integration/main.dart b/pkg/analysis_server/benchmark/integration/main.dart index 9b6436f632c..c149fb88fec 100644 --- a/pkg/analysis_server/benchmark/integration/main.dart +++ b/pkg/analysis_server/benchmark/integration/main.dart @@ -154,7 +154,7 @@ PerfArgs parseArgs(List 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()) { diff --git a/pkg/analysis_server/lib/src/edit/preview/http_preview_server.dart b/pkg/analysis_server/lib/src/edit/preview/http_preview_server.dart index 8af13c59145..e395a67adda 100644 --- a/pkg/analysis_server/lib/src/edit/preview/http_preview_server.dart +++ b/pkg/analysis_server/lib/src/edit/preview/http_preview_server.dart @@ -74,8 +74,7 @@ class HttpPreviewServer { List 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; diff --git a/pkg/analysis_server/lib/src/server/http_server.dart b/pkg/analysis_server/lib/src/server/http_server.dart index 391be383de0..72f32c3cc58 100644 --- a/pkg/analysis_server/lib/src/server/http_server.dart +++ b/pkg/analysis_server/lib/src/server/http_server.dart @@ -131,8 +131,7 @@ class HttpAnalysisServer { List 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; diff --git a/pkg/analysis_server/lib/src/services/completion/dart/language_model.dart b/pkg/analysis_server/lib/src/services/completion/dart/language_model.dart index 5f59c73ed38..a214ef6225a 100644 --- a/pkg/analysis_server/lib/src/services/completion/dart/language_model.dart +++ b/pkg/analysis_server/lib/src/services/completion/dart/language_model.dart @@ -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 _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 _transformInput(List 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'); - } } diff --git a/pkg/analysis_server/lib/src/services/correction/base_processor.dart b/pkg/analysis_server/lib/src/services/correction/base_processor.dart index bdf20eecd4f..3e191328e23 100644 --- a/pkg/analysis_server/lib/src/services/correction/base_processor.dart +++ b/pkg/analysis_server/lib/src/services/correction/base_processor.dart @@ -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; } } diff --git a/pkg/analysis_server/test/completion_test_support.dart b/pkg/analysis_server/test/completion_test_support.dart index 09eb049dd3a..76452613fd7 100644 --- a/pkg/analysis_server/test/completion_test_support.dart +++ b/pkg/analysis_server/test/completion_test_support.dart @@ -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'"); } diff --git a/pkg/analysis_server/test/src/edit/nnbd_migration/nnbd_migration_test_base.dart b/pkg/analysis_server/test/src/edit/nnbd_migration/nnbd_migration_test_base.dart index 6307ba8a012..3d7a13e347f 100644 --- a/pkg/analysis_server/test/src/edit/nnbd_migration/nnbd_migration_test_base.dart +++ b/pkg/analysis_server/test/src/edit/nnbd_migration/nnbd_migration_test_base.dart @@ -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 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> buildInfoForTestFiles(Map files, + {String includedRoot}) async { + var testPaths = []; + 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 _buildMigrationInfo(List 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> buildInfoForTestFiles(Map files, - {String includedRoot}) async { - var testPaths = []; - 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 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; - } } diff --git a/pkg/analysis_server/test/src/services/flutter/widget_description.dart b/pkg/analysis_server/test/src/services/flutter/widget_description.dart index 4a25d2d0a29..8cda4ec4f32 100644 --- a/pkg/analysis_server/test/src/services/flutter/widget_description.dart +++ b/pkg/analysis_server/test/src/services/flutter/widget_description.dart @@ -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'); }