Fix more problems in the sorted import checker (#445)
* Sorted imports: put ConditionalDeclaration and IfStatement in a separate scope * Allow intermediate imports
This commit is contained in:
parent
6d34b149a9
commit
dab25d5e31
|
@ -24,32 +24,17 @@ class ImportSortednessCheck : BaseAnalyzer
|
||||||
super(fileName, null, skipTests);
|
super(fileName, null, skipTests);
|
||||||
}
|
}
|
||||||
|
|
||||||
override void visit(const Module mod)
|
mixin ScopedVisit!Module;
|
||||||
|
mixin ScopedVisit!Statement;
|
||||||
|
mixin ScopedVisit!BlockStatement;
|
||||||
|
mixin ScopedVisit!StructBody;
|
||||||
|
mixin ScopedVisit!IfStatement;
|
||||||
|
mixin ScopedVisit!TemplateDeclaration;
|
||||||
|
mixin ScopedVisit!ConditionalDeclaration;
|
||||||
|
|
||||||
|
override void visit(const VariableDeclaration id)
|
||||||
{
|
{
|
||||||
level = 0;
|
|
||||||
imports[level] = [];
|
imports[level] = [];
|
||||||
mod.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(const Statement decl)
|
|
||||||
{
|
|
||||||
imports[++level] = [];
|
|
||||||
decl.accept(this);
|
|
||||||
level--;
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(const BlockStatement decl)
|
|
||||||
{
|
|
||||||
imports[++level] = [];
|
|
||||||
decl.accept(this);
|
|
||||||
level--;
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(const StructBody decl)
|
|
||||||
{
|
|
||||||
imports[++level] = [];
|
|
||||||
decl.accept(this);
|
|
||||||
level--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override void visit(const ImportDeclaration id)
|
override void visit(const ImportDeclaration id)
|
||||||
|
@ -84,6 +69,16 @@ private:
|
||||||
int level = 0;
|
int level = 0;
|
||||||
string[][int] imports;
|
string[][int] imports;
|
||||||
|
|
||||||
|
template ScopedVisit(NodeType)
|
||||||
|
{
|
||||||
|
override void visit(const NodeType n)
|
||||||
|
{
|
||||||
|
imports[++level] = [];
|
||||||
|
n.accept(this);
|
||||||
|
level--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void addImport(string importModuleName, const SingleImport singleImport)
|
void addImport(string importModuleName, const SingleImport singleImport)
|
||||||
{
|
{
|
||||||
import std.uni : sicmp;
|
import std.uni : sicmp;
|
||||||
|
@ -356,5 +351,36 @@ unittest
|
||||||
import std.range.primitives : isInputRange, walkLength;
|
import std.range.primitives : isInputRange, walkLength;
|
||||||
}, sac);
|
}, sac);
|
||||||
|
|
||||||
|
// condition declaration
|
||||||
|
assertAnalyzerWarnings(q{
|
||||||
|
import t2;
|
||||||
|
version(unittest)
|
||||||
|
{
|
||||||
|
import t1;
|
||||||
|
}
|
||||||
|
}, sac);
|
||||||
|
|
||||||
|
// if statements
|
||||||
|
assertAnalyzerWarnings(q{
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import t2;
|
||||||
|
if (true)
|
||||||
|
{
|
||||||
|
import t1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, sac);
|
||||||
|
|
||||||
|
// intermediate imports
|
||||||
|
assertAnalyzerWarnings(q{
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import t2;
|
||||||
|
int a = 1;
|
||||||
|
import t1;
|
||||||
|
}
|
||||||
|
}, sac);
|
||||||
|
|
||||||
stderr.writeln("Unittest for ImportSortednessCheck passed.");
|
stderr.writeln("Unittest for ImportSortednessCheck passed.");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue