Skip to content

Commit

Permalink
🚧 NEW: eslint (#736)
Browse files Browse the repository at this point in the history
* Update dependency nx to v19.7.2

* 🚧 NEW: eslint

* findFileOrUp local to eslint to avoid dep issues

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
jycouet and renovate[bot] authored Sep 12, 2024
1 parent 3c21b72 commit ae8115d
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 214 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-teachers-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kitql/eslint-config': patch
---

improve default config thx to inspector
5 changes: 5 additions & 0 deletions .changeset/little-rules-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kitql/internals': patch
---

add findFileOrUp
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@changesets/cli": "2.27.7",
"@vitest/coverage-v8": "2.0.5",
"esbuild": "0.23.1",
"nx": "19.6.1",
"nx": "19.7.2",
"prettier": "3.3.3",
"rimraf": "5.0.0"
}
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-config/.prettierrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ module.exports = {
'',
'^[./]', // inside
],
// importOrderSeparation: true,
}
22 changes: 2 additions & 20 deletions packages/eslint-config/cmd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env node
import { spawnSync } from 'node:child_process'
import fs from 'node:fs'
import { Option, program } from 'commander'

import { Log, red } from '@kitql/helpers'

import { findFileOrUp } from './helper/findFileOrUp.js'

const log = new Log('kitql-lint')

