Skip to content

Commit

Permalink
Merge pull request #2488 from sass/sdk-formatter
Browse files Browse the repository at this point in the history
Always use the SDK version of the formatter
  • Loading branch information
nex3 authored Jan 13, 2025
2 parents 9e6e3bf + 7f2f4af commit adb7016
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 46 deletions.
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ dev_dependencies:
analyzer: ^6.8.0
archive: ^3.1.2
crypto: ^3.0.0
dart_style: ^3.0.0
dartdoc: ^8.0.14
grinder: ^0.9.0
node_preamble: ^2.0.2
Expand Down
39 changes: 21 additions & 18 deletions test/double_check_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,24 @@ import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:test/test.dart';

import 'package:sass/src/util/map.dart';

import '../tool/grind/generate_deprecations.dart' as deprecations;
import '../tool/grind/synchronize.dart' as synchronize;

/// Tests that double-check that everything in the repo looks sensible.
void main() {
group("up-to-date generated", () {
group("synchronized file:", () {
synchronize.sources.forEach((sourcePath, targetPath) {
test(targetPath, () {
if (File(targetPath).readAsStringSync() !=
synchronize.synchronizeFile(sourcePath)) {
fail("$targetPath is out-of-date.\n"
"Run `dart run grinder` to update it.");
}
});
});
});

test("deprecations", () {
var inputText = File(deprecations.yamlPath).readAsStringSync();
var outputText = File(deprecations.dartPath).readAsStringSync();
var checksum = sha1.convert(utf8.encode(inputText));
if (!outputText.contains('// Checksum: $checksum')) {
fail('${deprecations.dartPath} is out-of-date.\n'
'Run `dart run grinder` to update it.');
for (var (sourcePath, targetPath) in synchronize.sources.pairs) {
test(targetPath, () => _assertChecksumMatches(sourcePath, targetPath));
}
});

test(
"deprecations",
() => _assertChecksumMatches(
deprecations.yamlPath, deprecations.dartPath));
},
// Windows sees different bytes than other OSes, possibly because of
// newline normalization issues.
Expand Down Expand Up @@ -182,3 +173,15 @@ void _checkVersionIncrementsAlong(
"at its major version as well");
}
}

/// Throws an error if the checksum in [outputPath] doesn't match the hash of
/// the contents of [inputPath].
void _assertChecksumMatches(String inputPath, String outputPath) {
var inputText = File(inputPath).readAsStringSync();
var outputText = File(outputPath).readAsStringSync();
var checksum = sha1.convert(utf8.encode(inputText));
if (!outputText.contains('// Checksum: $checksum')) {
fail('$outputPath is out-of-date.\n'
'Run `dart run grinder` to update it.');
}
}
13 changes: 0 additions & 13 deletions tool/assert-formatted.sh

This file was deleted.

10 changes: 3 additions & 7 deletions tool/grind/generate_deprecations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import 'dart:convert';
import 'dart:io';

import 'package:crypto/crypto.dart';
import 'package:dart_style/dart_style.dart';
import 'package:grinder/grinder.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart';

import 'utils.dart';
Expand All @@ -23,10 +21,9 @@ final _blockRegex =
@Depends(updateLanguageRepo)
void deprecations() {
var yamlFile = File(yamlPath);
var dartFile = File(dartPath);
var yamlText = yamlFile.readAsStringSync();
var data = loadYaml(yamlText, sourceUrl: yamlFile.uri) as Map;
var dartText = dartFile.readAsStringSync();
var dartText = File(dartPath).readAsStringSync();
var buffer = StringBuffer('''// START AUTOGENERATED CODE
//
// DO NOT EDIT. This section was generated from the language repo.
Expand Down Expand Up @@ -79,7 +76,6 @@ void deprecations() {
fail("Couldn't find block for generated code in lib/src/deprecation.dart");
}
var newCode = dartText.replaceFirst(_blockRegex, buffer.toString());
dartFile.writeAsStringSync(DartFormatter(
languageVersion: Version.parse(Platform.version.split(' ').first))
.format(newCode));
File(dartPath).writeAsStringSync(newCode);
DartFmt.format(dartPath);
}
13 changes: 6 additions & 7 deletions tool/grind/synchronize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:collection/collection.dart';
import 'package:crypto/crypto.dart';
import 'package:dart_style/dart_style.dart';
import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:source_span/source_span.dart';

import 'package:sass/src/util/map.dart';
import 'package:sass/src/util/nullable.dart';

/// The files to compile to synchronous versions.
Expand Down Expand Up @@ -47,8 +46,10 @@ final _sharedClasses = const ['EvaluateResult'];
/// to a synchronous equivalent.
@Task('Compile async code to synchronous code.')
void synchronize() {
sources.forEach((source, target) =>
File(target).writeAsStringSync(synchronizeFile(source)));
for (var (source, target) in sources.pairs) {
File(target).writeAsStringSync(synchronizeFile(source));
DartFmt.format(target);
}
}

/// Returns the result of synchronizing [source].
Expand All @@ -59,9 +60,7 @@ String synchronizeFile(String source) {
parseFile(path: source, featureSet: FeatureSet.latestLanguageVersion())
.unit
.accept(visitor);
return DartFormatter(
languageVersion: Version.parse(Platform.version.split(' ').first))
.format(visitor.result);
return visitor.result;
}

/// The visitor that traverses the asynchronous parse tree and converts it to
Expand Down

0 comments on commit adb7016

Please sign in to comment.