Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoided using temp.js #48

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions babelParser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
const babel = require('@babel/parser');
const fs = require('fs');

const code = fs.readFileSync(process.argv[2], 'utf8');

try {
const ast = babel.parse(code, {
sourceType: "module",
plugins: [],
});
console.log(JSON.stringify(ast));
} catch (error) {
console.error("Parsing error:", error);
}

process.stdin.setEncoding('utf8');

let code = '';

process.stdin.on('readable', () => {
let chunk;
while ((chunk = process.stdin.read()) !== null) {
code += chunk;
}
});

process.stdin.on('end', () => {
try {
const ast = babel.parse(code, {
sourceType: "module",
plugins: [],
});
console.log(JSON.stringify(ast));
} catch (error) {
console.error("Parsing error:", error);
}
});
27 changes: 9 additions & 18 deletions getFunctionData.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,18 @@ def get_function_data(repo_path='../inputData/testRepo'):
merge_commits = [commit for commit in repo.iter_commits('main') if commit.parents and len(commit.parents) > 1]
merge_commits.reverse()

def create_temp_file_and_get_ast(file_content, temp_file_path='temp.js'):
with open(temp_file_path, 'w') as f:
f.write(file_content)
ast = get_ast_from_js(file_content, temp_file_path)
if os.path.exists(temp_file_path):
os.remove(temp_file_path) # Clean up the temporary file
return ast

def get_ast_from_js(file_content, temp_file_path):
with open(temp_file_path, 'w') as temp_file:
temp_file.write(file_content)
result = subprocess.run(['node', 'babelParser.js', temp_file_path], capture_output=True, text=True)
if result.stderr:
print("Error in parsing:", result.stderr)
def get_ast_from_js(file_content):
process = subprocess.Popen(['node', 'babelParser.js'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate(input=file_content)
if stderr:
print("Error in parsing:", stderr)
return None
return json.loads(result.stdout)
return json.loads(stdout)

def get_functions_from_file(file_content):

# create ast from file content
ast = create_temp_file_and_get_ast(file_content)
ast = get_ast_from_js(file_content)

functions = []
try:
Expand Down Expand Up @@ -106,7 +97,7 @@ def get_full_function_at_commit(repo, commit_hash, function_name, file_path):
file_content = blob.data_stream.read().decode('utf-8')

# create ast from file content
ast = create_temp_file_and_get_ast(file_content)
ast = get_ast_from_js(file_content)

try:
# Define a function to recursively search for the function
Expand Down Expand Up @@ -208,7 +199,7 @@ def find_function(node, function_name):

if __name__ == '__main__':
start_time = time.time()
get_function_data() #pass this variable if you want to run another repo than testRepo: repo_path='../inputData/elixirsolutions'
get_function_data(repo_path='../inputData/elixirsolutions') #pass this variable if you want to run another repo than testRepo: repo_path='../inputData/elixirsolutions'
end_time = time.time()
elapsed_time = round((end_time - start_time) / 60, 2) # convert to minutes and round to 2 decimal places
print('✅ Printed function data to outputData/test_function_changes.json ✅')
Expand Down
Loading