From 3345a1953a486290213ed59abf579a4ae535cb2f Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Fri, 7 Jul 2023 22:33:01 +0200 Subject: [PATCH] improve public auto fix API --- src/dscanner/analysis/base.d | 2 - src/dscanner/analysis/run.d | 54 ++++++++++++++++++-------- src/dscanner/analysis/static_if_else.d | 1 - 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/dscanner/analysis/base.d b/src/dscanner/analysis/base.d index 112b956..dde778e 100644 --- a/src/dscanner/analysis/base.d +++ b/src/dscanner/analysis/base.d @@ -407,14 +407,12 @@ public: AutoFix.CodeReplacement[] resolveAutoFix( const Module mod, scope const(Token)[] tokens, - const Message message, const AutoFix.ResolveContext context, const AutoFixFormatting formatting, ) { cast(void) mod; cast(void) tokens; - cast(void) message; cast(void) context; cast(void) formatting; assert(0); diff --git a/src/dscanner/analysis/run.d b/src/dscanner/analysis/run.d index 44477d8..04c025a 100644 --- a/src/dscanner/analysis/run.d +++ b/src/dscanner/analysis/run.d @@ -996,7 +996,8 @@ MessageSet analyze(string fileName, const Module m, const StaticAnalysisConfig a foreach (message; check.messages) { if (resolveAutoFixes) - message.resolveMessageFromCheck(check, m, tokens, formattingConfig); + foreach (ref autofix; message.autofixes) + autofix.resolveAutoFixFromCheck(check, m, tokens, formattingConfig); set.insert(message); } } @@ -1004,8 +1005,8 @@ MessageSet analyze(string fileName, const Module m, const StaticAnalysisConfig a return set; } -private void resolveMessageFromCheck( - ref Message message, +private void resolveAutoFixFromCheck( + ref AutoFix autofix, BaseAnalyzer check, const Module m, scope const(Token)[] tokens, @@ -1014,16 +1015,12 @@ private void resolveMessageFromCheck( { import std.sumtype : match; - foreach (ref autofix; message.autofixes) - { - autofix.replacements.match!( - (AutoFix.ResolveContext context) { - autofix.replacements = check.resolveAutoFix(m, tokens, - message, context, formattingConfig); - }, - (_) {} - ); - } + autofix.replacements.match!( + (AutoFix.ResolveContext context) { + autofix.replacements = check.resolveAutoFix(m, tokens, context, formattingConfig); + }, + (_) {} + ); } void resolveAutoFixes(ref Message message, string fileName, @@ -1031,6 +1028,30 @@ void resolveAutoFixes(ref Message message, string fileName, scope const(Token)[] tokens, const Module m, const StaticAnalysisConfig analysisConfig, const AutoFixFormatting overrideFormattingConfig = AutoFixFormatting.invalid) +{ + resolveAutoFixes(message.checkName, message.autofixes, fileName, moduleCache, + tokens, m, analysisConfig, overrideFormattingConfig); +} + +AutoFix.CodeReplacement[] resolveAutoFix(string messageCheckName, AutoFix.ResolveContext context, + string fileName, + ref ModuleCache moduleCache, + scope const(Token)[] tokens, const Module m, + const StaticAnalysisConfig analysisConfig, + const AutoFixFormatting overrideFormattingConfig = AutoFixFormatting.invalid) +{ + AutoFix temp; + temp.replacements = context; + resolveAutoFixes(messageCheckName, (&temp)[0 .. 1], fileName, moduleCache, + tokens, m, analysisConfig, overrideFormattingConfig); + return temp.expectReplacements("resolving didn't work?!"); +} + +void resolveAutoFixes(string messageCheckName, AutoFix[] autofixes, string fileName, + ref ModuleCache moduleCache, + scope const(Token)[] tokens, const Module m, + const StaticAnalysisConfig analysisConfig, + const AutoFixFormatting overrideFormattingConfig = AutoFixFormatting.invalid) { import dsymbol.symbol : DSymbol; @@ -1054,14 +1075,15 @@ void resolveAutoFixes(ref Message message, string fileName, foreach (BaseAnalyzer check; getAnalyzersForModuleAndConfig(fileName, tokens, m, analysisConfig, moduleScope)) { - if (check.getName() == message.checkName) + if (check.getName() == messageCheckName) { - resolveMessageFromCheck(message, check, m, tokens, formattingConfig); + foreach (ref autofix; autofixes) + autofix.resolveAutoFixFromCheck(check, m, tokens, formattingConfig); return; } } - throw new Exception("Cannot find analyzer " ~ message.checkName + throw new Exception("Cannot find analyzer " ~ messageCheckName ~ " to resolve autofix with."); } diff --git a/src/dscanner/analysis/static_if_else.d b/src/dscanner/analysis/static_if_else.d index f8386ea..20f3656 100644 --- a/src/dscanner/analysis/static_if_else.d +++ b/src/dscanner/analysis/static_if_else.d @@ -63,7 +63,6 @@ final class StaticIfElse : BaseAnalyzer override AutoFix.CodeReplacement[] resolveAutoFix( const Module mod, scope const(Token)[] tokens, - const Message message, const AutoFix.ResolveContext context, const AutoFixFormatting formatting, )