Skip to content

Commit

Permalink
feat: add presets
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnyq committed Mar 31, 2023
1 parent 09f37a4 commit aaf95f3
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 67 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"iife",
"jsdelivr",
"npmrc",
"nuxt",
"pnpm",
"stroustrup",
"sxzz",
Expand Down
30 changes: 2 additions & 28 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
import { defineFlatConfig } from 'eslint-define-config'
import {
js,
jsx,
ts,
yml,
astro,
jsonc,
unicorn,
imports,
pkgOrder,
prettier,
markdown,
eslintComments,
} from './index.js'
import { all } from './index.js'

export default defineFlatConfig([
...js,
...jsx,
...ts,
...yml,
...astro,
...jsonc,
...imports,
...unicorn,
...pkgOrder,
...prettier,
...markdown,
...eslintComments,
])
export default defineFlatConfig(all)
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export * from './lib/jsonc.js'
export * from './lib/prettier.js'
export * from './lib/markdown.js'
export * from './lib/eslint-comments.js'

export * from './lib/presets.js'
3 changes: 1 addition & 2 deletions lib/astro.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import astroPlugin, { configs } from 'eslint-plugin-astro'
import astroParser from 'astro-eslint-parser'
import { GLOB_EXCLUDE, GLOB_ASTRO } from './shared.js'
import { GLOB_ASTRO } from './shared.js'

