-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: migrate double_literal_format to analysis_server_plugin #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrew-bekhiet-solid
merged 3 commits into
analysis_server_migration
from
274-migrate-double_literal_format
Jun 10, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 63 additions & 43 deletions
106
lib/src/lints/double_literal_format/fixes/double_literal_format_fix.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,84 @@ | ||
| part of '../double_literal_format_rule.dart'; | ||
| import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; | ||
| import 'package:analysis_server_plugin/edit/dart/dart_fix_kind_priority.dart'; | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||
| import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
| import 'package:solid_lints/src/lints/double_literal_format/double_literal_format_rule.dart'; | ||
| import 'package:solid_lints/src/lints/double_literal_format/double_literal_format_utils.dart'; | ||
|
|
||
| /// A Quick fix for `double_literal_format` rule | ||
| /// A Quick fix for [DoubleLiteralFormatRule] rule | ||
| /// Suggests the correct value for an issue | ||
| class _DoubleLiteralFormatFix extends DartFix { | ||
| class DoubleLiteralFormatFix extends ParsedCorrectionProducer { | ||
| static const _doubleLiteralFormatKind = FixKind( | ||
| 'solid_lints.fix.${DoubleLiteralFormatRule.lintName}', | ||
| DartFixKindPriority.standard, | ||
| "Fix double literal format", | ||
| ); | ||
|
|
||
| /// Creates a new instance of [DoubleLiteralFormatFix]. | ||
| DoubleLiteralFormatFix({required super.context}); | ||
|
|
||
| @override | ||
| FixKind get fixKind => _doubleLiteralFormatKind; | ||
|
|
||
| @override | ||
| void run( | ||
| CustomLintResolver resolver, | ||
| ChangeReporter reporter, | ||
| CustomLintContext context, | ||
| Diagnostic analysisError, | ||
| List<Diagnostic> others, | ||
| ) { | ||
| context.registry.addDoubleLiteral((node) { | ||
| // checks that the literal declaration is where our warning is located | ||
| if (!analysisError.sourceRange.intersects(node.sourceRange)) return; | ||
| FixKind get multiFixKind => const FixKind( | ||
| 'solid_lints.fix.multi.${DoubleLiteralFormatRule.lintName}', | ||
| DartFixKindPriority.standard, | ||
| "Fix double literal format across files", | ||
| ); | ||
|
|
||
| final lexeme = node.literal.lexeme; | ||
| String? correctLexeme; | ||
| @override | ||
| CorrectionApplicability get applicability => | ||
| CorrectionApplicability.automatically; | ||
|
|
||
| @override | ||
| Future<void> compute(ChangeBuilder builder) async { | ||
| final doubleLiteralNode = node; | ||
| if (doubleLiteralNode is! DoubleLiteral) return; | ||
|
|
||
| if (lexeme.hasLeadingZero) { | ||
| correctLexeme = _correctLeadingZeroLexeme(lexeme); | ||
| } else if (lexeme.hasLeadingDecimalPoint) { | ||
| correctLexeme = _correctLeadingDecimalPointLexeme(lexeme); | ||
| } else if (lexeme.hasTrailingZero) { | ||
| correctLexeme = _correctTrailingZeroLexeme(lexeme); | ||
| } | ||
| final lexeme = doubleLiteralNode.literal.lexeme; | ||
| if (!lexeme.hasLeadingZero && | ||
| !lexeme.hasLeadingDecimalPoint && | ||
| !lexeme.hasTrailingZero) { | ||
| return; | ||
| } | ||
|
|
||
| if (correctLexeme != null) { | ||
| final changeBuilder = reporter.createChangeBuilder( | ||
| message: 'Replace by $correctLexeme', | ||
| priority: 1, | ||
| ); | ||
| final correctLexeme = _correctTrailingZeroLexeme( | ||
| _correctLeadingZeroLexeme( | ||
| _correctLeadingDecimalPointLexeme( | ||
| lexeme, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| changeBuilder.addDartFileEdit((builder) { | ||
| builder.addSimpleReplacement( | ||
| SourceRange(node.offset, node.length), | ||
| correctLexeme!, | ||
| ); | ||
| }); | ||
| } | ||
| await builder.addDartFileEdit(file, (builder) { | ||
| builder.addSimpleReplacement( | ||
| doubleLiteralNode.sourceRange, | ||
| correctLexeme, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| String _correctLeadingZeroLexeme(String lexeme) => !lexeme.hasLeadingZero | ||
| ? lexeme | ||
| : _correctLeadingZeroLexeme(lexeme.substring(1)); | ||
|
|
||
| String _correctLeadingDecimalPointLexeme(String lexeme) => '0$lexeme'; | ||
| String _correctLeadingDecimalPointLexeme(String lexeme) => | ||
| lexeme.hasLeadingDecimalPoint ? '0$lexeme' : lexeme; | ||
|
|
||
| String _correctTrailingZeroLexeme(String lexeme) { | ||
| if (!lexeme.hasTrailingZero) { | ||
| return lexeme; | ||
| } else { | ||
| final mantissa = lexeme.split('e').first; | ||
| return _correctTrailingZeroLexeme( | ||
| lexeme.replaceFirst( | ||
| mantissa, | ||
| mantissa.substring(0, mantissa.length - 1), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| final mantissa = lexeme.toLowerCase().split('e').first; | ||
|
|
||
| return _correctTrailingZeroLexeme( | ||
| lexeme.replaceFirst( | ||
| mantissa, | ||
| mantissa.substring(0, mantissa.length - 1), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
lib/src/lints/double_literal_format/visitors/double_literal_format_visitor.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:solid_lints/src/lints/double_literal_format/double_literal_format_rule.dart' | ||
| show DoubleLiteralFormatRule; | ||
| import 'package:solid_lints/src/lints/double_literal_format/double_literal_format_utils.dart'; | ||
|
|
||
| /// A visitor that checks that double literals are formatted according to | ||
| /// [DoubleLiteralFormatRule]. | ||
| /// {@macro solid_lints.double_literal_format.example} | ||
| class DoubleLiteralFormatVisitor extends SimpleAstVisitor<void> { | ||
| final DoubleLiteralFormatRule _rule; | ||
|
|
||
| /// Creates a new instance of [DoubleLiteralFormatVisitor]. | ||
| DoubleLiteralFormatVisitor(this._rule); | ||
|
|
||
| @override | ||
| void visitDoubleLiteral(DoubleLiteral node) { | ||
| super.visitDoubleLiteral(node); | ||
|
|
||
| final lexeme = node.literal.lexeme; | ||
|
|
||
| if (lexeme.hasLeadingZero) { | ||
| _rule.reportAtNode( | ||
| node, | ||
| diagnosticCode: DoubleLiteralFormatRule.leadingZeroCode, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (lexeme.hasLeadingDecimalPoint) { | ||
| _rule.reportAtNode( | ||
| node, | ||
| diagnosticCode: DoubleLiteralFormatRule.leadingDecimalCode, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (lexeme.hasTrailingZero) { | ||
| _rule.reportAtNode( | ||
| node, | ||
| diagnosticCode: DoubleLiteralFormatRule.trailingZeroCode, | ||
| ); | ||
| return; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.