Skip to content

Commit

Permalink
Add some more strict linter rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Sep 23, 2023
1 parent 7c31237 commit 726d4c6
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 104 deletions.
25 changes: 23 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ linter:
# - avoid_dynamic_calls
- avoid_final_parameters
- avoid_print
- avoid_unused_constructor_parameters
- camel_case_types
- combinators_ordering
- comment_references
- dangling_library_doc_comments
- implicit_call_tearoffs
- invalid_case_patterns
- no_self_assignments
Expand All @@ -19,13 +23,30 @@ linter:
- prefer_const_constructors_in_immutables
- prefer_const_declarations
- prefer_const_literals_to_create_immutables
- prefer_expression_function_bodies
- prefer_final_fields
- prefer_final_in_for_each
- prefer_final_locals
- prefer_for_elements_to_map_fromIterable
- prefer_function_declarations_over_variables
- prefer_if_elements_to_conditional_expressions
- prefer_relative_imports
- sort_constructors_first
- type_literal_in_constant_pattern
- unnecessary_await_in_return
- unnecessary_brace_in_string_interps
- unnecessary_breaks
- unnecessary_const
- unnecessary_constructor_name
- unnecessary_getters_setters
# - unnecessary_lambdas
- unnecessary_late
- unnecessary_library_directive
- unnecessary_new
- unnecessary_null_aware_assignments
- unnecessary_null_aware_operator_on_extension_on_nullable
- unnecessary_null_checks
- unnecessary_null_in_if_null_operators
- unnecessary_parenthesis
- unnecessary_statements
- unnecessary_to_list_in_spreads
- unreachable_from_main
- use_super_parameters
5 changes: 2 additions & 3 deletions bin/benchmark/util/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import 'package:petitparser/petitparser.dart';

import 'benchmark.dart';

final defaultCharsInput =
List.generate(0xff, (value) => String.fromCharCode(value));
final defaultCharsInput = List.generate(0xff, String.fromCharCode);
final defaultStringInput = defaultCharsInput.join();

