Skip to content

Commit

Permalink
feat: java practice for naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzzaki committed Jan 27, 2020
1 parent 3739f52 commit b63c767
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/practices/Java/JavaNamingConventionsPractice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IPractice } from '../IPractice';
import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '../../model';
import { DxPractice } from '../DxPracticeDecorator';
import { PracticeContext } from '../../contexts/practice/PracticeContext';

@DxPractice({
id: 'Java.NamingConventions',
name: 'Use Java Naming Conventions',
impact: PracticeImpact.high,
suggestion: 'Java class names should begin capitalized as UpperCamelCase or PascalCase in a regular naming convention.',
reportOnlyOnce: true,
url: 'https://www.oracle.com/technetwork/java/codeconventions-135099.html',
})
export class JavaNamingConventionsPractice implements IPractice {
async isApplicable(ctx: PracticeContext): Promise<boolean> {
return ctx.projectComponent.language === ProgrammingLanguage.Java;
}

async evaluate(ctx: PracticeContext): Promise<PracticeEvaluationResult> {
if (!ctx.fileInspector) {
return PracticeEvaluationResult.unknown;
}

const regexDotJava = new RegExp('.java', 'i');
const javaFiles = await ctx.fileInspector.scanFor(regexDotJava, '/', { shallow: false });
javaFiles.forEach((file) => {
if (file.baseName !== file.baseName.replace(/^\w/, (firstChar) => firstChar.toUpperCase())) {
return PracticeEvaluationResult.notPracticing;
} else {
return PracticeEvaluationResult.practicing;
}
});

return PracticeEvaluationResult.notPracticing;
}
}
2 changes: 2 additions & 0 deletions src/practices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ThinPullRequestsPractice } from './LanguageIndependent/ThinPullRequests
import { SecurityVulnerabilitiesPractice } from './JavaScript/SecurityVulnerabilitiesPractice';
import { FirstTestPractice, SecondTestPractice, InvalidTestPractice } from '../scanner';
import { JavaPackageManagementUsedPractice } from './Java/JavaPackageManagementUsed';
import { JavaNamingConventionsPractice } from './Java/JavaNamingConventionsPractice';

// register practices here
export const practices = [
Expand Down Expand Up @@ -66,4 +67,5 @@ export const practices = [
ThinPullRequestsPractice,
SecurityVulnerabilitiesPractice,
JavaPackageManagementUsedPractice,
JavaNamingConventionsPractice,
];

0 comments on commit b63c767

Please sign in to comment.