NoLint: RAII push/pop

This commit is contained in:
Axel Ricard 2023-10-12 22:28:04 +02:00
parent ebed325964
commit 959e46970b
4 changed files with 33 additions and 24 deletions

View file

@ -415,10 +415,8 @@ public:
{ {
if(mod.moduleDeclaration !is null) if(mod.moduleDeclaration !is null)
{ {
auto currNoLint = NoLintFactory.fromModuleDeclaration(mod.moduleDeclaration); with(noLint.push(NoLintFactory.fromModuleDeclaration(mod.moduleDeclaration)))
noLint.push(currNoLint); mod.accept(this);
scope(exit) noLint.pop(currNoLint);
mod.accept(this);
} }
else else
mod.accept(this); mod.accept(this);
@ -431,11 +429,8 @@ public:
*/ */
override void visit(const(Declaration) decl) override void visit(const(Declaration) decl)
{ {
auto currNoLint = NoLintFactory.fromDeclaration(decl); with(noLint.push(NoLintFactory.fromDeclaration(decl)))
noLint.push(currNoLint); decl.accept(this);
scope(exit) noLint.pop(currNoLint);
decl.accept(this);
} }
AutoFix.CodeReplacement[] resolveAutoFix( AutoFix.CodeReplacement[] resolveAutoFix(

View file

@ -8,7 +8,6 @@ import std.regex: regex, matchAll;
import std.string: strip; import std.string: strip;
import std.typecons; import std.typecons;
struct NoLint struct NoLint
{ {
bool containsCheck(in string check) const bool containsCheck(in string check) const
@ -17,6 +16,18 @@ struct NoLint
disabledChecks[check] > 0; disabledChecks[check] > 0;
} }
// automatic pop when returned value goes out of scope
Poppable push(in Nullable!NoLint other)
{
if(other.isNull)
return Poppable((){});
foreach(item; other.get.getDisabledChecks.byKeyValue)
this.disabledChecks[item.key] += item.value;
return Poppable(() => this.pop(other));
}
package: package:
const(int[string]) getDisabledChecks() const const(int[string]) getDisabledChecks() const
{ {
@ -28,7 +39,7 @@ package:
disabledChecks[check]++; disabledChecks[check]++;
} }
void push(in Nullable!NoLint other) void merge(in Nullable!NoLint other)
{ {
if(other.isNull) if(other.isNull)
return; return;
@ -37,6 +48,7 @@ package:
this.disabledChecks[item.key] += item.value; this.disabledChecks[item.key] += item.value;
} }
private:
void pop(in Nullable!NoLint other) void pop(in Nullable!NoLint other)
{ {
if(other.isNull) if(other.isNull)
@ -51,9 +63,15 @@ package:
} }
} }
struct Poppable {
void delegate() onPop;
private: ~this() {
int[string] disabledChecks; onPop();
}
}
int[string] disabledChecks;
} }
struct NoLintFactory struct NoLintFactory
@ -63,7 +81,7 @@ struct NoLintFactory
NoLint noLint; NoLint noLint;
foreach(atAttribute; moduleDeclaration.atAttributes) foreach(atAttribute; moduleDeclaration.atAttributes)
noLint.push(NoLintFactory.fromAtAttribute(atAttribute)); noLint.merge(NoLintFactory.fromAtAttribute(atAttribute));
if(!noLint.getDisabledChecks.length) if(!noLint.getDisabledChecks.length)
return nullNoLint; return nullNoLint;
@ -75,7 +93,7 @@ struct NoLintFactory
{ {
NoLint noLint; NoLint noLint;
foreach(attribute; declaration.attributes) foreach(attribute; declaration.attributes)
noLint.push(NoLintFactory.fromAttribute(attribute)); noLint.merge(NoLintFactory.fromAttribute(attribute));
if(!noLint.getDisabledChecks.length) if(!noLint.getDisabledChecks.length)
return nullNoLint; return nullNoLint;
@ -163,7 +181,7 @@ private:
auto str = primaryExpression.primary.text.strip("\""); auto str = primaryExpression.primary.text.strip("\"");
Nullable!NoLint currNoLint = NoLintFactory.fromString(str); Nullable!NoLint currNoLint = NoLintFactory.fromString(str);
noLint.push(currNoLint); noLint.merge(currNoLint);
} }
} }

View file

@ -34,9 +34,8 @@ final class StyleChecker : BaseAnalyzer
override void visit(const ModuleDeclaration dec) override void visit(const ModuleDeclaration dec)
{ {
auto currNoLint = NoLintFactory.fromModuleDeclaration(dec); with(noLint.push(NoLintFactory.fromModuleDeclaration(dec)))
noLint.push(currNoLint); dec.accept(this);
scope(exit) noLint.pop(currNoLint);
foreach (part; dec.moduleName.identifiers) foreach (part; dec.moduleName.identifiers)
{ {

View file

@ -94,11 +94,8 @@ public:
{ {
_inStruct.insert(decl.structDeclaration !is null); _inStruct.insert(decl.structDeclaration !is null);
auto currNoLint = NoLintFactory.fromDeclaration(decl); with(noLint.push(NoLintFactory.fromDeclaration(decl)))
noLint.push(currNoLint); decl.accept(this);
scope(exit) noLint.pop(currNoLint);
decl.accept(this);
if (_inStruct.length > 1 && _inStruct[$-2] && decl.constructor && if (_inStruct.length > 1 && _inStruct[$-2] && decl.constructor &&
((decl.constructor.parameters && decl.constructor.parameters.parameters.length == 0) || ((decl.constructor.parameters && decl.constructor.parameters.parameters.length == 0) ||