Skip to content

Commit

Permalink
test, failure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ipeleg committed Sep 14, 2023
1 parent a63afa5 commit 5cce7df
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import logging
import os.path
from abc import ABC
from typing import List, Dict, Set, Any
from typing import List, Dict, Set, Any, Callable
import re
import json
import os
Expand All @@ -13,20 +14,24 @@

class JavascriptAliasMappingStrategy(ABC):

def _parse_export(self, file_content: str, pattern: str) -> Dict[str, Any] | None:
@staticmethod
def __parse_export(file_content: str, pattern: str) -> Dict[str, Any] | None:
module_export_match = re.search(pattern, file_content, re.DOTALL)

if module_export_match:
module_exports_str = module_export_match.group(1)
module_exports_str = re.sub(r'([{\s,])(\w+):', r'\1"\2":', module_exports_str).replace("'", "\"")
# for having for all the keys and values doube quotes and removing spaces
module_exports_str = re.sub(r'\s+', '', re.sub(r'([{\s,])(\w+):', r'\1"\2":', module_exports_str)
.replace("'", "\""))
print(module_exports_str)
module_exports: Dict[str, Any]= json.loads(module_exports_str)
return module_exports
return None

def parse_webpack_file(self, alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
@staticmethod
def _parse_webpack_file(alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
-> None:
module_exports_json = self._parse_export(file_content, MODULE_EXPORTS_PATTERN)
module_exports_json = JavascriptAliasMappingStrategy.__parse_export(file_content, MODULE_EXPORTS_PATTERN)

if module_exports_json:
aliases = module_exports_json.get("resolve", {}).get("alias", {})
Expand All @@ -35,7 +40,8 @@ def parse_webpack_file(self, alias_mapping: Dict[str, List[str]], file_content:
if package_name in relevant_packages:
alias_mapping.setdefault(package_name, []).append(imported_name)

def parse_tsconfig_file(self, alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
@staticmethod
def _parse_tsconfig_file(alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
-> None:
tsconfig_json = json.loads(file_content)
paths = tsconfig_json.get("compilerOptions", {}).get("paths", {})
Expand All @@ -45,8 +51,8 @@ def parse_tsconfig_file(self, alias_mapping: Dict[str, List[str]], file_content:
if package_name in relevant_packages:
alias_mapping.setdefault(package_name, []).append(imported_name)


def parse_babel_file(self, alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
@staticmethod
def _parse_babel_file(alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
-> None:
babelrc_json = json.loads(file_content)
plugins = babelrc_json.get("plugins", {})
Expand All @@ -59,28 +65,30 @@ def parse_babel_file(self, alias_mapping: Dict[str, List[str]], file_content: st
if package_name in relevant_packages:
alias_mapping.setdefault(package_name, []).append(imported_name)

def parse_rollup_file(self, alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
@staticmethod
def _parse_rollup_file(alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
-> None:
export_default_match = re.search(EXPORT_DEFAULT_PATTERN, file_content, re.DOTALL)

if export_default_match:
export_default_str = export_default_match.group(1)
export_default_str = re.sub(r'([{\s,])(\w+):', r'\1"\2":', export_default_str).replace("'", "\"")
export_default_str = re.sub(r'\s+', '', export_default_str)
# for having for all the keys and values doube quotes and removing spaces
export_default_str = re.sub(r'\s+', '', re.sub(r'([{\s,])(\w+):', r'\1"\2":', export_default_str)
.replace("'", "\""))

# Define a regular expression pattern to match the elements within the "plugins" list
# Defining a regular expression pattern to match the elements within the "plugins" list
pattern = r'alias\(\{[^)]*\}\)'

# Use the findall() function to find all matches of the pattern in the input string
matches = re.findall(pattern, export_default_str)

for alias_object_str in matches:
alias_object = json.loads(alias_object_str[6:-1]) # removing 'alias(' and ')'
print(alias_object)
for entry in alias_object.get("entries", []):
alias_mapping.setdefault(entry["replacement"], []).append(entry["find"])

if entry["replacement"] in relevant_packages:
alias_mapping.setdefault(entry["replacement"], []).append(entry["find"])

def parse_package_json_file(self, alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
@staticmethod
def _parse_package_json_file(alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
-> None:
package_json = json.loads(file_content)
aliases: Dict[str, str] = dict()
Expand All @@ -89,22 +97,26 @@ def parse_package_json_file(self, alias_mapping: Dict[str, List[str]], file_cont
if package_json.get("aliasify", {}).get("aliases"):
aliases.update(package_json["aliasify"]["aliases"])
for imported_name in aliases:
alias_mapping.setdefault(aliases[imported_name], []).append(imported_name)
if aliases[imported_name]:
alias_mapping.setdefault(aliases[imported_name], []).append(imported_name)

def parse_snowpack_file(self, alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
@staticmethod
def _parse_snowpack_file(alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
-> None:
module_exports_json = self._parse_export(file_content, MODULE_EXPORTS_PATTERN)
module_exports_json = JavascriptAliasMappingStrategy.__parse_export(file_content, MODULE_EXPORTS_PATTERN)

if module_exports_json:
aliases = module_exports_json.get("alias", {})
for imported_name in aliases:
package_name = aliases[imported_name]
if package_name in relevant_packages:
alias_mapping.setdefault(package_name, []).append(imported_name)
if package_name in relevant_packages:
alias_mapping.setdefault(package_name, []).append(imported_name)

def parse_vite_file(self, alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
@staticmethod
def _parse_vite_file(alias_mapping: Dict[str, List[str]], file_content: str, relevant_packages: Set[str])\
-> None:
export_default_match = self._parse_export(file_content, EXPORT_DEFAULT_PATTERN)
export_default_match = JavascriptAliasMappingStrategy.__parse_export(file_content, EXPORT_DEFAULT_PATTERN)

if export_default_match:
aliases = export_default_match.get("resolve", {}).get("alias", {})
Expand All @@ -113,5 +125,36 @@ def parse_vite_file(self, alias_mapping: Dict[str, List[str]], file_content: str
if package_name in relevant_packages:
alias_mapping.setdefault(package_name, []).append(imported_name)

def create_alias_mapping(self, root_dir: str, relevant_packages: List[str]) -> Dict[str, List[str]]:
return dict()
@staticmethod
def _get_file_name_to_function_map() -> Dict[str, Callable[[Dict[str, List[str]], str, Set[str]], None]]:
return {
"webpack.config.js": JavascriptAliasMappingStrategy._parse_webpack_file,
"tsconfig.json": JavascriptAliasMappingStrategy._parse_tsconfig_file,
".babelrc": JavascriptAliasMappingStrategy._parse_babel_file,
"babel.config.js": JavascriptAliasMappingStrategy._parse_babel_file,
"rollup.config.js": JavascriptAliasMappingStrategy._parse_rollup_file,
"package.json": JavascriptAliasMappingStrategy._parse_package_json_file,
"snowpack.config.js": JavascriptAliasMappingStrategy._parse_snowpack_file,
"vite.config.js": JavascriptAliasMappingStrategy._parse_vite_file
}

def create_alias_mapping(self, root_dir: str, relevant_packages: Set[str]) -> Dict[str, List[str]]:
logging.debug("[JavascriptAliasMappingStrategy](create_alias_mapping) - starting")
alias_mapping: dict[str, list[str]] = dict()
file_name_to_function_map = self._get_file_name_to_function_map()
for curr_root, _, f_names in os.walk(root_dir):
for file_name in f_names:
if file_name in file_name_to_function_map:
logging.debug(f"[JavascriptAliasMappingStrategy](create_alias_mapping) - starting parsing ${file_name}")
with open(os.path.join(curr_root, file_name)) as f:
file_content = f.read()
try:
file_name_to_function_map[file_name](alias_mapping, file_content, relevant_packages)
logging.debug(
f"[JavascriptAliasMappingStrategy](create_alias_mapping) - done parsing for ${file_name}")
except:
logging.error(f"[JavascriptAliasMappingStrategy](create_alias_mapping) - failure when "
f"parsing the file '${file_name}'. file content:\n{file_content}.\n",
exc_info=True)

return alias_mapping
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["module-resolver", {
"alias": {
"ax": "axios"
}
}]
]
}
9 changes: 9 additions & 0 deletions tests/common/sca/reachability/examples/babel/babelrc/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["module-resolver", {
"alias": {
"ax": "axios"
}
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["module-resolver", {
"alias": {
"ax": "axios"
}
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"alias": {
"ax": "axios"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"aliasify": {
"aliases": {
"ax": "axios"
}
}
}
11 changes: 11 additions & 0 deletions tests/common/sca/reachability/examples/rollup/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import alias from '@rollup/plugin-alias';

export default {
plugins: [
alias({
entries: [
{ find: 'ax', replacement: 'axios' }
]
})
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
alias: {
"ax": "axios"
}
};
8 changes: 8 additions & 0 deletions tests/common/sca/reachability/examples/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"ax": ["node_modules/axios"]
}
}
}
7 changes: 7 additions & 0 deletions tests/common/sca/reachability/examples/vite/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
resolve: {
alias: {
"ax": "axios"
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
resolve: {
alias: {
ax: 'axios'
}
}
};
Loading

0 comments on commit 5cce7df

Please sign in to comment.