Skip to content

Commit

Permalink
Merge pull request #438 from Workiva/hackFastFormat-support-organizeD…
Browse files Browse the repository at this point in the history
…irectives

FEDX-1727: Support `organizeDirectives` config via `hackFastFormat`
  • Loading branch information
rmconsole7-wk authored Oct 9, 2024
2 parents 7390f2d + 08f7f3d commit cc36448
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/src/tools/over_react_format_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class OverReactFormatTool extends DevTool {
/// Default is 80.
int? lineLength;

/// Whether or not to organize import/export directives.
///
/// Default is false.
bool? organizeDirectives;

@override
String? description =
'Format dart files in this package with over_react_format.';
Expand All @@ -31,7 +36,8 @@ class OverReactFormatTool extends DevTool {
final args = [
'run',
'over_react_format',
if (lineLength != null) '--line-length=$lineLength'
if (lineLength != null) '--line-length=$lineLength',
if (organizeDirectives == true) '--organize-directives',
];
final process = ProcessDeclaration(exe.dart, [...args, ...paths],
mode: ProcessStartMode.inheritStdio);
Expand Down
30 changes: 30 additions & 0 deletions lib/src/utils/format_tool_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ class FormatToolBuilder extends GeneralizingAstVisitor<void> {
logWarningMessageFor(KnownErrorOutcome.failedToParseFormatterArgs);
}
}

final organizeDirectives = getCascadeByProperty('organizeDirectives');
if (organizeDirectives != null) {
final valueExpression = organizeDirectives.rightHandSide;
if (valueExpression is BooleanLiteral) {
typedFormatDevTool.organizeDirectives = valueExpression.value;
} else {
logWarningMessageFor(
KnownErrorOutcome.failedToParseOrganizeDirective);
}
}
} else if (typedFormatDevTool is OverReactFormatTool) {
final lineLengthAssignment = getCascadeByProperty('lineLength');
if (lineLengthAssignment != null) {
Expand All @@ -98,6 +109,18 @@ class FormatToolBuilder extends GeneralizingAstVisitor<void> {
logWarningMessageFor(KnownErrorOutcome.failedToParseLineLength);
}
}

final organizeDirectivesAssignment =
getCascadeByProperty('organizeDirectives');
if (organizeDirectivesAssignment != null) {
final valueExpression = organizeDirectivesAssignment.rightHandSide;
if (valueExpression is BooleanLiteral) {
typedFormatDevTool.organizeDirectives = valueExpression.value;
} else {
logWarningMessageFor(
KnownErrorOutcome.failedToParseOrganizeDirective);
}
}
}
}
}
Expand All @@ -108,6 +131,7 @@ enum KnownErrorOutcome {
failedToReconstructFormatterArgs,
failedToParseFormatterArgs,
failedToParseLineLength,
failedToParseOrganizeDirective,
}

void logWarningMessageFor(KnownErrorOutcome outcome) {
Expand Down Expand Up @@ -137,6 +161,12 @@ This is likely because the list is not a ListLiteral.
errorMessage = '''Failed to parse the line-length configuration.
This is likely because assignment does not use an IntegerLiteral.
''';
break;
case KnownErrorOutcome.failedToParseOrganizeDirective:
errorMessage = '''Failed to parse the organizeDirectives configuration.
This is likely because assignment does not use an BooleanLiteral.
''';
break;
}
Expand Down

0 comments on commit cc36448

Please sign in to comment.