Skip to content

refactor: formated and linted code, added pre commit hooks #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn commitlint --edit $1
20 changes: 20 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env shAdd commentMore actions
. "$(dirname -- "$0")/_/husky.sh"

echo "[PRE-COMMIT] Running yarn lint..."
if yarn lint; then
echo "[PRE-COMMIT] ✅ Linting passed successfully"
else
echo "[PRE-COMMIT] ❌ Linting failed with exit code $?"
exit 1
fi

echo "[PRE-COMMIT] Running yarn format..."
if yarn format; then
echo "[PRE-COMMIT] ✅ Formatting completed successfully"
else
echo "[PRE-COMMIT] ❌ Formatting failed with exit code $?"
exit 1
fi

echo "[PRE-COMMIT] ✅ All pre-commit checks passed!"
19 changes: 15 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,34 @@ Go to your fork on GitHub and click "Compare & pull request".

## Running Locally

1. **Install dependencies:**
1. **Install Biome globally:**
Make sure you have Biome installed globally for code formatting and linting:
```bash
npm install -g @biomejs/biome
```
or
```bash
yarn global add @biomejs/biome
```
Learn more at: https://biomejs.dev/

2. **Install dependencies:**
```bash
npm install
```
or
```bash
yarn install
```
2. **Add Env**
3. **Add Env**
Create a `.env` file in the root directory with the following variables:
```bash
# Required: Your JigsawStack Public API key
VITE_TRANSLATION_WIDGET_PUBLIC_KEY=your_api_key_here
```
You can get your Public API key from the [JigsawStack Dashboard](https://jigsawstack.com).

3. **Start the development server:**
4. **Start the development server:**
```bash
npm run dev
```
Expand All @@ -77,7 +88,7 @@ Go to your fork on GitHub and click "Compare & pull request".
yarn dev
```

3. **Open your browser and visit:**
5. **Open your browser and visit:**
```
http://localhost:5173
```
Expand Down
12 changes: 6 additions & 6 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"$schema": "https://biomejs.dev/schemas/2.0.4/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"ignoreUnknown": false,
"ignore": []
"includes": ["src/**/*.ts", "!src/**/*.css"]
},
"formatter": {
"enabled": true,
Expand All @@ -20,11 +20,11 @@
"attributePosition": "auto",
"bracketSpacing": true
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": false
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
Expand Down
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default { extends: ['@commitlint/config-conventional'] };
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@
"types": "./dist/index.d.ts",
"scripts": {
"format": "biome format --write ./src",
"lint": "biome lint ./src",
"dev": "vite",
"build": "tsc && vite build"
"build": "tsc && vite build",
"prepare": "husky"
},
"devDependencies": {
"@commitlint/cli": "^19.8.1",
"@commitlint/config-conventional": "^19.8.1",
"@types/node": "^22.13.4",
"husky": "^9.1.7",
"pinst": "^3.0.0",
"terser": "^5.39.0",
"typescript": "~5.7.2",
"vite": "^6.1.0",
Expand Down
1 change: 0 additions & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export const MAX_CACHE_SIZE = 1000;
export const BATCH_SIZE = 10;
export const CACHE_PREFIX = "jss-";
Expand Down
2 changes: 1 addition & 1 deletion src/constants/languages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Language } from "../types";
import type { Language } from "../types";
export const languages: Language[] = [
{
code: "af",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styles from "./translation-widget.css?inline";
import { TranslationWidget } from "./widget";
import type { TranslationConfig } from "./types";
import type { TranslationConfig } from "./types";

declare global {
interface Window {
Expand Down Expand Up @@ -37,4 +37,4 @@ const initializeTranslationWidget = (publicKey: string, config?: TranslationConf
}
};

export default initializeTranslationWidget;
export default initializeTranslationWidget;
18 changes: 14 additions & 4 deletions src/lib/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export type TranslatableContent = {
isNested: boolean;
}[];
export class DocumentNavigator {
// Dummy instance method to avoid linter warning about static-only classesAdd commentMore actions
public init() {}

/**
* Retrieves text nodes eligible for translation from the document
* @returns Collection of text nodes ready for translation
Expand Down Expand Up @@ -39,15 +42,22 @@ export class DocumentNavigator {
const navigator = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, validator);
const groupedText = new Map<HTMLElement, Text[]>();

let currentNode: Node | null;
while ((currentNode = navigator.nextNode())) {
let currentNode: Node | null = navigator.nextNode();
while (currentNode) {
const parentElement = (currentNode as Text).parentElement;
if (!parentElement) continue;
if (!parentElement) {
currentNode = navigator.nextNode();
continue;
}

if (!groupedText.has(parentElement)) {
groupedText.set(parentElement, []);
}
groupedText.get(parentElement)!.push(currentNode as Text);
const textArray = groupedText.get(parentElement);
if (textArray) {
textArray.push(currentNode as Text);
}
currentNode = navigator.nextNode();
}

const results: TranslatableContent = [];
Expand Down
8 changes: 4 additions & 4 deletions src/lib/storage/localstorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import LZString from "lz-string";
import { TranslationContent } from "../../types";
import type { TranslationContent } from "../../types";

export class LocalStorageWrapper {
private prefix: string;
Expand All @@ -17,7 +17,7 @@ export class LocalStorageWrapper {

private shouldCompress(value: string): boolean {
return value.length > this.COMPRESSION_THRESHOLD;
}
}

private compress(value: string): string {
try {
Expand Down Expand Up @@ -86,7 +86,7 @@ export class LocalStorageWrapper {
// Store translation for a node in the page cache (object of originalText)
setNodeTranslation(originalText: string, targetLang: string, translatedText: string): void {
const pageKey = this.getPageKey(targetLang);
let translations: TranslationContent = this.getItem(pageKey) || {};
const translations: TranslationContent = this.getItem(pageKey) || {};
translations[originalText] = translatedText;
this.setItem(pageKey, translations);
}
Expand All @@ -110,7 +110,7 @@ export class LocalStorageWrapper {

clear(): void {
if (this.prefix) {
for (let key in localStorage) {
for (const key in localStorage) {
if (key.startsWith(this.prefix)) {
localStorage.removeItem(key);
delete this.cache[key];
Expand Down
2 changes: 0 additions & 2 deletions src/lib/translation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class TranslationService {
}

async translateBatchText(texts: string[], targetLang: string, maxRetries = 2, retryDelay = 100): Promise<string[] | null> {


let attempt = 0;
while (attempt < maxRetries) {
try {
Expand Down
8 changes: 6 additions & 2 deletions src/translation-widget.css
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ Base Styles :root {
font-size: 0.9rem;
font-weight: 500;
color: var(--jigts-text-color);
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
box-shadow:
0 10px 25px -5px rgba(0, 0, 0, 0.1),
0 10px 10px -5px rgba(0, 0, 0, 0.04);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
position: relative;
overflow: hidden;
Expand All @@ -142,7 +144,9 @@ Base Styles :root {

.jigts-widget-trigger:hover {
transform: scale(1.05) translateY(-2px);
box-shadow: 0 20px 40px -10px rgba(0, 0, 0, 0.15), 0 10px 20px -5px rgba(0, 0, 0, 0.1);
box-shadow:
0 20px 40px -10px rgba(0, 0, 0, 0.15),
0 10px 20px -5px rgba(0, 0, 0, 0.1);
}

.jigts-widget-trigger:active {
Expand Down
14 changes: 8 additions & 6 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { languages } from "../constants/languages";
import { TranslatableContent } from "../lib/dom";
import type { TranslatableContent } from "../lib/dom";
function generateHashForContent(nodes: TranslatableContent): string {
const content = nodes
.map((node) => {
Expand All @@ -13,11 +13,11 @@ function generateHashForContent(nodes: TranslatableContent): string {
}

function murmurhash3_32_gc(key: string, seed: number) {
let remainder = key.length & 3,
bytes = key.length - remainder;
let h1 = seed,
c1 = 0xcc9e2d51,
c2 = 0x1b873593;
const remainder = key.length & 3;
const bytes = key.length - remainder;
let h1 = seed;
const c1 = 0xcc9e2d51;
const c2 = 0x1b873593;
let i = 0;

while (i < bytes) {
Expand All @@ -39,9 +39,11 @@ function murmurhash3_32_gc(key: string, seed: number) {

switch (remainder) {
//@ts-expect-error - this is a valid case
// biome-ignore lint/suspicious/noFallthroughSwitchClause: intentional fallthrough in MurmurHash algorithm
case 3:
k1 ^= key.charCodeAt(i + 2) << 16;
//@ts-expect-error - this is a valid case
// biome-ignore lint/suspicious/noFallthroughSwitchClause: intentional fallthrough in MurmurHash algorithm
case 2:
k1 ^= key.charCodeAt(i + 1) << 8;
case 1:
Expand Down
Loading