Skip to content

Commit

Permalink
feat: flat config and add type check
Browse files Browse the repository at this point in the history
  • Loading branch information
hannoeru committed Jan 2, 2023
1 parent 41e121a commit f63249d
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 184 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"release": "pnpm bump && pnpm publish",
"bump": "pnpm dlx bumpp package.json packages/*/package.json -x 'pnpm changelog' --all",
"publish": "pnpm -r --filter './packages/*' publish --access public --no-git-checks",
"type-check": "tsc --noEmit"
"type-check": "pnpm -r type-check"
},
"devDependencies": {
"@hannoeru/eslint-config": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"scripts": {
"prepublishOnly": "pnpm run build",
"stub": "unbuild --stub",
"build": "unbuild"
"build": "unbuild",
"type-check": "tsc --noEmit"
},
"peerDependencies": {
"eslint": "^8.0.0"
Expand Down
27 changes: 23 additions & 4 deletions packages/eslint-plugin/src/configs/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { defineConfig } from '../utils'
import bestPracticeRules from './rules/best-practice'
import eslintComments from './rules/eslint-comments'

const ignorePatterns = [
'*.min.*',
Expand Down Expand Up @@ -72,7 +70,28 @@ export default defineConfig({
},
],

...eslintComments,
...bestPracticeRules,
// eslint-comments
// Require a eslint-enable comment for every eslint-disable comment
'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }],
// Disallow a eslint-enable comment for multiple eslint-disable comments
'eslint-comments/no-aggregating-enable': 'error',
// Disallow duplicate eslint-disable comments
'eslint-comments/no-duplicate-disable': 'error',
// Disallow eslint-disable comments without rule names
'eslint-comments/no-unlimited-disable': 'error',
// Disallow unused eslint-disable comments
'eslint-comments/no-unused-disable': 'error',
// Disallow unused eslint-enable comments
'eslint-comments/no-unused-enable': 'error',
// Disallow eslint-disable comments about specific rules
'eslint-comments/no-restricted-disable': 'off',
// Disallow ESLint directive-comments entirely
'eslint-comments/no-use': 'off',

// best-practice
'block-scoped-var': 'error',
'eqeqeq': ['error', 'smart'],
'no-alert': 'warn',
'vars-on-top': 'error',
},
})
26 changes: 23 additions & 3 deletions packages/eslint-plugin/src/configs/esnext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defineConfig } from '../utils'
import { resolverExtensions } from '../constants'
import unicornRules from './rules/unicorn'
import { defineConfig } from '../utils'

