Skip to content

Commit

Permalink
Merge pull request #57 from yumemi-inc/feature/#47
Browse files Browse the repository at this point in the history
Cache allRules
  • Loading branch information
morikann authored Nov 2, 2023
2 parents db202de + 12fd61c commit 643be02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
44 changes: 24 additions & 20 deletions tools/update_lint_rules/lib/src/services/lint_rule_service.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:async/async.dart';
import 'package:collection/collection.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
Expand Down Expand Up @@ -67,7 +68,7 @@ const _yumemiNotRecommendedRules = <({String name, String reason})>[
];

class LintRuleService {
const LintRuleService({
LintRuleService({
required AppClient appClient,
}) : _appClient = appClient;

Expand All @@ -90,10 +91,9 @@ class LintRuleService {
}

Future<NotRecommendedRules> getNotRecommendedRules() async {
// TODO: Reuse what has been obtained once.
final allRules = await getRules();

final notReccomendedAllRules =
final notRecommendedAllRules =
_yumemiNotRecommendedRules.map((notRecommendedRule) {
final rule = allRules.firstWhereOrNull(
(rule) => notRecommendedRule.name == rule.name,
Expand All @@ -111,28 +111,32 @@ class LintRuleService {
}
});
return (
flutter: notReccomendedAllRules.whereType<NotRecommendedFlutterRule>(),
dart: notReccomendedAllRules.whereType<NotRecommendedDartRule>()
flutter: notRecommendedAllRules.whereType<NotRecommendedFlutterRule>(),
dart: notRecommendedAllRules.whereType<NotRecommendedDartRule>()
);
}

final _allRulesMemo = AsyncMemoizer<Iterable<Rule>>();

@visibleForTesting
Future<Iterable<Rule>> getRules() async {
final url = Uri.https(
'raw.githubusercontent.com',
'dart-lang/sdk/main/pkg/linter/tool/machine/rules.json',
);
Future<Iterable<Rule>> getRules() => _allRulesMemo.runOnce(
() async {
final url = Uri.https(
'raw.githubusercontent.com',
'dart-lang/sdk/main/pkg/linter/tool/machine/rules.json',
);

final responseBody = await _appClient.read(url);
final responseBody = await _appClient.read(url);

final json = jsonDecode(responseBody) as List<dynamic>;
final json = jsonDecode(responseBody) as List<dynamic>;

final rules = json.map((e) => Rule.fromJson(e)).where(
(e) => switch (e.state) {
RuleState.stable || RuleState.experimental => true,
RuleState.deprecated || RuleState.removed => false,
},
);
return rules;
}
final rules = json.map((e) => Rule.fromJson(e)).where(
(e) => switch (e.state) {
RuleState.stable || RuleState.experimental => true,
RuleState.deprecated || RuleState.removed => false,
},
);
return rules;
},
);
}
2 changes: 1 addition & 1 deletion tools/update_lint_rules/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ packages:
source: hosted
version: "2.4.2"
async:
dependency: transitive
dependency: "direct main"
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
Expand Down
1 change: 1 addition & 0 deletions tools/update_lint_rules/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ environment:
sdk: ^3.1.0

dependencies:
async: ^2.11.0
collection: ^1.18.0
file: ^7.0.0
freezed_annotation: ^2.4.1
Expand Down

0 comments on commit 643be02

Please sign in to comment.