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:
Sebastian Wilzbach 2017-06-12 11:26:43 +02:00 committed by Basile Burg
parent 6d34b149a9
commit dab25d5e31
1 changed files with 50 additions and 24 deletions

View File

@ -24,32 +24,17 @@ class ImportSortednessCheck : BaseAnalyzer
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] = [];
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)
@ -84,6 +69,16 @@ private:
int level = 0;
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)
{
import std.uni : sicmp;
@ -356,5 +351,36 @@ unittest
import std.range.primitives : isInputRange, walkLength;
}, 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.");
}