Skip to content
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# Log file
*.log

# Code coverage files
.coverage

# Gradle files
.gradle

Expand Down
52 changes: 52 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Makefile targets for dvelopment an testing
# Use make help for more info

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

.PHONY: all
all: help

##@ Development

.PHONY: venv
venv: ## Create a Python virtual environment
$(info Creating Python 3 virtual environment...)
poetry shell

.PHONY: install
install: ## Install Python dependencies in virtual environment
$(info Installing dependencies...)
poetry config virtualenvs.in-project true
poetry install

.PHONY: lint
lint: ## Run the linter
$(info Running linting...)
flake8 cldk --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 cldk --count --max-complexity=10 --max-line-length=180 --statistics
pylint cldk --max-line-length=180

.PHONY: test
test: ## Run the unit tests
$(info Running tests...)
pytest --pspec --cov=cldk --cov-fail-under=50 --disable-warnings

##@ Build

.PHONY: clean
clean: ## Cleans up from previous compiles
$(info Cleaning up compile artifacts...)
rm -fr dist

.PHONY: refresh
refresh: ## Refresh code analyzer
$(info Refreshing CodeAnalyzer...)
wget $(curl -s https://api.github.com/repos/IBM/codenet-minerva-code-analyzer/releases/latest | grep "browser_download_url" | grep codeanalyzer.jar | cut -d '"' -f 4)
mv codeanalyzer.jar cldk/analysis/java/codeanalyzer/jar/codeanalyzer.jar

.PHONY: build
build: ## Builds a new Python wheel
$(info Building artifacts...)
poetry build
4 changes: 2 additions & 2 deletions cldk/analysis/javascript/treesitter/javascript_sitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def __get_top_level_functions(self, code: str) -> List[JsCallable]:

query = """
(program [
(function_declaration) @function
(function_declaration) @function
(export_statement (function_declaration) @function.export)
])
"""
Expand All @@ -210,7 +210,7 @@ def __get_top_level_generators(self, code: str) -> List[JsCallable]:

query = """
(program [
(generator_function_declaration) @generator
(generator_function_declaration) @generator
(export_statement (generator_function_declaration) @generator.export)
])
"""
Expand Down
2 changes: 1 addition & 1 deletion cldk/models/java/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@

from .constants_namespace import ConstantsNamespace

__all__ = [JApplication, JCallable, JType, JCompilationUnit, JGraphEdges, ConstantsNamespace]
__all__ = ["JApplication", "JCallable", "JType", "JCompilationUnit", "JGraphEdges", "ConstantsNamespace"]
3 changes: 3 additions & 0 deletions cldk/models/java/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ class JCallable(BaseModel):
cyclomatic_complexity: int | None

def __hash__(self):
"""
Returns the hash value of the declaration.
"""
return hash(self.declaration)

@model_validator(mode="after")
Expand Down
4 changes: 4 additions & 0 deletions cldk/models/javascript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
"""
JavaScript package
"""

from .models import JsParameter, JsCallable, JsClass, JsProgram

__all__ = ["JsParameter", "JsCallable", "JsClass", "JsProgram"]
2 changes: 1 addition & 1 deletion cldk/models/javascript/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JsParameter(BaseModel):
name: str
default_value: Optional[str] = None
is_rest: bool = False
# type: Optional[str] - might be able to extract from JsDoc
# type Optional[str] - might be able to extract from JsDoc


class JsCallable(BaseModel):
Expand Down
1 change: 1 addition & 0 deletions cldk/models/treesitter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Capture:
name: str

def __init__(self, captures: Dict[str, List[Node]]):
self.captures = []
for capture_name, captures in captures.items():
self.captures = [self.Capture(node=node, name=capture_name) for node in captures]

Expand Down
307 changes: 298 additions & 9 deletions poetry.lock

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ tree-sitter-javascript = "0.23.0"
[tool.poetry.group.dev.dependencies]
toml = "^0.10.2"
pytest = "8.3.3"
pytest-pspec = "^0.0.4"
pytest-cov = "^5.0.0"
pylint = "^3.2.2"
flake8 = "^7.0.0"
black = "^24.4.2"
coverage = "^7.5.3"
jupyter = "^1.0.0"

# Documentation
Expand All @@ -58,9 +64,53 @@ mkdocstrings = "0.26.1"
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

########################################
# Tool configurations
########################################
[tool.flake8]
max-line-length = 180
count = true

[tool.black]
line-length = 180

[tool.pylint.'MESSAGES CONTROL']
disable = "no-member,protected-access,global-statement"

[tool.pylint.FORMAT]
max-line-length = 180

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "--pspec --cov=cldk --cov-fail-under=50"
testpaths = ["tests"]

[tool.coverage.run]
source = ["cldk"]
omit = [
"venv/*",
".venv/*"
]

[tool.coverage.report]
show_missing = true
exclude_lines = [
"pragma: no cover",
"pragma: no branch",
"pass",
"subprocess.CalledProcessError",
"sys.exit",
"if __name__ == .__main__.:"
]
ignore_errors = true

[tool.coverage.xml]
output="./coverage.xml"

[tool.coverage.html]
title = "Test Coverage Report"
directory = "coverage_html_report"

[tool.cldk.testing]
sample-application = "tests/resources/java/application/"
sample-application-analysis-json = "tests/resources/java/analysis_json/"
Expand Down
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code Quality

[flake8]
max-line-length = 180
per-file-ignores =
*/__init__.py: F401 E402
2 changes: 1 addition & 1 deletion tests/tree_sitter/c/test_c_tree_sitter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from cldk.treesitter.c import CSitter
from cldk.analysis.c.treesitter import CSitter


class TestCTreeSitter(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/tree_sitter/go/test_go_tree_sitter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from cldk.treesitter.go import GoSitter
from cldk.analysis.go.treesitter import GoSitter


class TestGoTreeSitter(TestCase):
Expand Down
24 changes: 22 additions & 2 deletions tests/tree_sitter/javascript/test_javascript_tree_sitter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
################################################################################
# Copyright IBM Corporation 2024
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################

"""
Javascript Treesitter Tests
"""

from typing import List

from unittest import TestCase

from cldk.treesitter.javascript import JavascriptSitter
from cldkmodels.javascript.models import JsCallable
from cldk.analysis.javascript.treesitter import JavascriptSitter
from cldk.models.javascript.models import JsCallable


class TestJavascriptTreeSitter(TestCase):
Expand Down