Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Enable and fix a number of lints, test on oldest supported SDK #15

Merged
merged 1 commit into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: dart

dart:
- dev
- stable
- 2.0.0

dart_task:
- test: --platform vm,chrome
Expand All @@ -13,7 +13,11 @@ matrix:
- dart: dev
dart_task: dartfmt
- dart: dev
dart_task: analyzer
dart_task:
dartanalyzer: --fatal-warnings --fatal-hints .
- dart: 2.0.0
dart_task:
dartanalyzer: --fatal-warnings .

# Only building master means that we don't run two builds for each pull request.
branches:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.5

- Update Dart SDK constraint to `>=2.0.0 <3.0.0`.

## 1.0.4

* Add @alwaysThrows annotation to error method.
Expand Down
93 changes: 93 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
include: package:pedantic/analysis_options.yaml
analyzer:
# strong-mode:
# implicit-casts: false
linter:
rules:
- always_declare_return_types
#- annotate_overrides
- avoid_bool_literals_in_conditional_expressions
- avoid_classes_with_only_static_members
- avoid_empty_else
- avoid_function_literals_in_foreach_calls
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_relative_lib_imports
- avoid_renaming_method_parameters
- avoid_return_types_on_setters
#- avoid_returning_null
- avoid_returning_null_for_future
- avoid_returning_null_for_void
- avoid_returning_this
- avoid_shadowing_type_parameters
- avoid_single_cascade_in_expression_statements
- avoid_types_as_parameter_names
- avoid_unused_constructor_parameters
- await_only_futures
- camel_case_types
- cancel_subscriptions
#- cascade_invocations
- comment_references
- constant_identifier_names
- control_flow_in_finally
- directives_ordering
- empty_catches
- empty_constructor_bodies
- empty_statements
- file_names
- hash_and_equals
- implementation_imports
- invariant_booleans
- iterable_contains_unrelated_type
- join_return_with_assignment
- library_names
- library_prefixes
- list_remove_unrelated_type
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- no_duplicate_case_values
- non_constant_identifier_names
- null_closures
- omit_local_variable_types
- only_throw_errors
- overridden_fields
- package_api_docs
- package_names
- package_prefixed_library_names
- prefer_adjacent_string_concatenation
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_const_constructors
- prefer_contains
- prefer_equal_for_default_values
- prefer_final_fields
#- prefer_final_locals
- prefer_generic_function_type_aliases
- prefer_initializing_formals
- prefer_interpolation_to_compose_strings
- prefer_is_empty
- prefer_is_not_empty
- prefer_null_aware_operators
#- prefer_single_quotes
- prefer_typing_uninitialized_variables
- recursive_getters
- slash_for_doc_comments
- test_types_in_equals
- throw_in_finally
- type_init_formals
- unawaited_futures
- unnecessary_await_in_return
- unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_new
- unnecessary_null_aware_assignments
- unnecessary_parenthesis
- unnecessary_statements
- unnecessary_this
- unrelated_type_equality_checks
- use_function_type_syntax_for_parameters
- use_rethrow_when_possible
- valid_regexps
- void_checks
6 changes: 3 additions & 3 deletions lib/src/eager_span_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'span_scanner.dart';
// sdk#23770 is fully complete, we should move the shared code into a mixin.

/// A regular expression matching newlines across platforms.
final _newlineRegExp = new RegExp(r"\r\n?|\n");
final _newlineRegExp = RegExp(r"\r\n?|\n");

