Skip to content

Commit

Permalink
feat: strict limitations on the scope of files to which the rules app…
Browse files Browse the repository at this point in the history
…ly (#20)
  • Loading branch information
Garfield550 authored Jan 3, 2025
1 parent fe4452a commit b5a7df3
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 14 deletions.
5 changes: 5 additions & 0 deletions packages/eslint-config/src/configs/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import process from 'node:process'
import type { OptionsTypeScript } from '../options.js'
import type { TypedFlatConfigItem } from '../types.js'

import { GLOB_SRC } from '../globs.js'
import plugins from '../plugins.js'
import { getFlatConfigName } from '../utils/index.js'

const name = getFlatConfigName('imports')
const files: string[] = [GLOB_SRC]

export function imports(options: OptionsTypeScript = {}): TypedFlatConfigItem[] {
const {
Expand All @@ -17,6 +19,7 @@ export function imports(options: OptionsTypeScript = {}): TypedFlatConfigItem[]
return [
{
name: name.setup,
files,
plugins: {
'import-x': plugins['pluginImportX'],
'unused-imports': plugins['pluginUnusedImports'],
Expand All @@ -30,6 +33,7 @@ export function imports(options: OptionsTypeScript = {}): TypedFlatConfigItem[]
},
{
name: name.rules,
files,
rules: {
// eslint-plugin-import-x
// https://github.com/un-ts/eslint-plugin-import-x
Expand Down Expand Up @@ -59,6 +63,7 @@ export function imports(options: OptionsTypeScript = {}): TypedFlatConfigItem[]
},
{
name: name.stylistic,
files,
rules: {
// rules that conflict with eslint-plugin-perfectionist are disabled.
'sort-imports': 'off',
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-config/src/configs/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import globals from 'globals'
import type { OptionsJavaScript } from '../options.js'
import type { TypedFlatConfigItem } from '../types.js'

import { GLOB_SRC } from '../globs.js'
import plugins from '../plugins.js'
import { getFlatConfigName } from '../utils/index.js'

const name = getFlatConfigName('javascript')
const files: string[] = [GLOB_SRC]

const commonjsGlobalsOffList = Object.keys(globals.commonjs).map<
Record<string, Linter.GlobalConf>
Expand All @@ -27,6 +29,7 @@ export function javascript(options: OptionsJavaScript = {}): TypedFlatConfigItem
return [
{
name: name.setup,
files,
plugins: {
'import-x': plugins['pluginImportX'],
'unused-imports': plugins['pluginUnusedImports'],
Expand Down Expand Up @@ -80,6 +83,7 @@ export function javascript(options: OptionsJavaScript = {}): TypedFlatConfigItem
},
{
name: name.rules,
files,
rules: {
...js.configs.recommended.rules,

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/src/configs/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function json(options: OptionsJson = {}): TypedFlatConfigItem[] {
? stylistic
: ({ indentWidth: 2 } as const)

const files = [GLOB_JSON, GLOB_JSON5, GLOB_JSONC, ...extraFiles]
const files: string[] = [GLOB_JSON, GLOB_JSON5, GLOB_JSONC, ...extraFiles]

return [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/src/configs/next-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import plugins from '../plugins.js'
import { getFlatConfigName } from '../utils/index.js'

const name = getFlatConfigName('next-js')
const files = [GLOB_SRC]
const files: string[] = [GLOB_SRC]

export function nextJs(): TypedFlatConfigItem[] {
return [
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/src/configs/node-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const isModule = getPackageJson()?.type === 'module'
export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
const { module = isModule, extraFiles = [] } = options

const files = [GLOB_JS, ...extraFiles]
const files: string[] = [GLOB_JS, ...extraFiles]

return [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/src/configs/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import plugins from '../plugins.js'
import { getFlatConfigName } from '../utils/index.js'

const name = getFlatConfigName('react')
const files = [GLOB_SRC]
const files: string[] = [GLOB_SRC]

export function react(options: OptionsReact = {}): TypedFlatConfigItem[] {
const { stylistic = true, reactCompiler = false } = options
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-config/src/configs/regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ import * as pluginRegexp from 'eslint-plugin-regexp'

import type { TypedFlatConfigItem } from '../types.js'

import { GLOB_SRC } from '../globs.js'
import plugins from '../plugins.js'
import { getFlatConfigName } from '../utils/index.js'

const name = getFlatConfigName('regexp')
const files: string[] = [GLOB_SRC]

export function regexp(): TypedFlatConfigItem[] {
return [
{
name: name.setup,
files,
plugins: {
regexp: plugins['pluginRegexp'],
},
},
{
name: name.rules,
files,
rules: {
// eslint-plugin-regexp
// https://github.com/ota-meshi/eslint-plugin-regexp
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-config/src/configs/tailwindcss.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { TypedFlatConfigItem } from '../types.js'

import { GLOB_HTML, GLOB_POSTCSS, GLOB_SRC, GLOB_STYLE } from '../globs.js'
import plugins from '../plugins.js'
import { getFlatConfigName } from '../utils/index.js'

const name = getFlatConfigName('tailwindcss')
const files: string[] = [GLOB_SRC, GLOB_STYLE, GLOB_POSTCSS, GLOB_HTML]

export function tailwindcss(): TypedFlatConfigItem[] {
return [
{
name: name.setup,
files,
plugins: {
tailwindcss: plugins['pluginTailwindCSS'],
},
Expand All @@ -20,6 +23,7 @@ export function tailwindcss(): TypedFlatConfigItem[] {
},
{
name: name.rules,
files,
rules: {
// eslint-plugin-tailwindcss
// https://github.com/francoismassart/eslint-plugin-tailwindcss
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/src/configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function typescript(options: OptionsTypeScript = {}): TypedFlatConfigItem
reactTypeCheck = false,
} = options

const files = [
const files: string[] = [
GLOB_TS,
GLOB_TSX,
...extraFileExtensions.map((extension) => `**/*${extension}`),
Expand Down
32 changes: 25 additions & 7 deletions packages/eslint-config/src/configs/unicorn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,55 @@ import globals from 'globals'

import type { TypedFlatConfigItem } from '../types.js'

import { GLOB_ALL_SRC, GLOB_SRC } from '../globs.js'
import plugins from '../plugins.js'
import { getFlatConfigName } from '../utils/index.js'

const name = getFlatConfigName('unicorn')
const files: string[] = [GLOB_SRC]
const allFiles: string[] = [...GLOB_ALL_SRC]

export function unicorn(): TypedFlatConfigItem[] {
return [
{
name: name.setup,
name: `${name.setup}/all-src`,
files: allFiles,
plugins: {
unicorn: plugins['pluginUnicorn'],
},
},
{
name: `${name.setup}/src`,
files,
languageOptions: {
globals: {
...globals.builtin,
},
},
},
{
name: name.rules,
name: `${name.rules}/all-src`,
files: allFiles,
rules: {
// eslint-plugin-unicorn
// https://github.com/sindresorhus/eslint-plugin-unicorn
...pluginUnicorn.configs['flat/recommended'].rules,

// Ignore case for specific files
// Let all files use kebab-case for filenames.
// kebab-case forces filenames to be lowercase and hyphen-separated,
// some file systems are case-insensitive, so this rule can help prevent
// issues with importing files on case-sensitive file systems.
'unicorn/filename-case': ['error', {
cases: { kebabCase: true },
// Ignore case for specific files
ignore: ['README.md', 'LICENSE.md', 'CHANGELOG.md', 'CODE_OF_CONDUCT.md'],
}],
},
},
{
name: `${name.rules}/src`,
files,
rules: {
// eslint-plugin-unicorn
// https://github.com/sindresorhus/eslint-plugin-unicorn
...pluginUnicorn.configs['flat/recommended'].rules,
},
},
]
}
9 changes: 7 additions & 2 deletions packages/eslint-config/src/globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export const GLOB_TSX = '**/*.?([cm])tsx'

export const GLOB_STYLE = '**/*.{c,le,sc}ss'
export const GLOB_CSS = '**/*.css'
export const GLOB_POSTCSS = '**/*.{p,post}css'
export const GLOB_LESS = '**/*.less'
export const GLOB_SCSS = '**/*.scss'
export const GLOB_POSTCSS = '**/*.{p,post}css'

export const GLOB_JSON = '**/*.json'
export const GLOB_JSON5 = '**/*.json5'
Expand All @@ -29,8 +29,8 @@ export const GLOB_VUE = '**/*.vue'
export const GLOB_YAML = '**/*.y?(a)ml'
export const GLOB_TOML = '**/*.toml'
export const GLOB_XML = '**/*.xml'
export const GLOB_SVG = '**/*.svg'
export const GLOB_HTML = '**/*.htm?(l)'
export const GLOB_SVG = '**/*.svg'
export const GLOB_ASTRO = '**/*.astro'
export const GLOB_ASTRO_TS = '**/*.astro/*.ts'
export const GLOB_GRAPHQL = '**/*.{g,graph}ql'
Expand All @@ -48,14 +48,19 @@ export const GLOB_TESTS = [
export const GLOB_ALL_SRC = [
GLOB_SRC,
GLOB_STYLE,
GLOB_POSTCSS,
GLOB_JSON,
GLOB_JSON5,
GLOB_JSONC,
GLOB_MARKDOWN,
GLOB_SVELTE,
GLOB_VUE,
GLOB_YAML,
GLOB_TOML,
GLOB_XML,
GLOB_HTML,
GLOB_SVG,
GLOB_ASTRO,
]

export const GLOB_EXCLUDE = [
Expand Down

0 comments on commit b5a7df3

Please sign in to comment.