Skip to content

Commit 37ace7b

Browse files
authored
chore: improve linting with eslint (#6)
* chore: improve linting with eslint * Test commit with hooks disabled * Test with simplified hook * Test with lint-staged in hook * add precommit hook * Update lint-staged script to add fixed files back to staging * Test lint-staged with a file containing linting issues * Update lint-staged script to use nx affected:lint * Test nx affected:lint with a new file * Add custom lint-staged script
1 parent 9aeb55e commit 37ace7b

22 files changed

+879
-351
lines changed

.eslintignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
dist
3+
.nx
4+
coverage

.eslintrc.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"root": true,
3+
"ignorePatterns": ["**/*"],
4+
"plugins": ["@nx"],
5+
"overrides": [
6+
{
7+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
8+
"rules": {
9+
"@nx/enforce-module-boundaries": [
10+
"error",
11+
{
12+
"enforceBuildableLibDependency": true,
13+
"allow": [],
14+
"depConstraints": [
15+
{
16+
"sourceTag": "*",
17+
"onlyDependOnLibsWithTags": ["*"]
18+
}
19+
]
20+
}
21+
],
22+
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
23+
"semi": ["error", "never"]
24+
}
25+
},
26+
{
27+
"files": ["*.ts", "*.tsx"],
28+
"extends": ["plugin:@nx/typescript"],
29+
"rules": {}
30+
},
31+
{
32+
"files": ["*.js", "*.jsx"],
33+
"extends": ["plugin:@nx/javascript"],
34+
"rules": {}
35+
}
36+
]
37+
}

.husky/pre-commit

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
# Simple pre-commit hook to run lint-staged
3+
4+
# Exit on error
5+
set -e
6+
7+
echo "Running pre-commit hook: linting staged files..."
8+
npx --no-install lint-staged

.vscode/extensions.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"recommendations": [
3-
43
"nrwl.angular-console",
5-
"esbenp.prettier-vscode"
4+
"esbenp.prettier-vscode",
5+
"dbaeumer.vscode-eslint"
66
]
77
}

.vscode/settings.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"editor.codeActionsOnSave": {
3+
"source.fixAll.eslint": true
4+
},
5+
"editor.formatOnSave": true,
6+
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
7+
"eslint.validate": [
8+
"javascript",
9+
"javascriptreact",
10+
"typescript",
11+
"typescriptreact",
12+
"html"
13+
],
14+
"eslint.format.enable": true,
15+
"eslint.nodePath": "node_modules",
16+
"[typescript]": {
17+
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
18+
},
19+
"[markdown]": {
20+
"editor.defaultFormatter": "esbenp.prettier-vscode"
21+
}
22+
}

docs/ARCHITECTURE.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,24 @@ The application will be deployed to Google Cloud Platform:
173173
- Follow Angular style guide
174174
- Use TypeScript for type safety
175175
- Write unit tests for components and services
176+
- Use single quotes for strings in TypeScript and JavaScript files
177+
- Code style is enforced by ESLint
176178

177-
2. **Git Workflow**:
179+
2. **Code Style Enforcement**:
180+
181+
- ESLint is configured to enforce single quotes for strings
182+
- The configuration is defined in the project's `.eslintrc.json` files
183+
- VS Code is set up to automatically fix quote style on save
184+
- Linting can be run manually with `npx nx lint webapp` or `npx nx lint webapp --fix`
185+
- The lint target is defined in the project.json file for proper NX integration
186+
187+
3. **Git Workflow**:
178188

179189
- Feature branch strategy
180190
- Pull request reviews
181191
- Semantic versioning
182192

183-
3. **Documentation**:
193+
4. **Documentation**:
184194
- Code documentation with JSDoc
185195
- API documentation
186196
- User guides for complex features