/// A [SpanScanner] that tracks the line and column eagerly, like [LineScanner].
class EagerSpanScanner extends SpanScanner {
Expand All @@ -22,14 +22,14 @@ class EagerSpanScanner extends SpanScanner {
int _column = 0;

LineScannerState get state =>
new _EagerSpanScannerState(this, position, line, column);
_EagerSpanScannerState(this, position, line, column);

bool get _betweenCRLF => peekChar(-1) == $cr && peekChar() == $lf;

set state(LineScannerState state) {
if (state is! _EagerSpanScannerState ||
!identical((state as _EagerSpanScannerState)._scanner, this)) {
throw new ArgumentError("The given LineScannerState was not returned by "
throw ArgumentError("The given LineScannerState was not returned by "
"this LineScanner.");
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'package:source_span/source_span.dart';

import 'string_scanner.dart';

/// An exception thrown by a [StringScanner] that failed to parse a string.
class StringScannerException extends SourceSpanFormatException {
String get source => super.source;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/line_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'string_scanner.dart';
// Note that much of this code is duplicated in eager_span_scanner.dart.

/// A regular expression matching newlines across platforms.
final _newlineRegExp = new RegExp(r"\r\n?|\n");
final _newlineRegExp = RegExp(r"\r\n?|\n");

/// A subclass of [StringScanner] that tracks line and column information.
class LineScanner extends StringScanner {
Expand All @@ -29,15 +29,15 @@ class LineScanner extends StringScanner {
///
/// This does not include the scanner's match information.
LineScannerState get state =>
new LineScannerState._(this, position, line, column);
LineScannerState._(this, position, line, column);

/// Whether the current position is between a CR character and an LF
/// charactet.
bool get _betweenCRLF => peekChar(-1) == $cr && peekChar() == $lf;

set state(LineScannerState state) {
if (!identical(state._scanner, this)) {
throw new ArgumentError("The given LineScannerState was not returned by "
throw ArgumentError("The given LineScannerState was not returned by "
"this LineScanner.");
}

Expand Down
16 changes: 7 additions & 9 deletions lib/src/relative_span_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'utils.dart';
class RelativeSpanScanner extends StringScanner implements SpanScanner {
/// The source of the scanner.
///
/// This caches line break information and is used to generate [Span]s.
/// This caches line break information and is used to generate [SourceSpan]s.
final SourceFile _sourceFile;

/// The start location of the span within which this scanner is scanning.
Expand All @@ -40,16 +40,16 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
: column;
}

LineScannerState get state => new _SpanScannerState(this, position);
LineScannerState get state => _SpanScannerState(this, position);

set state(LineScannerState state) {
if (state is! _SpanScannerState ||
!identical((state as _SpanScannerState)._scanner, this)) {
throw new ArgumentError("The given LineScannerState was not returned by "
throw ArgumentError("The given LineScannerState was not returned by "
"this LineScanner.");
}

this.position = state.position;
position = state.position;
}

FileSpan get lastSpan => _lastSpan;
Expand Down Expand Up @@ -86,14 +86,12 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
validateErrorArgs(string, match, position, length);

if (match == null && position == null && length == null) match = lastMatch;
if (position == null) {
position = match == null ? this.position : match.start;
}
if (length == null) length = match == null ? 1 : match.end - match.start;
position ??= match == null ? this.position : match.start;
length ??= match == null ? 1 : match.end - match.start;

var span = _sourceFile.span(_startLocation.offset + position,
_startLocation.offset + position + length);
throw new StringScannerException(message, span, string);
throw StringScannerException(message, span, string);
}
}

Expand Down
24 changes: 11 additions & 13 deletions lib/src/span_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ import 'string_scanner.dart';
import 'utils.dart';

/// A subclass of [LineScanner] that exposes matched ranges as source map
/// [Span]s.
/// [FileSpan]s.
class SpanScanner extends StringScanner implements LineScanner {
/// The source of the scanner.
///
/// This caches line break information and is used to generate [Span]s.
/// This caches line break information and is used to generate [FileSpan]s.
final SourceFile _sourceFile;

int get line => _sourceFile.getLine(position);
int get column => _sourceFile.getColumn(position);

LineScannerState get state => new _SpanScannerState(this, position);
LineScannerState get state => _SpanScannerState(this, position);

set state(LineScannerState state) {
if (state is! _SpanScannerState ||
!identical((state as _SpanScannerState)._scanner, this)) {
throw new ArgumentError("The given LineScannerState was not returned by "
throw ArgumentError("The given LineScannerState was not returned by "
"this LineScanner.");
}

this.position = state.position;
position = state.position;
}

/// The [FileSpan] for [lastMatch].
Expand All @@ -57,7 +57,7 @@ class SpanScanner extends StringScanner implements LineScanner {
/// [FileSpan]s as well as for error reporting. It can be a [String], a
/// [Uri], or `null`.
SpanScanner(String string, {sourceUrl, int position})
: _sourceFile = new SourceFile.fromString(string, url: sourceUrl),
: _sourceFile = SourceFile.fromString(string, url: sourceUrl),
super(string, sourceUrl: sourceUrl, position: position);

/// Creates a new [SpanScanner] that eagerly computes line and column numbers.
Expand All @@ -76,9 +76,9 @@ class SpanScanner extends StringScanner implements LineScanner {

/// Creates a new [SpanScanner] that scans within [span].
///
/// This scans through [span.text], but emits new spans from [span.file] in
/// This scans through [span]`.text, but emits new spans from [span]`.file` in
/// their appropriate relative positions. The [string] field contains only
/// [span.text], and [position], [line], and [column] are all relative to the
/// [span]`.text`, and [position], [line], and [column] are all relative to the
/// span.
factory SpanScanner.within(FileSpan span) = RelativeSpanScanner;

Expand All @@ -103,13 +103,11 @@ class SpanScanner extends StringScanner implements LineScanner {
validateErrorArgs(string, match, position, length);

if (match == null && position == null && length == null) match = lastMatch;
if (position == null) {
position = match == null ? this.position : match.start;
}
if (length == null) length = match == null ? 0 : match.end - match.start;
position ??= match == null ? this.position : match.start;
length ??= match == null ? 0 : match.end - match.start;

var span = _sourceFile.span(position, position + length);
throw new StringScannerException(message, span, string);
throw StringScannerException(message, span, string);
}
}

Expand Down
22 changes: 10 additions & 12 deletions lib/src/string_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'utils.dart';
/// When compiled to JS, forward slashes are always escaped in [RegExp.pattern].
///
/// See issue 17998.
final _slashAutoEscape = new RegExp("/").pattern == "\\/";
final _slashAutoEscape = RegExp("/").pattern == "\\/";

/// A class that scans through a string using [Pattern]s.
class StringScanner {
Expand All @@ -29,7 +29,7 @@ class StringScanner {
int get position => _position;
set position(int position) {
if (position < 0 || position > string.length) {
throw new ArgumentError("Invalid position $position");
throw ArgumentError("Invalid position $position");
}

_position = position;
Expand Down Expand Up @@ -84,7 +84,7 @@ class StringScanner {
/// This returns `null` if [offset] points outside the string. It doesn't
/// affect [lastMatch].
int peekChar([int offset]) {
if (offset == null) offset = 0;
offset ??= 0;
var index = position + offset;
if (index < 0 || index >= string.length) return null;
return string.codeUnitAt(index);
Expand Down Expand Up @@ -115,7 +115,7 @@ class StringScanner {
} else if (character == $double_quote) {
name = r'"\""';
} else {
name = '"${new String.fromCharCode(character)}"';
name = '"${String.fromCharCode(character)}"';
}
}

Expand Down Expand Up @@ -181,7 +181,7 @@ class StringScanner {
/// Unlike [String.substring], [end] defaults to [position] rather than the
/// end of the string.
String substring(int start, [int end]) {
if (end == null) end = position;
end ??= position;
return string.substring(start, end);
}

Expand All @@ -203,20 +203,18 @@ class StringScanner {
validateErrorArgs(string, match, position, length);

if (match == null && position == null && length == null) match = lastMatch;
if (position == null) {
position = match == null ? this.position : match.start;
}
if (length == null) length = match == null ? 0 : match.end - match.start;
position ??= match == null ? this.position : match.start;
length ??= match == null ? 0 : match.end - match.start;

var sourceFile = new SourceFile.fromString(string, url: sourceUrl);
var sourceFile = SourceFile.fromString(string, url: sourceUrl);
var span = sourceFile.span(position, position + length);
throw new StringScannerException(message, span, string);
throw StringScannerException(message, span, string);
}

// TODO(nweiz): Make this handle long lines more gracefully.
/// Throws a [FormatException] describing that [name] is expected at the
/// current position in the string.
void _fail(String name) {
error("expected $name.", position: this.position, length: 0);
error("expected $name.", position: position, length: 0);
}
}
Loading