Skip to content

Commit

Permalink
Merge pull request #26 from Senior-Capstone-2024/switchCase
Browse files Browse the repository at this point in the history
creates a switch statement code smell detection. 1.0
  • Loading branch information
willeand authored Jun 4, 2024
2 parents e009fe0 + 4d134ec commit f798a59
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion output/data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"12":["Code Smell: Consider refactoring to reduce method length"],"30":["Code smell found: Long Parameters in method public static int addNumbers (int a, int b, int c, int d){"],"38":["Code Smell: Consider refactoring to reduce method length"]}
{"69":["Similar logic found in switch cases based on character usage","Similar logic found in switch cases based on character usage","Similar logic found in switch cases based on character usage"],"92":["Similar logic found in switch cases based on character usage","Similar logic found in switch cases based on character usage"]}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
{
"command": "extension.createJSON",
"title": "createJSON"
},
{
"command": "extension.badSwitch",
"title": "badSwitch"
}
],
"menus": {
Expand Down
66 changes: 66 additions & 0 deletions src/badSwitch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as vscode from 'vscode';

export function badSwitch(myMap: Map<number, string[]>) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No active text editor.');
return myMap; // Early return if no editor
}

let text = editor.document.getText();
// Regex to match Java switch statements
let switchRegex = /switch\s*\(([^)]+)\)\s*{([^}]*)}/g;
let match;
while ((match = switchRegex.exec(text))) {
let switchCases = match[2].split('case').slice(1);
if (switchCases.length >= 3) {
let lineIndex = editor.document.positionAt(match.index).line;
let errorMessage = `Code smell found: Switch statement with ${switchCases.length} cases`;
if (!myMap.has(lineIndex)) {
myMap.set(lineIndex, [errorMessage]);
} else {
myMap.get(lineIndex)?.push(errorMessage);
}

// Initialize character array for each case
let charArrays: number[][] = [];
for (let i = 0; i < switchCases.length; i++) {
let caseCharArray = new Array(26).fill(0);
let caseLines = switchCases[i].split('\n').map(line => line.trim().toLowerCase());
for (let line of caseLines) {
for (let char of line) {
if (char >= 'a' && char <= 'z') {
let index = char.charCodeAt(0) - 'a'.charCodeAt(0);
caseCharArray[index]++;
}
}
}
charArrays.push(caseCharArray);
}

// Compare each case to every other case at least once
for (let i = 0; i < charArrays.length; i++) {
for (let j = i + 1; j < charArrays.length; j++) {
let similarity = calculateSimilarity(charArrays[i], charArrays[j]);
if (similarity >= 0.8) { // Adjust similarity threshold as needed
let similarLogicMessage = `Similar logic found in switch cases based on character usage`;
if (!myMap.get(lineIndex)?.includes(similarLogicMessage)) {
myMap.get(lineIndex)?.push(similarLogicMessage);
}
break; // Exit inner loop if similar logic found
}
}
}
}
}

return myMap;
}

function calculateSimilarity(arr1: number[], arr2: number[]): number {
let similarity = 0;
for (let i = 0; i < 26; i++) {
similarity += Math.min(arr1[i], arr2[i]);
}
return similarity / arr1.reduce((a, b) => a + b, 0); // Normalize by total characters in the first case
}
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { findLowercaseEnums } from './findLowercaseEnums';
import { findConstantCap} from './findConstantCap';
import { longMethod } from './longMethod';
import { longParameters } from './longParameters';
import { badSwitch } from './badSwitch';

const myMap: Map<number, string[]> = new Map();
const filePath = 'C:\\Users\\Will\\Desktop\\cw\\gradeFast-1.0\\output\\data.json';
Expand Down Expand Up @@ -58,6 +59,10 @@ export function activate(context: vscode.ExtensionContext) {
longParameters(myMap);
} );

const disposable9 = vscode.commands.registerCommand('extension.badSwitch', () => {
badSwitch(myMap);
} );

const commentController = vscode.comments.createCommentController('comment-sample', 'Comment API Sample');
context.subscriptions.push(commentController);

Expand Down

0 comments on commit f798a59

Please sign in to comment.