program.addOption(new Option('-f, --format', 'format'))
Expand All @@ -21,23 +22,6 @@ program.addOption(
program.parse(process.argv)
const options_cli = program.opts()

const findFileOrUp = (fileName) => {
// Find file recursively 4 levels max up
for (let i = 0; i < 4; i++) {
try {
const path = '../'.repeat(i) + fileName
if (fs.statSync(path)) {
return path
}
} catch (error) {
// nothing to do
}
}

log.error(red(`${fileName} not found`))
process.exit(1)
}

const pathPrettierIgnore = findFileOrUp('.prettierignore')
const pathPrettierCjs = findFileOrUp('.prettierrc.cjs')

Expand Down Expand Up @@ -86,8 +70,6 @@ function eslintRun() {
const cmdEsLint =
preToUse +
`eslint` +
// ignore?
` --ignore-pattern ${pathPrettierIgnore}` +
// format or not
`${format ? ' --fix' : ''}` +
// exec
Expand Down
64 changes: 41 additions & 23 deletions packages/eslint-config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { includeIgnoreFile } from '@eslint/compat'
import js from '@eslint/js'
import svelte from 'eslint-plugin-svelte'
import unusedImports from 'eslint-plugin-unused-imports'
import globals from 'globals'
import ts from 'typescript-eslint'

import { findFileOrUp } from './helper/findFileOrUp.js'

const pathPrettierIgnore = findFileOrUp('.prettierignore', { absolute: true })

/** @type {import('eslint').Linter.Config[]} */
export const config = [
js.configs.recommended,
{
name: '@kitql:prettier:ignores',
ignores: includeIgnoreFile(pathPrettierIgnore).ignores,
},
{
name: 'eslint/defaults/recommended',
...js.configs.recommended, // TODO, would be nice to have a name by default?
},
...ts.configs.recommended,
...svelte.configs['flat/recommended'],
{
name: '@kitql:languages',
languageOptions: {
globals: {
...globals.browser,
Expand All @@ -18,6 +31,7 @@ export const config = [
},
},
{
name: '@kitql:svelte:languages',
files: ['**/*.svelte'],
languageOptions: {
parserOptions: {
Expand All @@ -26,10 +40,34 @@ export const config = [
},
},
{
ignores: ['build/', '.svelte-kit/', 'dist/'],
name: '@kitql:ignores',
ignores: ['build/', '.svelte-kit/', 'dist/', '**/build/', '**/.svelte-kit/', '**/dist/'],
},
{
name: '@kitql rules',
name: '@kitql:unused-imports',
plugins: {
'unused-imports': unusedImports,
},
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'off',

'unused-imports/no-unused-imports': 'error',
'unused-imports/no-unused-vars': 'off',
// 'unused-imports/no-unused-vars': [
// 'warn',
// {
// vars: 'all',
// varsIgnorePattern: '^_',
// args: 'after-used',
// argsIgnorePattern: '^_',
// },
// ],
'no-empty': ['error', { allowEmptyCatch: true }],
},
},
{
name: '@kitql:rules',
rules: {
'no-console': [
'error',
Expand All @@ -52,26 +90,6 @@ export const config = [
'svelte/no-inner-declarations': 'off',
},
},
{
plugins: {
'unused-imports': unusedImports,
},
rules: {
'no-unused-vars': 'off', // or "@typescript-eslint/no-unused-vars": "off",
'@typescript-eslint/no-unused-vars': 'off',
'unused-imports/no-unused-imports': 'error',
'unused-imports/no-unused-vars': 'off',
// 'unused-imports/no-unused-vars': [
// 'warn',
// {
// vars: 'all',
// varsIgnorePattern: '^_',
// args: 'after-used',
// argsIgnorePattern: '^_',
// },
// ],
},
},
]

export default config
22 changes: 22 additions & 0 deletions packages/eslint-config/helper/findFileOrUp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { statSync } from 'fs'
import { resolve } from 'path'

export const findFileOrUp = (fileName, options) => {
// Find file recursively 4 levels max up
for (let i = 0; i < 4; i++) {
try {
const pathFound = '../'.repeat(i) + fileName
if (statSync(pathFound)) {
if (options?.absolute) {
return resolve(pathFound)
}
return pathFound
}
} catch (error) {
// nothing to do
}
}

console.error(`"${fileName}" not found`)
return null
}
7 changes: 5 additions & 2 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@
"format": "node ./cmd.js -f",
"format:example": "kitql-lint --format",
"lint": "node ./cmd.js --verbose -p none",
"lint:example": "kitql-lint"
"lint:example": "kitql-lint",
"inspector": "npx @eslint/config-inspector"
},
"peerDependencies": {
"prettier": "^3.3.3"
},
"dependencies": {
"@eslint/compat": "^1.1.1",
"@eslint/js": "^9.10.0",
"@kitql/helpers": "workspace:*",
"@theguild/prettier-config": "2.0.7",
"@types/eslint": "9.6.1",
"@typescript-eslint/parser": "^8.5.0",
"commander": "12.1.0",
"eslint": "9.10.0",
"eslint": "^9.10.0",
"eslint-plugin-svelte": "2.43.0",
"eslint-plugin-unused-imports": "^4.1.3",
"globals": "15.9.0",
Expand Down
60 changes: 30 additions & 30 deletions packages/handles/package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
{
"name": "@kitql/handles",
"description": "Some useful handles for SvelteKit",
"keywords": [
"svelte",
"sveltekit"
],
"version": "0.2.0",
"license": "MIT",
"type": "module",
"description": "Some useful handles for SvelteKit",
"repository": {
"type": "git",
"url": "https://github.com/jycouet/kitql",
"directory": "packages/handles",
"homepage": "https://github.com/jycouet/kitql/tree/main/packages/handles"
},
"license": "MIT",
"engines": {
"node": "^16.14 || >=18"
},
"exports": {
".": {
"types": "./esm/index.d.ts",
"require": "./cjs/index.js",
"svelte": "./esm/index.js",
"default": "./esm/index.js"
}
},
"types": "./esm/index.d.ts",
"files": [
"dist",
"!dist/**/*.spec.*",
"!dist/**/*.test.*"
],
"keywords": [
"svelte",
"sveltekit"
],
"scripts": {
"build": "vite build && svelte-package && node ../../scripts/package.js",
"check": "svelte-check",
Expand All @@ -49,10 +32,6 @@
"@sveltejs/kit": "^2.4.0",
"svelte": "^3.54.0 || ^4.0.0 || ^5.0.0"
},
"dependencies": {
"esm-env": "^1.0.0",
"vary": "^1.1.2"
},
"devDependencies": {
"@kitql/eslint-config": "workspace:*",
"@kitql/helpers": "workspace:*",
Expand All @@ -70,10 +49,31 @@
"vite": "^5.4.0",
"vitest": "2.0.5"
},
"dependencies": {
"esm-env": "^1.0.0",
"vary": "^1.1.2"
},
"sideEffects": false,
"publishConfig": {
"directory": "dist",
"access": "public"
},
"sideEffects": false,
"svelte": "./esm/index.js"
}
"files": [
"dist",
"!dist/**/*.test.*",
"!dist/**/*.spec.*"
],
"svelte": "./esm/index.js",
"types": "./esm/index.d.ts",
"exports": {
".": {
"types": "./esm/index.d.ts",
"require": "./cjs/index.js",
"svelte": "./esm/index.js",
"default": "./esm/index.js"
}
},
"engines": {
"node": "^16.14 || >=18"
}
}
13 changes: 12 additions & 1 deletion packages/internals/src/lib/fs/fs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'

import { getFilesUnder, getRelativePackagePath, read, relative, write } from './fs.js'
import { findFileOrUp, getFilesUnder, getRelativePackagePath, read, relative, write } from './fs.js'

describe('fs', () => {
it('should get package path single', async () => {
Expand Down Expand Up @@ -107,4 +107,15 @@ describe('fs', () => {
`)
}
})

it('should find a file', async () => {
const res = findFileOrUp('package.json')
expect(res).toMatchInlineSnapshot(`"package.json"`)
})

it('should find a file absolute', async () => {
const res = findFileOrUp('package.json', { absolute: true })
expect(res).not.toBe('package.json')
expect(res).match(/package.json$/)
})
})
27 changes: 25 additions & 2 deletions packages/internals/src/lib/fs/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'fs'
import { dirname, join, relative } from 'path'
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'fs'
import { dirname, join, relative, resolve } from 'path'

import { Log, red } from '@kitql/helpers'

export function read(pathFile: string) {
try {
Expand Down Expand Up @@ -64,3 +66,24 @@ export function getRelativePackagePath(packageName: string) {
}

export { relative, dirname }

export const findFileOrUp = (fileName: string, options?: { absolute: boolean }) => {
// Find file recursively 4 levels max up
for (let i = 0; i < 4; i++) {
try {
const pathFound = '../'.repeat(i) + fileName
if (statSync(pathFound)) {
if (options?.absolute) {
return resolve(pathFound)
}
return pathFound
}
} catch (error) {
// nothing to do
}
}

const log = new Log('kitql-internals')
log.error(red(`${fileName} not found`))
return null
}
10 changes: 9 additions & 1 deletion packages/internals/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
export { getFilesUnder, read, write, relative, dirname, getRelativePackagePath } from './fs/fs.js'
export {
getFilesUnder,
read,
write,
relative,
dirname,
getRelativePackagePath,
findFileOrUp,
} from './fs/fs.js'
export { parseTs, extractHtmlElementAttr_Text } from './ast/ast.js'
Loading

0 comments on commit ae8115d

Please sign in to comment.