Skip to content

Commit

Permalink
Enable and fix standard lints, test on oldest supported Dart sdk (flu…
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Apr 26, 2019
1 parent 6010304 commit 8aa847c
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 94 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: dart
sudo: false

dart:
#- stable
- 2.0.0
- dev

dart_task:
- test: -p vm
xvfb: false
Expand Down
40 changes: 40 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
include: package:pedantic/analysis_options.yaml
linter:
rules:
- avoid_empty_else
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_unused_constructor_parameters
- await_only_futures
- camel_case_types
- cancel_subscriptions
- constant_identifier_names
- control_flow_in_finally
- directives_ordering
- empty_catches
- empty_constructor_bodies
- empty_statements
- hash_and_equals
- implementation_imports
- iterable_contains_unrelated_type
- library_names
- library_prefixes
- list_remove_unrelated_type
- non_constant_identifier_names
- overridden_fields
- package_api_docs
- package_names
- package_prefixed_library_names
- prefer_equal_for_default_values
- prefer_final_fields
- prefer_generic_function_type_aliases
- prefer_is_not_empty
- slash_for_doc_comments
- test_types_in_equals
- throw_in_finally
- type_init_formals
- unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_new
- unrelated_type_equality_checks
- valid_regexps
4 changes: 2 additions & 2 deletions lib/boolean_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import 'src/none.dart';
/// same parsed structure are considered equal.
abstract class BooleanSelector {
/// A selector that accepts all inputs.
static const all = const All();
static const all = All();

/// A selector that accepts no inputs.
static const none = const None();
static const none = None();

/// All the variables in this selector, in the order they appear.
Iterable<String> get variables;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/evaluator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'ast.dart';
import 'visitor.dart';

typedef bool _Semantics(String variable);
typedef _Semantics = bool Function(String variable);

/// A visitor for evaluating boolean selectors against a specific set of
/// semantics.
Expand Down
14 changes: 7 additions & 7 deletions lib/src/impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ class BooleanSelectorImpl implements BooleanSelector {
/// This will throw a [SourceSpanFormatException] if the selector is
/// malformed or if it uses an undefined variable.
BooleanSelectorImpl.parse(String selector)
: _selector = new Parser(selector).parse();
: _selector = Parser(selector).parse();

BooleanSelectorImpl._(this._selector);

Iterable<String> get variables => _selector.variables;

bool evaluate(semantics) => _selector.accept(new Evaluator(semantics));
bool evaluate(semantics) => _selector.accept(Evaluator(semantics));

BooleanSelector intersection(BooleanSelector other) {
if (other == BooleanSelector.all) return this;
if (other == BooleanSelector.none) return other;
return other is BooleanSelectorImpl
? new BooleanSelectorImpl._(new AndNode(_selector, other._selector))
: new IntersectionSelector(this, other);
? BooleanSelectorImpl._(AndNode(_selector, other._selector))
: IntersectionSelector(this, other);
}

BooleanSelector union(BooleanSelector other) {
if (other == BooleanSelector.all) return other;
if (other == BooleanSelector.none) return this;
return other is BooleanSelectorImpl
? new BooleanSelectorImpl._(new OrNode(_selector, other._selector))
: new UnionSelector(this, other);
? BooleanSelectorImpl._(OrNode(_selector, other._selector))
: UnionSelector(this, other);
}

void validate(bool isDefined(String variable)) {
_selector.accept(new Validator(isDefined));
_selector.accept(Validator(isDefined));
}

String toString() => _selector.toString();
Expand Down
5 changes: 2 additions & 3 deletions lib/src/intersection_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ class IntersectionSelector implements BooleanSelector {
_selector1.evaluate(semantics) && _selector2.evaluate(semantics);

BooleanSelector intersection(BooleanSelector other) =>
new IntersectionSelector(this, other);
IntersectionSelector(this, other);

BooleanSelector union(BooleanSelector other) =>
new UnionSelector(this, other);
BooleanSelector union(BooleanSelector other) => UnionSelector(this, other);

void validate(bool isDefined(String variable)) {
_selector1.validate(isDefined);
Expand Down
21 changes: 10 additions & 11 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Parser {
/// The scanner that tokenizes the selector.
final Scanner _scanner;

Parser(String selector) : _scanner = new Scanner(selector);
Parser(String selector) : _scanner = Scanner(selector);

/// Parses the selector.
///
Expand All @@ -27,7 +27,7 @@ class Parser {
var selector = _conditional();

if (_scanner.peek().type != TokenType.endOfFile) {
throw new SourceSpanFormatException(
throw SourceSpanFormatException(
"Expected end of input.", _scanner.peek().span);
}

Expand All @@ -45,12 +45,11 @@ class Parser {

var whenTrue = _conditional();
if (!_scanner.scan(TokenType.colon)) {
throw new SourceSpanFormatException(
'Expected ":".', _scanner.peek().span);
throw SourceSpanFormatException('Expected ":".', _scanner.peek().span);
}

var whenFalse = _conditional();
return new ConditionalNode(condition, whenTrue, whenFalse);
return ConditionalNode(condition, whenTrue, whenFalse);
}

/// Parses a logical or:
Expand All @@ -60,7 +59,7 @@ class Parser {
Node _or() {
var left = _and();
if (!_scanner.scan(TokenType.or)) return left;
return new OrNode(left, _or());
return OrNode(left, _or());
}

/// Parses a logical and:
Expand All @@ -70,7 +69,7 @@ class Parser {
Node _and() {
var left = _simpleExpression();
if (!_scanner.scan(TokenType.and)) return left;
return new AndNode(left, _and());
return AndNode(left, _and());
}

/// Parses a simple expression:
Expand All @@ -84,21 +83,21 @@ class Parser {
switch (token.type) {
case TokenType.not:
var child = _simpleExpression();
return new NotNode(child, token.span.expand(child.span));
return NotNode(child, token.span.expand(child.span));

case TokenType.leftParen:
var child = _conditional();
if (!_scanner.scan(TokenType.rightParen)) {
throw new SourceSpanFormatException(
throw SourceSpanFormatException(
'Expected ")".', _scanner.peek().span);
}
return child;

case TokenType.identifier:
return new VariableNode((token as IdentifierToken).name, token.span);
return VariableNode((token as IdentifierToken).name, token.span);

default:
throw new SourceSpanFormatException("Expected expression.", token.span);
throw SourceSpanFormatException("Expected expression.", token.span);
}
}
}
21 changes: 10 additions & 11 deletions lib/src/scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ import 'token.dart';
/// A regular expression matching both whitespace and single-line comments.
///
/// This will only match if consumes at least one character.
final _whitespaceAndSingleLineComments =
new RegExp(r"([ \t\n]+|//[^\n]*(\n|$))+");
final _whitespaceAndSingleLineComments = RegExp(r"([ \t\n]+|//[^\n]*(\n|$))+");

/// A regular expression matching the body of a multi-line comment, after `/*`
/// but before `*/` or a nested `/*`.
///
/// This will only match if it consumes at least one character.
final _multiLineCommentBody = new RegExp(r"([^/*]|/[^*]|\*[^/])+");
final _multiLineCommentBody = RegExp(r"([^/*]|/[^*]|\*[^/])+");

/// A regular expression matching a hyphenated identifier.
///
/// This is like a standard Dart identifier, except that it can also contain
/// hyphens.
final _hyphenatedIdentifier = new RegExp(r"[a-zA-Z_-][a-zA-Z0-9_-]*");
final _hyphenatedIdentifier = RegExp(r"[a-zA-Z_-][a-zA-Z0-9_-]*");

/// A scanner that converts a boolean selector string into a stream of tokens.
class Scanner {
Expand All @@ -35,7 +34,7 @@ class Scanner {
/// Whether the scanner has emitted a [TokenType.endOfFile] token.
bool _endOfFileEmitted = false;

Scanner(String selector) : _scanner = new SpanScanner(selector);
Scanner(String selector) : _scanner = SpanScanner(selector);

/// Returns the next token that will be returned by [next].
///
Expand Down Expand Up @@ -70,11 +69,11 @@ class Scanner {

/// Scan and return the next token in the stream.
Token _getNext() {
if (_endOfFileEmitted) throw new StateError("No more tokens.");
if (_endOfFileEmitted) throw StateError("No more tokens.");

_consumeWhitespace();
if (_scanner.isDone) {
return new Token(TokenType.endOfFile, _scanner.spanFrom(_scanner.state));
return Token(TokenType.endOfFile, _scanner.spanFrom(_scanner.state));
}

switch (_scanner.peekChar()) {
Expand Down Expand Up @@ -104,7 +103,7 @@ class Scanner {
Token _scanOperator(TokenType type) {
var start = _scanner.state;
_scanner.readChar();
return new Token(type, _scanner.spanFrom(start));
return Token(type, _scanner.spanFrom(start));
}

/// Scans a `||` operator and returns the appropriate token.
Expand All @@ -113,7 +112,7 @@ class Scanner {
Token _scanOr() {
var start = _scanner.state;
_scanner.expect("||");
return new Token(TokenType.or, _scanner.spanFrom(start));
return Token(TokenType.or, _scanner.spanFrom(start));
}

/// Scans a `&&` operator and returns the appropriate token.
Expand All @@ -122,13 +121,13 @@ class Scanner {
Token _scanAnd() {
var start = _scanner.state;
_scanner.expect("&&");
return new Token(TokenType.and, _scanner.spanFrom(start));
return Token(TokenType.and, _scanner.spanFrom(start));
}

/// Scans and returns an identifier token.
Token _scanIdentifier() {
_scanner.expect(_hyphenatedIdentifier, name: "expression");
return new IdentifierToken(_scanner.lastMatch[0], _scanner.lastSpan);
return IdentifierToken(_scanner.lastMatch[0], _scanner.lastSpan);
}

/// Consumes all whitespace and comments immediately following the cursor's
Expand Down
18 changes: 9 additions & 9 deletions lib/src/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ class IdentifierToken implements Token {
/// An enumeration of types of tokens.
class TokenType {
/// A `(` character.
static const leftParen = const TokenType._("left paren");
static const leftParen = TokenType._("left paren");

/// A `)` character.
static const rightParen = const TokenType._("right paren");
static const rightParen = TokenType._("right paren");

/// A `||` sequence.
static const or = const TokenType._("or");
static const or = TokenType._("or");

/// A `&&` sequence.
static const and = const TokenType._("and");
static const and = TokenType._("and");

/// A `!` character.
static const not = const TokenType._("not");
static const not = TokenType._("not");

/// A `?` character.
static const questionMark = const TokenType._("question mark");
static const questionMark = TokenType._("question mark");

/// A `:` character.
static const colon = const TokenType._("colon");
static const colon = TokenType._("colon");

/// A named identifier.
static const identifier = const TokenType._("identifier");
static const identifier = TokenType._("identifier");

/// The end of the selector.
static const endOfFile = const TokenType._("end of file");
static const endOfFile = TokenType._("end of file");

/// The name of the token type.
final String name;
Expand Down
5 changes: 2 additions & 3 deletions lib/src/union_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ class UnionSelector implements BooleanSelector {
_selector1.evaluate(semantics) || _selector2.evaluate(semantics);

BooleanSelector intersection(BooleanSelector other) =>
new IntersectionSelector(this, other);
IntersectionSelector(this, other);

BooleanSelector union(BooleanSelector other) =>
new UnionSelector(this, other);
BooleanSelector union(BooleanSelector other) => UnionSelector(this, other);

void validate(bool isDefined(String variable)) {
_selector1.validate(isDefined);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:source_span/source_span.dart';
import 'ast.dart';
import 'visitor.dart';

typedef bool _IsDefined(String variable);
typedef _IsDefined = bool Function(String variable);

/// An AST visitor that ensures that all variables are valid.
class Validator extends RecursiveVisitor {
Expand All @@ -17,6 +17,6 @@ class Validator extends RecursiveVisitor {

void visitVariable(VariableNode node) {
if (_isDefined(node.name)) return;
throw new SourceSpanFormatException("Undefined variable.", node.span);
throw SourceSpanFormatException("Undefined variable.", node.span);
}
}
7 changes: 4 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
name: boolean_selector
version: 1.0.4
version: 1.0.5-dev
description: A flexible syntax for boolean expressions.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/boolean_selector

environment:
sdk: '>=2.0.0-dev.58 <3.0.0'
sdk: '>=2.0.0 <3.0.0'

dependencies:
source_span: '^1.0.0'
source_span: ^1.0.0
string_scanner: '>=0.1.1 <2.0.0'

dev_dependencies:
pedantic: ^1.0.0
test: ^1.2.0
Loading

0 comments on commit 8aa847c

Please sign in to comment.