final List<MapEntry<String, Benchmark>> _benchmarkEntries = (() {
Future.delayed(const Duration(milliseconds: 1)).then((_) {
stdout.writeln(['name', 'parser', 'accept', 'native'].join('\t'));
for (var benchmarkEntry in _benchmarkEntries) {
for (final benchmarkEntry in _benchmarkEntries) {
benchmarkEntry.value();
}
});
Expand Down
2 changes: 1 addition & 1 deletion bin/prolog/prolog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void main(List<String> arguments) {
stdin
.transform(systemEncoding.decoder)
.transform(const LineSplitter())
.map((query) => Term.parse(query))
.map(Term.parse)
.asyncMap((goal) async {
await db.query(goal).forEach(stdout.writeln);
}).forEach((each) => stdout.write('?- '));
Expand Down
2 changes: 2 additions & 0 deletions lib/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
///
/// The grammar is adapted from [Dart programming language specification](https://www.ecma-international.org/publications-and-standards/standards/ecma-408/).
/// Unfortunately, it is unable to parse all valid Dart programs yet.
library dart;

export 'src/dart/grammar.dart';
2 changes: 2 additions & 0 deletions lib/json.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// This package contains a complete implementation of [JSON](https://json.org/).
library json;

import 'src/json/definition.dart';
import 'src/json/types.dart';

Expand Down
2 changes: 2 additions & 0 deletions lib/lisp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
///
/// The code is reasonably complete to run and evaluate reasonably complex
/// programs from the console or the web browser.
library lisp;

export 'src/lisp/cons.dart';
export 'src/lisp/environment.dart';
export 'src/lisp/evaluator.dart';
Expand Down
2 changes: 2 additions & 0 deletions lib/math.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// This package contains a simple expression parser.
library math;

export 'src/math/ast.dart';
export 'src/math/common.dart';
export 'src/math/parser.dart';
2 changes: 2 additions & 0 deletions lib/prolog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
///
/// The code is reasonably complete to run and evaluate reasonably complex
/// programs from the console or the web browser.
library prolog;

export 'src/prolog/evaluator.dart';
export 'src/prolog/grammar.dart';
export 'src/prolog/parser.dart';
2 changes: 2 additions & 0 deletions lib/regexp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Based on the following blog posts:
// - http://xysun.github.io/posts/regex-parsing-thompsons-algorithm.html
// - https://deniskyashif.com/2019/02/17/implementing-a-regular-expression-engine/.
library regexp;

export 'src/regexp/nfa.dart';
export 'src/regexp/node.dart';
export 'src/regexp/parser.dart';
2 changes: 2 additions & 0 deletions lib/smalltalk.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// This package contains the complete grammar of Smalltalk.
///
/// It was automatically exported from PetitParser for Smalltalk.
library smalltalk;

export 'src/smalltalk/ast.dart';
export 'src/smalltalk/grammar.dart';
export 'src/smalltalk/parser.dart';
Expand Down
47 changes: 20 additions & 27 deletions lib/src/lisp/native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,23 @@ class NativeEnvironment extends Environment {
throw ArgumentError('Invalid define: $args');
}

static Lambda _lambda(Environment lambdaEnv, dynamic lambdaArgs) {
return (evalEnv, evalArgs) {
final inner = lambdaEnv.create();
var names = lambdaArgs.head;
var values = evalArguments(evalEnv, evalArgs);
while (names != null && values != null) {
inner.define(names.head, values.head);
names = names.tail;
values = values.tail;
}
return evalList(inner, lambdaArgs.tail);
};
}

static dynamic _quote(Environment env, dynamic args) {
return args.head;
}

static dynamic _eval(Environment env, dynamic args) {
return eval(env.create(), eval(env, args.head));
}
static Lambda _lambda(Environment lambdaEnv, dynamic lambdaArgs) =>
(evalEnv, evalArgs) {
final inner = lambdaEnv.create();
var names = lambdaArgs.head;
var values = evalArguments(evalEnv, evalArgs);
while (names != null && values != null) {
inner.define(names.head, values.head);
names = names.tail;
values = values.tail;
}
return evalList(inner, lambdaArgs.tail);
};

static dynamic _quote(Environment env, dynamic args) => args.head;

static dynamic _eval(Environment env, dynamic args) =>
eval(env.create(), eval(env, args.head));

static dynamic _apply(Environment env, dynamic args) {
final Function function = eval(env, args.head);
Expand All @@ -100,9 +96,8 @@ class NativeEnvironment extends Environment {
return evalList(inner, args.tail);
}

static dynamic _set(Environment env, dynamic args) {
return env[args.head] = eval(env, args.tail.head);
}
static dynamic _set(Environment env, dynamic args) =>
env[args.head] = eval(env, args.tail.head);

static dynamic _print(Environment env, dynamic args) {
final buffer = StringBuffer();
Expand Down Expand Up @@ -156,9 +151,7 @@ class NativeEnvironment extends Environment {
return false;
}

static dynamic _not(Environment env, dynamic args) {
return !eval(env, args.head);
}
static dynamic _not(Environment env, dynamic args) => !eval(env, args.head);

static dynamic _plus(Environment env, dynamic args) {
num value = eval(env, args.head);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/math/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final parser = () {
builder.group()
..left(char('+').trim(), (a, op, b) => Binary('+', a, b, (x, y) => x + y))
..left(char('-').trim(), (a, op, b) => Binary('-', a, b, (x, y) => x - y));
return builder.build().end();
return resolve(builder.build()).end();
}();

Expression _createValue(String value) => Value(num.parse(value));
Expand Down
2 changes: 1 addition & 1 deletion lib/src/regexp/nfa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Nfa extends RegexpPattern {

void _addStates(NfaState state, Set<NfaState> states) {
if (!states.add(state)) return;
for (var other in state.epsilons) {
for (final other in state.epsilons) {
_addStates(other, states);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/src/regexp/parser.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:petitparser/definition.dart';
import 'package:petitparser/expression.dart';
import 'package:petitparser/parser.dart';

Expand All @@ -8,9 +9,8 @@ final nodeParser = () {

const meta = r'\.()!*+?|&';
builder
..primitive(noneOf(meta).map((char) => LiteralNode(char)))
..primitive(
anyOf(meta).skip(before: char(r'\')).map((char) => LiteralNode(char)))
..primitive(noneOf(meta).map(LiteralNode.new))
..primitive(anyOf(meta).skip(before: char(r'\')).map(LiteralNode.new))
..primitive(char('.').map((_) => DotNode()));

builder.group().wrapper(char('('), char(')'), (_, value, __) => value);
Expand Down Expand Up @@ -38,5 +38,5 @@ final nodeParser = () {
..left(char('|'), (left, _, right) => AlternationNode(left, right))
..left(char('&'), (left, _, right) => IntersectionNode(left, right));

return builder.build().end();
return resolve(builder.build()).end();
}();
2 changes: 1 addition & 1 deletion lib/src/smalltalk/objects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ void bootstrap() {
arrayBehavior.addMethod(
'at:put:', (self, index, object) => self[index] = object);

classBehavior.addMethod('new', (self) => SmalltalkObject(self));
classBehavior.addMethod('new', SmalltalkObject.new);
}
28 changes: 12 additions & 16 deletions lib/src/smalltalk/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ ArrayNode buildArray(List statements) {
return result;
}

ValueNode buildAssignment(ValueNode node, List parts) {
return parts.reversed.fold(
node,
(result, variableAndToken) =>
AssignmentNode(variableAndToken[0], variableAndToken[1], result));
}
ValueNode buildAssignment(ValueNode node, List parts) => parts.reversed.fold(
node,
(result, variableAndToken) =>
AssignmentNode(variableAndToken[0], variableAndToken[1], result));

ValueNode buildBlock(List arguments, SequenceNode body) {
final result = BlockNode(body);
Expand All @@ -161,16 +159,14 @@ ValueNode buildCascade(ValueNode value, List parts) {
return value;
}

ValueNode buildMessage(ValueNode receiver, List parts) {
return parts
.where((selectorAndArguments) => selectorAndArguments.isNotEmpty)
.fold(receiver, (receiver, selectorAndArguments) {
final message = MessageNode(receiver);
addTo<Token>(message.selectorToken, selectorAndArguments);
addTo<ValueNode>(message.arguments, selectorAndArguments);
return message;
});
}
ValueNode buildMessage(ValueNode receiver, List parts) => parts
.where((selectorAndArguments) => selectorAndArguments.isNotEmpty)
.fold(receiver, (receiver, selectorAndArguments) {
final message = MessageNode(receiver);
addTo<Token>(message.selectorToken, selectorAndArguments);
addTo<ValueNode>(message.arguments, selectorAndArguments);
return message;
});

MethodNode buildMethod(List parts) {
final result = MethodNode();
Expand Down
2 changes: 2 additions & 0 deletions lib/src/uri/authority.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// Further parse the URI authority into username, password, hostname and port.
///
/// Accepts input of the form "[username[:password]@]hostname[:port]".
library authority;

import 'package:petitparser/petitparser.dart';

final authority = seq3(
Expand Down
2 changes: 2 additions & 0 deletions lib/src/uri/query.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// Parsers an URI query.
///
/// Accepts input of the form "{key[=value]}&...".
library query;

import 'package:petitparser/petitparser.dart';

final query = _param.plusSeparated('&'.toParser()).map(
Expand Down
2 changes: 2 additions & 0 deletions lib/uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
///
/// The accepted inputs and decomposition matches the example given in
/// Appendix B of the standard: https://tools.ietf.org/html/rfc3986#appendix-B.
library uri;

import 'package:petitparser/petitparser.dart';

import 'src/uri/authority.dart' as lib_authority;
Expand Down
37 changes: 0 additions & 37 deletions test/dart_file_tests.dart

This file was deleted.

5 changes: 2 additions & 3 deletions test/lisp_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ void main() {
final native = NativeEnvironment();
final standard = StandardEnvironment(native);

dynamic exec(String value, [Environment? env]) {
return evalString(lispParser, env ?? standard.create(), value);
}
dynamic exec(String value, [Environment? env]) =>
evalString(lispParser, env ?? standard.create(), value);

group('Cell', () {
test('Name', () {
Expand Down
6 changes: 3 additions & 3 deletions test/smalltalk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ TypeMatcher<SequenceNode> isSequenceNode(
List<Matcher> statements = const []}) =>
isA<SequenceNode>()
.having((node) => node.temporaries, 'temporaries',
temporaries.map((each) => isVariableNode(each)))
temporaries.map(isVariableNode))
.having((node) => node.statements, 'statements', statements);

TypeMatcher<ReturnNode> isReturnNode(Matcher value) =>
Expand All @@ -88,7 +88,7 @@ TypeMatcher<BlockNode> isBlockNode(
List<Matcher> statements = const []}) =>
isA<BlockNode>()
.having((node) => node.arguments, 'arguments',
arguments.map((each) => isVariableNode(each)))
arguments.map(isVariableNode))
.having((node) => node.body, 'body',
isSequenceNode(temporaries: temporaries, statements: statements));

Expand All @@ -108,7 +108,7 @@ TypeMatcher<MethodNode> isMethodNode(String selector, SelectorType selectorType,
.having((node) => node.selector, 'selector', selector)
.having((node) => node.selectorType, 'selectorType', selectorType)
.having((node) => node.arguments, 'arguments',
arguments.map((each) => isVariableNode(each)))
arguments.map(isVariableNode))
.having((node) => node.pragmas, 'pragmas', pragmas)
.having((node) => node.body, 'body',
isSequenceNode(temporaries: temporaries, statements: statements));
Expand Down
Loading

0 comments on commit 726d4c6

Please sign in to comment.