From 0b8a0f606d7ce4f87e345e90ffff6209027222d2 Mon Sep 17 00:00:00 2001 From: Goodwine <2022649+Goodwine@users.noreply.github.com> Date: Wed, 17 Aug 2022 15:00:13 -0700 Subject: [PATCH] Transform all enum-like classes to dart enums (#1777) * Transform all enum-like classes to dart enums Fixes #1746 * fix new linter warnings upon upgrading to 2.17 regarding "[new MyClass]" to "[MyClass.new]" --- .../ast/sass/expression/binary_operation.dart | 35 +++++++++---------- .../ast/sass/expression/unary_operation.dart | 13 ++++--- lib/src/ast/selector/attribute.dart | 16 ++++----- lib/src/ast/selector/combinator.dart | 13 +++---- lib/src/callable/async.dart | 2 +- lib/src/extend/mode.dart | 10 +++--- lib/src/functions/color.dart | 2 +- lib/src/functions/list.dart | 2 +- lib/src/functions/map.dart | 2 +- lib/src/functions/math.dart | 2 +- lib/src/functions/meta.dart | 2 +- lib/src/functions/selector.dart | 2 +- lib/src/functions/string.dart | 2 +- lib/src/syntax.dart | 12 +++---- lib/src/value.dart | 2 +- lib/src/value/calculation.dart | 13 ++++--- lib/src/value/list.dart | 13 ++++--- lib/src/visitor/serialize.dart | 27 +++++--------- pkg/sass_api/pubspec.yaml | 2 +- pubspec.yaml | 2 +- 20 files changed, 77 insertions(+), 97 deletions(-) diff --git a/lib/src/ast/sass/expression/binary_operation.dart b/lib/src/ast/sass/expression/binary_operation.dart index d0075e602..d3b45d920 100644 --- a/lib/src/ast/sass/expression/binary_operation.dart +++ b/lib/src/ast/sass/expression/binary_operation.dart @@ -88,51 +88,48 @@ class BinaryOperationExpression implements Expression { /// A binary operator constant. /// /// {@category AST} -@sealed -class BinaryOperator { +enum BinaryOperator { /// The Microsoft equals operator, `=`. - static const singleEquals = BinaryOperator._("single equals", "=", 0); + singleEquals('single equals', '=', 0), /// The disjunction operator, `or`. - static const or = BinaryOperator._("or", "or", 1); + or('or', 'or', 1), /// The conjunction operator, `and`. - static const and = BinaryOperator._("and", "and", 2); + and('and', 'and', 2), /// The equality operator, `==`. - static const equals = BinaryOperator._("equals", "==", 3); + equals('equals', '==', 3), /// The inequality operator, `!=`. - static const notEquals = BinaryOperator._("not equals", "!=", 3); + notEquals('not equals', '!=', 3), /// The greater-than operator, `>`. - static const greaterThan = BinaryOperator._("greater than", ">", 4); + greaterThan('greater than', '>', 4), /// The greater-than-or-equal-to operator, `>=`. - static const greaterThanOrEquals = - BinaryOperator._("greater than or equals", ">=", 4); + greaterThanOrEquals('greater than or equals', '>=', 4), /// The less-than operator, `<`. - static const lessThan = BinaryOperator._("less than", "<", 4); + lessThan('less than', '<', 4), /// The less-than-or-equal-to operator, `<=`. - static const lessThanOrEquals = - BinaryOperator._("less than or equals", "<=", 4); + lessThanOrEquals('less than or equals', '<=', 4), /// The addition operator, `+`. - static const plus = BinaryOperator._("plus", "+", 5); + plus('plus', '+', 5), /// The subtraction operator, `-`. - static const minus = BinaryOperator._("minus", "-", 5); + minus('minus', '-', 5), /// The multiplication operator, `*`. - static const times = BinaryOperator._("times", "*", 6); + times('times', '*', 6), /// The division operator, `/`. - static const dividedBy = BinaryOperator._("divided by", "/", 6); + dividedBy('divided by', '/', 6), /// The modulo operator, `%`. - static const modulo = BinaryOperator._("modulo", "%", 6); + modulo('modulo', '%', 6); /// The English name of [this]. final String name; @@ -145,7 +142,7 @@ class BinaryOperator { /// An operator with higher precedence binds tighter. final int precedence; - const BinaryOperator._(this.name, this.operator, this.precedence); + const BinaryOperator(this.name, this.operator, this.precedence); String toString() => name; } diff --git a/lib/src/ast/sass/expression/unary_operation.dart b/lib/src/ast/sass/expression/unary_operation.dart index b323be51b..201e1a821 100644 --- a/lib/src/ast/sass/expression/unary_operation.dart +++ b/lib/src/ast/sass/expression/unary_operation.dart @@ -38,21 +38,20 @@ class UnaryOperationExpression implements Expression { /// A unary operator constant. /// /// {@category AST} -@sealed -class UnaryOperator { +enum UnaryOperator { /// The numeric identity operator, `+`. - static const plus = UnaryOperator._("plus", "+"); + plus('plus', '+'), /// The numeric negation operator, `-`. - static const minus = UnaryOperator._("minus", "-"); + minus('minus', '-'), /// The leading slash operator, `/`. /// /// This is a historical artifact. - static const divide = UnaryOperator._("divide", "/"); + divide('divide', '/'), /// The boolean negation operator, `not`. - static const not = UnaryOperator._("not", "not"); + not('not', 'not'); /// The English name of [this]. final String name; @@ -60,7 +59,7 @@ class UnaryOperator { /// The Sass syntax for [this]. final String operator; - const UnaryOperator._(this.name, this.operator); + const UnaryOperator(this.name, this.operator); String toString() => name; } diff --git a/lib/src/ast/selector/attribute.dart b/lib/src/ast/selector/attribute.dart index 55c40d5c0..0fbae6a29 100644 --- a/lib/src/ast/selector/attribute.dart +++ b/lib/src/ast/selector/attribute.dart @@ -69,31 +69,31 @@ class AttributeSelector extends SimpleSelector { } /// An operator that defines the semantics of an [AttributeSelector]. -class AttributeOperator { +enum AttributeOperator { /// The attribute value exactly equals the given value. - static const equal = AttributeOperator._("="); + equal('='), /// The attribute value is a whitespace-separated list of words, one of which /// is the given value. - static const include = AttributeOperator._("~="); + include('~='), /// The attribute value is either exactly the given value, or starts with the /// given value followed by a dash. - static const dash = AttributeOperator._("|="); + dash('|='), /// The attribute value begins with the given value. - static const prefix = AttributeOperator._("^="); + prefix('^='), /// The attribute value ends with the given value. - static const suffix = AttributeOperator._("\$="); + suffix('\$='), /// The attribute value contains the given value. - static const substring = AttributeOperator._("*="); + substring('*='); /// The operator's token text. final String _text; - const AttributeOperator._(this._text); + const AttributeOperator(this._text); String toString() => _text; } diff --git a/lib/src/ast/selector/combinator.dart b/lib/src/ast/selector/combinator.dart index cdffa0c8f..12fa922fc 100644 --- a/lib/src/ast/selector/combinator.dart +++ b/lib/src/ast/selector/combinator.dart @@ -2,30 +2,27 @@ // MIT-style license that can be found in the LICENSE file or at // https://opensource.org/licenses/MIT. -import 'package:meta/meta.dart'; - /// A combinator that defines the relationship between selectors in a /// [ComplexSelector]. /// /// {@category AST} -@sealed -class Combinator { +enum Combinator { /// Matches the right-hand selector if it's immediately adjacent to the /// left-hand selector in the DOM tree. - static const nextSibling = Combinator._("+"); + nextSibling('+'), /// Matches the right-hand selector if it's a direct child of the left-hand /// selector in the DOM tree. - static const child = Combinator._(">"); + child('>'), /// Matches the right-hand selector if it comes after the left-hand selector /// in the DOM tree. - static const followingSibling = Combinator._("~"); + followingSibling('~'); /// The combinator's token text. final String _text; - const Combinator._(this._text); + const Combinator(this._text); String toString() => _text; } diff --git a/lib/src/callable/async.dart b/lib/src/callable/async.dart index 6b1038653..28965bc80 100644 --- a/lib/src/callable/async.dart +++ b/lib/src/callable/async.dart @@ -36,7 +36,7 @@ abstract class AsyncCallable { /// The argument declaration is parsed from [arguments], which should not /// include parentheses. Throws a [SassFormatException] if parsing fails. /// - /// See [new Callable] for more details. + /// See [Callable.new] for more details. factory AsyncCallable.function(String name, String arguments, FutureOr callback(List arguments)) => AsyncBuiltInCallable.function(name, arguments, callback); diff --git a/lib/src/extend/mode.dart b/lib/src/extend/mode.dart index a8ce6dbb2..addac4375 100644 --- a/lib/src/extend/mode.dart +++ b/lib/src/extend/mode.dart @@ -3,28 +3,28 @@ // https://opensource.org/licenses/MIT. /// Different modes in which extension can run. -class ExtendMode { +enum ExtendMode { /// Normal mode, used with the `@extend` rule. /// /// This preserves existing selectors and extends each target individually. - static const normal = ExtendMode._("normal"); + normal('normal'), /// Replace mode, used by the `selector-replace()` function. /// /// This replaces existing selectors and requires every target to match to /// extend a given compound selector. - static const replace = ExtendMode._("replace"); + replace('replace'), /// All-targets mode, used by the `selector-extend()` function. /// /// This preserves existing selectors but requires every target to match to /// extend a given compound selector. - static const allTargets = ExtendMode._("allTargets"); + allTargets('allTargets'); /// The name of the mode. final String name; - const ExtendMode._(this.name); + const ExtendMode(this.name); String toString() => name; } diff --git a/lib/src/functions/color.dart b/lib/src/functions/color.dart index 646b1e0b6..64e7d35c3 100644 --- a/lib/src/functions/color.dart +++ b/lib/src/functions/color.dart @@ -863,7 +863,7 @@ SassColor _transparentize(List arguments) { .clamp(0, 1)); } -/// Like [new BuiltInCallable.function], but always sets the URL to +/// Like [BuiltInCallable.function], but always sets the URL to /// `sass:color`. BuiltInCallable _function( String name, String arguments, Value callback(List arguments)) => diff --git a/lib/src/functions/list.dart b/lib/src/functions/list.dart index 942b2ee8b..fb6696058 100644 --- a/lib/src/functions/list.dart +++ b/lib/src/functions/list.dart @@ -148,7 +148,7 @@ final _slash = _function("slash", r"$elements...", (arguments) { return SassList(list, ListSeparator.slash); }); -/// Like [new BuiltInCallable.function], but always sets the URL to `sass:list`. +/// Like [BuiltInCallable.function], but always sets the URL to `sass:list`. BuiltInCallable _function( String name, String arguments, Value callback(List arguments)) => BuiltInCallable.function(name, arguments, callback, url: "sass:list"); diff --git a/lib/src/functions/map.dart b/lib/src/functions/map.dart index 8a9b07412..e317e2287 100644 --- a/lib/src/functions/map.dart +++ b/lib/src/functions/map.dart @@ -218,7 +218,7 @@ SassMap _deepMergeImpl(SassMap map1, SassMap map2) { return SassMap(result); } -/// Like [new BuiltInCallable.function], but always sets the URL to `sass:map`. +/// Like [BuiltInCallable.function], but always sets the URL to `sass:map`. BuiltInCallable _function( String name, String arguments, Value callback(List arguments)) => BuiltInCallable.function(name, arguments, callback, url: "sass:map"); diff --git a/lib/src/functions/math.dart b/lib/src/functions/math.dart index f9c406103..bc6c8e0ce 100644 --- a/lib/src/functions/math.dart +++ b/lib/src/functions/math.dart @@ -328,7 +328,7 @@ BuiltInCallable _numberFunction(String name, num transform(num value)) { }); } -/// Like [new _function.function], but always sets the URL to `sass:math`. +/// Like [_function.function], but always sets the URL to `sass:math`. BuiltInCallable _function( String name, String arguments, Value callback(List arguments)) => BuiltInCallable.function(name, arguments, callback, url: "sass:math"); diff --git a/lib/src/functions/meta.dart b/lib/src/functions/meta.dart index e9366fb56..4ed71f8bf 100644 --- a/lib/src/functions/meta.dart +++ b/lib/src/functions/meta.dart @@ -79,7 +79,7 @@ final local = UnmodifiableListView([ }) ]); -/// Like [new BuiltInCallable.function], but always sets the URL to `sass:meta`. +/// Like [BuiltInCallable.function], but always sets the URL to `sass:meta`. BuiltInCallable _function( String name, String arguments, Value callback(List arguments)) => BuiltInCallable.function(name, arguments, callback, url: "sass:meta"); diff --git a/lib/src/functions/selector.dart b/lib/src/functions/selector.dart index 3dfce07e6..1bf2378b2 100644 --- a/lib/src/functions/selector.dart +++ b/lib/src/functions/selector.dart @@ -162,7 +162,7 @@ CompoundSelector? _prependParent(CompoundSelector compound) { } } -/// Like [new BuiltInCallable.function], but always sets the URL to +/// Like [BuiltInCallable.function], but always sets the URL to /// `sass:selector`. BuiltInCallable _function( String name, String arguments, Value callback(List arguments)) => diff --git a/lib/src/functions/string.dart b/lib/src/functions/string.dart index 04a441e95..a6372d420 100644 --- a/lib/src/functions/string.dart +++ b/lib/src/functions/string.dart @@ -168,7 +168,7 @@ int _codepointForIndex(int index, int lengthInCodepoints, return result; } -/// Like [new BuiltInCallable.function], but always sets the URL to +/// Like [BuiltInCallable.function], but always sets the URL to /// `sass:string`. BuiltInCallable _function( String name, String arguments, Value callback(List arguments)) => diff --git a/lib/src/syntax.dart b/lib/src/syntax.dart index eefeceb23..9c737c957 100644 --- a/lib/src/syntax.dart +++ b/lib/src/syntax.dart @@ -2,22 +2,20 @@ // MIT-style license that can be found in the LICENSE file or at // https://opensource.org/licenses/MIT. -import 'package:meta/meta.dart'; import 'package:path/path.dart' as p; /// An enum of syntaxes that Sass can parse. /// /// {@category Compile} -@sealed -class Syntax { +enum Syntax { /// The CSS-superset SCSS syntax. - static const scss = Syntax._("SCSS"); + scss('SCSS'), /// The whitespace-sensitive indented syntax. - static const sass = Syntax._("Sass"); + sass('Sass'), /// The plain CSS syntax, which disallows special Sass features. - static const css = Syntax._("CSS"); + css('CSS'); /// Returns the default syntax to use for a file loaded from [path]. static Syntax forPath(String path) { @@ -34,7 +32,7 @@ class Syntax { /// The name of the syntax. final String _name; - const Syntax._(this._name); + const Syntax(this._name); String toString() => _name; } diff --git a/lib/src/value.dart b/lib/src/value.dart index ed138c05d..94ccea8cb 100644 --- a/lib/src/value.dart +++ b/lib/src/value.dart @@ -32,7 +32,7 @@ export 'value/string.dart'; /// A SassScript value. /// /// All SassScript values are unmodifiable. New values can be constructed using -/// subclass constructors like [new SassString]. Untyped values can be cast to +/// subclass constructors like [SassString.new]. Untyped values can be cast to /// particular types using `assert*()` functions like [assertString], which /// throw user-friendly error messages if they fail. /// diff --git a/lib/src/value/calculation.dart b/lib/src/value/calculation.dart index cb8d7d8ea..ececb7783 100644 --- a/lib/src/value/calculation.dart +++ b/lib/src/value/calculation.dart @@ -365,19 +365,18 @@ class CalculationOperation { /// An enumeration of possible operators for [CalculationOperation]. /// /// {@category Value} -@sealed -class CalculationOperator { +enum CalculationOperator { /// The addition operator. - static const plus = CalculationOperator._("plus", "+", 1); + plus('plus', '+', 1), /// The subtraction operator. - static const minus = CalculationOperator._("minus", "-", 1); + minus('minus', '-', 1), /// The multiplication operator. - static const times = CalculationOperator._("times", "*", 2); + times('times', '*', 2), /// The division operator. - static const dividedBy = CalculationOperator._("divided by", "/", 2); + dividedBy('divided by', '/', 2); /// The English name of [this]. final String name; @@ -393,7 +392,7 @@ class CalculationOperator { @internal final int precedence; - const CalculationOperator._(this.name, this.operator, this.precedence); + const CalculationOperator(this.name, this.operator, this.precedence); String toString() => name; } diff --git a/lib/src/value/list.dart b/lib/src/value/list.dart index e37d18df3..68ca5c91c 100644 --- a/lib/src/value/list.dart +++ b/lib/src/value/list.dart @@ -80,22 +80,21 @@ class SassList extends Value { /// An enum of list separator types. /// /// {@category Value} -@sealed -class ListSeparator { +enum ListSeparator { /// A space-separated list. - static const space = ListSeparator._("space", " "); + space('space', ' '), /// A comma-separated list. - static const comma = ListSeparator._("comma", ","); + comma('comma', ','), /// A slash-separated list. - static const slash = ListSeparator._("slash", "/"); + slash('slash', '/'), /// A separator that hasn't yet been determined. /// /// Singleton lists and empty lists don't have separators defined. This means /// that list functions will prefer other lists' separators if possible. - static const undecided = ListSeparator._("undecided", null); + undecided('undecided', null); final String _name; @@ -105,7 +104,7 @@ class ListSeparator { /// `null`. final String? separator; - const ListSeparator._(this._name, this.separator); + const ListSeparator(this._name, this.separator); String toString() => _name; } diff --git a/lib/src/visitor/serialize.dart b/lib/src/visitor/serialize.dart index ed848fe2a..5bae8be6e 100644 --- a/lib/src/visitor/serialize.dart +++ b/lib/src/visitor/serialize.dart @@ -6,7 +6,6 @@ import 'dart:math' as math; import 'dart:typed_data'; import 'package:charcode/charcode.dart'; -import 'package:meta/meta.dart'; import 'package:source_maps/source_maps.dart'; import 'package:string_scanner/string_scanner.dart'; @@ -1448,8 +1447,7 @@ class _SerializeVisitor /// An enum of generated CSS styles. /// /// {@category Compile} -@sealed -class OutputStyle { +enum OutputStyle { /// The standard CSS style, with each declaration on its own line. /// /// ```css @@ -1457,36 +1455,29 @@ class OutputStyle { /// width: 100px; /// } /// ``` - static const expanded = OutputStyle._("expanded"); + expanded, /// A CSS style that produces as few bytes of output as possible. /// /// ```css /// .sidebar{width:100px} /// ``` - static const compressed = OutputStyle._("compressed"); - - /// The name of the style. - final String _name; - - const OutputStyle._(this._name); - - String toString() => _name; + compressed; } /// An enum of line feed sequences. -class LineFeed { +enum LineFeed { /// A single carriage return. - static const cr = LineFeed._('cr', '\r'); + cr('cr', '\r'), /// A carriage return followed by a line feed. - static const crlf = LineFeed._('crlf', '\r\n'); + crlf('crlf', '\r\n'), /// A single line feed. - static const lf = LineFeed._('lf', '\n'); + lf('lf', '\n'), /// A line feed followed by a carriage return. - static const lfcr = LineFeed._('lfcr', '\n\r'); + lfcr('lfcr', '\n\r'); /// The name of this sequence.. final String name; @@ -1494,7 +1485,7 @@ class LineFeed { /// The text to emit for this line feed. final String text; - const LineFeed._(this.name, this.text); + const LineFeed(this.name, this.text); String toString() => name; } diff --git a/pkg/sass_api/pubspec.yaml b/pkg/sass_api/pubspec.yaml index 392681355..0e21ebbc9 100644 --- a/pkg/sass_api/pubspec.yaml +++ b/pkg/sass_api/pubspec.yaml @@ -7,7 +7,7 @@ description: Additional APIs for Dart Sass. homepage: https://github.com/sass/dart-sass environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=2.17.0 <3.0.0" dependencies: sass: 1.54.5 diff --git a/pubspec.yaml b/pubspec.yaml index ac1fb48b0..26652ac7a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,7 +8,7 @@ executables: sass: sass environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=2.17.0 <3.0.0" dependencies: args: ^2.0.0