Skip to content

Commit

Permalink
added the solidjs eslint rules, improved the naming convention rules
Browse files Browse the repository at this point in the history
  • Loading branch information
MP281X committed May 14, 2024
1 parent 5a5ddd2 commit 268055b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"type": "module",
"version": "1.0.4",
"version": "1.0.5",
"name": "@mp281x/shared-config",

"globImports": ["**/src/*.ts"],
Expand Down Expand Up @@ -40,6 +40,7 @@
"eslint": "9.2.0",
"@eslint/js": "9.2.0",
"typescript-eslint": "7.8.0",
"eslint-plugin-solid": "0.14.0",
"eslint-plugin-svelte": "2.39.0",
"eslint-plugin-unicorn": "53.0.0",
"eslint-config-prettier": "9.1.0",
Expand Down
50 changes: 46 additions & 4 deletions src/eslint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable perfectionist/sort-imports */
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */

Expand All @@ -18,6 +17,8 @@ import perfectionist from 'eslint-plugin-perfectionist'
import svelte from 'eslint-plugin-svelte'
import svelteParser from 'svelte-eslint-parser'

import solid from 'eslint-plugin-solid/dist/plugin.js'

import { parseConfig } from './lib/parseConfig'

export default ts.config(
Expand Down Expand Up @@ -54,7 +55,33 @@ export default ts.config(
'@typescript-eslint/unbound-method': 'off', // cause errors with the typescript compiler api

// enforce variable naming style
'@typescript-eslint/naming-convention': ['error', { format: ['camelCase', 'snake_case', 'UPPER_CASE'], selector: 'objectLiteralProperty' }]
'@typescript-eslint/naming-convention': [
'error',
// allow
{ format: ['camelCase'], selector: 'default' },
{ format: ['camelCase'], leadingUnderscore: 'allow', selector: 'parameter' },
{ format: ['PascalCase'], selector: 'typeLike' },

// boolean variables should start with one of these prefix
{ format: ['PascalCase'], prefix: ['is', 'should', 'has', 'can', 'did', 'will'], selector: 'variable', types: ['boolean'] },
// allow only camelCase object keys
{ format: ['camelCase'], selector: 'objectLiteralProperty' },
// disallow the rules for evrithing that require the quotes
{
format: null, // eslint-disable-line unicorn/no-null
modifiers: ['requiresQuotes'],
selector: [
'classProperty',
'objectLiteralProperty',
'typeProperty',
'classMethod',
'objectLiteralMethod',
'typeMethod',
'accessor',
'enumMember'
]
}
]
}
},
// js/ts
Expand Down Expand Up @@ -160,11 +187,26 @@ export default ts.config(
]
}
},
// solid
{
files: ['**/*.tsx'],
plugins: { solid: solid.plugin },
rules: {
'solid/components-return-once': 'error', // disallow early returns in jsx components
'solid/event-handlers': 'error', // make event handlers names consistent "onclick" -> "onClick"
'solid/jsx-no-script-url': 'error', // allow only valid urls in the href prop
'solid/no-destructure': 'error', // don't deconstruct jsx props
'solid/no-innerhtml': 'error', // don't allow the innerHtml prop
'solid/prefer-for': 'error', // use the <For> components instead of the jsx map
'solid/prefer-show': 'error', // use the <Show> component instead of the jsx ternary
'solid/reactivity': 'error', // prevent reactivity error
'solid/self-closing-comp': ['error', { component: 'all', html: 'all' }] // force self closing tags if there are no chidlren
}
},
// svelte
{
extends: svelte.configs['flat/recommended'] as any, // eslint-disable-line @typescript-eslint/no-explicit-any
files: ['**/*.svelte'],
// @ts-expect-error
extends: svelte.configs['flat/recommended'],
languageOptions: { parser: svelteParser, parserOptions: { parser: ts.parser } },
rules: {
'svelte/block-lang': ['error', { enforceScriptPresent: true, script: ['ts'] }], // require lang="ts" in the script tag
Expand Down
6 changes: 3 additions & 3 deletions src/lib/globImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const fileImportsObj = (glob: string, cwd: string) => {
}

const globImportsObj = (globImports: string[], cwd: string) => {
const _globImports = new Map<string, Expression>()
const importObj = new Map<string, Expression>()

for (const glob of globImports) _globImports.set(glob, fileImportsObj(glob, cwd))
for (const glob of globImports) importObj.set(glob, fileImportsObj(glob, cwd))

return objectFactory(Object.fromEntries(_globImports))
return objectFactory(Object.fromEntries(importObj))
}

export const genGlobImportsFile = (globImports: string[], cwd: string) => {
Expand Down
9 changes: 9 additions & 0 deletions src/lib/parseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ export const parseConfig = <Res>(path: string): Res | undefined => {

return undefined
}

if (import.meta.vitest) {
const { expect, it } = import.meta.vitest

it('parse .gitingore', () => {
const gitignore = parseConfig<string[]>('.gitignore') ?? []
expect(gitignore).include('**/node_modules')
})
}
6 changes: 3 additions & 3 deletions src/task-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ handleKeypress()
const { cmd, flags, task } = getArgs()
const projects = findProjects()

const monorepo = projects.length > 1
const isMonorepo = projects.length > 1

// codegen
for (const { cwd, globImports } of projects) {
Expand Down Expand Up @@ -90,9 +90,9 @@ for (const { name } of projects) {
// run the scripts
for (const { cwd, name, scripts } of projects) {
if (!flags.includes('--run')) continue
if (monorepo && !scripts.includes(task)) continue
if (isMonorepo && !scripts.includes(task)) continue

if (monorepo) await execCmd({ cmd: ['run', '--silent', task], cwd, mode: 'async', title: `${name}:${task}` })
if (isMonorepo) await execCmd({ cmd: ['run', '--silent', task], cwd, mode: 'async', title: `${name}:${task}` })
else await execCmd({ cmd, cwd, mode: 'async', title: `${name}:${task}` })
}

Expand Down

0 comments on commit 268055b

Please sign in to comment.