Skip to content

Commit

Permalink
Require Dart 3.0, update and fix lints (dart-archive/csslib#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Jan 9, 2024
1 parent 8d5a4ef commit a668a2a
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 310 deletions.
2 changes: 1 addition & 1 deletion pkgs/csslib/.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, windows-latest]
sdk: [2.19.0, dev]
sdk: [3.0, dev]
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
Expand Down
4 changes: 4 additions & 0 deletions pkgs/csslib/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 pkgs/csslib/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 pkgs/csslib/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 pkgs/csslib/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 pkgs/csslib/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 pkgs/csslib/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
Loading

0 comments on commit a668a2a

Please sign in to comment.