Skip to content

Commit

Permalink
add description to patterns (#5)
Browse files Browse the repository at this point in the history
* add description to patterns

* add md files

* update category

* update description

* update parameters description

* add multiple tests

* update test

* update

* update

* update

* update

* update docker image

* try another approach

* update test

* update

* final
  • Loading branch information
stefanvacareanu7 authored Jan 31, 2025
1 parent deeeea7 commit 33a37ab
Showing 17 changed files with 334 additions and 20 deletions.
20 changes: 15 additions & 5 deletions .codacyrc
Original file line number Diff line number Diff line change
@@ -20,15 +20,25 @@
"value": 8
}
]
},
{
"patternId": "ccn-minor",
"parameters": [
{
"name": "threshold",
"value": 0
}
]
}
]
}
],
"files": [
"/src/dist/src/configCreator.js",
"/src/dist/src/engineImpl.js",
"/src/dist/src/lizard.js",
"/src/dist/src/lizardIssuesEngine.js",
"/src/dist/src/logging.js"
"/dist/src/configCreator.js",
"/dist/src/engineImpl.js",
"/dist/src/lizard.js",
"/dist/src/lizardIssuesEngine.js",
"/dist/src/lizardMetricsEngine.js",
"/dist/src/logging.js"
]
}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:lts-alpine3.20 as builder
FROM node:lts-alpine3.21 as builder

COPY package.json package-lock.json ./
COPY src src
@@ -9,7 +9,7 @@ COPY tsconfig.json ./
RUN npm install &&\
npm run build

FROM python:3.9-alpine3.20
FROM python:3.9-alpine3.21

RUN pip install lizard &&\
apk add --no-cache nodejs &&\
3 changes: 3 additions & 0 deletions docs/description/ccn-critical.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Critical Cyclomatic Complexity control

Check the Cyclomatic Complexity value of a function or logic block. If the threshold is not met, raise a Critical issue. The default threshold is 10.
3 changes: 3 additions & 0 deletions docs/description/ccn-medium.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Medium Cyclomatic Complexity control

Check the Cyclomatic Complexity value of a function or logic block. If the threshold is not met, raise a Medium issue. The default threshold is 7.
3 changes: 3 additions & 0 deletions docs/description/ccn-minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Minor Cyclomatic Complexity control

Check the Cyclomatic Complexity value of a function or logic block. If the threshold is not met, raise a Minor issue. The default threshold is 4.
110 changes: 110 additions & 0 deletions docs/description/description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
[
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "nloc-minor",
"title": "Minor NLOC control - Number of Lines of Code (without comments)",
"description": "Check the number of lines of code (without comments) in a function. If the threshold is not met, raise a Minor issue. The default threshold is 20.",
"timeToFix": 5
},
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "nloc-medium",
"title": "Medium NLOC control - Number of Lines of Code (without comments)",
"description": "Check the number of lines of code (without comments) in a function. If the threshold is not met, raise a Medium issue. The default threshold is 50.",
"timeToFix": 5
},
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "nloc-critical",
"title": "Critical NLOC control - Number of Lines of Code (without comments)",
"description": "Check the number of lines of code (without comments) in a function or logic block. If the threshold is not met, raise a Critical issue. The default threshold is 100.",
"timeToFix": 5
},
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "ccn-minor",
"title": "Minor Cyclomatic Complexity control",
"description": "Check the Cyclomatic Complexity value of a function or logic block. If the threshold is not met, raise a Minor issue. The default threshold is 4.",
"timeToFix": 5
},
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "ccn-medium",
"title": "Medium Cyclomatic Complexity control",
"description": "Check the Cyclomatic Complexity value of a function or logic block. If the threshold is not met, raise a Medium issue. The default threshold is 7.",
"timeToFix": 5
},
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "ccn-critical",
"title": "Critical Cyclomatic Complexity control",
"description": "Check the Cyclomatic Complexity value of a function or logic block. If the threshold is not met, raise a Critical issue. The default threshold is 10.",
"timeToFix": 5
},
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "parameter-count-minor",
"title": "Minor Parameter count control",
"description": "Check the number of parameters sent to a function. If the threshold is not met, raise a Minor issue. The default threshold is 3.",
"timeToFix": 5
},
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "parameter-count-medium",
"title": "Medium Parameter count control",
"description": "Check the number of parameters sent to a function. If the threshold is not met, raise a Medium issue. The default threshold is 5.",
"timeToFix": 5
},
{
"parameters": [
{
"name": "threshold",
"description": "The minimum value upon the issue is being triggered."
}
],
"patternId": "parameter-count-critical",
"title": "Critical Parameter count control",
"description": "Check the number of parameters sent to a function. If the threshold is not met, raise a Critical issue. The default threshold is 9.",
"timeToFix": 5
}
]
3 changes: 3 additions & 0 deletions docs/description/nloc-critical.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Critical NLOC control - Number of Lines of Code (without comments)

Check the number of lines of code (without comments) in a function or logic block. If the threshold is not met, raise a Critical issue. The default threshold is 100.
3 changes: 3 additions & 0 deletions docs/description/nloc-medium.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Medium NLOC control - Number of Lines of Code (without comments)

Check the number of lines of code (without comments) in a function. If the threshold is not met, raise a Medium issue. The default threshold is 50.
3 changes: 3 additions & 0 deletions docs/description/nloc-minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Minor NLOC control - Number of Lines of Code (without comments)

