Skip to content

Commit

Permalink
Add eslint rules and lint project (#56)
Browse files Browse the repository at this point in the history
* Add lint config and run format

* Move contributing.md to root
  • Loading branch information
Kitenite authored Jul 12, 2024
1 parent 4678361 commit f440301
Show file tree
Hide file tree
Showing 34 changed files with 196 additions and 10,354 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cd app && npm run format
cd app && npm run lint && npm run format
File renamed without changes.
Binary file modified app/bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion app/electron/main/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ function addClassToAst(ast: t.File, className: string) {
let processed = false;
traverse(ast, {
JSXOpeningElement(path) {
if (processed) return;
if (processed) {
return;
}
let classNameAttr = null;
path.node.attributes.forEach((attribute) => {
if (t.isJSXAttribute(attribute) && attribute.name.name === 'className') {
Expand Down
20 changes: 15 additions & 5 deletions app/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
: RENDERER_DIST;

// Disable GPU Acceleration for Windows 7
if (os.release().startsWith('6.1')) app.disableHardwareAcceleration();
if (os.release().startsWith('6.1')) {
app.disableHardwareAcceleration();
}

// Set application name for Windows 10+ notifications
if (process.platform === 'win32') app.setAppUserModelId(app.getName());
if (process.platform === 'win32') {
app.setAppUserModelId(app.getName());
}

if (!app.requestSingleInstanceLock()) {
app.quit();
Expand Down Expand Up @@ -67,7 +71,9 @@ function initMainWindow() {

// Ensure links open externally
win.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('https:')) shell.openExternal(url);
if (url.startsWith('https:')) {
shell.openExternal(url);
}
return { action: 'deny' };
});
}
Expand All @@ -77,13 +83,17 @@ function listenForAppEvents() {

app.on('window-all-closed', () => {
win = null;
if (process.platform !== 'darwin') app.quit();
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('second-instance', () => {
if (win) {
// Focus on the main window if the user tried to open another
if (win.isMinimized()) win.restore();
if (win.isMinimized()) {
win.restore();
}
win.focus();
}
});
Expand Down
4 changes: 3 additions & 1 deletion app/electron/main/tunnel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export class TunnelService {
}

async close() {
if (this.tunnel) await this.tunnel.close();
if (this.tunnel) {
await this.tunnel.close();
}
}

async getPassword() {
Expand Down
6 changes: 4 additions & 2 deletions app/electron/preload/webview/changes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-ignore - No external dependencies for webview preload
// @ts-expect-error - No external dependencies for webview preload
import { CssNode, Declaration, Rule, generate, parse, walk } from './csstree.esm.js';
import { EditorAttributes } from '/common/constants';

Expand Down Expand Up @@ -130,7 +130,9 @@ export class CssStyleChange {
}

jsToCssProperty(key: string) {
if (!key) return '';
if (!key) {
return '';
}
return key.replace(/([A-Z])/g, '-$1').toLowerCase();
}
}
10 changes: 5 additions & 5 deletions app/electron/preload/webview/elements/finder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// License: MIT
// Author: Anton Medvedev <[email protected]>
// Source: https://github.com/antonmedv/finder
// @ts-ignore
// @ts-expect-error - No declaration
import { generate, parse } from '../changes/csstree.esm.js';

type Knot = {
Expand Down Expand Up @@ -82,7 +82,7 @@ function bottomUpSearch(
fallback?: () => Path | null,
): Path | null {
let path: Path | null = null;
let stack: Knot[][] = [];
const stack: Knot[][] = [];
let current: Element | null = input;
let i = 0;
while (current) {
Expand Down Expand Up @@ -115,7 +115,7 @@ function bottomUpSearch(
level = [nthChild(level[0], nth)];
}
}
for (let node of level) {
for (const node of level) {
node.level = i;
}
stack.push(level);
Expand All @@ -142,7 +142,7 @@ function findUniquePath(stack: Knot[][], fallback?: () => Path | null): Path | n
if (paths.length > config.threshold) {
return fallback ? fallback() : null;
}
for (let candidate of paths) {
for (const candidate of paths) {
if (unique(candidate)) {
return candidate;
}
Expand Down Expand Up @@ -278,7 +278,7 @@ function notEmpty<T>(value: T | null | undefined): value is T {

function* combinations(stack: Knot[][], path: Knot[] = []): Generator<Knot[]> {
if (stack.length > 0) {
for (let node of stack[0]) {
for (const node of stack[0]) {
yield* combinations(stack.slice(1, stack.length), path.concat(node));
}
} else {
Expand Down
28 changes: 20 additions & 8 deletions app/electron/preload/webview/elements/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { finder } from './finder';
import { EditorAttributes } from '/common/constants';
import { ElementMetadata } from '/common/models';

export const handleMouseEvent = (e: MouseEvent): Object => {
export const handleMouseEvent = (e: MouseEvent): object => {
const scroll = { coordinates: { x: e.clientX, y: e.clientY } };
if (e.type === 'scroll' || e.type === 'wheel') {
return scroll;
Expand All @@ -14,7 +14,9 @@ export const handleMouseEvent = (e: MouseEvent): Object => {
}

const el = deepElementFromPoint(e.clientX, e.clientY);
if (!el) return scroll;
if (!el) {
return scroll;
}

const tagName = el.tagName.toLowerCase();
const rect = el.getBoundingClientRect();
Expand All @@ -37,7 +39,9 @@ export const handleMouseEvent = (e: MouseEvent): Object => {

const getParentRect = (el: HTMLElement): DOMRect | null => {
const parent = el.parentElement;
if (!parent) return null;
if (!parent) {
return null;
}
return parent.getBoundingClientRect();
};

Expand All @@ -63,14 +67,22 @@ export const getUniqueSelector = (el: HTMLElement): string => {

export const deepElementFromPoint = (x: number, y: number): Element | undefined => {
const el = document.elementFromPoint(x, y);
if (!el) return;
if (!el) {
return;
}
const crawlShadows = (node: Element): Element => {
if (node?.shadowRoot) {
const potential = node.shadowRoot.elementFromPoint(x, y);
if (potential == node) return node;
else if (potential?.shadowRoot) return crawlShadows(potential);
else return potential || node;
} else return node;
if (potential == node) {
return node;
} else if (potential?.shadowRoot) {
return crawlShadows(potential);
} else {
return potential || node;
}
} else {
return node;
}
};

const nested_shadow = crawlShadows(el);
Expand Down
2 changes: 1 addition & 1 deletion app/electron/preload/webview/eventBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class EventBridge {
this.setListenToHostEvents();
}

LOCAL_EVENT_HANDLERS: Record<string, (e: any) => Object> = {
LOCAL_EVENT_HANDLERS: Record<string, (e: any) => object> = {
mouseover: handleMouseEvent,
click: handleMouseEvent,
dblclick: handleMouseEvent,
Expand Down
36 changes: 36 additions & 0 deletions app/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { fixupConfigRules } from '@eslint/compat';
import pluginJs from '@eslint/js';
import pluginReactConfig from 'eslint-plugin-react/configs/recommended.js';
import globals from 'globals';
import tseslint from 'typescript-eslint';

export default [
{ files: ['**/*.{js,mjs,cjs,ts,jsx,tsx}'] },
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{ languageOptions: { globals: { ...globals.browser, ...globals.node } } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
...fixupConfigRules(pluginReactConfig),
{
ignores: [
'node_modules/',
'dist/',
'dist-electron/',
'release/',
'src/out/',
'electron/preload/webview/changes/csstree.esm.js',
],
},
{
rules: {
curly: ['error', 'all'],
'prefer-const': 'warn',
'react/react-in-jsx-scope': 'off',
'react/jsx-uses-react': 'off',
'react/no-unknown-property': 'off',
'react/prop-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'warn',
},
},
];
13 changes: 9 additions & 4 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"pree2e": "vite build --mode=test",
"e2e": "playwright test",
"test": "bun test",
"lint": "eslint .",
"lint": "eslint --fix .",
"format": "prettier --write ."
},
"dependencies": {
Expand Down Expand Up @@ -54,6 +54,8 @@
"tailwindcss-animate": "^1.0.7"
},
"devDependencies": {
"@eslint/compat": "^1.1.1",
"@eslint/js": "^9.7.0",
"@playwright/test": "^1.42.1",
"@types/bun": "^1.1.6",
"@types/css-tree": "^2.3.8",
Expand All @@ -67,8 +69,11 @@
"css-tree": "^2.3.1",
"electron": "^31.1.0",
"electron-builder": "^24.13.3",
"eslint": "^9.6.0",
"eslint": "9.x",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-react": "^7.34.3",
"globals": "^15.8.0",
"husky": "^8.0.0",
"mobx": "^6.12.4",
"mobx-react-lite": "^4.0.7",
"postcss": "^8.4.38",
Expand All @@ -78,9 +83,9 @@
"react-dom": "^18.2.0",
"tailwindcss": "^3.4.4",
"typescript": "^5.4.2",
"typescript-eslint": "^7.16.0",
"vite": "^5.1.5",
"vite-plugin-electron": "^0.28.4",
"vite-plugin-electron-renderer": "^0.14.5",
"husky": "^8.0.0"
"vite-plugin-electron-renderer": "^0.14.5"
}
}
4 changes: 3 additions & 1 deletion app/src/components/theme-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export function ThemeProvider({
export const useTheme = () => {
const context = useContext(ThemeProviderContext);

if (context === undefined) throw new Error('useTheme must be used within a ThemeProvider');
if (context === undefined) {
throw new Error('useTheme must be used within a ThemeProvider');
}

return context;
};
4 changes: 3 additions & 1 deletion app/src/components/ui/use-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ function toast({ ...props }: Toast) {
id,
open: true,
onOpenChange: (open) => {
if (!open) dismiss();
if (!open) {
dismiss();
}
},
},
});
Expand Down
7 changes: 5 additions & 2 deletions app/src/lib/editor/engine/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export class CodeManager {

async getTailwindClasses(stylesheet: string) {
const tailwindResult = CssToTailwindTranslator(stylesheet);
if (tailwindResult.code !== 'OK')
if (tailwindResult.code !== 'OK') {
throw new Error('Failed to translate CSS to Tailwind CSS.');
}
return tailwindResult.data;
}

Expand All @@ -59,7 +60,9 @@ export class CodeManager {
for (const twRes of tailwindResults) {
const { resultVal, selectorName } = twRes;
const dataOnlookId = await this.getDataOnlookId(selectorName, webview);
if (!dataOnlookId) continue;
if (!dataOnlookId) {
continue;
}

let writeParam = writeParams.get(dataOnlookId);
if (!writeParam) {
Expand Down
4 changes: 3 additions & 1 deletion app/src/lib/editor/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export class EditorEngine {
updateStyle(style: string, value: string) {
this.state.selected.forEach((elementMetadata) => {
const webview = this.webviews.get(elementMetadata.webviewId);
if (!webview) return;
if (!webview) {
return;
}
webview.send(WebviewChannels.UPDATE_STYLE, {
selector: elementMetadata.selector,
style,
Expand Down
8 changes: 6 additions & 2 deletions app/src/lib/editor/engine/overlay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ export class OverlayManager {
};

updateParentRect = (el: HTMLElement) => {
if (!el) return;
if (!el) {
return;
}
const rect = el.getBoundingClientRect();
this.parentRect.render(rect);
};
Expand All @@ -126,7 +128,9 @@ export class OverlayManager {
};

updateEditRect = (el: HTMLElement) => {
if (!el) return;
if (!el) {
return;
}
const rect = el.getBoundingClientRect();
this.editRect.render(rect);
};
Expand Down
14 changes: 10 additions & 4 deletions app/src/lib/editor/engine/styles/autolayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ export function getInputValues(value: string): {
mode: LayoutMode;
value: string;
} {
if (value === 'fit-content') return { mode: LayoutMode.Fit, value: value };
if (value === '100%' || value === 'auto') return { mode: LayoutMode.Fill, value: '100%' };
if (value.includes('%')) return { mode: LayoutMode.Relative, value: value };
if (value === 'fit-content') {
return { mode: LayoutMode.Fit, value: value };
}
if (value === '100%' || value === 'auto') {
return { mode: LayoutMode.Fill, value: '100%' };
}
if (value.includes('%')) {
return { mode: LayoutMode.Relative, value: value };
}
return { mode: LayoutMode.Fixed, value: value };
}

Expand All @@ -42,7 +48,7 @@ export function getStyles(
parentRect: DOMRect,
): Record<string, string> {
const { width, height } = computedStyles;
let MODE_PROPERTIES = {
const MODE_PROPERTIES = {
[LayoutMode.Fit]: 'fit-content',
[LayoutMode.Fill]: '100%',
[LayoutMode.Relative]: getRelativeValue(property, computedStyles, parentRect),
Expand Down
Loading

0 comments on commit f440301

Please sign in to comment.