Skip to content

Commit

Permalink
[#1945] Enforce stricter formatting rules for TypeScript files (#1957)
Browse files Browse the repository at this point in the history
Currently, our ESLint ruleset does not enforce certain formatting rules
for TypeScript code, such as delimiters in TypeScript interfaces and
spacing in type annotations.

Let's update the ESLint ruleset to check for proper delimiters and
spacing in type annotations. This will enforce consistency throughout
the codebase and improve code readability.
  • Loading branch information
vvidday authored Mar 25, 2023
1 parent 6d4963b commit 34c2898
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
60 changes: 60 additions & 0 deletions docs/dg/styleGuides.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Our coding standards are mostly based on those at [se-education.org/guides](http
* [**Markdown/MarkBind** coding standard](https://se-education.org/guides/conventions/markdown.html)
* [**Java** coding standard](https://se-education.org/guides/conventions/java/index.html)
* [**JavaScript** coding standard](https://se-education.org/guides/conventions/javascript.html)
* **TypeScript**: In addition to the JavaScript coding standard, follow the [**recommended ESLint rules**](https://typescript-eslint.io/rules/) and the formatting rules [described below](#typescript-specific-formatting).
* **Vue Components**: Follow the [**Vue style guide**](https://vuejs.org/style-guide/), up to the **Recommended** section.
* **Documentation**: Follow the [**Google developer documentation style guide**](https://developers.google.com/style).

Expand Down Expand Up @@ -122,5 +123,64 @@ public LocalDateTime parseDate(String dateString) throws NullPointerException, P
// Code here
}
```
## TypeScript specific formatting

For TypeScript specific code, such as within an `interface` or type annotations, we also stipulate the following standards:
* Use semicolons as delimiters for TypeScript interfaces and types.

Negative Examples:
```typescript
// missing semicolon delimiter
interface Foo {
name: string
greet(): string
}

// using incorrect delimiter
interface Foo {
name: string,
greet(): string,
}
```
Positive Example:
```typescript
// semicolon delimiter
interface Foo {
name: string;
greet(): string;
}
```
* For type annotations, use a space after but not before.

Negative Examples:
```typescript
let foo:string = "bar";
let foo :string = "bar";
let foo : string = "bar";

function foo():string {}
function foo() :string {}
function foo() : string {}

class Foo {
name:string;
}

class Foo {
name :string;
}

class Foo {
name : string;
}
```
Positive Examples:
```typescript
let foo: string = "bar";

function foo(): string {}

class Foo {
name: string;
}
```
4 changes: 3 additions & 1 deletion frontend/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@
],
"SwitchCase": 0
}
]
],
"@typescript-eslint/member-delimiter-style": "error",
"@typescript-eslint/type-annotation-spacing": "error"
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/types/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ interface SortingFunction<T> {
interface Api {
loadJSON: (fname: string) => Promise<unknown>;
loadSummary: () => Promise<{
creationDate: string,
reportGenerationTime: string,
errorMessages: { [key: string]: ErrorMessage },
names: string[],
creationDate: string;
reportGenerationTime: string;
errorMessages: { [key: string]: ErrorMessage };
names: string[];
} | null>;
loadCommits: (repoName: string) => Promise<User[]>;
loadAuthorship: (repoName: string) => Promise<AuthorshipSchema>;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Commit, DailyCommit, User as UserType } from '../types/types';
import { AuthorFileTypeContributions } from '../types/zod/commits-type';

export default class User implements UserType {
checkedFileTypeContribution : number;
checkedFileTypeContribution: number;

commits: Commit[];

Expand Down

0 comments on commit 34c2898

Please sign in to comment.