Skip to content
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

Support both flat and legacy ESLint configuration styles 🥙 #19

Merged
merged 9 commits into from
Aug 2, 2024
Merged
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
16 changes: 16 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": [
"./lib/all-legacy"
],
"rules": {
// "semi": [1, "always"]
},
"parserOptions": {
"ecmaFeatures": {
"modules": true
},
"ecmaVersion": 2018,
"requireConfigFile": false,
"sourceType": "module"
}
}
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Install Dependencies
run: yarn install --immutable

- name: Lint
run: yarn lint --ignore-patterns '**/*.ts'

- name: Run Tests
run: yarn test --coverage

Expand Down
3 changes: 3 additions & 0 deletions config/all-legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import buildLegacyConfig from './eslint/build-legacy-config'

export default buildLegacyConfig()
4 changes: 2 additions & 2 deletions config/all.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import buildConfig from './eslint/build-config'
import buildFlatConfig from './eslint/build-flat-config'

export default buildConfig()
export default buildFlatConfig()
45 changes: 0 additions & 45 deletions config/eslint/build-config.ts

This file was deleted.

42 changes: 42 additions & 0 deletions config/eslint/build-flat-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { FILE_PATHS } from './constants'
import Plugins from './plugins'
import Rules from './rules'

const buildFlatConfig = () => ([{
plugins: {
'eslint-comments': Plugins.EslintComments,
'inclusive-language': Plugins.InclusiveLanguage,
'promise': Plugins.Promise,
'sort-destructure-keys': Plugins.SortDestructureKeys,
'sort-exports': Plugins.SortExports,
},
rules: {
...Rules.ESLintComments,
...Rules.InclusiveLanguage,
...Rules.Promise,
...Rules.SortDestructureKeys,
...Rules.SortExports,
},
}, {
files: FILE_PATHS.TESTS,
languageOptions: {
globals: {
'jest/globals': true,
},
},
plugins: {
'jest': Plugins.Jest,
'jest-formatting': Plugins.JestFormatting,
},
rules: {
...Rules.Jest,
...Rules.JestFormatting,
},
}, {
files: FILE_PATHS.TESTS_TYPESCRIPT,
rules: {
...Rules.JestTypescript,
},
}])

export default buildFlatConfig
40 changes: 40 additions & 0 deletions config/eslint/build-legacy-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FILE_PATHS } from './constants'
import Rules from './rules'

const buildLegacyConfig = () => ({
env: {
'jest/globals': true,
},
plugins: [
'eslint-comments',
'inclusive-language',
'promise',
'sort-destructure-keys',
'sort-exports',
],
rules: {
...Rules.ESLintComments,
...Rules.InclusiveLanguage,
...Rules.Promise,
...Rules.SortDestructureKeys,
...Rules.SortExports,
},
overrides: [{
files: FILE_PATHS.TESTS,
plugins: [
'jest',
'jest-formatting',
],
rules: {
...Rules.Jest,
...Rules.JestFormatting,
},
}, {
files: FILE_PATHS.TESTS_TYPESCRIPT,
rules: {
...Rules.JestTypescript,
},
}],
})

export default buildLegacyConfig
16 changes: 16 additions & 0 deletions config/eslint/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const FILE_PATHS = {
TESTS: [
'*.spec.*',
'*.test.*',
],
TESTS_TYPESCRIPT: [
'*.spec.ts',
'*.spec.tsx',
'*.test.ts',
'*.test.tsx',
]
}

export {
FILE_PATHS,
}
21 changes: 21 additions & 0 deletions config/eslint/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @ts-nocheck

import EslintComments from 'eslint-plugin-eslint-comments'
import InclusiveLanguage from 'eslint-plugin-inclusive-language'
import Jest from 'eslint-plugin-jest'
import JestFormatting from 'eslint-plugin-jest-formatting'
import Promise from 'eslint-plugin-promise'
import SortDestructureKeys from 'eslint-plugin-sort-destructure-keys'
import SortExports from 'eslint-plugin-sort-exports'

const Plugins = {
EslintComments,
InclusiveLanguage,
Jest,
JestFormatting,
Promise,
SortDestructureKeys,
SortExports,
}

export default Plugins
20 changes: 20 additions & 0 deletions config/eslint/rules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ESLintComments from './eslint-comments'
import InclusiveLanguage from './inclusive-language'
import { Jest, JestTypescript } from './jest'
import JestFormatting from './jest-formatting'
import Promise from './promise'
import SortDestructureKeys from './sort-destructure-keys'
import SortExports from './sort-exports'

const Rules = {
ESLintComments,
InclusiveLanguage,
Jest,
JestFormatting,
JestTypescript,
Promise,
SortDestructureKeys,
SortExports,
}

export default Rules
4 changes: 2 additions & 2 deletions config/eslint/rules/jest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://github.com/jest-community/eslint-plugin-jest

