Improvements for getting touched functions in JavaScript code (#1187)

* Skip error entries and anonymous functions right away

* Add a few comments to explain get_touched_functions

* Don't stop at the first function containing a line

To support functions which are defined inside other functions.

* Skip a function when it has already been added to the set

* Add some get_touched_functions tests with JavaScript code
This commit is contained in:
Marco Castelluccio 2019-12-19 12:15:40 +01:00 коммит произвёл GitHub
Родитель 184dbfb2d3
Коммит c479ab2c26
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 54 добавлений и 4 удалений

Просмотреть файл

@ -262,18 +262,24 @@ def get_touched_functions(path, deleted_lines, added_lines, content):
touched_function_names = set()
functions = function_data["spans"]
functions = [
function
for function in functions
if not function["error"] and function["name"] != "<anonymous>"
]
def get_touched(functions, lines):
last_f = 0
for line in lines:
for function in functions[last_f:]:
if function["error"] or function["end_line"] < line:
# Skip functions which we already passed.
if function["end_line"] < line:
last_f += 1
continue
if function["start_line"] <= line:
# If the line belongs to this function, add the function to the set of touched functions.
elif function["start_line"] <= line:
touched_function_names.add(function["name"])
break
last_f += 1
# Get functions touched by added lines.
get_touched(functions, added_lines)

Просмотреть файл

@ -870,3 +870,47 @@ void func2() {
)
assert touched_functions == {("func2", 5, 7)}
# top-level and a JavaScript function touched.
touched_functions = repository.get_touched_functions(
"file.js",
[],
[1, 4],
"""let j = 0;
function func() {
let i = 0;
}""",
)
assert touched_functions == {("func", 3, 5)}
# An anonymous function touched inside another function.
touched_functions = repository.get_touched_functions(
"file.jsm",
[],
[4],
"""function outer_func() {
let i = 0;
let f = function() {
let j = 0;
}();
}""",
)
assert touched_functions == {("outer_func", 1, 6)}
# A function touched inside another function.
touched_functions = repository.get_touched_functions(
"file.jsm",
[],
[4],
"""function outer_func() {
let i = 0;
function inner_func() {
let j = 0;
}
}""",
)
assert touched_functions == {("outer_func", 1, 6), ("inner_func", 3, 5)}