|
1 | 1 | import ql |
| 2 | +private import YAML |
| 3 | +private import codeql_ql.ast.internal.Module |
2 | 4 |
|
3 | | -Import imports(Import imp) { |
| 5 | +private FileOrModule getResolvedModule(Import imp) { |
| 6 | + result = imp.getResolvedModule() and |
| 7 | + // skip the top-level language files |
| 8 | + not result.asFile() = any(QLPack p).getLanguageLib() |
| 9 | +} |
| 10 | + |
| 11 | +private Import imports(Import imp) { |
4 | 12 | ( |
5 | 13 | exists(File file, TopLevel top | |
6 | | - imp.getResolvedModule().asFile() = file and |
| 14 | + getResolvedModule(imp).asFile() = file and |
7 | 15 | top.getLocation().getFile() = file and |
8 | 16 | result = top.getAMember() |
9 | 17 | ) |
10 | 18 | or |
11 | 19 | exists(Module mod | |
12 | | - imp.getResolvedModule().asModule() = mod and |
| 20 | + getResolvedModule(imp).asModule() = mod and |
13 | 21 | result = mod.getAMember() |
14 | 22 | ) |
15 | 23 | ) |
16 | 24 | } |
17 | 25 |
|
18 | | -Import getAnImport(AstNode parent) { |
| 26 | +private Import getAnImport(AstNode parent) { |
19 | 27 | result = parent.(TopLevel).getAMember() |
20 | 28 | or |
21 | 29 | result = parent.(Module).getAMember() |
22 | 30 | } |
23 | 31 |
|
24 | | -pragma[inline] |
25 | | -predicate importsFromSameFolder(Import a, Import b) { |
26 | | - exists(string base | |
27 | | - a.getImportString().regexpCapture("(.*)\\.[^\\.]*", 1) = base and |
28 | | - b.getImportString().regexpCapture("(.*)\\.[^\\.]*", 1) = base |
29 | | - ) |
30 | | - or |
31 | | - not a.getImportString().matches("%.%") and |
32 | | - not b.getImportString().matches("%.%") |
33 | | -} |
34 | | - |
35 | 32 | predicate problem(Import imp, Import redundant, string message) { |
36 | 33 | not exists(imp.importedAs()) and |
37 | 34 | not exists(redundant.importedAs()) and |
38 | 35 | not exists(imp.getModuleExpr().getQualifier*().getArgument(_)) and // any type-arguments, and we ignore, they might be different. |
39 | 36 | // skip the top-level language files, they have redundant imports, and that's fine. |
40 | | - not exists(imp.getLocation().getFile().getParentContainer().getFile("qlpack.yml")) and |
| 37 | + not imp.getLocation().getFile() = any(QLPack p).getLanguageLib() and |
41 | 38 | // skip the DataFlowImpl.qll and similar, they have redundant imports in some copies. |
42 | 39 | not imp.getLocation() |
43 | 40 | .getFile() |
44 | 41 | .getBaseName() |
45 | 42 | .regexpMatch([".*Impl\\d?\\.qll", "DataFlowImpl.*\\.qll"]) and |
46 | | - // skip two imports that imports different things from the same folder. |
47 | | - not importsFromSameFolder(imp, redundant) and |
48 | 43 | // if the redundant is public, and the imp is private, then the redundant might add things that are exported. |
49 | 44 | not (imp.isPrivate() and not redundant.isPrivate()) and |
50 | 45 | // Actually checking if the import is redundant: |
51 | 46 | exists(AstNode parent | |
52 | 47 | imp = getAnImport(parent) and |
53 | | - redundant = getAnImport(parent) and |
54 | | - redundant.getLocation().getStartLine() > imp.getLocation().getStartLine() |
| 48 | + redundant = getAnImport(parent) |
55 | 49 | | |
56 | 50 | message = "Redundant import, the module is already imported inside $@." and |
57 | 51 | // only looking for things directly imported one level down. Otherwise things gets complicated (lots of cycles). |
58 | 52 | exists(Import inner | inner = imports(imp) | |
59 | | - redundant.getResolvedModule() = inner.getResolvedModule() and |
| 53 | + getResolvedModule(redundant) = getResolvedModule(inner) and |
60 | 54 | not inner.isPrivate() and // if the inner is private, then it's not propagated out. |
| 55 | + not inner.isDeprecated() and |
61 | 56 | not exists(inner.importedAs()) |
62 | 57 | ) |
63 | 58 | or |
64 | 59 | message = "Duplicate import, the module is already imported by $@." and |
65 | 60 | // two different import statements, that import the same thing |
66 | | - imp.getResolvedModule() = redundant.getResolvedModule() |
| 61 | + getResolvedModule(imp) = getResolvedModule(redundant) and |
| 62 | + redundant.getLocation().getStartLine() > imp.getLocation().getStartLine() |
67 | 63 | ) |
68 | 64 | } |
0 commit comments