Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Require Dart 3.0, update and fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Dec 18, 2023
1 parent 17346e5 commit d2a3107
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 309 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.1-wip

- Require Dart 3.0

## 1.0.0

- Rev to `1.0.0` (note however that there are no API changes from `0.17.x`).
Expand Down
6 changes: 6 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ analyzer:
strict-casts: true
strict-inference: true
strict-raw-types: true
errors:
comment_references: ignore #too many false positives

linter:
rules:
- prefer_expression_function_bodies
5 changes: 2 additions & 3 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ StyleSheet parseCss(
String cssInput, {
List<css.Message>? errors,
css.PreprocessorOptions? opts,
}) {
return css.parse(cssInput, errors: errors, options: opts ?? _default);
}
}) =>
css.parse(cssInput, errors: errors, options: opts ?? _default);

/// Pretty printer for CSS.
String prettyPrint(StyleSheet ss) =>
Expand Down
78 changes: 31 additions & 47 deletions lib/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ParserState extends TokenizerState {

void _createMessages({List<Message>? errors, PreprocessorOptions? options}) {
errors ??= [];
options ??= PreprocessorOptions(useColors: false, inputFile: 'memory');
options ??= const PreprocessorOptions(useColors: false, inputFile: 'memory');

messages = Messages(options: options, printHandler: errors.add);
}
Expand Down Expand Up @@ -258,24 +258,18 @@ class _Parser {
///////////////////////////////////////////////////////////////////
// Basic support methods
///////////////////////////////////////////////////////////////////
int _peek() {
return _peekToken.kind;
}
int _peek() => _peekToken.kind;

Token _next({bool unicodeRange = false}) {
final next = _previousToken = _peekToken;
_peekToken = tokenizer.next(unicodeRange: unicodeRange);
return next;
}

bool _peekKind(int kind) {
return _peekToken.kind == kind;
}
bool _peekKind(int kind) => _peekToken.kind == kind;

// Is the next token a legal identifier? This includes pseudo-keywords.
bool _peekIdentifier() {
return TokenKind.isIdentifier(_peekToken.kind);
}
bool _peekIdentifier() => TokenKind.isIdentifier(_peekToken.kind);

/// Marks the parser/tokenizer look ahead to support Less nested selectors.
ParserState get _mark => ParserState(_peekToken, _previousToken, tokenizer);
Expand Down Expand Up @@ -792,9 +786,8 @@ class _Parser {
}

var declGroup = processDeclarations(checkBrace: false);
if (declGroup.declarations.any((decl) {
return decl is Declaration && decl is! IncludeMixinAtDeclaration;
})) {
if (declGroup.declarations.any((decl) =>
decl is Declaration && decl is! IncludeMixinAtDeclaration)) {
var newDecls = <Declaration>[];
for (var include in productions) {
// If declGroup has items that are declarations then we assume
Expand Down Expand Up @@ -2038,40 +2031,31 @@ class _Parser {
DartStyleExpression? processOneNumber(Expressions exprs, int part) {
var value = marginValue(exprs.expressions[0]);
if (value != null) {
switch (part) {
case _marginPartLeft:
return MarginExpression(exprs.span, left: value);
case _marginPartTop:
return MarginExpression(exprs.span, top: value);
case _marginPartRight:
return MarginExpression(exprs.span, right: value);
case _marginPartBottom:
return MarginExpression(exprs.span, bottom: value);
case _borderPartLeft:
case _borderPartLeftWidth:
return BorderExpression(exprs.span, left: value);
case _borderPartTop:
case _borderPartTopWidth:
return BorderExpression(exprs.span, top: value);
case _borderPartRight:
case _borderPartRightWidth:
return BorderExpression(exprs.span, right: value);
case _borderPartBottom:
case _borderPartBottomWidth:
return BorderExpression(exprs.span, bottom: value);
case _heightPart:
return HeightExpression(exprs.span, value);
case _widthPart:
return WidthExpression(exprs.span, value);
case _paddingPartLeft:
return PaddingExpression(exprs.span, left: value);
case _paddingPartTop:
return PaddingExpression(exprs.span, top: value);
case _paddingPartRight:
return PaddingExpression(exprs.span, right: value);
case _paddingPartBottom:
return PaddingExpression(exprs.span, bottom: value);
}
return switch (part) {
_marginPartLeft => MarginExpression(exprs.span, left: value),
_marginPartTop => MarginExpression(exprs.span, top: value),
_marginPartRight => MarginExpression(exprs.span, right: value),
_marginPartBottom => MarginExpression(exprs.span, bottom: value),
_borderPartLeft ||
_borderPartLeftWidth =>
BorderExpression(exprs.span, left: value),
_borderPartTop ||
_borderPartTopWidth =>
BorderExpression(exprs.span, top: value),
_borderPartRight ||
_borderPartRightWidth =>
BorderExpression(exprs.span, right: value),
_borderPartBottom ||
_borderPartBottomWidth =>
BorderExpression(exprs.span, bottom: value),
_heightPart => HeightExpression(exprs.span, value),
_widthPart => WidthExpression(exprs.span, value),
_paddingPartLeft => PaddingExpression(exprs.span, left: value),
_paddingPartTop => PaddingExpression(exprs.span, top: value),
_paddingPartRight => PaddingExpression(exprs.span, right: value),
_paddingPartBottom => PaddingExpression(exprs.span, bottom: value),
_ => null
};
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Messages {
final List<Message> messages = <Message>[];

Messages({PreprocessorOptions? options, this.printHandler = print})
: options = options ?? PreprocessorOptions();
: options = options ?? const PreprocessorOptions();

/// Report a compile-time CSS error.
void error(String message, SourceSpan? span) {
Expand Down
42 changes: 18 additions & 24 deletions lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,12 @@ class Rgba implements _StyleProperty, ColorBase {

factory Rgba.fromColor(Color color) => color.rgba;

factory Rgba.fromArgbValue(num value) {
return Rgba(
(value.toInt() & 0xff000000) >> 0x18, // a
(value.toInt() & 0xff0000) >> 0x10, // r
(value.toInt() & 0xff00) >> 8, // g
value.toInt() & 0xff,
); // b
}
factory Rgba.fromArgbValue(num value) => Rgba(
(value.toInt() & 0xff000000) >> 0x18, // a
(value.toInt() & 0xff0000) >> 0x10, // r
(value.toInt() & 0xff00) >> 8, // g
value.toInt() & 0xff,
); // b

factory Rgba.fromHsla(Hsla hsla) {
// Convert to Rgba.
Expand Down Expand Up @@ -752,10 +750,9 @@ class PointXY implements _StyleProperty {
const PointXY(this.x, this.y);

@override
String? get cssExpression {
// TODO(terry): TBD
return null;
}
String? get cssExpression =>
// TODO(terry): TBD
null;
}

// TODO(terry): Implement style and color.
Expand All @@ -779,14 +776,12 @@ class Border implements _StyleProperty {
int get height => top! + bottom!;

@override
String get cssExpression {
return (top == left && bottom == right && top == right)
? '${left}px'
: "${top != null ? '$top' : '0'}px "
"${right != null ? '$right' : '0'}px "
"${bottom != null ? '$bottom' : '0'}px "
"${left != null ? '$left' : '0'}px";
}
String get cssExpression => (top == left && bottom == right && top == right)
? '${left}px'
: "${top != null ? '$top' : '0'}px "
"${right != null ? '$right' : '0'}px "
"${bottom != null ? '$bottom' : '0'}px "
"${left != null ? '$left' : '0'}px";
}

/// Font style constants.
Expand Down Expand Up @@ -1069,10 +1064,9 @@ class Font implements _StyleProperty {
}

@override
int get hashCode {
// TODO(jimhug): Lot's of potential collisions here. List of fonts, etc.
return size!.toInt() % family![0].hashCode;
}
int get hashCode =>
// TODO(jimhug): Lot's of potential collisions here. List of fonts, etc.
size!.toInt() % family![0].hashCode;

@override
bool operator ==(Object other) {
Expand Down
142 changes: 48 additions & 94 deletions lib/src/token_kind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -507,24 +507,20 @@ class TokenKind {
}

/// Return the token that matches the unit ident found.
static int matchUnits(String text, int offset, int length) {
return matchList(_UNITS, 'unit', text, offset, length);
}
static int matchUnits(String text, int offset, int length) =>
matchList(_UNITS, 'unit', text, offset, length);

/// Return the token that matches the directive name found.
static int matchDirectives(String text, int offset, int length) {
return matchList(_DIRECTIVES, 'type', text, offset, length);
}
static int matchDirectives(String text, int offset, int length) =>
matchList(_DIRECTIVES, 'type', text, offset, length);

/// Return the token that matches the margin directive name found.
static int matchMarginDirectives(String text, int offset, int length) {
return matchList(MARGIN_DIRECTIVES, 'type', text, offset, length);
}
static int matchMarginDirectives(String text, int offset, int length) =>
matchList(MARGIN_DIRECTIVES, 'type', text, offset, length);

/// Return the token that matches the media operator found.
static int matchMediaOperator(String text, int offset, int length) {
return matchList(MEDIA_OPERATORS, 'type', text, offset, length);
}
static int matchMediaOperator(String text, int offset, int length) =>
matchList(MEDIA_OPERATORS, 'type', text, offset, length);

static String? idToValue(Iterable<Object?> identList, int tokenId) {
for (var entry in identList) {
Expand Down Expand Up @@ -564,9 +560,7 @@ class TokenKind {
}

/// Return RGB value as [int] from a color entry in _EXTENDED_COLOR_NAMES.
static int colorValue(Map<String, Object> entry) {
return entry['value'] as int;
}
static int colorValue(Map<String, Object> entry) => entry['value'] as int;

static String? hexToColorName(Object hexValue) {
for (final entry in _EXTENDED_COLOR_NAMES) {
Expand Down Expand Up @@ -604,82 +598,44 @@ class TokenKind {
return invertResult.toString();
}

static String kindToString(int kind) {
switch (kind) {
case TokenKind.UNUSED:
return 'ERROR';
case TokenKind.END_OF_FILE:
return 'end of file';
case TokenKind.LPAREN:
return '(';
case TokenKind.RPAREN:
return ')';
case TokenKind.LBRACK:
return '[';
case TokenKind.RBRACK:
return ']';
case TokenKind.LBRACE:
return '{';
case TokenKind.RBRACE:
return '}';
case TokenKind.DOT:
return '.';
case TokenKind.SEMICOLON:
return ';';
case TokenKind.AT:
return '@';
case TokenKind.HASH:
return '#';
case TokenKind.PLUS:
return '+';
case TokenKind.GREATER:
return '>';
case TokenKind.TILDE:
return '~';
case TokenKind.ASTERISK:
return '*';
case TokenKind.NAMESPACE:
return '|';
case TokenKind.COLON:
return ':';
case TokenKind.PRIVATE_NAME:
return '_';
case TokenKind.COMMA:
return ',';
case TokenKind.SPACE:
return ' ';
case TokenKind.TAB:
return '\t';
case TokenKind.NEWLINE:
return '\n';
case TokenKind.RETURN:
return '\r';
case TokenKind.PERCENT:
return '%';
case TokenKind.SINGLE_QUOTE:
return "'";
case TokenKind.DOUBLE_QUOTE:
return '"';
case TokenKind.SLASH:
return '/';
case TokenKind.EQUALS:
return '=';
case TokenKind.CARET:
return '^';
case TokenKind.DOLLAR:
return '\$';
case TokenKind.LESS:
return '<';
case TokenKind.BANG:
return '!';
case TokenKind.MINUS:
return '-';
case TokenKind.BACKSLASH:
return '\\';
default:
throw StateError('Unknown TOKEN');
}
}
static String kindToString(int kind) => switch (kind) {
TokenKind.UNUSED => 'ERROR',
TokenKind.END_OF_FILE => 'end of file',
TokenKind.LPAREN => '(',
TokenKind.RPAREN => ')',
TokenKind.LBRACK => '[',
TokenKind.RBRACK => ']',
TokenKind.LBRACE => '{',
TokenKind.RBRACE => '}',
TokenKind.DOT => '.',
TokenKind.SEMICOLON => ';',
TokenKind.AT => '@',
TokenKind.HASH => '#',
TokenKind.PLUS => '+',
TokenKind.GREATER => '>',
TokenKind.TILDE => '~',
TokenKind.ASTERISK => '*',
TokenKind.NAMESPACE => '|',
TokenKind.COLON => ':',
TokenKind.PRIVATE_NAME => '_',
TokenKind.COMMA => ',',
TokenKind.SPACE => ' ',
TokenKind.TAB => '\t',
TokenKind.NEWLINE => '\n',
TokenKind.RETURN => '\r',
TokenKind.PERCENT => '%',
TokenKind.SINGLE_QUOTE => "'",
TokenKind.DOUBLE_QUOTE => '"',
TokenKind.SLASH => '/',
TokenKind.EQUALS => '=',
TokenKind.CARET => '^',
TokenKind.DOLLAR => '\$',
TokenKind.LESS => '<',
TokenKind.BANG => '!',
TokenKind.MINUS => '-',
TokenKind.BACKSLASH => '\\',
_ => throw StateError('Unknown TOKEN')
};

static bool isKindIdentifier(int kind) {
switch (kind) {
Expand Down Expand Up @@ -724,9 +680,7 @@ class TokenKind {
}
}

static bool isIdentifier(int kind) {
return kind == IDENTIFIER;
}
static bool isIdentifier(int kind) => kind == IDENTIFIER;
}

// Note: these names should match TokenKind names
Expand Down
Loading

0 comments on commit d2a3107

Please sign in to comment.