Skip to content

Commit

Permalink
Merge pull request #89 from pbredenberg/PB/feat-snake-case-conventions
Browse files Browse the repository at this point in the history
chore: add naming convention rules and test cases
  • Loading branch information
onebytegone committed Jun 9, 2023
2 parents e57eddb + 0629485 commit 39387fe
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
59 changes: 53 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,64 @@ module.exports = {
'no-empty-function': [ 'error', { 'allow': [ 'constructors' ] } ],

'@typescript-eslint/adjacent-overload-signatures': 'error',

// Disable ESLint's camelcase so we can override with our own
// naming convention rules.
'camelcase': 'off',

'@typescript-eslint/naming-convention': [
'error',
{
'selector': [ 'classProperty', 'classMethod' ],
'modifiers': [ 'private', 'protected' ],
'leadingUnderscore': 'require',
'format': [ 'camelCase' ],
selector: 'classProperty',
modifiers: [ 'private' ],
format: [ 'camelCase' ],
leadingUnderscore: 'require',
},
{
selector: 'classProperty',
modifiers: [ 'protected' ],
format: [ 'camelCase' ],
leadingUnderscore: 'require',
},
{
selector: 'classProperty',
modifiers: [ 'private', 'static' ],
format: [ 'snake_case' ],
leadingUnderscore: 'require',
},
{
selector: 'classProperty',
modifiers: [ 'protected', 'static' ],
format: [ 'snake_case' ],
leadingUnderscore: 'require',
},
{
selector: 'classProperty',
modifiers: [ 'public', 'static' ],
format: [ 'snake_case' ],
leadingUnderscore: 'forbid',
},
{
selector: 'enum',
format: [ 'PascalCase' ],
},
{
selector: 'typeLike',
format: [ 'PascalCase' ],
},
{
selector: 'variable',
format: [ 'camelCase' ],
},
{
selector: 'parameter',
format: [ 'camelCase' ],
leadingUnderscore: 'allow',
},
{
'selector': [ 'class', 'interface' ],
'format': [ 'PascalCase' ],
selector: 'variable',
modifiers: [ 'global' ],
format: [ 'UPPER_CASE', 'camelCase', 'PascalCase' ],
},
],
// no-shadow is incompatible with TypeScript code.
Expand Down
13 changes: 13 additions & 0 deletions test-cases/javascript/TestClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

class TestClass {
constructor(age) {
this.age = age;
}

getAge() {
return this.age;
}
}

module.exports = TestClass;
56 changes: 56 additions & 0 deletions test-cases/typescript/TestClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Test class that adheres to our ESLint rules for TypeScript
* @param test
*/
export class TestClass {

protected static _banana_sweetness: string;

private static _apple_type: string;

private readonly _appleColor: string;

public constructor(appleColor: string, appleType: string, bananaSweetness: string) {
this._appleColor = appleColor;
TestClass._apple_type = appleType;
TestClass._banana_sweetness = bananaSweetness;
}

public static get_apple_type(): string {
return TestClass._apple_type;
}

public getOrange(): string {
return this._headersContentTypeAsString();
}

public getApple(): string {
return TestClass.get_apple_type();
}

protected _getBananaSweetness(): string {
return TestClass._banana_sweetness;
}

protected get _getHeaders(): Record<string, unknown> {
return {
// This demonstrates how our rules allow for non-standard
// object-literal property names, which are sometimes required
// when working with 3rd party APIs that do not account for our
// naming conventions.
'Content-Type': 'application/json',
'DC': 'test',
TA: 'test',
helloThere: 'test',
HelloThere: 'test',
MY_PROPERTY_NAME: 'test',
'127.0.0.1': true,
'aws:rep:deleting': true,
'hello[a]': 'hello',
};
}

private _headersContentTypeAsString(): string {
return `${this._getHeaders['Content-Type']}`;
}
}
12 changes: 12 additions & 0 deletions test-cases/typescript/test-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const GLOBALLY_DEFINED = 'test';

/**
* Test function that adheres to our ESLint rules for TypeScript
* @param argument
*/
export default function testFunction(argument: string): string {
const locallyDefinedConst = 'test',
returnValue = argument + locallyDefinedConst;

return returnValue + GLOBALLY_DEFINED;
}

0 comments on commit 39387fe

Please sign in to comment.