Skip to content

Commit 33fe339

Browse files
authored
update lints, require Dart 3.0 (dart-archive/yaml#156)
* switch expressions * dry up logic
1 parent de85a1a commit 33fe339

17 files changed

+109
-162
lines changed

pkgs/yaml/.github/workflows/test-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
matrix:
4848
# Add macos-latest and/or windows-latest if relevant for this package.
4949
os: [ubuntu-latest]
50-
sdk: [2.19.0, dev]
50+
sdk: [3.0.0, dev]
5151
steps:
5252
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
5353
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d

pkgs/yaml/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 3.1.3-wip
22

3+
* Require Dart 3.0
4+
35
## 3.1.2
46

57
* Require Dart 2.19

pkgs/yaml/analysis_options.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,4 @@ linter:
1616
- package_api_docs
1717
- prefer_const_declarations
1818
- prefer_expression_function_bodies
19-
- prefer_relative_imports
20-
- test_types_in_equals
2119
- use_string_buffers
22-
- use_super_parameters

pkgs/yaml/lib/src/equality.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ bool deepEquals(Object? obj1, Object? obj2) => _DeepEquals().equals(obj1, obj2);
2424
/// A class that provides access to the list of parent objects used for loop
2525
/// detection.
2626
class _DeepEquals {
27-
final _parents1 = [];
28-
final _parents2 = [];
27+
final _parents1 = <Object?>[];
28+
final _parents2 = <Object?>[];
2929

3030
/// Returns whether [obj1] and [obj2] are structurally equivalent.
3131
bool equals(Object? obj1, Object? obj2) {
@@ -101,19 +101,19 @@ class _DeepEquals {
101101
/// self-referential structures, and returns the same hash code for
102102
/// [YamlScalar]s and their values.
103103
int deepHashCode(Object? obj) {
104-
var parents = [];
104+
var parents = <Object?>[];
105105

106106
int deepHashCodeInner(Object? value) {
107107
if (parents.any((parent) => identical(parent, value))) return -1;
108108

109109
parents.add(value);
110110
try {
111111
if (value is Map) {
112-
var equality = const UnorderedIterableEquality();
112+
var equality = const UnorderedIterableEquality<Object?>();
113113
return equality.hash(value.keys.map(deepHashCodeInner)) ^
114114
equality.hash(value.values.map(deepHashCodeInner));
115115
} else if (value is Iterable) {
116-
return const IterableEquality().hash(value.map(deepHashCode));
116+
return const IterableEquality<Object?>().hash(value.map(deepHashCode));
117117
} else if (value is YamlScalar) {
118118
return (value.value as Object?).hashCode;
119119
} else {

pkgs/yaml/lib/src/error_listener.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import 'yaml_exception.dart';
99

10-
/// A listener that is notified of [YamlError]s during scanning/parsing.
10+
/// A listener that is notified of [YamlException]s during scanning/parsing.
1111
abstract class ErrorListener {
1212
/// This method is invoked when an [error] has been found in the YAML.
1313
void onError(YamlException error);

pkgs/yaml/lib/src/event.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import 'package:source_span/source_span.dart';
99

10+
import 'parser.dart';
1011
import 'style.dart';
1112
import 'yaml_document.dart';
1213

pkgs/yaml/lib/src/loader.dart

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,14 @@ class Loader {
8080
}
8181

8282
/// Composes a node.
83-
YamlNode _loadNode(Event firstEvent) {
84-
switch (firstEvent.type) {
85-
case EventType.alias:
86-
return _loadAlias(firstEvent as AliasEvent);
87-
case EventType.scalar:
88-
return _loadScalar(firstEvent as ScalarEvent);
89-
case EventType.sequenceStart:
90-
return _loadSequence(firstEvent as SequenceStartEvent);
91-
case EventType.mappingStart:
92-
return _loadMapping(firstEvent as MappingStartEvent);
93-
default:
94-
throw StateError('Unreachable');
95-
}
96-
}
83+
YamlNode _loadNode(Event firstEvent) => switch (firstEvent.type) {
84+
EventType.alias => _loadAlias(firstEvent as AliasEvent),
85+
EventType.scalar => _loadScalar(firstEvent as ScalarEvent),
86+
EventType.sequenceStart =>
87+
_loadSequence(firstEvent as SequenceStartEvent),
88+
EventType.mappingStart => _loadMapping(firstEvent as MappingStartEvent),
89+
_ => throw StateError('Unreachable')
90+
};
9791

9892
/// Registers an anchor.
9993
void _registerAnchor(String? anchor, YamlNode node) {
@@ -220,61 +214,37 @@ class Loader {
220214

221215
// Dispatch on the first character.
222216
var firstChar = scalar.value.codeUnitAt(0);
223-
switch (firstChar) {
224-
case $dot:
225-
case $plus:
226-
case $minus:
227-
return _parseNumber(scalar);
228-
case $n:
229-
case $N:
230-
return length == 4 ? _parseNull(scalar) : null;
231-
case $t:
232-
case $T:
233-
return length == 4 ? _parseBool(scalar) : null;
234-
case $f:
235-
case $F:
236-
return length == 5 ? _parseBool(scalar) : null;
237-
case $tilde:
238-
return length == 1 ? YamlScalar.internal(null, scalar) : null;
239-
default:
240-
if (firstChar >= $0 && firstChar <= $9) return _parseNumber(scalar);
241-
return null;
242-
}
217+
return switch (firstChar) {
218+
$dot || $plus || $minus => _parseNumber(scalar),
219+
$n || $N => length == 4 ? _parseNull(scalar) : null,
220+
$t || $T => length == 4 ? _parseBool(scalar) : null,
221+
$f || $F => length == 5 ? _parseBool(scalar) : null,
222+
$tilde => length == 1 ? YamlScalar.internal(null, scalar) : null,
223+
_ => (firstChar >= $0 && firstChar <= $9) ? _parseNumber(scalar) : null
224+
};
243225
}
244226

245227
/// Parse a null scalar.
246228
///
247229
/// Returns a Dart `null` if parsing fails.
248-
YamlScalar? _parseNull(ScalarEvent scalar) {
249-
switch (scalar.value) {
250-
case '':
251-
case 'null':
252-
case 'Null':
253-
case 'NULL':
254-
case '~':
255-
return YamlScalar.internal(null, scalar);
256-
default:
257-
return null;
258-
}
259-
}
230+
YamlScalar? _parseNull(ScalarEvent scalar) => switch (scalar.value) {
231+
'' ||
232+
'null' ||
233+
'Null' ||
234+
'NULL' ||
235+
'~' =>
236+
YamlScalar.internal(null, scalar),
237+
_ => null
238+
};
260239

261240
/// Parse a boolean scalar.
262241
///
263242
/// Returns `null` if parsing fails.
264-
YamlScalar? _parseBool(ScalarEvent scalar) {
265-
switch (scalar.value) {
266-
case 'true':
267-
case 'True':
268-
case 'TRUE':
269-
return YamlScalar.internal(true, scalar);
270-
case 'false':
271-
case 'False':
272-
case 'FALSE':
273-
return YamlScalar.internal(false, scalar);
274-
default:
275-
return null;
276-
}
277-
}
243+
YamlScalar? _parseBool(ScalarEvent scalar) => switch (scalar.value) {
244+
'true' || 'True' || 'TRUE' => YamlScalar.internal(true, scalar),
245+
'false' || 'False' || 'FALSE' => YamlScalar.internal(false, scalar),
246+
_ => null
247+
};
278248

279249
/// Parses a numeric scalar.
280250
///

pkgs/yaml/lib/src/null_span.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import 'package:source_span/source_span.dart';
99

10+
import 'yaml_node.dart';
11+
1012
/// A [SourceSpan] with no location information.
1113
///
1214
/// This is used with [YamlMap.wrap] and [YamlList.wrap] to provide means of
@@ -20,5 +22,5 @@ class NullSpan extends SourceSpanMixin {
2022
@override
2123
final text = '';
2224

23-
NullSpan(sourceUrl) : start = SourceLocation(0, sourceUrl: sourceUrl);
25+
NullSpan(Object? sourceUrl) : start = SourceLocation(0, sourceUrl: sourceUrl);
2426
}

pkgs/yaml/lib/src/scanner.dart

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Scanner {
124124

125125
/// The number of tokens that have been emitted.
126126
///
127-
/// This doesn't count tokens in [tokens].
127+
/// This doesn't count tokens in [_tokens].
128128
var _tokensParsed = 0;
129129

130130
/// Whether the next token in [_tokens] is ready to be returned.
@@ -249,21 +249,12 @@ class Scanner {
249249
/// See http://yaml.org/spec/1.2/spec.html#nb-char.
250250
bool get _isNonBreak {
251251
var char = _scanner.peekChar();
252-
if (char == null) return false;
253-
switch (char) {
254-
case LF:
255-
case CR:
256-
case BOM:
257-
return false;
258-
case TAB:
259-
case NEL:
260-
return true;
261-
default:
262-
return (char >= 0x00020 && char <= 0x00007E) ||
263-
(char >= 0x000A0 && char <= 0x00D7FF) ||
264-
(char >= 0x0E000 && char <= 0x00FFFD) ||
265-
(char >= 0x10000 && char <= 0x10FFFF);
266-
}
252+
return switch (char) {
253+
null => false,
254+
LF || CR || BOM => false,
255+
TAB || NEL => true,
256+
_ => _isStandardCharacter(char),
257+
};
267258
}
268259

269260
/// Whether the character at the current position is a printable character
@@ -272,21 +263,12 @@ class Scanner {
272263
/// See http://yaml.org/spec/1.2/spec.html#nb-char.
273264
bool get _isNonSpace {
274265
var char = _scanner.peekChar();
275-
if (char == null) return false;
276-
switch (char) {
277-
case LF:
278-
case CR:
279-
case BOM:
280-
case SP:
281-
return false;
282-
case NEL:
283-
return true;
284-
default:
285-
return (char >= 0x00020 && char <= 0x00007E) ||
286-
(char >= 0x000A0 && char <= 0x00D7FF) ||
287-
(char >= 0x0E000 && char <= 0x00FFFD) ||
288-
(char >= 0x10000 && char <= 0x10FFFF);
289-
}
266+
return switch (char) {
267+
null => false,
268+
LF || CR || BOM || SP => false,
269+
NEL => true,
270+
_ => _isStandardCharacter(char),
271+
};
290272
}
291273

292274
/// Returns Whether or not the current character begins a documentation
@@ -830,7 +812,7 @@ class Scanner {
830812
}
831813
}
832814

833-
/// Scans a [TokenType.YAML_DIRECTIVE] or [TokenType.tagDirective] token.
815+
/// Scans a [TokenType.versionDirective] or [TokenType.tagDirective] token.
834816
///
835817
/// %YAML 1.2 # a comment \n
836818
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1593,32 +1575,28 @@ class Scanner {
15931575
/// See http://yaml.org/spec/1.2/spec.html#ns-plain-safe(c).
15941576
bool _isPlainSafeAt(int offset) {
15951577
var char = _scanner.peekChar(offset);
1596-
switch (char) {
1597-
case COMMA:
1598-
case LEFT_SQUARE:
1599-
case RIGHT_SQUARE:
1600-
case LEFT_CURLY:
1601-
case RIGHT_CURLY:
1578+
return switch (char) {
1579+
null => false,
1580+
COMMA ||
1581+
LEFT_SQUARE ||
1582+
RIGHT_SQUARE ||
1583+
LEFT_CURLY ||
1584+
RIGHT_CURLY =>
16021585
// These characters are delimiters in a flow context and thus are only
16031586
// safe in a block context.
1604-
return _inBlockContext;
1605-
case SP:
1606-
case TAB:
1607-
case LF:
1608-
case CR:
1609-
case BOM:
1610-
return false;
1611-
case NEL:
1612-
return true;
1613-
default:
1614-
return char != null &&
1615-
((char >= 0x00020 && char <= 0x00007E) ||
1616-
(char >= 0x000A0 && char <= 0x00D7FF) ||
1617-
(char >= 0x0E000 && char <= 0x00FFFD) ||
1618-
(char >= 0x10000 && char <= 0x10FFFF));
1619-
}
1587+
_inBlockContext,
1588+
SP || TAB || LF || CR || BOM => false,
1589+
NEL => true,
1590+
_ => _isStandardCharacter(char)
1591+
};
16201592
}
16211593

1594+
bool _isStandardCharacter(int char) =>
1595+
(char >= 0x00020 && char <= 0x00007E) ||
1596+
(char >= 0x000A0 && char <= 0x00D7FF) ||
1597+
(char >= 0x0E000 && char <= 0x00FFFD) ||
1598+
(char >= 0x10000 && char <= 0x10FFFF);
1599+
16221600
/// Returns the hexidecimal value of [char].
16231601
int _asHex(int char) {
16241602
if (char <= NUMBER_9) return char - NUMBER_0;
@@ -1656,7 +1634,7 @@ class _SimpleKey {
16561634
/// The index of the token that begins the simple key.
16571635
///
16581636
/// This is the index relative to all tokens emitted, rather than relative to
1659-
/// [_tokens].
1637+
/// [location].
16601638
final int tokenNumber;
16611639

16621640
/// The source location of the beginning of the simple key.

pkgs/yaml/lib/src/style.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
// ignore_for_file: constant_identifier_names
99

10+
import 'yaml_node.dart';
11+
1012
/// An enum of source scalar styles.
1113
class ScalarStyle {
1214
/// No source style was specified.

0 commit comments

Comments
 (0)