-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Senior-Capstone-2024/longMethod
Code Smell Long-Method Completed version 1.0
- Loading branch information
Showing
4 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"3":["Improper Capitalized Primitive Type"],"4":["Improper Capitalized Primitive Type"],"7":["Lowercase ENUM found!"],"18":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"19":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"20":["Improper Capitalized Primitive Type"],"35":["Improper Capitalized Primitive Type"],"179":["enum fruit"]} | ||
{"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"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
export function longMethod(myMap: Map<number, string[]>): Map<number, string[]> { | ||
// Check for active text editor | ||
const editor = vscode.window.activeTextEditor; | ||
if (!editor) { | ||
vscode.window.showErrorMessage('No active text editor.'); | ||
return myMap; // Early return if no editor | ||
} | ||
|
||
// Get the document text | ||
const document = editor.document; | ||
const text = document.getText(); | ||
|
||
// Pattern to identify method definitions | ||
const methodPattern = /(\b(?:public|protected|private|static|\s)+[^\s]+\s+[^\s]+\s*\([^)]*\)\s*{)/g; | ||
|
||
let match; | ||
while ((match = methodPattern.exec(text)) !== null) { | ||
const startPos = document.positionAt(match.index); | ||
const startLine = startPos.line; | ||
|
||
// Count the number of lines in the method | ||
const methodLines = countMethodLines(document, startLine); | ||
|
||
if (methodLines > 10) { | ||
const errorMessage = "Code Smell: Consider refactoring to reduce method length"; | ||
if (!myMap.has(startLine)) { | ||
myMap.set(startLine, []); | ||
} | ||
const currentErrors = myMap.get(startLine) || []; | ||
currentErrors.push(errorMessage); | ||
myMap.set(startLine, currentErrors); | ||
} | ||
} | ||
|
||
return myMap; | ||
} | ||
|
||
// Helper function to count method lines | ||
function countMethodLines(document: vscode.TextDocument, startLine: number): number { | ||
let lineNumber = startLine; | ||
let methodOpen = true; | ||
let openBraces = 0; | ||
let closeBraces = 0; | ||
|
||
while (lineNumber < document.lineCount && methodOpen) { | ||
const lineText = document.lineAt(lineNumber).text.trim(); | ||
openBraces += (lineText.match(/{/g) || []).length; | ||
closeBraces += (lineText.match(/}/g) || []).length; | ||
|
||
if (closeBraces > 0 && openBraces === closeBraces) { | ||
methodOpen = false; | ||
} | ||
|
||
lineNumber++; | ||
} | ||
|
||
return lineNumber - startLine; | ||
} |