Skip to content

Commit

Permalink
chore: update eslintrc and use react-jsx (pmndrs#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Aug 15, 2021
1 parent 0960822 commit e47ea03
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 58 deletions.
80 changes: 60 additions & 20 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,74 @@
"plugin:import/errors",
"plugin:import/warnings"
],
"plugins": ["@typescript-eslint", "react", "prettier", "react-hooks", "import", "jest"],
"plugins": [
"@typescript-eslint",
"react",
"prettier",
"react-hooks",
"import",
"jest"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
},
"rules": {
"curly": ["warn", "multi-line", "consistent"],
"no-console": "off",
"no-empty-pattern": "warn",
"no-duplicate-imports": "error",
"import/no-unresolved": ["error", { "commonjs": true, "amd": true }],
"import/export": "error",
"import/named": "off",
"import/namespace": "off",
"import/default": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"no-unused-vars": ["warn", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }],
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }],
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "off",
"jest/consistent-test-it": ["error", { "fn": "it", "withinDescribe": "it" }]
}
},
"rules": {
"curly": ["warn", "multi-line", "consistent"],
"no-console": "off",
"import/no-unresolved": ["error", { "commonjs": true, "amd": true }],
"import/export": "error",
"@typescript-eslint/no-duplicate-imports": ["error"],
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{ "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }
],
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "off",
"jest/consistent-test-it": [
"error",
{ "fn": "it", "withinDescribe": "it" }
],
"import/order": [
"error",
{
"alphabetize": { "order": "asc", "caseInsensitive": true },
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
"object"
],
"newlines-between": "never",
"pathGroups": [
{
"pattern": "react",
"group": "builtin",
"position": "before"
}
],
"pathGroupsExcludedImportTypes": ["builtin"]
}
],
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off",
"sort-imports": [
"error",
{
"ignoreDeclarationSort": true
}
]
},
"settings": {
"react": {
"version": "detect"
Expand Down
7 changes: 6 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ module.exports = (api, targets) => {
],
],
plugins: [
'@babel/plugin-transform-react-jsx',
[
'@babel/plugin-transform-react-jsx',
{
runtime: 'automatic',
},
],
['@babel/plugin-transform-typescript', { isTSX: true }],
],
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import createImpl, {
State,
StateCreator,
StateSelector,
Subscribe,
StoreApi,
Subscribe,
} from './vanilla'
export * from './vanilla'

Expand Down
31 changes: 17 additions & 14 deletions tests/basic.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from 'react'
import ReactDOM from 'react-dom'
import {
Component as ClassComponent,
useEffect,
useLayoutEffect,
useState,
} from 'react'
import { act, fireEvent, render } from '@testing-library/react'
import create, { StateSelector, EqualityChecker, SetState } from '../src/index'
import ReactDOM from 'react-dom'
import create, { EqualityChecker, SetState, StateSelector } from '../src/index'

it('creates a store hook and api object', () => {
let params
Expand Down Expand Up @@ -39,7 +44,7 @@ it('uses the store with no args', async () => {

function Counter() {
const { count, inc } = useStore()
React.useEffect(inc, [inc])
useEffect(inc, [inc])
return <div>count: {count}</div>
}

Expand All @@ -57,7 +62,7 @@ it('uses the store with selectors', async () => {
function Counter() {
const count = useStore((s) => s.count)
const inc = useStore((s) => s.inc)
React.useEffect(inc, [inc])
useEffect(inc, [inc])
return <div>count: {count}</div>
}

Expand Down Expand Up @@ -137,7 +142,7 @@ it('re-renders with useLayoutEffect', async () => {

function Component() {
const { state } = useStore()
React.useLayoutEffect(() => {
useLayoutEffect(() => {
useStore.setState({ state: true })
}, [])
return <>{`${state}`}</>
Expand All @@ -157,7 +162,7 @@ it('can batch updates', async () => {

function Counter() {
const { count, inc } = useStore()
React.useEffect(() => {
useEffect(() => {
ReactDOM.unstable_batchedUpdates(() => {
inc()
inc()
Expand Down Expand Up @@ -279,7 +284,7 @@ it('can throw an error in selector', async () => {
// @ts-expect-error This function is supposed to throw an error
s.value.toUpperCase()

class ErrorBoundary extends React.Component<{}, { hasError: boolean }> {
class ErrorBoundary extends ClassComponent<{}, { hasError: boolean }> {
constructor(props: {}) {
super(props)
this.state = { hasError: false }
Expand Down Expand Up @@ -323,7 +328,7 @@ it('can throw an error in equality checker', async () => {
// @ts-expect-error This function is supposed to throw an error
a.value.trim() === b.value?.trim()

class ErrorBoundary extends React.Component<{}, { hasError: boolean }> {
class ErrorBoundary extends ClassComponent<{}, { hasError: boolean }> {
constructor(props: {}) {
super(props)
this.state = { hasError: false }
Expand Down Expand Up @@ -514,15 +519,13 @@ it('ensures the correct subscriber is removed on unmount', async () => {
}

function CountWithInitialIncrement() {
React.useLayoutEffect(increment, [])
useLayoutEffect(increment, [])
return <Count />
}

function Component() {
const [Counter, setCounter] = React.useState(
() => CountWithInitialIncrement
)
React.useLayoutEffect(() => {
const [Counter, setCounter] = useState(() => CountWithInitialIncrement)
useLayoutEffect(() => {
setCounter(() => Count)
}, [])
return (
Expand Down
18 changes: 9 additions & 9 deletions tests/context.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Component as ClassComponent, useEffect, useState } from 'react'
import { render } from '@testing-library/react'
import create from '../src/index'
import createContext from '../src/context'
import create from '../src/index'

type CounterState = {
count: number
Expand All @@ -19,7 +19,7 @@ it('creates and uses context store', async () => {

function Counter() {
const { count, inc } = useStore()
React.useEffect(inc, [inc])
useEffect(inc, [inc])
return <div>count: {count * 1}</div>
}

Expand All @@ -44,7 +44,7 @@ it('uses context store with selectors', async () => {
function Counter() {
const count = useStore((state) => state.count)
const inc = useStore((state) => state.inc)
React.useEffect(inc, [inc])
useEffect(inc, [inc])
return <div>count: {count * 1}</div>
}

Expand All @@ -68,19 +68,19 @@ it('uses context store api', async () => {

function Counter() {
const storeApi = useStoreApi()
const [count, setCount] = React.useState(0)
React.useEffect(
const [count, setCount] = useState(0)
useEffect(
() =>
storeApi.subscribe(
() => setCount(storeApi.getState().count),
(state) => state.count
),
[storeApi]
)
React.useEffect(() => {
useEffect(() => {
storeApi.setState({ count: storeApi.getState().count + 1 })
}, [storeApi])
React.useEffect(() => {
useEffect(() => {
if (count === 1) {
storeApi.destroy()
storeApi.setState({ count: storeApi.getState().count + 1 })
Expand All @@ -101,7 +101,7 @@ it('uses context store api', async () => {
it('throws error when not using provider', async () => {
console.error = jest.fn()

class ErrorBoundary extends React.Component<{}, { hasError: boolean }> {
class ErrorBoundary extends ClassComponent<{}, { hasError: boolean }> {
constructor(props: {}) {
super(props)
this.state = { hasError: false }
Expand Down
9 changes: 8 additions & 1 deletion tests/middlewareTypes.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { produce } from 'immer'
import type { Draft } from 'immer'
import create, { UseStore } from '../src'
import { NamedSet, devtools, persist } from '../src/middleware'
import { State, StateCreator } from '../src/vanilla'
import { devtools, NamedSet, persist } from '../src/middleware'

type TImmerConfigFn<T extends State> = (fn: (draft: Draft<T>) => void) => void
type TImmerConfig<T extends State> = StateCreator<T, TImmerConfigFn<T>>
Expand Down Expand Up @@ -66,6 +66,7 @@ it('should have correct type when creating store with devtool', () => {

return <></>
}
TestComponent
})

it('should have correct type when creating store with devtool and immer', () => {
Expand Down Expand Up @@ -94,6 +95,7 @@ it('should have correct type when creating store with devtool and immer', () =>

return <></>
}
TestComponent
})

it('should have correct type when creating store with devtool and persist', () => {
Expand Down Expand Up @@ -126,6 +128,7 @@ it('should have correct type when creating store with devtool and persist', () =

return <></>
}
TestComponent
})

it('should have correct type when creating store without middleware', () => {
Expand All @@ -144,6 +147,7 @@ it('should have correct type when creating store without middleware', () => {

return <></>
}
TestComponent
})

it('should have correct type when creating store with persist', () => {
Expand Down Expand Up @@ -174,6 +178,7 @@ it('should have correct type when creating store with persist', () => {

return <></>
}
TestComponent
})

it('should have correct type when creating store with immer', () => {
Expand All @@ -198,6 +203,7 @@ it('should have correct type when creating store with immer', () => {

return <></>
}
TestComponent
})

it('should have correct type when creating store with devtool, persist and immer', () => {
Expand Down Expand Up @@ -231,4 +237,5 @@ it('should have correct type when creating store with devtool, persist and immer

return <></>
}
TestComponent
})
5 changes: 2 additions & 3 deletions tests/persistAsync.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import { act, cleanup, render } from '@testing-library/react'
import { act, render } from '@testing-library/react'
import create from '../src/index'
import { persist } from '../src/middleware'

Expand Down Expand Up @@ -211,7 +210,7 @@ describe('persist middleware with async configuration', () => {
state: { count: 42 },
version: 12,
}),
setItem: (_: string, value: string) => {},
setItem: (_: string, _value: string) => {},
}

const useStore = create(
Expand Down
2 changes: 1 addition & 1 deletion tests/persistSync.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('persist middleware with sync configuration', () => {
state: { count: 42 },
version: 12,
}),
setItem: (_: string, value: string) => {},
setItem: (_: string, _value: string) => {},
}

const useStore = create(
Expand Down
14 changes: 7 additions & 7 deletions tests/types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import create, {
Destroy,
EqualityChecker,
GetState,
PartialState,
SetState,
State,
StateCreator,
StateListener,
StateSelector,
PartialState,
EqualityChecker,
StateCreator,
SetState,
GetState,
StoreApi,
Subscribe,
Destroy,
UseStore,
StoreApi,
} from '../src/index'

it('can use exposed types', () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "esnext",
"strict": true,
"jsx": "preserve",
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"moduleResolution": "node",
Expand Down

0 comments on commit e47ea03

Please sign in to comment.