Skip to content

Commit 1c3afdf

Browse files
authored
Address some comments from #38632 (2019) if they still apply (#171461)
Closes flutter/flutter#38632. Mostly clarifies how validation works, and reuses some code.
1 parent c2aa518 commit 1c3afdf

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

packages/flutter_tools/lib/src/plugins.dart

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ class Plugin {
7676
if (errors.isNotEmpty) {
7777
throwToolExit('Invalid plugin specification $name.\n${errors.join('\n')}');
7878
}
79-
if (pluginYaml != null && pluginYaml['platforms'] != null) {
79+
if (pluginYaml?['platforms'] != null) {
80+
// SAFETY: Assumes that validatePluginYaml(pluginYaml) has been called.
8081
return Plugin._fromMultiPlatformYaml(
8182
name,
8283
path,
83-
pluginYaml,
84+
pluginYaml!,
8485
flutterConstraint,
8586
dependencies,
8687
fileSystem,
@@ -110,11 +111,10 @@ class Plugin {
110111
bool isDirectDependency, {
111112
required bool isDevDependency,
112113
}) {
113-
assert(pluginYaml['platforms'] != null, 'Invalid multi-platform plugin specification $name.');
114+
// SAFETY: This constructor is only invoked from .fromYaml, which validates.
114115
final YamlMap platformsYaml = pluginYaml['platforms'] as YamlMap;
115-
116116
assert(
117-
_validateMultiPlatformYaml(platformsYaml).isEmpty,
117+
_validateMultiPlatformYaml(parentMap: pluginYaml).isEmpty,
118118
'Invalid multi-platform plugin specification $name.',
119119
);
120120

@@ -303,20 +303,26 @@ class Plugin {
303303
}
304304

305305
if (usesNewPluginFormat) {
306-
if (yaml['platforms'] != null && yaml['platforms'] is! YamlMap) {
307-
const String errorMessage =
308-
'flutter.plugin.platforms should be a map with the platform name as the key';
309-
return <String>[errorMessage];
310-
}
311-
return _validateMultiPlatformYaml(yaml['platforms'] as YamlMap?);
306+
return _validateMultiPlatformYaml(parentMap: yaml);
312307
} else {
313308
return _validateLegacyYaml(yaml);
314309
}
315310
}
316311

317-
static List<String> _validateMultiPlatformYaml(YamlMap? yaml) {
312+
static List<String> _validateMultiPlatformYaml({required YamlMap parentMap}) {
313+
final Object? platforms = parentMap['platforms'];
314+
if (platforms is! YamlMap?) {
315+
const String errorMessage =
316+
'flutter.plugin.platforms should be a map with the platform name as the key';
317+
return <String>[errorMessage];
318+
}
319+
if (platforms == null) {
320+
return <String>['Invalid "platforms" specification.'];
321+
}
322+
final YamlMap yaml = platforms;
323+
318324
bool isInvalid(String key, bool Function(YamlMap) validate) {
319-
if (!yaml!.containsKey(key)) {
325+
if (!yaml.containsKey(key)) {
320326
return false;
321327
}
322328
final dynamic yamlValue = yaml[key];
@@ -329,9 +335,6 @@ class Plugin {
329335
return !validate(yamlValue);
330336
}
331337

332-
if (yaml == null) {
333-
return <String>['Invalid "platforms" specification.'];
334-
}
335338
return <String>[
336339
if (isInvalid(AndroidPlugin.kConfigKey, AndroidPlugin.validate))
337340
'Invalid "android" plugin specification.',

0 commit comments

Comments
 (0)