Check the number of lines of code (without comments) in a function. If the threshold is not met, raise a Minor issue. The default threshold is 20.
3 changes: 3 additions & 0 deletions docs/description/parameter-count-critical.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Critical Parameter count control

Check the number of parameters sent to a function. If the threshold is not met, raise a Critical issue. The default threshold is 9.
3 changes: 3 additions & 0 deletions docs/description/parameter-count-medium.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Medium Parameter count control

Check the number of parameters sent to a function. If the threshold is not met, raise a Medium issue. The default threshold is 5.
3 changes: 3 additions & 0 deletions docs/description/parameter-count-minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Minor Parameter count control

Check the number of parameters sent to a function. If the threshold is not met, raise a Minor issue. The default threshold is 3.
29 changes: 29 additions & 0 deletions docs/multiple-tests/all-patterns/patterns.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<module name="root">
<module name="ccn-minor">
<property name="threshold" value="1" />
</module>
<module name="ccn-medium">
<property name="threshold" value="7" />
</module>
<module name="ccn-critical">
<property name="threshold" value="10" />
</module>
<module name="nloc-minor">
<property name="threshold" value="20" />
</module>
<module name="nloc-medium">
<property name="threshold" value="50" />
</module>
<module name="nloc-critical">
<property name="threshold" value="100" />
</module>
<module name="parameter-count-minor">
<property name="threshold" value="3" />
</module>
<module name="parameter-count-medium">
<property name="threshold" value="5" />
</module>
<module name="parameter-count-critical">
<property name="threshold" value="9" />
</module>
</module>
6 changes: 6 additions & 0 deletions docs/multiple-tests/all-patterns/results.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<checkstyle version="4.3">
<file name="all-patterns.ts">
<error source="ccn-minor" line="42" message="Method (anonymous) has a cyclomatic complexity of 3 (limit is 1)" severity="info" />
</file>
</checkstyle>
132 changes: 132 additions & 0 deletions docs/multiple-tests/all-patterns/src/all-patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { exec } from "child_process"
import fs from "fs"

import { LizardOptions } from "./configCreator"

export interface LizardMethodResult {
"name": string;
"fromLine": number;
"toLine": number;
"file": string;
"nloc": number;
"ccn": number;
"params": number;
"tokens": number;
}

export interface LizardFileResult {
"file": string;
"nloc": number;
"maxCcn": number;
"averageNloc": number;
"averageCcn": number;
"averageTokens": number;
"methodsCount": number;
}

export interface LizardResults {
"methods": LizardMethodResult[];
"files": LizardFileResult[];
}

export const runLizardCommand = (
options: LizardOptions
): Promise<LizardResults> => {
// create a file with the list of files to analyze
const filesList = options.files.join("\n")
const filesListPath = "/codacy/filesList.txt"
fs.writeFileSync(filesListPath, filesList)

// run lizard command
return new Promise((resolve, reject) => {
exec(`lizard -f ${filesListPath}`, (error, stdout, stderr) => {
if (error) {
reject(error)
}
if (stderr) {
reject(stderr)
}

resolve(parseLizardResults(stdout))
})
})
}

const parseLizardResults = (output: string): LizardResults => {
// NOTE: lizard supports generating the output in XML or CSV format, but both results lack some information
// (files are not listed for the CSV output, and only starting line is included for the XML output); so the best
// option is to parse the plain text output

// parse the output in an array of lines
const lines = output.split("\n")

const results: LizardResults = {
"methods": [],
"files": []
}

let isMethodSection = false
let isFileSection = false

lines.forEach(line => {
line = line.trim()

if (line.startsWith("===")) {
isMethodSection = false
isFileSection = false
}
if (line.startsWith("===") || line.startsWith("---") || line === "" || line.includes("file analyzed")) return

if (line.includes("NLOC CCN token PARAM length location")) {
isMethodSection = true
return
}

if (line.includes("NLOC Avg.NLOC AvgCCN Avg.token function_cnt file")) {
isFileSection = true
return
}

if (isMethodSection) {
const lineSplitted = line.replaceAll(/\s+|@/g, " ")
.trim()
.split(" ")
if (lineSplitted.length != 8) return
const [nloc, ccn, tokens, params, , name, fromToLine, file] = lineSplitted
const [fromLine, toLine] = fromToLine.split("-")

results.methods.push({
"name": name,
"fromLine": parseInt(fromLine),
"toLine": parseInt(toLine),
"file": file,
"nloc": parseInt(nloc),
"ccn": parseInt(ccn),
"params": parseInt(params),
"tokens": parseInt(tokens)
})
}

if (isFileSection) {
const lineSplitted = line.replaceAll(/\s+/g, " ")
.trim()
.split(" ")
if (lineSplitted.length != 6) return
const [nloc, avgNloc, avgCcn, avgTokens, methodsCount, file] = lineSplitted

results.files.push({
file,
"nloc": parseInt(nloc),
"maxCcn": results.methods
.filter((m) => m.file === file)
.reduce((max, m) => Math.max(m.ccn, max), 0),
"averageNloc": parseFloat(avgNloc),
"averageCcn": parseFloat(avgCcn),
"averageTokens": parseFloat(avgTokens),
"methodsCount": parseInt(methodsCount)
})
}
})

return results
}
Loading
Oops, something went wrong.

0 comments on commit 33a37ab

Please sign in to comment.