Skip to content

Commit c37d357

Browse files
authored
[dart_services] Update to analyzer v6 (#2726)
1 parent 966c546 commit c37d357

File tree

7 files changed

+130
-131
lines changed

7 files changed

+130
-131
lines changed

pkgs/dart_services/lib/src/analysis.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Analyzer {
7878
Future<void> _checkPackageReferences(List<ImportDirective> imports) async {
7979
final unsupportedImports = project.getUnsupportedImports(
8080
imports,
81-
sourcesFileList: [kMainDart],
81+
sourceFiles: {kMainDart},
8282
);
8383

8484
if (unsupportedImports.isNotEmpty) {

pkgs/dart_services/lib/src/compiling.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Compiler {
5252
}) async {
5353
final imports = getAllImportsFor(source);
5454
final unsupportedImports =
55-
getUnsupportedImports(imports, sourcesFileList: [kMainDart]);
55+
getUnsupportedImports(imports, sourceFiles: {kMainDart});
5656
if (unsupportedImports.isNotEmpty) {
5757
return CompilationResults(problems: [
5858
for (final import in unsupportedImports)
@@ -121,7 +121,7 @@ class Compiler {
121121
Future<DDCCompilationResults> compileDDC(String source) async {
122122
final imports = getAllImportsFor(source);
123123
final unsupportedImports =
124-
getUnsupportedImports(imports, sourcesFileList: [kMainDart]);
124+
getUnsupportedImports(imports, sourceFiles: {kMainDart});
125125
if (unsupportedImports.isNotEmpty) {
126126
return DDCCompilationResults.failed([
127127
for (final import in unsupportedImports)

pkgs/dart_services/lib/src/project_creator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ ${_sdk.experiments.map((experiment) => ' - $experiment').join('\n')}
125125
// `flutter packages get` has been run with a _subset_ of all supported
126126
// Firebase packages, the ones that don't require a Firebase app to be
127127
// configured in JavaScript, before executing Dart. Now add the full set of
128-
// supported Firebase pacakges. This workaround is a very fragile hack.
128+
// supported Firebase packages. This workaround is a very fragile hack.
129129
packages = {
130130
...supportedBasicDartPackages,
131131
...supportedFlutterPackages,

pkgs/dart_services/lib/src/project_templates.dart

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:io';
66

77
import 'package:analyzer/dart/ast/ast.dart';
8+
import 'package:meta/meta.dart';
89
import 'package:path/path.dart' as path;
910

1011
/// Sets of project template directory paths.
@@ -166,27 +167,33 @@ const Set<String> _allowedDartImports = {
166167
};
167168

168169
/// Returns whether [imports] denote use of Flutter Web.
169-
bool usesFlutterWeb(Iterable<ImportDirective> imports) {
170-
return imports.any((import) {
171-
final uriString = import.uri.stringValue;
172-
if (uriString == null) return false;
173-
if (uriString == 'dart:ui') return true;
174-
175-
final packageName = _packageNameFromPackageUri(uriString);
176-
return packageName != null &&
177-
_packagesIndicatingFlutter.contains(packageName);
178-
});
170+
bool usesFlutterWeb(Iterable<ImportDirective> imports) =>
171+
imports.any((import) => isFlutterWebImport(import.uri.stringValue));
172+
173+
/// Whether the [importString] represents an import
174+
/// that denotes use of Flutter Web.
175+
@visibleForTesting
176+
bool isFlutterWebImport(String? importString) {
177+
if (importString == null) return false;
178+
if (importString == 'dart:ui') return true;
179+
180+
final packageName = _packageNameFromPackageUri(importString);
181+
return packageName != null &&
182+
_packagesIndicatingFlutter.contains(packageName);
179183
}
180184

181185
/// Returns whether [imports] denote use of Firebase.
182-
bool usesFirebase(Iterable<ImportDirective> imports) {
183-
return imports.any((import) {
184-
final uriString = import.uri.stringValue;
185-
if (uriString == null) return false;
186+
bool usesFirebase(Iterable<ImportDirective> imports) =>
187+
imports.any((import) => isFirebaseImport(import.uri.stringValue));
186188

187-
final packageName = _packageNameFromPackageUri(uriString);
188-
return packageName != null && firebasePackages.contains(packageName);
189-
});
189+
/// Whether the [importString] represents an import
190+
/// that denotes use of a Firebase package.
191+
@visibleForTesting
192+
bool isFirebaseImport(String? importString) {
193+
if (importString == null) return false;
194+
195+
final packageName = _packageNameFromPackageUri(importString);
196+
return packageName != null && firebasePackages.contains(packageName);
190197
}
191198

192199
/// If [uriString] represents a 'package:' URI, then returns the package name;
@@ -200,54 +207,60 @@ String? _packageNameFromPackageUri(String uriString) {
200207
}
201208

202209
/// Goes through imports list and returns list of unsupported imports.
203-
///
204-
/// Optional [sourcesFileList] contains a list of the source filenames
210+
/// Optional [sourceFiles] contains a list of the source filenames
205211
/// which are all part of this overall sources file set (these are to
206212
/// be allowed).
207213
///
208-
/// Note: The filenames in [sourcesFileList] were sanitized of any
209-
/// 'package:'/etc syntax as the file set arrives from the endpoint, and before
210-
/// being passed to [getUnsupportedImports]. This is done so the list can't be
211-
/// used to bypass unsupported imports.
214+
/// Note: The filenames in [sourceFiles] were sanitized of any
215+
/// 'package:'/etc syntax as the file set arrives from the endpoint, and
216+
/// before being passed to [getUnsupportedImports].This is done so
217+
/// the list can't be used to bypass unsupported imports.
212218
List<ImportDirective> getUnsupportedImports(
213219
List<ImportDirective> imports, {
214-
List<String>? sourcesFileList,
220+
Set<String>? sourceFiles,
215221
}) {
216-
return imports.where((import) {
217-
final uriString = import.uri.stringValue;
218-
if (uriString == null || uriString.isEmpty) {
219-
return false;
220-
}
221-
222-
// All non-VM 'dart:' imports are ok.
223-
if (uriString.startsWith('dart:')) {
224-
return !_allowedDartImports.contains(uriString);
225-
}
226-
227-
// Filenames from within this compilation files={} sources file set
228-
// are OK. (These filenames have been sanitized to prevent 'package:'
229-
// (and other) prefixes, so the a filename cannot be used to bypass
230-
// import restrictions (see comment above)).
231-
if (sourcesFileList != null && sourcesFileList.contains(uriString)) {
232-
return false;
233-
}
234-
235-
final uri = Uri.tryParse(uriString);
236-
if (uri == null) return false;
237-
238-
// We allow a specific set of package imports.
239-
if (uri.scheme == 'package') {
240-
if (uri.pathSegments.isEmpty) return true;
241-
final package = uri.pathSegments.first;
242-
return !isSupportedPackage(package);
243-
}
244-
245-
// Don't allow file imports.
246-
return true;
247-
}).toList();
222+
return imports
223+
.where((import) => isUnsupportedImport(import.uri.stringValue,
224+
sourceFiles: sourceFiles ?? const {}))
225+
.toList(growable: false);
248226
}
249227

250-
bool isSupportedPackage(String package) {
251-
return _packagesIndicatingFlutter.contains(package) ||
252-
supportedBasicDartPackages.contains(package);
228+
/// Whether the [importString] represents an import
229+
/// that is unsupported.
230+
@visibleForTesting
231+
bool isUnsupportedImport(
232+
String? importString, {
233+
Set<String> sourceFiles = const {},
234+
}) {
235+
if (importString == null || importString.isEmpty) {
236+
return false;
237+
}
238+
// All non-VM 'dart:' imports are ok.
239+
if (importString.startsWith('dart:')) {
240+
return !_allowedDartImports.contains(importString);
241+
}
242+
// Filenames from within this compilation files={} sources file set
243+
// are OK. (These filenames have been sanitized to prevent 'package:'
244+
// (and other) prefixes, so the a filename cannot be used to bypass
245+
// import restrictions (see comment above)).
246+
if (sourceFiles.contains(importString)) {
247+
return false;
248+
}
249+
250+
final uri = Uri.tryParse(importString);
251+
if (uri == null) return false;
252+
253+
// We allow a specific set of package imports.
254+
if (uri.scheme == 'package') {
255+
if (uri.pathSegments.isEmpty) return true;
256+
final package = uri.pathSegments.first;
257+
return !isSupportedPackage(package);
258+
}
259+
260+
// Don't allow file imports.
261+
return true;
253262
}
263+
264+
bool isSupportedPackage(String package) =>
265+
_packagesIndicatingFlutter.contains(package) ||
266+
supportedBasicDartPackages.contains(package);

pkgs/dart_services/pubspec.lock

Lines changed: 21 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/dart_services/pubspec.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ environment:
66
sdk: ^3.2.0
77

88
dependencies:
9-
analysis_server_lib: ^0.2.4
10-
analyzer: ^5.13.0
9+
analysis_server_lib: ^0.2.5
10+
analyzer: ^6.3.0
1111
args: ^2.4.2
1212
bazel_worker: ^1.0.2
1313
collection: ^1.18.0
@@ -17,6 +17,7 @@ dependencies:
1717
http: ^1.0.0
1818
json_annotation: any
1919
logging: ^1.2.0
20+
meta: ^1.11.0
2021
path: ^1.8.3
2122
resp_client: ^1.2.0
2223
shelf: ^1.4.1
@@ -27,10 +28,10 @@ dev_dependencies:
2728
async: ^2.11.0
2829
build_runner: ^2.4.5
2930
dart_flutter_team_lints: ^2.1.1
30-
grinder: ^0.9.4
31+
grinder: ^0.9.5
3132
json_serializable: any
3233
package_config: ^2.1.0
3334
shelf_router_generator: ^1.0.6
3435
synchronized: ^3.1.0
35-
test: ^1.24.3
36+
test: ^1.24.9
3637
test_descriptor: ^2.0.1

0 commit comments

Comments
 (0)