forked from firka/flutter
Relax the warning of unavailable tokens in gen_defaults when a default value is provided (#140872)
Fixes some false positives and false negatives from the `gen_defaults` tool regarding unavailable tokens in the Material spec. Fixes #140871 # Before  # After  These new issues that are logged are being fixed in https://github.com/flutter/flutter/pull/140573 --- *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
This commit is contained in:
@@ -215,10 +215,10 @@ class _${blockName}DefaultsM3 extends InputDecorationTheme {
|
||||
? componentColor(componentToken1)
|
||||
: componentColor(componentToken2);
|
||||
final double width = (
|
||||
getToken('$componentToken1.width') ??
|
||||
getToken('$componentToken1.height') ??
|
||||
getToken('$componentToken2.width') ??
|
||||
getToken('$componentToken2.height') ??
|
||||
getToken('$componentToken1.width', optional: true) ??
|
||||
getToken('$componentToken1.height', optional: true) ??
|
||||
getToken('$componentToken2.width', optional: true) ??
|
||||
getToken('$componentToken2.height', optional: true) ??
|
||||
1.0) as double;
|
||||
return 'BorderSide(color: $borderColor${width != 1.0 ? ", width: $width" : ""})';
|
||||
}
|
||||
|
||||
@@ -38,7 +38,11 @@ abstract class TokenTemplate {
|
||||
bool tokenAvailable(String tokenName) => _tokens.containsKey(tokenName);
|
||||
|
||||
/// Resolve a token while logging its usage.
|
||||
dynamic getToken(String tokenName) {
|
||||
/// There will be no log if [optional] is true and the token doesn't exist.
|
||||
dynamic getToken(String tokenName, {bool optional = false}) {
|
||||
if (optional && !tokenAvailable(tokenName)) {
|
||||
return null;
|
||||
}
|
||||
tokenLogger.log(tokenName);
|
||||
return _tokens[tokenName];
|
||||
}
|
||||
@@ -108,14 +112,17 @@ abstract class TokenTemplate {
|
||||
/// If there is a value for the given token, this will return
|
||||
/// the value prepended with [colorSchemePrefix].
|
||||
///
|
||||
/// Otherwise it will return [defaultValue].
|
||||
/// Otherwise it will return [defaultValue] if provided or 'null' if not.
|
||||
///
|
||||
/// If a [defaultValue] is not provided and the token doesn't exist, the token
|
||||
/// lookup is logged and a warning will be shown at the end of the process.
|
||||
///
|
||||
/// See also:
|
||||
/// * [componentColor], that provides support for an optional opacity.
|
||||
String color(String colorToken, [String defaultValue = 'null']) {
|
||||
return tokenAvailable(colorToken)
|
||||
? '$colorSchemePrefix${getToken(colorToken)}'
|
||||
: defaultValue;
|
||||
String color(String colorToken, [String? defaultValue]) {
|
||||
final String effectiveDefault = defaultValue ?? 'null';
|
||||
final dynamic tokenVal = getToken(colorToken, optional: defaultValue != null);
|
||||
return tokenVal == null ? effectiveDefault : '$colorSchemePrefix$tokenVal';
|
||||
}
|
||||
|
||||
/// Generate a [ColorScheme] color name for the given token or a transparent
|
||||
@@ -244,7 +251,11 @@ abstract class TokenTemplate {
|
||||
return 'null';
|
||||
}
|
||||
final String borderColor = componentColor(componentToken);
|
||||
final double width = (getToken('$componentToken.width') ?? getToken('$componentToken.height') ?? 1.0) as double;
|
||||
final double width = (
|
||||
getToken('$componentToken.width', optional: true) ??
|
||||
getToken('$componentToken.height', optional: true) ??
|
||||
1.0
|
||||
) as double;
|
||||
return 'BorderSide(color: $borderColor${width != 1.0 ? ", width: $width" : ""})';
|
||||
}
|
||||
|
||||
|
||||
@@ -28,16 +28,20 @@ class TokenLogger {
|
||||
// Sorted set of used tokens.
|
||||
final SplayTreeSet<String> _usedTokens = SplayTreeSet<String>();
|
||||
|
||||
// Set of tokens that were referenced on some templates, but do not exist.
|
||||
final Set<String> _unavailableTokens = <String>{};
|
||||
|
||||
void clear() {
|
||||
_allTokens.clear();
|
||||
_versionMap.clear();
|
||||
_usedTokens.clear();
|
||||
_unavailableTokens.clear();
|
||||
}
|
||||
|
||||
/// Logs a token.
|
||||
void log(String token) {
|
||||
if (!_allTokens.containsKey(token)) {
|
||||
print('\x1B[31m' 'Token unavailable: $token' '\x1B[0m');
|
||||
_unavailableTokens.add(token);
|
||||
return;
|
||||
}
|
||||
_usedTokens.add(token);
|
||||
@@ -76,6 +80,14 @@ class TokenLogger {
|
||||
}
|
||||
|
||||
print('Tokens used: ${_usedTokens.length}/${_allTokens.length}');
|
||||
|
||||
if (_unavailableTokens.isNotEmpty) {
|
||||
print('');
|
||||
print('\x1B[31m' 'Some referenced tokens do not exist: ${_unavailableTokens.length}' '\x1B[0m');
|
||||
for (final String token in _unavailableTokens) {
|
||||
print(' $token');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Dumps version and tokens usage to a file.
|
||||
|
||||
Reference in New Issue
Block a user