Skip to content

Commit c37f0fc

Browse files
authored
Merge pull request #159 from codecov/dana/js-import-lines
search for imports, add them to the JS analyzer
2 parents 4f51e9e + 657e28d commit c37f0fc

6 files changed

Lines changed: 71 additions & 7 deletions

File tree

codecov_cli/services/staticanalysis/analyzers/general.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ def _get_parent_chain(self, node):
5454
while cur:
5555
yield cur
5656
cur = cur.parent
57+
58+
def get_import_lines(self, root_node, imports_query):
59+
import_lines = set()
60+
for (a, _) in imports_query.captures(root_node):
61+
import_lines.add((a.start_point[0] + 1, a.end_point[0] - a.start_point[0]))
62+
return import_lines

codecov_cli/services/staticanalysis/analyzers/javascript_es6/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
(method_definition) @elemen
1414
"""
1515

16+
imports_query_str = """
17+
(import_statement) @elemen
18+
(import) @elemen
19+
"""
20+
1621

1722
class ES6Analyzer(BaseAnalyzer):
1823
condition_statements = [
@@ -35,6 +40,7 @@ def __init__(self, path, actual_code, **options):
3540
self.JS_LANGUAGE = Language(staticcodecov_languages.__file__, "javascript")
3641
self.parser = Parser()
3742
self.parser.set_language(self.JS_LANGUAGE)
43+
self.import_lines = set()
3844

3945
def get_code_hash(self, start_byte, end_byte):
4046
j = hashlib.md5()
@@ -47,6 +53,7 @@ def process(self):
4753
root_node = tree.root_node
4854
function_query = self.JS_LANGUAGE.query(function_query_str)
4955
method_query = self.JS_LANGUAGE.query(method_query_str)
56+
imports_query = self.JS_LANGUAGE.query(imports_query_str)
5057
combined_results = function_query.captures(root_node) + method_query.captures(
5158
root_node
5259
)
@@ -63,6 +70,9 @@ def process(self):
6370
"complexity_metrics": self._get_complexity_metrics(body_node),
6471
}
6572
)
73+
74+
self.import_lines = self.get_import_lines(root_node, imports_query)
75+
6676
h = hashlib.md5()
6777
h.update(self.actual_code)
6878
return {
@@ -73,4 +83,5 @@ def process(self):
7383
"hash": h.hexdigest(),
7484
"filename": str(self.path),
7585
"language": "javascript",
86+
"import_lines": sorted(self.import_lines),
7687
}

codecov_cli/services/staticanalysis/analyzers/python/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ def process(self):
9393
self.definitions_lines.add(
9494
(a.start_point[0] + 1, a.end_point[0] - a.start_point[0])
9595
)
96-
for (a, _) in imports_query.captures(root_node):
97-
self.import_lines.add(
98-
(a.start_point[0] + 1, a.end_point[0] - a.start_point[0])
99-
)
96+
97+
self.import_lines = self.get_import_lines(root_node, imports_query)
98+
10099
h = hashlib.md5()
101100
h.update(self.actual_code)
102101
statements = sorted(

samples/example_module.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Default export
2+
export default function greet(name) {
3+
console.log(`Hello, ${name}!`);
4+
}
5+
6+
// Named exports
7+
export function sum(a, b) {
8+
return a + b;
9+
}
10+
11+
export const pi = 3.14159;
12+

samples/inputs/sample_003.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,34 @@ async function getData() {
9595
}
9696
}
9797

98+
// Importing a default export
99+
import greet from '../example_module.js';
100+
101+
// Importing named exports
102+
import { sum, pi } from '../example_module.js';
103+
104+
// Importing a default export and renaming it
105+
import { default as renamedGreet } from '../example_module.js';
106+
107+
// Importing everything from a module
108+
import * as moduleExports from '../example_module.js';
109+
110+
// Importing a default export and specific named exports together
111+
import greet, { pi as constantPi } from '../example_module.js';
112+
113+
//Dynamic import by using import(), which is different from the previous imports (static imports)
114+
let myModule;
115+
if (typeof window === "undefined") {
116+
myModule = await import("../example_module.js");
117+
} else {
118+
myModule = await import("../example_module.js");
119+
}
120+
greet('Alice');
121+
console.log(sum(2, 3));
122+
console.log(pi);
123+
console.log(renamedGreet('Bob'));
124+
console.log(moduleExports.sum(4, 5));
125+
console.log(moduleExports.pi);
126+
console.log(greet('Charlie'));
127+
console.log(constantPi);
98128
getData();

samples/outputs/sample_003.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
75,
1818
79,
1919
88,
20-
97
20+
97,
21+
100,
22+
103,
23+
106,
24+
109,
25+
112
2126
],
2227
"executable_lines": [],
23-
"number_lines": 98,
24-
"hash": "ad3f8572135398ba290dcb36ff0410c2",
28+
"number_lines": 128,
29+
"hash": "6cfa011ad0c3793848438c3fff8bd5b3",
2530
"filename": "samples/inputs/sample_003.js",
2631
"language": "javascript",
32+
"import_lines": [[99, 0], [102, 0], [105, 0], [108, 0], [111, 0], [116, 0], [118, 0]],
2733
"functions": [
2834
{
2935
"identifier": "myFunction",

0 commit comments

Comments
 (0)