docs/CODE_STYLE.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Code Style Guidelines
2+
3+
This document outlines the code style guidelines for the EczEase project.
4+
5+
## TypeScript/JavaScript Style Guide
6+
7+
### Quotes
8+
9+
- **Single quotes** (`'`) are used for string literals
10+
- Double quotes (`"`) should only be used when necessary to avoid escaping (e.g., `'I\'m a string'` vs `"I'm a string"`)
11+
- **Template literals** (backticks `` ` ``) are preserved and should be used for:
12+
- Multi-line strings
13+
- String interpolation with expressions: `` `Hello ${name}` ``
14+
- HTML templates or complex text formatting
15+
16+
### Semicolons
17+
18+
- **No semicolons** at the end of statements
19+
- Relying on JavaScript's Automatic Semicolon Insertion (ASI)
20+
- Be careful with:
21+
- Lines starting with `(`, `[`, or `/` - consider prefixing with a semicolon if needed
22+
- Immediately Invoked Function Expressions (IIFEs)
23+
24+
## Angular-Specific Guidelines
25+
26+
### Dependency Injection
27+
28+
- Always use the `inject()` function instead of constructor injection
29+
- Declare dependencies as `private readonly` class properties:
30+
31+
```typescript
32+
export class ExampleComponent {
33+
private readonly http = inject(HttpClient);
34+
private readonly userService = inject(UserService);
35+
36+
// Rest of the component...
37+
}
38+
```
39+
40+
### Signals and State Management
41+
42+
- Use Angular's signals for reactive state management
43+
- Declare read-only signals with `signal<T>()` and models with `model<T>()`
44+
- Access signal values directly with parentheses: `this.mySignal()`
45+
46+
## Running Linting
47+
48+
To check for code style issues or fix them automatically:
49+
50+
```bash
51+
# Check for linting issues
52+
npx nx lint webapp
53+
54+
# Fix linting issues automatically
55+
npx nx lint webapp --fix
56+
```
57+
58+
### Automatic Linting with Git Hooks
59+
60+
This project uses Git hooks to automatically run linting before each commit:
61+
62+
1. When you commit changes, a pre-commit hook will run
63+
2. The hook will run `eslint --fix` on all staged files with .ts, .js, and .html extensions
64+
3. If linting fails or cannot automatically fix issues, the commit will be blocked
65+
4. Fix the issues manually and try committing again
66+
67+
To temporarily bypass the pre-commit hook (not recommended), use:
68+
69+
```bash
70+
git commit -m "Your message" --no-verify
71+
```
72+
73+
## Best Practices
74+
75+
1. **Always check linting before committing code**
76+
2. **Configure your editor to format on save** to avoid manual style fixes
77+
3. **Don't disable style rules** without team discussion

lint-staged.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
3+
// This script runs 'nx affected:lint --fix' without passing file paths
4+
// to lint only the affected projects instead of the entire webapp
5+
6+
const { execSync } = require("child_process");
7+
8+
try {
9+
// Run the NX affected lint command without passing specific files
10+
console.log("Running nx affected:lint --fix...");
11+
execSync("npx nx affected:lint --fix", { stdio: "inherit" });
12+
13+
// Add any files modified by linting back to the staging area
14+
console.log("Adding fixed files back to staging area...");
15+
execSync("git add .", { stdio: "inherit" });
16+
17+
process.exit(0);
18+
} catch (error) {
19+
console.error("Error running nx affected:lint:", error.message);
20+
process.exit(1);
21+
}

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
"scripts": {
66
"firebase:deploy:firestore": "firebase deploy --only firestore",
77
"firebase:deploy:rules": "firebase deploy --only firestore:rules",
8-
"firebase:deploy:webapp": "firebase deploy --only hosting:webapp --project=eczease-prod"
8+
"firebase:deploy:webapp": "firebase deploy --only hosting:webapp --project=eczease-prod",
9+
"lint-staged": "lint-staged"
910
},
1011
"private": true,
12+
"lint-staged": {
13+
"*.{ts,js,html}": "node lint-staged.js"
14+
},
1115
"dependencies": {
1216
"@analogjs/content": "^1.14.1",
1317
"@analogjs/router": "^1.14.1",
@@ -38,14 +42,22 @@
3842
"@analogjs/vite-plugin-angular": "^1.14.1",
3943
"@analogjs/vitest-angular": "^1.14.1",
4044
"@angular-devkit/build-angular": "^19.0.0",
45+
"@angular-eslint/eslint-plugin": "^19.2.1",
46+
"@angular-eslint/eslint-plugin-template": "^19.2.1",
47+
"@angular-eslint/template-parser": "^19.2.1",
4148
"@angular/compiler-cli": "^19.0.0",
4249
"@nx/angular": "^20.0.0",
4350
"@nx/devkit": "^20.0.0",
4451
"@nx/eslint": "^20.0.0",
52+
"@nx/eslint-plugin": "^20.6.2",
4553
"@nx/vite": "^20.0.0",
4654
"@nx/workspace": "20.6.2",
55+
"@typescript-eslint/eslint-plugin": "^8.27.0",
4756
"autoprefixer": "^10.4.0",
57+
"eslint": "8.56.0",
58+
"husky": "^9.0.0",
4859
"jsdom": "^22.1.0",
60+
"lint-staged": "^15.0.0",
4961
"nx": "20.6.2",
5062
"postcss": "^8.4.5",
5163
"tailwindcss": "^3.0.2",

0 commit comments

Comments
 (0)