-
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 #2 from surenpoghosian/antipatterns
[antipatterns] add support for more antipatterns
- Loading branch information
Showing
19 changed files
with
376 additions
and
52 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,24 @@ | ||
import { BasePattern } from './BasePattern'; | ||
import { Hint } from '../Reports/Hint'; | ||
|
||
export class LargeClassPattern extends BasePattern { | ||
analyze(content: string): Hint[] { | ||
const hints: Hint[] = []; | ||
const classRegex = /class\s+\w+\s*\{[^]*?\}/g; | ||
const methodRegex = /(?:public|private|protected|static)?\s*(?:async\s+)?(?:function)?\s*\w+\(.*?\)\s*\{[^]*?\}/g; | ||
const propertyRegex = /(?:public|private|protected|static)?\s*\w+\s*:\s*\w+\s*;/g; | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = classRegex.exec(content)) !== null) { | ||
const classBody = match[0]; | ||
const methodCount = (classBody.match(methodRegex) || []).length; | ||
const propertyCount = (classBody.match(propertyRegex) || []).length; | ||
|
||
if (methodCount > 15 || propertyCount > 15) { // Threshold can be adjusted | ||
hints.push(new Hint(`Possible Large Class detected: This class has ${methodCount} methods and ${propertyCount} properties. Consider splitting it into smaller classes.`)); | ||
} | ||
} | ||
|
||
return hints; | ||
} | ||
} |
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,21 @@ | ||
import { BasePattern } from './BasePattern'; | ||
import { Hint } from '../Reports/Hint'; | ||
|
||
export class LazyClassPattern extends BasePattern { | ||
analyze(content: string): Hint[] { | ||
const hints: Hint[] = []; | ||
const classRegex = /class\s+\w+\s*\{[^]*?\}/g; | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = classRegex.exec(content)) !== null) { | ||
const classBody = match[0]; | ||
const methodCount = (classBody.match(/(?:public|private|protected|static)?\s*(?:async\s+)?(?:function)?\s*\w+\(.*?\)\s*\{[^]*?\}/g) || []).length; | ||
|
||
if (methodCount <= 1) { | ||
hints.push(new Hint(`Possible Lazy Class detected: This class has only ${methodCount} method(s). Consider merging it with another class.`)); | ||
} | ||
} | ||
|
||
return hints; | ||
} | ||
} |
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,19 @@ | ||
import { BasePattern } from './BasePattern'; | ||
import { Hint } from '../Reports/Hint'; | ||
|
||
export class LongParameterListPattern extends BasePattern { | ||
analyze(content: string): Hint[] { | ||
const hints: Hint[] = []; | ||
const methodRegex = /\w+\(([^)]*)\)\s*\{/g; | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = methodRegex.exec(content)) !== null) { | ||
const params = match[1].split(',').map(param => param.trim()); | ||
if (params.length > 5) { // Threshold can be adjusted | ||
hints.push(new Hint(`Possible Long Parameter List detected: Method with ${params.length} parameters. Consider refactoring.`)); | ||
} | ||
} | ||
|
||
return hints; | ||
} | ||
} |
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,16 @@ | ||
import { BasePattern } from './BasePattern'; | ||
import { Hint } from '../Reports/Hint'; | ||
|
||
export class MiddleManPattern extends BasePattern { | ||
analyze(content: string): Hint[] { | ||
const hints: Hint[] = []; | ||
const methodDelegationRegex = /\w+\(\s*\)\s*{\s*return\s+\w+\.\w+\(\);?\s*}/g; | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = methodDelegationRegex.exec(content)) !== null) { | ||
hints.push(new Hint(`Possible Middle Man detected: This method simply delegates its work to another method. Consider removing it.`)); | ||
} | ||
|
||
return hints; | ||
} | ||
} |
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,24 @@ | ||
import { BasePattern } from './BasePattern'; | ||
import { Hint } from '../Reports/Hint'; | ||
|
||
export class PrimitiveObsessionPattern extends BasePattern { | ||
analyze(content: string): Hint[] { | ||
const hints: Hint[] = []; | ||
const primitiveRegex = /\b(string|number|boolean)\b/g; | ||
const occurrences = new Map<string, number>(); | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = primitiveRegex.exec(content)) !== null) { | ||
const type = match[0]; | ||
occurrences.set(type, (occurrences.get(type) || 0) + 1); | ||
} | ||
|
||
occurrences.forEach((count, type) => { | ||
if (count > 10) { // Threshold can be adjusted | ||
hints.push(new Hint(`Possible Primitive Obsession detected: "${type}" is used ${count} times. Consider encapsulating in a custom type.`)); | ||
} | ||
}); | ||
|
||
return hints; | ||
} | ||
} |
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,19 @@ | ||
import { BasePattern } from './BasePattern'; | ||
import { Hint } from '../Reports/Hint'; | ||
|
||
export class SpeculativeGeneralityPattern extends BasePattern { | ||
analyze(content: string): Hint[] { | ||
const hints: Hint[] = []; | ||
const interfaceRegex = /interface\s+\w+\s*\{[^]*?\}/g; | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = interfaceRegex.exec(content)) !== null) { | ||
const interfaceBody = match[0]; | ||
if (interfaceBody.includes('<T>') && !interfaceBody.includes('T used')) { // Simplified check for generic misuse | ||
hints.push(new Hint(`Possible Speculative Generality detected: Interface uses generics without clear necessity.`)); | ||
} | ||
} | ||
|
||
return hints; | ||
} | ||
} |
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,19 @@ | ||
import { BasePattern } from './BasePattern'; | ||
import { Hint } from '../Reports/Hint'; | ||
|
||
export class SwitchStatementOverusePattern extends BasePattern { | ||
analyze(content: string): Hint[] { | ||
const hints: Hint[] = []; | ||
const switchRegex = /switch\s*\(.*?\)\s*\{[^]*?\}/g; | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = switchRegex.exec(content)) !== null) { | ||
const caseCount = (match[0].match(/case\s+/g) || []).length; | ||
if (caseCount > 5) { // Threshold can be adjusted | ||
hints.push(new Hint(`Possible Switch Statement Overuse detected: This switch statement has ${caseCount} cases. Consider refactoring.`)); | ||
} | ||
} | ||
|
||
return hints; | ||
} | ||
} |
Oops, something went wrong.