Skip to content

Commit

Permalink
update lints, require Dart 3.0 (#156)
Browse files Browse the repository at this point in the history
* switch expressions
* dry up logic
  • Loading branch information
kevmoo authored Dec 12, 2023
1 parent 98a3aab commit 509fd72
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest]
sdk: [2.19.0, dev]
sdk: [3.0.0, dev]
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 3.1.3-wip

* Require Dart 3.0

## 3.1.2

* Require Dart 2.19
Expand Down
3 changes: 0 additions & 3 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ linter:
- package_api_docs
- prefer_const_declarations
- prefer_expression_function_bodies
- prefer_relative_imports
- test_types_in_equals
- use_string_buffers
- use_super_parameters
10 changes: 5 additions & 5 deletions lib/src/equality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ bool deepEquals(Object? obj1, Object? obj2) => _DeepEquals().equals(obj1, obj2);
/// A class that provides access to the list of parent objects used for loop
/// detection.
class _DeepEquals {
final _parents1 = [];
final _parents2 = [];
final _parents1 = <Object?>[];
final _parents2 = <Object?>[];

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

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

parents.add(value);
try {
if (value is Map) {
var equality = const UnorderedIterableEquality();
var equality = const UnorderedIterableEquality<Object?>();
return equality.hash(value.keys.map(deepHashCodeInner)) ^
equality.hash(value.values.map(deepHashCodeInner));
} else if (value is Iterable) {
return const IterableEquality().hash(value.map(deepHashCode));
return const IterableEquality<Object?>().hash(value.map(deepHashCode));
} else if (value is YamlScalar) {
return (value.value as Object?).hashCode;
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/error_listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import 'yaml_exception.dart';

/// A listener that is notified of [YamlError]s during scanning/parsing.
/// A listener that is notified of [YamlException]s during scanning/parsing.
abstract class ErrorListener {
/// This method is invoked when an [error] has been found in the YAML.
void onError(YamlException error);
Expand Down
1 change: 1 addition & 0 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import 'package:source_span/source_span.dart';

import 'parser.dart';
import 'style.dart';
import 'yaml_document.dart';

Expand Down
90 changes: 30 additions & 60 deletions lib/src/loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,14 @@ class Loader {
}

/// Composes a node.
YamlNode _loadNode(Event firstEvent) {
switch (firstEvent.type) {
case EventType.alias:
return _loadAlias(firstEvent as AliasEvent);
case EventType.scalar:
return _loadScalar(firstEvent as ScalarEvent);
case EventType.sequenceStart:
return _loadSequence(firstEvent as SequenceStartEvent);
case EventType.mappingStart:
return _loadMapping(firstEvent as MappingStartEvent);
default:
throw StateError('Unreachable');
}
}
YamlNode _loadNode(Event firstEvent) => switch (firstEvent.type) {
EventType.alias => _loadAlias(firstEvent as AliasEvent),
EventType.scalar => _loadScalar(firstEvent as ScalarEvent),
EventType.sequenceStart =>
_loadSequence(firstEvent as SequenceStartEvent),
EventType.mappingStart => _loadMapping(firstEvent as MappingStartEvent),
_ => throw StateError('Unreachable')
};

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

// Dispatch on the first character.
var firstChar = scalar.value.codeUnitAt(0);
switch (firstChar) {
case $dot:
case $plus:
case $minus:
return _parseNumber(scalar);
case $n:
case $N:
return length == 4 ? _parseNull(scalar) : null;
case $t:
case $T:
return length == 4 ? _parseBool(scalar) : null;
case $f:
case $F:
return length == 5 ? _parseBool(scalar) : null;
case $tilde:
return length == 1 ? YamlScalar.internal(null, scalar) : null;
default:
if (firstChar >= $0 && firstChar <= $9) return _parseNumber(scalar);
return null;
}
return switch (firstChar) {
$dot || $plus || $minus => _parseNumber(scalar),
$n || $N => length == 4 ? _parseNull(scalar) : null,
$t || $T => length == 4 ? _parseBool(scalar) : null,
$f || $F => length == 5 ? _parseBool(scalar) : null,
$tilde => length == 1 ? YamlScalar.internal(null, scalar) : null,
_ => (firstChar >= $0 && firstChar <= $9) ? _parseNumber(scalar) : null
};
}

/// Parse a null scalar.
///
/// Returns a Dart `null` if parsing fails.
YamlScalar? _parseNull(ScalarEvent scalar) {
switch (scalar.value) {
case '':
case 'null':
case 'Null':
case 'NULL':
case '~':
return YamlScalar.internal(null, scalar);
default:
return null;
}
}
YamlScalar? _parseNull(ScalarEvent scalar) => switch (scalar.value) {
'' ||
'null' ||
'Null' ||
'NULL' ||
'~' =>
YamlScalar.internal(null, scalar),
_ => null
};

/// Parse a boolean scalar.
///
/// Returns `null` if parsing fails.
YamlScalar? _parseBool(ScalarEvent scalar) {
switch (scalar.value) {
case 'true':
case 'True':
case 'TRUE':
return YamlScalar.internal(true, scalar);
case 'false':
case 'False':
case 'FALSE':
return YamlScalar.internal(false, scalar);
default:
return null;
}
}
YamlScalar? _parseBool(ScalarEvent scalar) => switch (scalar.value) {
'true' || 'True' || 'TRUE' => YamlScalar.internal(true, scalar),
'false' || 'False' || 'FALSE' => YamlScalar.internal(false, scalar),
_ => null
};

/// Parses a numeric scalar.
///
Expand Down
4 changes: 3 additions & 1 deletion lib/src/null_span.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import 'package:source_span/source_span.dart';

import 'yaml_node.dart';

/// A [SourceSpan] with no location information.
///
/// This is used with [YamlMap.wrap] and [YamlList.wrap] to provide means of
Expand All @@ -20,5 +22,5 @@ class NullSpan extends SourceSpanMixin {
@override
final text = '';

NullSpan(sourceUrl) : start = SourceLocation(0, sourceUrl: sourceUrl);
NullSpan(Object? sourceUrl) : start = SourceLocation(0, sourceUrl: sourceUrl);
}
88 changes: 33 additions & 55 deletions lib/src/scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Scanner {

/// The number of tokens that have been emitted.
///
/// This doesn't count tokens in [tokens].
/// This doesn't count tokens in [_tokens].
var _tokensParsed = 0;

/// Whether the next token in [_tokens] is ready to be returned.
Expand Down Expand Up @@ -249,21 +249,12 @@ class Scanner {
/// See http://yaml.org/spec/1.2/spec.html#nb-char.
bool get _isNonBreak {
var char = _scanner.peekChar();
if (char == null) return false;
switch (char) {
case LF:
case CR:
case BOM:
return false;
case TAB:
case NEL:
return true;
default:
return (char >= 0x00020 && char <= 0x00007E) ||
(char >= 0x000A0 && char <= 0x00D7FF) ||
(char >= 0x0E000 && char <= 0x00FFFD) ||
(char >= 0x10000 && char <= 0x10FFFF);
}
return switch (char) {
null => false,
LF || CR || BOM => false,
TAB || NEL => true,
_ => _isStandardCharacter(char),
};
}

/// Whether the character at the current position is a printable character
Expand All @@ -272,21 +263,12 @@ class Scanner {
/// See http://yaml.org/spec/1.2/spec.html#nb-char.
bool get _isNonSpace {
var char = _scanner.peekChar();
if (char == null) return false;
switch (char) {
case LF:
case CR:
case BOM:
case SP:
return false;
case NEL:
return true;
default:
return (char >= 0x00020 && char <= 0x00007E) ||
(char >= 0x000A0 && char <= 0x00D7FF) ||
(char >= 0x0E000 && char <= 0x00FFFD) ||
(char >= 0x10000 && char <= 0x10FFFF);
}
return switch (char) {
null => false,
LF || CR || BOM || SP => false,
NEL => true,
_ => _isStandardCharacter(char),
};
}

/// Returns Whether or not the current character begins a documentation
Expand Down Expand Up @@ -830,7 +812,7 @@ class Scanner {
}
}

/// Scans a [TokenType.YAML_DIRECTIVE] or [TokenType.tagDirective] token.
/// Scans a [TokenType.versionDirective] or [TokenType.tagDirective] token.
///
/// %YAML 1.2 # a comment \n
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -1593,32 +1575,28 @@ class Scanner {
/// See http://yaml.org/spec/1.2/spec.html#ns-plain-safe(c).
bool _isPlainSafeAt(int offset) {
var char = _scanner.peekChar(offset);
switch (char) {
case COMMA:
case LEFT_SQUARE:
case RIGHT_SQUARE:
case LEFT_CURLY:
case RIGHT_CURLY:
return switch (char) {
null => false,
COMMA ||
LEFT_SQUARE ||
RIGHT_SQUARE ||
LEFT_CURLY ||
RIGHT_CURLY =>
// These characters are delimiters in a flow context and thus are only
// safe in a block context.
return _inBlockContext;
case SP:
case TAB:
case LF:
case CR:
case BOM:
return false;
case NEL:
return true;
default:
return char != null &&
((char >= 0x00020 && char <= 0x00007E) ||
(char >= 0x000A0 && char <= 0x00D7FF) ||
(char >= 0x0E000 && char <= 0x00FFFD) ||
(char >= 0x10000 && char <= 0x10FFFF));
}
_inBlockContext,
SP || TAB || LF || CR || BOM => false,
NEL => true,
_ => _isStandardCharacter(char)
};
}

bool _isStandardCharacter(int char) =>
(char >= 0x00020 && char <= 0x00007E) ||
(char >= 0x000A0 && char <= 0x00D7FF) ||
(char >= 0x0E000 && char <= 0x00FFFD) ||
(char >= 0x10000 && char <= 0x10FFFF);

/// Returns the hexidecimal value of [char].
int _asHex(int char) {
if (char <= NUMBER_9) return char - NUMBER_0;
Expand Down Expand Up @@ -1656,7 +1634,7 @@ class _SimpleKey {
/// The index of the token that begins the simple key.
///
/// This is the index relative to all tokens emitted, rather than relative to
/// [_tokens].
/// [location].
final int tokenNumber;

/// The source location of the beginning of the simple key.
Expand Down
2 changes: 2 additions & 0 deletions lib/src/style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

// ignore_for_file: constant_identifier_names

import 'yaml_node.dart';

/// An enum of source scalar styles.
class ScalarStyle {
/// No source style was specified.
Expand Down
1 change: 1 addition & 0 deletions lib/src/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import 'package:source_span/source_span.dart';

import 'scanner.dart';
import 'style.dart';

/// A token emitted by a [Scanner].
Expand Down
Loading

0 comments on commit 509fd72

Please sign in to comment.