Skip to content

Commit

Permalink
Merge pull request #24 from Senior-Capstone-2024/longParameters
Browse files Browse the repository at this point in the history
Code Smell long Parameters Completed Version 1.0
  • Loading branch information
willeand authored Jun 4, 2024
2 parents 5790ecc + 3aa174a commit f3c83dc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion output/data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"3":["Improper Capitalized Primitive Type"],"4":["Improper Capitalized Primitive Type"],"12":["Code Smell: Consider refactoring to reduce method length"],"15":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"16":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"17":["Improper Capitalized Primitive Type"],"31":["Code Smell: Consider refactoring to reduce method length"],"50":["Improper Capitalized Primitive Type"]}
{"3":["Improper Capitalized Primitive Type"],"4":["Improper Capitalized Primitive Type"],"12":["Code Smell: Consider refactoring to reduce method length"],"15":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"16":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"17":["Improper Capitalized Primitive Type"],"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"],"57":["Improper Capitalized Primitive Type"]}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
{
"command": "extension.longMethod",
"title": "findLongMethod"
},
{
"command": "extension.longParameters",
"title": "findLongParameters"
}
],
"menus": {
Expand Down
13 changes: 9 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { findDuplicateCode } from './findDuplicateCode';
import { findLowercaseEnums } from './findLowercaseEnums';
import { findConstantCap} from './findConstantCap';
import { longMethod } from './longMethod';
import { longParameters } from './longParameters';

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

const disposable6 = vscode.commands.registerCommand('extension.findConstantCap', findConstantCap);

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

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

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

Expand All @@ -55,9 +64,5 @@ export function activate(context: vscode.ExtensionContext) {
}
};

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

NoteCommentController.registerCommands(context);
}
29 changes: 29 additions & 0 deletions src/longParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as vscode from 'vscode';

export function longParameters(myMap: Map<number, string[]>): 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();
// Updated regex to match Java method declarations with 3 or more parameters and exclude main method and methods with 1 or 2 parameters
let methodRegex = /(\b(?:public|private|protected|static|\s)+[\w<>]+\s+(\w+)\s*\(([^)]+)\)\s*{)/g;
let match;
while ((match = methodRegex.exec(text))) {
let methodName = match[1];
let parameters = match[3].split(',').map(param => param.trim());
if (methodName !== 'main' && parameters.length >= 3) {
let lineIndex = editor.document.positionAt(match.index).line;
let errorMessage = `Code smell found: Long Parameters in method ${methodName}`;
if (!myMap.has(lineIndex)) {
myMap.set(lineIndex, [errorMessage]);
} else {
myMap.get(lineIndex)?.push(errorMessage);
}
}
}

return myMap;
}

0 comments on commit f3c83dc

Please sign in to comment.