export const JestRules = {
export const Jest = {
'jest/consistent-test-it': 2,
'jest/expect-expect': [2, {
assertFunctionNames: ['expect', 'expect*'],
Expand Down Expand Up @@ -64,7 +64,7 @@ export const JestRules = {
'jest/valid-title': 2,
}

export const JestTypescriptRules = {
export const JestTypescript = {
'jest/no-untyped-mock-factory': 2,
'jest/unbound-method': 1,
}
17 changes: 10 additions & 7 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export default [{
rules: {
'comma-dangle': [2, 'always-multiline'],
quotes: [2, 'single'],
semi: [2, 'never'],
},
}]
import config from './config/all.ts'

export default [
...config,
{
rules: {
// semi: [2, 'always'],
}
}
]
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"chalk": "5.3.0",
"chokidar": "3.6.0",
"commander": "12.1.0",
"eslint": "9.4.0",
"eslint": "9.8.0",
"eslint-plugin-eslint-comments": "3.2.0",
"eslint-plugin-inclusive-language": "2.2.1",
"eslint-plugin-jest": "28.6.0",
Expand All @@ -44,10 +44,12 @@
"@babel/core": "7.24.6",
"@babel/preset-env": "7.24.6",
"@babel/preset-typescript": "7.24.7",
"@rollup/plugin-node-resolve": "15.2.3",
"@rollup/plugin-replace": "5.0.7",
"@rollup/plugin-typescript": "11.1.6",
"@types/eslint": "8.56.10",
"@types/eslint": "9.6.0",
"@types/jest": "29.5.12",
"@types/markdownlint-rule-helpers": "0.21.5",
"@types/node-notifier": "8.0.5",
"babel-plugin-transform-import-meta": "2.2.1",
"jest": "29.7.0",
Expand Down
4 changes: 4 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFileSync } from 'fs'
import { resolve } from 'path'

import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import typescript from '@rollup/plugin-typescript'
import { terser } from 'rollup-plugin-terser'
Expand All @@ -15,6 +16,9 @@ export default {
format: 'es',
},
plugins: [
nodeResolve({
preferBuiltins: true,
}),
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
preventAssignment: true,
Expand Down
30 changes: 22 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ program
.addHelpText('beforeAll', '\n✈️ Lint Pilot ✈️\n')
.showHelpAfterError('\n💡 Run `lint-pilot --help` for more information.\n')

const runLinter = async ({ cache, filePattern, fix, linter, ignore }: RunLinter) => {
const runLinter = async ({ cache, eslintUseLegacyConfig, filePattern, fix, linter, ignore }: RunLinter) => {
const startTime = new Date().getTime()
colourLog.info(`Running ${linter.toLowerCase()}...`)

Expand All @@ -35,6 +35,7 @@ const runLinter = async ({ cache, filePattern, fix, linter, ignore }: RunLinter)

const report: LintReport = await linters[linter].lintFiles({
cache,
eslintUseLegacyConfig,
files,
fix,
})
Expand All @@ -44,7 +45,7 @@ const runLinter = async ({ cache, filePattern, fix, linter, ignore }: RunLinter)
return report
}

const runLintPilot = ({ cache, filePatterns, fix, title, watch }: RunLintPilot) => {
const runLintPilot = ({ cache, eslintUseLegacyConfig, filePatterns, fix, title, watch }: RunLintPilot) => {
const commonArgs = {
cache,
fix,
Expand All @@ -54,6 +55,7 @@ const runLintPilot = ({ cache, filePatterns, fix, title, watch }: RunLintPilot)
Promise.all([
runLinter({
...commonArgs,
eslintUseLegacyConfig,
filePattern: filePatterns.includePatterns[Linter.ESLint],
linter: Linter.ESLint,
}),
Expand Down Expand Up @@ -96,12 +98,14 @@ program
.option('--cache', 'cache linting results', false)
.option('--clearCache', 'clear the cache', false)

.option('--ignore-dirs <directories...>', 'Directories to ignore globally')
.option('--ignore-patterns <patterns...>', 'File patterns to ignore globally')
.option('--eslint-include <patterns...>', 'File patterns to include for ESLint')
.option('--ignore-dirs <directories...>', 'directories to ignore globally')
.option('--ignore-patterns <patterns...>', 'file patterns to ignore globally')
.option('--eslint-include <patterns...>', 'file patterns to include for ESLint')

.option('--debug', 'output additional debug information including the list of files being linted', false)
.action(({ cache, clearCache, debug, emoji, eslintInclude, fix, ignoreDirs, ignorePatterns, title, watch }) => {
.option('--eslint-use-legacy-config', 'set to true to use the legacy ESLint configuration', false)

.action(({ cache, clearCache, debug, emoji, eslintInclude, eslintUseLegacyConfig, fix, ignoreDirs, ignorePatterns, title, watch }) => {
clearTerminal()
colourLog.title(`${emoji} ${title} ${emoji}`)
console.log()
Expand All @@ -117,7 +121,17 @@ program
ignoreDirs,
ignorePatterns,
})
runLintPilot({ cache, filePatterns, fix, title, watch })

const lintPilotOptions = {
cache,
eslintUseLegacyConfig,
filePatterns,
fix,
title,
watch,
}

runLintPilot(lintPilotOptions)

if (watch) {
watchFiles({
Expand All @@ -129,7 +143,7 @@ program
clearTerminal()
colourLog.info(message)
console.log()
runLintPilot({ cache, filePatterns, fix, title, watch })
runLintPilot(lintPilotOptions)
})
}
})
Expand Down
Loading
Loading