export default defineConfig({
env: {
Expand Down Expand Up @@ -110,6 +109,27 @@ export default defineConfig({
'promise/catch-or-return': 'off',

// unicorns
...unicornRules,
// Pass error message when throwing errors
'unicorn/error-message': 'error',
// Uppercase regex escapes
'unicorn/escape-case': 'error',
// Array.isArray instead of instanceof
'unicorn/no-instanceof-array': 'error',
// Prevent deprecated `new Buffer()`
'unicorn/no-new-buffer': 'error',
// Keep regex literals safe!
'unicorn/no-unsafe-regex': 'off',
// Lowercase number formatting for octal, hex, binary (0x1'error' instead of 0X1'error')
'unicorn/number-literal-case': 'error',
// includes over indexOf when checking for existence
'unicorn/prefer-includes': 'error',
// String methods startsWith/endsWith instead of more complicated stuff
'unicorn/prefer-starts-ends-with': 'error',
// textContent instead of innerText
'unicorn/prefer-text-content': 'error',
// Enforce throwing type error when throwing error while checking typeof
'unicorn/prefer-type-error': 'error',
// Use new when throwing error
'unicorn/throw-new-error': 'error',
},
})
109 changes: 107 additions & 2 deletions packages/eslint-plugin/src/configs/react.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineConfig } from '../utils'
import reactRules from './rules/react'

export default defineConfig({
overrides: [
Expand All @@ -24,7 +23,113 @@ export default defineConfig({
'react/prop-types': 'off',
'react/no-unknown-property': 'off',

...reactRules,
// Prevent usage of button elements without an explicit type attribute
'react/button-has-type': 'off',
// Prevent missing displayName in a React component definition
'react/display-name': ['error', { ignoreTranspilerName: false }],
// Prevent extraneous defaultProps on components
'react/default-props-match-prop-types': 'error',
// Forbid foreign propTypes; forbids using another component's prop types unless they are explicitly imported/exported
'react/forbid-foreign-prop-types': 'error',
// Forbid certain propTypes
'react/forbid-prop-types': ['error', { forbid: ['any', 'array'] }],
// Standardize the way function component get defined
'react/function-component-definition': [
'error',
{ namedComponents: 'arrow-function', unnamedComponents: 'arrow-function' },
],
// Enforce using <> instead of <React.Fragment> for fragments
'react/jsx-fragments': ['error', 'syntax'],
// Prevent using this.state within a this.setState
'react/no-access-state-in-setstate': 'error',
// Prevent using Array index in key prop
'react/no-array-index-key': 'error',
// Prevent usage of setState in componentDidUpdate
'react/no-did-update-set-state': 'error',
// Prevent usage of shouldComponentUpdate when extending React.PureComponent
'react/no-redundant-should-component-update': 'error',
// Prevent this from being used in stateless functional components
'react/no-this-in-sfc': 'error',
// Prevent common casing typos
'react/no-typos': 'error',
// Prevent usage of UNSAFE_ methods
'react/no-unsafe': ['error', { checkAliases: true }],
// Prevent definitions of unused prop types
'react/no-unused-prop-types': 'error',
// Attempts to discover all state fields in a React component and warn if any of them are never read.
'react/no-unused-state': 'error',
// Prevent usage of setState in componentWillUpdate
'react/no-will-update-set-state': 'error',
// Enforce ES5 or ES6 class for React Components
'react/prefer-es6-class': 'error',
// Enforce stateless React Components to be written as a pure function
'react/prefer-stateless-function': ['error', { ignorePureComponents: true }],
// Prevent extra closing tags for components without children
'react/self-closing-comp': 'error',
// Enforce state initialization style
'react/state-in-constructor': ['error', 'never'],
// Enforce style prop value being an object
'react/style-prop-object': 'error',
// Prevent void DOM elements (e.g. <img />, <br />) from receiving children
'react/void-dom-elements-no-children': 'error',

// JSX

// Enforce a new line after jsx elements and expressions
'react/jsx-newline': 'off',
// Prevent usage of javascript: in URLs
'react/jsx-no-script-url': 'error',
// Prevent react contexts from taking non-stable values
'react/jsx-no-constructed-context-values': 'error',
// Enforce boolean attributes notation in JSX
'react/jsx-boolean-value': 'error',
// Enforce or disallow spaces inside of curly braces in JSX attributes and expressions
'react/jsx-child-element-spacing': 'error',
// Validate closing bracket location in JSX
'react/jsx-closing-bracket-location': ['error', { location: 'tag-aligned' }],
// Validate closing tag location in JSX
'react/jsx-closing-tag-location': 'error',
// Enforce curly braces or disallow unnecessary curly braces in JSX props and/or children
'react/jsx-curly-brace-presence': ['error', { propElementValues: 'always' }],
// Enforce or disallow spaces inside of curly braces in JSX attributes
'react/jsx-curly-spacing': ['error', 'never', { allowMultiline: true }],
// Enforce or disallow spaces around equal signs in JSX attributes
'react/jsx-equals-spacing': ['error', 'never'],
// Restrict file extensions that may contain JSX
'react/jsx-filename-extension': ['error', { extensions: ['.jsx', '.tsx'] }],
// Validate props indentation in JSX
'react/jsx-indent-props': ['error', 2],
// Validate JSX indentation
'react/jsx-indent': ['error', 2],
'react/jsx-key': [
'error',
{
checkFragmentShorthand: true,
checkKeyMustBeforeSpread: true,
},
],
// Enforce position of the first prop in JSX
'react/jsx-first-prop-new-line': ['error', 'multiline'],
// Limit maximum of props on a single line in JSX
'react/jsx-max-props-per-line': ['error', { maximum: { single: 5, multi: 1 } }],
// Disallow unnecessary fragments
'react/jsx-no-useless-fragment': ['error', { allowExpressions: true }],
// Limits every line in JSX to one expression each
'react/jsx-one-expression-per-line': 'off',
// Enforce PascalCase for user-defined JSX components
'react/jsx-pascal-case': [
'error',
{
allowNamespace: true,
allowLeadingUnderscore: true,
},
],
// Disallow multiple spaces between inline JSX props (fixable)
'react/jsx-props-no-multi-spaces': 'error',
// Validate whitespace in and around the JSX opening and closing brackets
'react/jsx-tag-spacing': 'error',
// Prevent missing parentheses around multilines JSX
'react/jsx-wrap-multilines': 'error',
},
},
],
Expand Down
9 changes: 0 additions & 9 deletions packages/eslint-plugin/src/configs/rules/best-practice.ts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/eslint-plugin/src/configs/rules/eslint-comments.ts

This file was deleted.

111 changes: 0 additions & 111 deletions packages/eslint-plugin/src/configs/rules/react.ts

This file was deleted.

27 changes: 0 additions & 27 deletions packages/eslint-plugin/src/configs/rules/unicorn.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/eslint-plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ export function defineConfig(config: TSESLint.Linter.Config): TSESLint.Linter.Co
return config
}

export function defineRules(rules: TSESLint.Linter.Config['rules']): TSESLint.Linter.Config['rules'] {
return rules
}

export function definePlugin(plugin: TSESLint.Linter.Plugin): TSESLint.Linter.Plugin {
return plugin
}
Expand Down

0 comments on commit f63249d

Please sign in to comment.