Skip to content

Commit 42a36a7

Browse files
authored
Merge pull request #306 from sass/update-dart
Update Dart SDK constraint to 3.13.0
2 parents cf13adb + b19fac7 commit 42a36a7

37 files changed

Lines changed: 391 additions & 446 deletions

‎analysis_options.yaml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
analyzer:
2-
errors:
3-
missing_js_lib_annotation: ignore
1+
include: package:sass_analysis/analysis_options.yaml

‎bin/sass_migrator.dart‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import 'package:sass_migrator/src/node_interop_stub.dart'
99

1010
import 'package:sass_migrator/src/runner.dart';
1111

12-
// We can't declare args as a List<String> or Iterable<String> beacause of
13-
// dart-lang/sdk#36627.
14-
main(Iterable args) {
12+
void main(Iterable<String> args) {
1513
var argv = process.argv;
16-
if (argv != null) args = argv.skip(2);
14+
if (argv != null) args = argv.skip(2).cast<String>();
1715
MigratorRunner().execute(args.cast<String>());
1816
}

‎lib/src/exception.dart‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
// https://opensource.org/licenses/MIT.
66

77
import 'package:sass_api/sass_api.dart';
8-
import 'package:source_span/source_span.dart';
98

109
/// An exception thrown by a migrator.
11-
class MigrationException implements Exception {
10+
class MigrationException(
1211
/// An explanation of why migration failed.
13-
final String message;
14-
15-
MigrationException(this.message);
16-
12+
final String message,
13+
) implements Exception {
14+
@override
1715
String toString() => "Error: $message";
1816
}
1917

@@ -24,8 +22,6 @@ class MigrationException implements Exception {
2422
///
2523
/// This extends [SassException] to ensure that migrator exceptions are
2624
/// formatted the same way as the syntax errors Sass throws.
27-
class MigrationSourceSpanException extends SassException
28-
implements MigrationException {
29-
MigrationSourceSpanException(String message, FileSpan span)
30-
: super(message, span);
31-
}
25+
class MigrationSourceSpanException(super.message, super.span)
26+
extends SassException
27+
implements MigrationException;

‎lib/src/io/node.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import 'package:file/file.dart';
88
import 'package:js/js.dart';
99
import 'package:node_interop/node.dart';
10+
// ignore: implementation_imports
1011
import 'package:node_io/src/file_system.dart';
1112

1213
export 'package:node_io/src/directory.dart';
@@ -22,7 +23,7 @@ external bool get supportsAnsiEscapes;
2223
void printStderr(Object message) => process.stderr.write("$message\n");
2324

2425
int get exitCode => process.exitCode;
25-
void set exitCode(int code) {
26+
set exitCode(int code) {
2627
process.exitCode = code;
2728
}
2829

‎lib/src/migration_visitor.dart‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ import 'util/scoped_ast_visitor.dart';
2626
/// If [migrateDependencies] is enabled, this visitor will construct and run a
2727
/// new instance of itself (using [newInstance]) each time it encounters an
2828
/// `@import` or `@use` rule.
29-
abstract class MigrationVisitor extends ScopedAstVisitor {
30-
/// A mapping from URLs to migrated contents for stylesheets already migrated.
31-
final _migrated = <Uri, String>{};
29+
abstract class MigrationVisitor(
30+
/// Cache used to load stylesheets.
31+
@protected final ImportCache importCache, {
3232

3333
/// True if dependencies should be migrated as well.
34-
@protected
35-
final bool migrateDependencies;
36-
37-
/// Cache used to load stylesheets.
38-
@protected
39-
final ImportCache importCache;
34+
@protected required final bool migrateDependencies,
35+
}) extends ScopedAstVisitor {
36+
/// A mapping from URLs to migrated contents for stylesheets already migrated.
37+
final _migrated = <Uri, String>{};
4038

4139
/// Map of missing dependency URLs to the spans that import/use them.
4240
Map<Uri, FileSpan> get missingDependencies =>
@@ -69,8 +67,6 @@ abstract class MigrationVisitor extends ScopedAstVisitor {
6967
/// syntax, in which case this returns an empty string.
7068
String get semicolon => isIndented ? "" : ";";
7169

72-
MigrationVisitor(this.importCache, {required this.migrateDependencies});
73-
7470
/// Runs a new migration on [stylesheet] (and its dependencies, if
7571
/// [migrateDependencies] is true) and returns a map of migrated contents.
7672
Map<Uri, String> run(Stylesheet stylesheet, Importer importer) {

‎lib/src/migrator.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ import 'utils.dart';
3030
/// Most migrators will want to create a subclass of [MigrationVisitor] and
3131
/// implement [migrateFile] with `MyMigrationVisitor(this, entrypoint).run()`.
3232
abstract class Migrator extends Command<Map<Uri, String>> {
33+
@override
3334
String get invocation => super.invocation.replaceFirst(
3435
"[arguments]",
3536
"[options] <entrypoints.scss...>",
3637
);
3738

39+
@override
3840
String get usage =>
3941
"${super.usage}\n\n"
4042
"See also https://sass-lang.com/documentation/cli/migrator#$name";
@@ -70,6 +72,7 @@ abstract class Migrator extends Command<Map<Uri, String>> {
7072
///
7173
/// Entrypoints and dependencies that did not require any changes will not be
7274
/// included in the results.
75+
@override
7376
Map<Uri, String> run() {
7477
var allMigrated = <Uri, String>{};
7578
var importer = FilesystemImporter('.');
@@ -79,7 +82,7 @@ abstract class Migrator extends Command<Map<Uri, String>> {
7982
for (var item in globalResults!['pkg-importer'] as List<String>)
8083
if (item == 'node') NodePackageImporter('.'),
8184
],
82-
loadPaths: globalResults!['load-path'],
85+
loadPaths: globalResults!['load-path'] as List<String>?,
8386
);
8487

8588
var entrypoints = [

‎lib/src/migrators/calc_interpolation.dart‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import '../patch.dart';
1212

1313
/// Removes interpolation in calculation functions.
1414
class CalculationInterpolationMigrator extends Migrator {
15+
@override
1516
final name = "calc-interpolation";
17+
@override
1618
final description =
1719
r"Removes interpolation in calculation functions"
1820
r"`calc()`, `clamp()`, `min()`, and `max()`";
@@ -33,12 +35,10 @@ class CalculationInterpolationMigrator extends Migrator {
3335
}
3436
}
3537

36-
class _CalculationInterpolationVisitor extends MigrationVisitor {
37-
_CalculationInterpolationVisitor(
38-
super.importCache, {
39-
required super.migrateDependencies,
40-
});
41-
38+
class _CalculationInterpolationVisitor(
39+
super.importCache, {
40+
required super.migrateDependencies,
41+
}) extends MigrationVisitor {
4242
@override
4343
void visitFunctionExpression(FunctionExpression node) {
4444
const calcFunctions = ['calc', 'clamp', 'min', 'max'];
@@ -58,7 +58,7 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
5858
for (var match in interpolation.allMatches(newArg)) {
5959
var noInterpolation = match[0]!.substring(2, match[0]!.length - 1);
6060
if (hasOperation.hasMatch(noInterpolation)) {
61-
noInterpolation = '(' + noInterpolation + ')';
61+
noInterpolation = '($noInterpolation)';
6262
}
6363
newArg = newArg.toString().replaceAll(match[0]!, noInterpolation);
6464
}

‎lib/src/migrators/color.dart‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import '../utils.dart';
1616

1717
/// Migrates off of legacy color functions.
1818
class ColorMigrator extends Migrator {
19+
@override
1920
final name = "color";
21+
@override
2022
final description = "Migrates off of legacy color functions.";
2123

2224
@override
@@ -40,15 +42,11 @@ class ColorMigrator extends Migrator {
4042
/// URL for the sass:color module.
4143
final _colorUrl = Uri(scheme: 'sass', path: 'color');
4244

43-
class _ColorMigrationVisitor extends MigrationVisitor {
44-
final References references;
45-
46-
_ColorMigrationVisitor(
47-
this.references,
48-
super.importCache, {
49-
required super.migrateDependencies,
50-
});
51-
45+
class _ColorMigrationVisitor(
46+
final References references,
47+
super.importCache, {
48+
required super.migrateDependencies,
49+
}) extends MigrationVisitor {
5250
/// The namespace of an existing `@use "sass:color"` rule in the current
5351
/// file, if any.
5452
String? _colorModuleNamespace;

‎lib/src/migrators/division.dart‎

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ const _calcFunctions = {
4242
/// Migrates stylesheets that use the `/` operator for division to use the
4343
/// `divide` function instead.
4444
class DivisionMigrator extends Migrator {
45+
@override
4546
final name = "division";
47+
@override
4648
final description = """
4749
Use the math.div() function instead of the / division operator
4850
@@ -88,17 +90,12 @@ More info: https://sass-lang.com/d/slash-div""";
8890
/// The set of constant divisors that should be migrated to multiplication.
8991
const _allowedDivisors = {2, 4, 5, 8, 10, 20, 40, 50, 80, 100, 1000};
9092

91-
class _DivisionMigrationVisitor extends MigrationVisitor {
92-
final bool isPessimistic;
93-
final bool useMultiplication;
94-
95-
_DivisionMigrationVisitor(
96-
super.importCache,
97-
this.isPessimistic,
98-
this.useMultiplication, {
99-
required super.migrateDependencies,
100-
});
101-
93+
class _DivisionMigrationVisitor(
94+
super.importCache,
95+
final bool isPessimistic,
96+
final bool useMultiplication, {
97+
required super.migrateDependencies,
98+
}) extends MigrationVisitor {
10299
/// True when division is allowed by the context the current node is in.
103100
var _isDivisionAllowed = false;
104101

@@ -196,7 +193,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
196193
/// operands.
197194
@override
198195
void visitBinaryOperationExpression(BinaryOperationExpression node) {
199-
if (node.operator == BinaryOperator.dividedBy) {
196+
if (node.operator == .dividedBy) {
200197
_visitSlashOperation(node);
201198
} else {
202199
_withContext(
@@ -255,7 +252,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
255252
}) {
256253
_withContext(() {
257254
if (node.expression
258-
case BinaryOperationExpression(operator: BinaryOperator.dividedBy) &&
255+
case BinaryOperationExpression(operator: .dividedBy) &&
259256
var expression) {
260257
if (_visitSlashOperation(expression) && !negated) {
261258
addPatch(patchDelete(node.span, end: 1));
@@ -272,7 +269,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
272269
@override
273270
void visitUnaryOperationExpression(UnaryOperationExpression node) {
274271
if (node case UnaryOperationExpression(
275-
operator: UnaryOperator.minus,
272+
operator: .minus,
276273
:ParenthesizedExpression operand,
277274
)) {
278275
visitParenthesizedExpression(operand, negated: true);
@@ -316,7 +313,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
316313

317314
if (channels case ListExpression(
318315
hasBrackets: false,
319-
separator: ListSeparator.space,
316+
separator: .space,
320317
contents: [_, _, BinaryOperationExpression last],
321318
)) {
322319
// Handles cases like `rgb(10 20 30/2 / 0.5)`, since converting `30/2`
@@ -355,8 +352,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
355352
}
356353
var status = _NumberStatus.of(node);
357354

358-
if ((!_isDivisionAllowed && _onlySlash(node)) ||
359-
status == _NumberStatus.no) {
355+
if ((!_isDivisionAllowed && _onlySlash(node)) || status == .no) {
360356
// Definitely not division
361357
if (_isDivisionAllowed || _containsInterpolation(node)) {
362358
// We only want to convert a non-division slash operation to a
@@ -368,9 +364,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
368364
}
369365
return true;
370366
}
371-
if (_expectsNumericResult ||
372-
status == _NumberStatus.yes ||
373-
!isPessimistic) {
367+
if (_expectsNumericResult || status == .yes || !isPessimistic) {
374368
// Definitely division
375369
_withContext(
376370
() => super.visitBinaryOperationExpression(node),
@@ -397,10 +391,8 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
397391
/// Returns true if patched and false otherwise.
398392
bool _tryMultiplication(BinaryOperationExpression node) {
399393
if (!useMultiplication) return false;
400-
if (node.right case NumberExpression(
401-
unit: null,
402-
value: var divisor,
403-
) when _allowedDivisors.contains(divisor)) {
394+
if (node.right case NumberExpression(unit: null, value: var divisor)
395+
when _allowedDivisors.contains(divisor)) {
404396
var operatorSpan = node.left.span
405397
.extendThroughWhitespace()
406398
.end
@@ -418,7 +410,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
418410
/// unnecessary interpolation.
419411
void _visitSlashListArguments(Expression node) {
420412
switch (node) {
421-
case BinaryOperationExpression(operator: BinaryOperator.dividedBy):
413+
case BinaryOperationExpression(operator: .dividedBy):
422414
_visitSlashListArguments(node.left);
423415
_patchOperatorToComma(node);
424416
_visitSlashListArguments(node.right);
@@ -435,24 +427,20 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
435427
/// Returns true if we assume that [operator] always operators on numbers.
436428
///
437429
/// This is true for `*`, `%`, `<`, `<=`, `>`, and `>=`.
438-
bool _operatesOnNumbers(BinaryOperator operator) => {
439-
BinaryOperator.times,
440-
BinaryOperator.modulo,
441-
BinaryOperator.lessThan,
442-
BinaryOperator.lessThanOrEquals,
443-
BinaryOperator.greaterThan,
444-
BinaryOperator.greaterThanOrEquals,
430+
bool _operatesOnNumbers(BinaryOperator operator) => <BinaryOperator>{
431+
.times,
432+
.modulo,
433+
.lessThan,
434+
.lessThanOrEquals,
435+
.greaterThan,
436+
.greaterThanOrEquals,
445437
}.contains(operator);
446438

447439
/// Returns true if [node] is entirely composed of number literals and slash
448440
/// operations.
449441
bool _onlySlash(Expression node) => switch (node) {
450442
NumberExpression() => true,
451-
BinaryOperationExpression(
452-
operator: BinaryOperator.dividedBy,
453-
:var left,
454-
:var right,
455-
) =>
443+
BinaryOperationExpression(operator: .dividedBy, :var left, :var right) =>
456444
_onlySlash(left) && _onlySlash(right),
457445
_ => false,
458446
};
@@ -490,9 +478,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
490478
void _patchParensIfAny(SassNode node) {
491479
switch (node) {
492480
case ParenthesizedExpression(
493-
expression: BinaryOperationExpression(
494-
operator: BinaryOperator.dividedBy,
495-
),
481+
expression: BinaryOperationExpression(operator: .dividedBy),
496482
):
497483
return;
498484
case ParenthesizedExpression():
@@ -503,7 +489,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
503489

504490
/// Runs [operation] with the given context.
505491
void _withContext(
506-
void operation(), {
492+
void Function() operation, {
507493
bool? isDivisionAllowed,
508494
bool? expectsNumericResult,
509495
bool? inCalcContext,
@@ -532,9 +518,7 @@ enum _NumberStatus {
532518
/// definitely not a number, and [maybe] otherwise.
533519
static _NumberStatus of(Expression node) => switch (node) {
534520
NumberExpression() ||
535-
BinaryOperationExpression(
536-
operator: BinaryOperator.times || BinaryOperator.modulo,
537-
) => yes,
521+
BinaryOperationExpression(operator: .times || .modulo) => yes,
538522
BooleanExpression() ||
539523
ColorExpression() ||
540524
ListExpression() ||

0 commit comments

Comments
 (0)