Verify that 'utf8.encode()' is not used in the analyzer.

To be removed after Dart SDK 3.1 is published, and the analyzer
SDK constraints updated.

See
https://dart-review.googlesource.com/c/sdk/+/254903

Change-Id: I2c3321d991cd3e123f08c5a360487362be16f258
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/313920
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Konstantin Shcheglov 2023-07-17 17:28:31 +00:00 committed by Commit Queue
parent ed776ebbc6
commit 57ac271bf1
7 changed files with 71 additions and 6 deletions

View file

@ -6,7 +6,7 @@ library _fe_analyzer_shared.scanner;
import 'dart:typed_data' show Uint8List;
import 'dart:convert' show unicodeReplacementCharacterRune, utf8;
import 'dart:convert' show Utf8Encoder, unicodeReplacementCharacterRune;
import 'token.dart' show Token;
@ -98,7 +98,7 @@ ScannerResult _tokenizeAndRecover(Scanner scanner,
{List<int>? bytes, String? source}) {
Token tokens = scanner.tokenize();
if (scanner.hasErrors) {
if (bytes == null) bytes = utf8.encode(source!);
if (bytes == null) bytes = const Utf8Encoder().convert(source!);
tokens = scannerRecovery(bytes, tokens, scanner.lineStarts);
}
return new ScannerResult(tokens, scanner.lineStarts, scanner.hasErrors);

View file

@ -30,6 +30,7 @@ import 'tool/test_all.dart' as tool;
import 'utilities/test_all.dart' as utilities;
import 'verify_error_fix_status_test.dart' as verify_error_fix_status;
import 'verify_no_solo_test.dart' as verify_no_solo;
import 'verify_no_utf8_encode_test.dart' as verify_no_utf8_encode;
import 'verify_sorted_test.dart' as verify_sorted;
import 'verify_tests_test.dart' as verify_tests;
@ -60,6 +61,7 @@ void main() {
utilities.main();
verify_error_fix_status.main();
verify_no_solo.main();
verify_no_utf8_encode.main();
verify_sorted.main();
verify_tests.main();
defineReflectiveSuite(() {

View file

@ -0,0 +1,62 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// 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:collection';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
import 'package:analyzer_utilities/package_root.dart';
import 'package:test/test.dart';
/// TODO(scheglov) Remove it after SDK 3.1 published.
void main() {
group('_fe_analyzer_shared', () {
buildTests(packagePath: '_fe_analyzer_shared');
});
group('analyzer', () {
buildTests(packagePath: 'analyzer');
});
}
void buildTests({required String packagePath}) {
final provider = PhysicalResourceProvider.INSTANCE;
final pathContext = provider.pathContext;
final pkgRootPath = pathContext.normalize(packageRoot);
final libFolder = provider
.getFolder(pkgRootPath)
.getChildAssumingFolder(packagePath)
.getChildAssumingFolder('lib');
for (final file in libFolder.allFiles) {
if (file_paths.isDart(pathContext, file.path)) {
test(file.path, () {
final content = file.readAsStringSync();
if (content.contains('utf8.encode')) {
fail('Should not use `utf8.encode` before SDK 3.1');
}
});
}
}
}
extension on Folder {
Iterable<File> get allFiles sync* {
final queue = Queue<Folder>();
queue.add(this);
while (queue.isNotEmpty) {
final current = queue.removeFirst();
final children = current.getChildren();
for (final child in children) {
if (child is File) {
yield child;
} else if (child is Folder) {
queue.add(child);
}
}
}
}
}

View file

@ -96,7 +96,7 @@ class ApiSignature {
/// Collect a string.
void addString(String s) {
List<int> bytes = utf8.encode(s);
final bytes = const Utf8Encoder().convert(s);
addInt(bytes.length);
addBytes(bytes);
}

View file

@ -403,7 +403,7 @@ class Builder {
_ensureNoVTable();
return _strings.putIfAbsent(value, () {
// TODO(scheglov) optimize for ASCII strings
List<int> bytes = utf8.encode(value);
final bytes = const Utf8Encoder().convert(value);
int length = bytes.length;
_prepare(4, 1, additionalBytes: length);
Offset<String> result = Offset(_tail);

View file

@ -178,7 +178,8 @@ class BufferedSink {
/// Write the [value] as UTF8 encoded byte array.
void writeStringUtf8(String value) {
writeUint8List(const Utf8Encoder().convert(value));
final bytes = const Utf8Encoder().convert(value);
writeUint8List(bytes);
}
void writeStringUtf8Iterable(Iterable<String> items) {

View file

@ -104,7 +104,7 @@ class KernelCompilationService {
});
requestChannel.add('file.readAsBytes', (uriStr) async {
final content = uriStrToFile(uriStr).content;
return utf8.encode(content);
return const Utf8Encoder().convert(content);
});
requestChannel.add('file.readAsStringSync', (uriStr) async {
return uriStrToFile(uriStr).content;