/** @type {import('eslint-define-config').FlatESLintConfig[]} */
export const astro = [
{
files: [GLOB_ASTRO],
ignores: GLOB_EXCLUDE,
plugins: {
astro: astroPlugin,
},
Expand Down
3 changes: 0 additions & 3 deletions lib/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import globals from 'globals'
import jsConfig from '@eslint/js'
import importPlugin from 'eslint-plugin-import'
import unicornPlugin from 'eslint-plugin-unicorn'
import { GLOB_EXCLUDE } from './shared.js'

/** @type {import('eslint-define-config').FlatESLintConfig[]} */
export const js = [
Expand All @@ -21,7 +20,6 @@ export const js = [

{
files: ['**/scripts/*', '**/cli.*'],
ignores: GLOB_EXCLUDE,
rules: {
'no-console': 'off',
},
Expand All @@ -40,7 +38,6 @@ export const js = [
export const jsx = [
{
files: ['**/*.jsx'],
ignores: GLOB_EXCLUDE,
languageOptions: {
parserOptions: {
ecmaFeatures: {
Expand Down
3 changes: 1 addition & 2 deletions lib/jsonc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import jsoncPlugin, { configs } from 'eslint-plugin-jsonc'
import jsoncParser from 'jsonc-eslint-parser'
import { GLOB_EXCLUDE, GLOB_JSON, GLOB_JSON5, GLOB_JSONC } from './shared.js'
import { GLOB_JSON, GLOB_JSON5, GLOB_JSONC } from './shared.js'

/** @type {import('eslint-define-config').FlatESLintConfig[]} */
export const jsonc = [
{
files: [GLOB_JSON, GLOB_JSON5, GLOB_JSONC, '**/*rc'],
ignores: GLOB_EXCLUDE,
plugins: {
jsonc: jsoncPlugin,
},
Expand Down
9 changes: 6 additions & 3 deletions lib/markdown.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import markdownPlugin from 'eslint-plugin-markdown'
import { GLOB_EXCLUDE, GLOB_MARKDOWN } from './shared.js'
import tsPlugin from '@typescript-eslint/eslint-plugin'
import { GLOB_MARKDOWN, GLOB_SRC, GLOB_VUE } from './shared.js'

/** @type {import('eslint-define-config').FlatESLintConfig[]} */
export const markdown = [
{
files: [GLOB_MARKDOWN],
ignores: GLOB_EXCLUDE,
plugins: {
markdown: markdownPlugin,
},
processor: 'markdown/markdown',
},
{
files: ['**/*.md/*'],
files: [`**/*.md/${GLOB_SRC}`, `**/*.md/${GLOB_VUE}`],
languageOptions: {
parserOptions: {
ecmaFeatures: {
impliedStrict: true,
},
},
},
plugins: {
'@typescript-eslint': tsPlugin,
},
rules: {
...markdownPlugin.configs.recommended.overrides[1].rules,
'no-undef': 'off',
Expand Down
44 changes: 40 additions & 4 deletions lib/presets.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import jsConfig from '@eslint/js'
// @ts-check

import { ts } from './ts.js'
import { yml } from './yml.js'
import { vue } from './vue.js'
import { astro } from './astro.js'
import { prettier } from './prettier.js'
import { markdown } from './markdown.js'
import { jsonc, pkgOrder } from './jsonc.js'
import { imports, js, jsx, unicorn } from './js.js'
import { eslintComments } from './eslint-comments.js'
import { GLOB_EXCLUDE } from './shared.js'

/**
* @typedef { import('eslint-define-config').FlatESLintConfig } FlatESLintConfig
*/

/** @type {import('eslint-define-config').FlatESLintConfig[]} */
export const presetBasic = [
jsConfig.configs.recommended,
/** @type {FlatESLintConfig[]} */
export const basic = [
{ ignores: GLOB_EXCLUDE },
...js,
...jsx,
...ts,
Expand All @@ -22,3 +30,31 @@ export const presetBasic = [
...markdown,
...eslintComments,
]

/** @type {FlatESLintConfig[]} */
export const all = [...vue, ...astro, ...basic, ...prettier]

/** @type {(config?: FlatESLintConfig | FlatESLintConfig[], enables?: Partial<{
* vue: boolean
* prettier: boolean
* markdown: boolean
* }>) => FlatESLintConfig[]} */
export function ntnyq(
config = [],
{ vue: enableVue = true, prettier: enablePrettier = true, markdown: enableMarkdown = true } = {},
) {
const configs = [...basic]
if (enableVue !== false) {
configs.push(...vue)
}
if (enableMarkdown !== false) {
configs.push(...markdown)
}
if (enablePrettier !== false) {
configs.push(...prettier)
}
if (Object.keys(config).length > 0) {
configs.push(...(Array.isArray(config) ? config : [config]))
}
return configs
}
5 changes: 1 addition & 4 deletions lib/prettier.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import prettierPlugin from 'eslint-plugin-prettier'
import prettierConfig from 'eslint-config-prettier'

const prettierConflictRules = { ...prettierConfig.rules }
delete prettierConflictRules['vue/html-self-closing']

/** @type {import('eslint-define-config').FlatESLintConfigItem[]} */
export const prettier = [
{
plugins: {
prettier: prettierPlugin,
},
rules: {
...prettierConflictRules,
...prettierConfig.rules,
...prettierPlugin.configs.recommended.rules,
'prettier/prettier': 'warn',
},
Expand Down
25 changes: 13 additions & 12 deletions lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const GLOB_MARKDOWN = '**/*.md'
export const GLOB_YAML = '**/*.y?(a)ml'
export const GLOB_HTML = '**/*.htm?(l)'

export const GLOB_ALL_SRC = /** @type {const} */ ([
export const GLOB_ALL_SRC = [
GLOB_SRC,
GLOB_STYLE,
GLOB_JSON,
Expand All @@ -33,16 +33,12 @@ export const GLOB_ALL_SRC = /** @type {const} */ ([
GLOB_VUE,
GLOB_YAML,
GLOB_HTML,
])

export const GLOB_NODE_MODULES = /** @type {const} */ ('**/node_modules/**')
export const GLOB_DIST = /** @type {const} */ ('**/dist/**')
export const GLOB_LOCKFILE = /** @type {const} */ ([
'**/package-lock.json',
'**/yarn.lock',
'**/pnpm-lock.yaml',
])
export const GLOB_EXCLUDE = /** @type {const} */ ([
]

export const GLOB_NODE_MODULES = '**/node_modules/**'
export const GLOB_DIST = '**/dist/**'
export const GLOB_LOCKFILE = ['**/package-lock.json', '**/yarn.lock', '**/pnpm-lock.yaml']
export const GLOB_EXCLUDE = [
GLOB_NODE_MODULES,
GLOB_DIST,
...GLOB_LOCKFILE,
Expand All @@ -52,10 +48,15 @@ export const GLOB_EXCLUDE = /** @type {const} */ ([
'**/output',
'**/coverage',
'**/temp',
'**/cache',
'**/fixtures',
'**/.vitepress/cache',
'**/.nuxt',
'**/.vercel',
'**/.changeset',
'**/__snapshots__',
'**/auto-import.d.ts',
'**/components.d.ts',
'**/.npmrc',
'**/.yarnrc',
])
]
13 changes: 10 additions & 3 deletions lib/ts.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import tsParser from '@typescript-eslint/parser'
import tsPlugin from '@typescript-eslint/eslint-plugin'
import { GLOB_EXCLUDE, GLOB_TS, GLOB_TSX } from './shared.js'
import { GLOB_TS, GLOB_TSX } from './shared.js'

export { tsParser, tsPlugin }

/** @type {import('eslint-define-config').FlatESLintConfig[]} */
export const ts = [
{
files: [GLOB_TS, GLOB_TSX],
ignores: GLOB_EXCLUDE,
languageOptions: {
parser: tsParser,
parserOptions: {
Expand Down Expand Up @@ -53,7 +54,6 @@ export const ts = [

{
files: ['**/*.d.ts'],
ignores: GLOB_EXCLUDE,
rules: {
'import/no-duplicates': 'off',
'import/newline-after-import': 'off',
Expand All @@ -67,4 +67,11 @@ export const ts = [
'max-lines-per-function': 'off',
},
},

{
files: ['**/*.js', '**/*.cjs'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
},
},
]
47 changes: 44 additions & 3 deletions lib/vue.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,64 @@
import { getPackageInfoSync } from 'local-pkg'
import vuePlugin from 'eslint-plugin-vue'
import vueParser from 'vue-eslint-parser'
import { GLOB_EXCLUDE, GLOB_VUE } from './shared.js'
import tsPlugin from '@typescript-eslint/eslint-plugin'
import { ts } from './ts.js'
import { GLOB_VUE } from './shared.js'

export { vueParser, vuePlugin }

export function getVueVersion() {
const pkg = getPackageInfoSync('vue', { paths: [process.cwd()] })
if (pkg && typeof pkg.version === 'string' && !Number.isNaN(+pkg.version[0])) {
return +pkg.version[0]
}
return 3
}
const isVue3 = getVueVersion() === 3

/** @type {import('eslint-define-config').Rules} */
const vueBaseRules = {}

/** @type {import('eslint-define-config').Rules} */
const vue2Rules = {
...vueBaseRules,
}

/** @type {import('eslint-define-config').Rules} */
const vue3Rules = {
...vueBaseRules,
}

/** @type {import('eslint-define-config').FlatESLintConfig[]} */
export const vue = [
{
files: [GLOB_VUE],
ignores: GLOB_EXCLUDE,
plugins: {
vue: vuePlugin,
'@typescript-eslint': tsPlugin,
},
languageOptions: {
parser: vueParser,
parserOptions: {
parser: '@typescript-eslint/parser',
sourceType: 'module',
extraFileExtensions: ['.vue'],
ecmaFeatures: {
jsx: true,
},
},
},
processor: vuePlugin.processors['.vue'],
rules: {
...ts[0].rules,
},
},
{
plugins: {
vue: vuePlugin,
},
rules: {
...vuePlugin.configs['vue3-recommended'],
...(isVue3 ? vue3Rules : vue2Rules),
},
},
]
3 changes: 1 addition & 2 deletions lib/yml.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import ymlPlugin, { configs } from 'eslint-plugin-yml'
import ymlParser from 'yaml-eslint-parser'
import { GLOB_EXCLUDE, GLOB_YAML } from './shared.js'
import { GLOB_YAML } from './shared.js'

/** @type {import('eslint-define-config').FlatESLintConfig[]} */
export const yml = [
{
files: [GLOB_YAML],
ignores: GLOB_EXCLUDE,
languageOptions: {
parser: ymlParser,
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"eslint-plugin-yml": "^1.5.0",
"globals": "^13.20.0",
"jsonc-eslint-parser": "^2.2.0",
"local-pkg": "^0.4.3",
"prettier": "^2.8.7",
"vue-eslint-parser": "^9.1.1",
"yaml-eslint-parser": "^1.2.0"
Expand All @@ -64,7 +65,7 @@
"typescript": "5.0.3"
},
"engines": {
"node": ">=14.19.0"
"node": ">=16.14.0"
},
"prettier": "@ntnyq/prettier-config"
}
Loading

0 comments on commit aaf95f3

Please sign in to comment.