Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Nov 15, 2023
1 parent dc6eedc commit c8850c3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
20 changes: 10 additions & 10 deletions firebase_rules_generator/lib/src/common/rules_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class RulesContext {
/// The resolver
final Resolver resolver;

/// All paths available in the current context
final Set<String> paths;
/// All wildcards available in the current context
final Set<String> wildcards;

/// The current indentation level
final int indent;
Expand All @@ -19,15 +19,15 @@ class RulesContext {

RulesContext._(
this.resolver, {
required this.paths,
required this.wildcards,
required this.indent,
}) : functions = const [];

/// Create the root context
RulesContext.root(
this.resolver, {
this.functions = const [],
}) : paths = {},
}) : wildcards = {},
indent = 2;

/// Get the first resolvable element with the given [name] and type [T]
Expand All @@ -39,17 +39,17 @@ class RulesContext {
.first;
}

/// Dive deeper into the context with additional paths and indentation
/// Dive deeper into the context with additional wildcards and indentation
///
/// A clean dive will overwrite existing context
RulesContext dive({bool clean = false, Set<String>? paths}) {
final newPaths = {
if (!clean) ...this.paths,
...?paths?.where((e) => e != '_'),
RulesContext dive({bool clean = false, Set<String>? wildcards}) {
final newWildcards = {
if (!clean) ...this.wildcards,
...?wildcards?.where((e) => e != '_'),
};
return RulesContext._(
resolver,
paths: newPaths,
wildcards: newWildcards,
indent: indent + 2,
);
}
Expand Down
15 changes: 7 additions & 8 deletions firebase_rules_generator/lib/src/common/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ void validateFunctionParameters({
required FunctionExpression function,
required String Function(String wildcard) createExpectedSignature,
}) {
final String expectedWildcardParameterName;
final pathWildcards = RegExp(wildcardMatcher).allMatches(path);
if (pathWildcards.length > 1) {
final String expectedWildcard;
final wildcards = RegExp(wildcardMatcher).allMatches(path);
if (wildcards.length > 1) {
throw InvalidGenerationSourceError(
'Path cannot contain more than one wildcard: $path',
);
} else if (pathWildcards.length == 1) {
expectedWildcardParameterName = pathWildcards.single[1]!;
} else if (wildcards.length == 1) {
expectedWildcard = wildcards.single[1]!;
} else {
expectedWildcardParameterName = '_';
expectedWildcard = '_';
}

final expectedSignature =
createExpectedSignature(expectedWildcardParameterName);
final expectedSignature = createExpectedSignature(expectedWildcard);
final actualSignature = function.parameters.toString();
if (expectedSignature != actualSignature) {
throw InvalidGenerationSourceError(
Expand Down
4 changes: 2 additions & 2 deletions firebase_rules_generator/lib/src/common/visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ Stream<String> visitParameter(

validate?.call(function);

final pathParameter = getParameterName(function, 0);
final wildcard = getParameterName(function, 0);
final elements = function.body.childEntities
.whereType<ListLiteral>()
.firstOrNull
?.elements;

final newContext = context.dive(clean: cleanContext, paths: {pathParameter});
final newContext = context.dive(clean: cleanContext, wildcards: {wildcard});
if (elements == null) {
yield* visit(newContext, function.body);
} else {
Expand Down
5 changes: 3 additions & 2 deletions firebase_rules_generator/lib/src/database/sanitizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ String sanitizeRules(String input) {
/// Sanitize path parameter prefixes from rules
String sanitizePaths(RulesContext context, String input) {
var sanitized = input;
for (final path in context.paths) {
sanitized = sanitized.replaceAll(RegExp(r'\b' + path + r'\b'), '\$$path');
for (final wildcard in context.wildcards) {
sanitized =
sanitized.replaceAll(RegExp(r'\b' + wildcard + r'\b'), '\$$wildcard');
}
return sanitized;
}
15 changes: 7 additions & 8 deletions firebase_rules_linter/lib/lints/invalid_match_function.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ class InvalidMatchFunction extends DartLintRule {
required FunctionExpression function,
required String Function(String wildcard) createExpectedSignature,
}) {
final String expectedWildcardParameterName;
final pathWildcards = RegExp(wildcardMatcher).allMatches(path);
if (pathWildcards.length > 1) {
final String expectedWildcard;
final wildcards = RegExp(wildcardMatcher).allMatches(path);
if (wildcards.length > 1) {
// The path is invalid. This is handled by another lint.
return null;
} else if (pathWildcards.length == 1) {
expectedWildcardParameterName = pathWildcards.single[1]!;
} else if (wildcards.length == 1) {
expectedWildcard = wildcards.single[1]!;
} else {
expectedWildcardParameterName = '_';
expectedWildcard = '_';
}

final expectedSignature =
createExpectedSignature(expectedWildcardParameterName);
final expectedSignature = createExpectedSignature(expectedWildcard);
final actualSignature = function.parameters.toString();
if (expectedSignature != actualSignature) {
return expectedSignature;
Expand Down

0 comments on commit c8850c3

Please sign in to comment.