5
5
import 'dart:io' ;
6
6
7
7
import 'package:analyzer/dart/ast/ast.dart' ;
8
+ import 'package:meta/meta.dart' ;
8
9
import 'package:path/path.dart' as path;
9
10
10
11
/// Sets of project template directory paths.
@@ -166,27 +167,33 @@ const Set<String> _allowedDartImports = {
166
167
};
167
168
168
169
/// 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);
179
183
}
180
184
181
185
/// 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));
186
188
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);
190
197
}
191
198
192
199
/// If [uriString] represents a 'package:' URI, then returns the package name;
@@ -200,54 +207,60 @@ String? _packageNameFromPackageUri(String uriString) {
200
207
}
201
208
202
209
/// 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
205
211
/// which are all part of this overall sources file set (these are to
206
212
/// be allowed).
207
213
///
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.
212
218
List <ImportDirective > getUnsupportedImports (
213
219
List <ImportDirective > imports, {
214
- List <String >? sourcesFileList ,
220
+ Set <String >? sourceFiles ,
215
221
}) {
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 );
248
226
}
249
227
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 ;
253
262
}
263
+
264
+ bool isSupportedPackage (String package) =>
265
+ _packagesIndicatingFlutter.contains (package) ||
266
+ supportedBasicDartPackages.contains (package);
0 commit comments