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

style: Improve eslint setup #109

Merged
merged 1 commit into from
Aug 31, 2023
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
27 changes: 24 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/* global module */
const extend = [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react/recommended',
'plugin:react-hooks/recommended',
'prettier',
]

const plugins = ['prettier', 'react', '@typescript-eslint', 'eslint-plugin-react-hooks', 'eslint-plugin-jest']

module.exports = {
ignorePatterns: ['node_modules', 'examples', 'lib'],
env: {
Expand All @@ -10,10 +19,11 @@ module.exports = {
version: 'detect',
},
},
extends: ['plugin:@typescript-eslint/recommended', 'plugin:react/recommended', 'prettier'],
extends: extend,
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
module: 'readonly',
},
parser: '@typescript-eslint/parser',
parserOptions: {
Expand All @@ -23,7 +33,7 @@ module.exports = {
ecmaVersion: 2018,
sourceType: 'module',
},
plugins: ['prettier', 'react', '@typescript-eslint'],
plugins,
rules: {
'react/prop-types': [0],
'react/no-unescaped-entities': [0],
Expand Down Expand Up @@ -61,6 +71,8 @@ module.exports = {
],
},
],
'no-empty': 'off',
'no-constant-condition': 'off',
},
overrides: [
{
Expand Down Expand Up @@ -96,6 +108,15 @@ module.exports = {
'@typescript-eslint/no-var-requires': 'off',
},
},
{
files: ['*.test.ts', '*.test.tsx', '*.spec.ts', '*.spec.tsx'],
env: {
jest: true,
node: true,
browser: true,
es6: true,
},
},
],
reportUnusedDisableDirectives: true,
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
"babel-jest": "^28.1.3",
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.2.3",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"husky": "^8.0.1",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
Expand Down
2 changes: 1 addition & 1 deletion posthog-core/test/posthog.capture.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('PostHog Core', () => {

expect(mocks.fetch).toHaveBeenCalledTimes(1)
const [url, options] = mocks.fetch.mock.calls[0]
expect(url).toMatch(/^https:\/\/app\.posthog\.com\/e\/\?ip=1&_=[0-9]+&v=[0-9\.a-z\-]+$/)
expect(url).toMatch(/^https:\/\/app\.posthog\.com\/e\/\?ip=1&_=[0-9]+&v=[0-9.a-z-]+$/)
expect(options.method).toBe('POST')
const body = parseBody(mocks.fetch.mock.calls[0])

Expand Down
5 changes: 2 additions & 3 deletions posthog-node/src/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,6 @@ class FeatureFlagsPoller {

try {
return await this.fetch(url, options)
} catch (err) {
throw err
} finally {
clearTimeout(abortTimeout)
}
Expand Down Expand Up @@ -487,13 +485,14 @@ function matchProperty(
case 'lte':
return typeof overrideValue == typeof value && overrideValue <= value
case 'is_date_after':
case 'is_date_before':
case 'is_date_before': {
const parsedDate = convertToDateTime(value)
const overrideDate = convertToDateTime(overrideValue)
if (operator === 'is_date_before') {
return overrideDate < parsedDate
}
return overrideDate > parsedDate
}
default:
console.error(`Unknown operator: ${operator}`)
return false
Expand Down
6 changes: 2 additions & 4 deletions posthog-react-native/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ module.exports = {
roots: ['<rootDir>'],
testEnvironment: 'node',
transform: {
'^.+\\.ts?$': 'ts-jest',
'^.+\\.ts$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'js', 'json', 'node', 'tsx'],
collectCoverage: true,
clearMocks: true,
coverageDirectory: 'coverage',
testPathIgnorePatterns: ['node_modules', 'examples'],
testPathIgnorePatterns: ['<rootDir>/lib/', 'node_modules', 'examples'],
fakeTimers: { enableGlobally: true },
transformIgnorePatterns: [],

testPathIgnorePatterns: ['<rootDir>/lib/', '/node_modules/'],
}
1 change: 1 addition & 0 deletions posthog-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@babel/cli": "^7.19.3",
"@react-native-async-storage/async-storage": "^1.17.10",
"@react-navigation/native": "^5.0.10",
"@types/jest": "^28.1.5",
"@types/react-native": "^0.69.1",
"expo": "^45.0.6",
"expo-application": "^4.0.0",
Expand Down
11 changes: 7 additions & 4 deletions posthog-react-native/src/PostHogProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react'
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import { GestureResponderEvent, StyleProp, View, ViewStyle } from 'react-native'
import { PostHog, PostHogOptions } from './posthog-rn'
import { autocaptureFromTouchEvent } from './autocapture'
Expand Down Expand Up @@ -39,7 +39,10 @@ export const PostHogProvider = ({
// Check if the client is a promise and resolve it if so
const [posthog, setPosthog] = useState<PostHog | undefined>(client instanceof Promise ? undefined : client)

const autocaptureOptions = autocapture && typeof autocapture !== 'boolean' ? autocapture : {}
const autocaptureOptions = useMemo(
() => (autocapture && typeof autocapture !== 'boolean' ? autocapture : {}),
[autocapture]
)
const captureAll = autocapture === true
const captureNone = autocapture === false

Expand All @@ -65,7 +68,7 @@ export const PostHogProvider = ({
} else if (!posthog && apiKey) {
PostHog.initAsync(apiKey, options).then(setPosthog)
}
}, [client, apiKey])
}, [client, apiKey, posthog, options])

useEffect(() => {
if (!posthog) {
Expand All @@ -86,7 +89,7 @@ export const PostHogProvider = ({
autocaptureFromTouchEvent(e, posthog, autocaptureOptions)
}
},
[posthog, autocapture]
[captureTouches, posthog, autocaptureOptions]
)

return (
Expand Down
12 changes: 6 additions & 6 deletions posthog-react-native/src/hooks/useNavigationTracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react'
import { useCallback, useEffect } from 'react'
import { OptionalReactNativeNavigation } from '../optional/OptionalReactNativeNavigation'
import type { PostHog } from '../posthog-rn'
import { PostHogAutocaptureNavigationTrackerOptions } from '../types'
Expand All @@ -13,14 +13,14 @@ function _useNavigationTracker(options?: PostHogAutocaptureNavigationTrackerOpti
const posthog = client || contextClient

if (!OptionalReactNativeNavigation) {
// NOTE: This is taken care of by the export but we keep this here for TS
return
// NOTE: This is taken care of by the export, but we keep this here for TS
throw new Error('No OptionalReactNativeNavigation')
}

const routes = OptionalReactNativeNavigation.useNavigationState((state) => state?.routes)
const navigation = OptionalReactNativeNavigation.useNavigation()

const trackRoute = (): void => {
const trackRoute = useCallback((): void => {
if (!posthog) {
return
}
Expand All @@ -45,7 +45,7 @@ function _useNavigationTracker(options?: PostHogAutocaptureNavigationTrackerOpti
const properties = options?.routeToProperties?.(currentRouteName, params)
posthog.screen(currentRouteName, properties)
}
}
}, [navigation, options, posthog])

useEffect(() => {
// NOTE: The navigation stacks may not be fully rendered initially. This means the first route can be missed (it doesn't update useNavigationState)
Expand All @@ -55,7 +55,7 @@ function _useNavigationTracker(options?: PostHogAutocaptureNavigationTrackerOpti
return
}
trackRoute()
}, [routes])
}, [routes, trackRoute])
}

export const useNavigationTracker = OptionalReactNativeNavigation
Expand Down
72 changes: 72 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,13 @@
dependencies:
"@jridgewell/trace-mapping" "0.3.9"

"@eslint-community/eslint-utils@^4.2.0":
version "4.4.0"
resolved "https://registry.yarnpkg.com/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz#a23514e8fb9af1269d5f7788aa556798d61c6b59"
integrity sha512-1/sA4dwrzBAyeUoQ6oxahHKmrZvsnLCg4RfxW3ZFGGmQkSNQPFNLV9CUEFQP1x9EYXHTo5p6xdhZM1Ne9p/AfA==
dependencies:
eslint-visitor-keys "^3.3.0"

"@eslint/eslintrc@^1.3.0":
version "1.3.0"
resolved "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.3.0.tgz"
Expand Down Expand Up @@ -2542,6 +2549,11 @@
resolved "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.2.tgz"
integrity sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==

"@types/semver@^7.3.12":
version "7.5.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.0.tgz#591c1ce3a702c45ee15f47a42ade72c2fd78978a"
integrity sha512-G8hZ6XJiHnuhQKR7ZmysCeJWE08o8T0AXtk5darsCaTVsYZhhgUrq53jizaR2FvsoeCwJhlmwTjkXBY5Pn/ZHw==

"@types/stack-utils@^2.0.0":
version "2.0.1"
resolved "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.1.tgz"
Expand Down Expand Up @@ -2611,6 +2623,14 @@
"@typescript-eslint/types" "5.30.7"
"@typescript-eslint/visitor-keys" "5.30.7"

"@typescript-eslint/[email protected]":
version "5.62.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.62.0.tgz#d9457ccc6a0b8d6b37d0eb252a23022478c5460c"
integrity sha512-VXuvVvZeQCQb5Zgf4HAxc04q5j+WrNAtNh9OwCsCgpKqESMTu3tF/jhZ3xG6T4NZwWl65Bg8KuS2uEvhSfLl0w==
dependencies:
"@typescript-eslint/types" "5.62.0"
"@typescript-eslint/visitor-keys" "5.62.0"

"@typescript-eslint/[email protected]":
version "5.30.7"
resolved "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.30.7.tgz"
Expand All @@ -2625,6 +2645,11 @@
resolved "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.30.7.tgz"
integrity sha512-ocVkETUs82+U+HowkovV6uxf1AnVRKCmDRNUBUUo46/5SQv1owC/EBFkiu4MOHeZqhKz2ktZ3kvJJ1uFqQ8QPg==

"@typescript-eslint/[email protected]":
version "5.62.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.62.0.tgz#258607e60effa309f067608931c3df6fed41fd2f"
integrity sha512-87NVngcbVXUahrRTqIK27gD2t5Cu1yuCXxbLcFtCzZGlfyVWWh8mLHkoxzjsB6DDNnvdL+fW8MiwPEJyGJQDgQ==

"@typescript-eslint/[email protected]":
version "5.30.7"
resolved "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.30.7.tgz"
Expand All @@ -2638,6 +2663,19 @@
semver "^7.3.7"
tsutils "^3.21.0"

"@typescript-eslint/[email protected]":
version "5.62.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.62.0.tgz#7d17794b77fabcac615d6a48fb143330d962eb9b"
integrity sha512-CmcQ6uY7b9y694lKdRB8FEel7JbU/40iSAPomu++SjLMntB+2Leay2LO6i8VnJk58MtE9/nQSFIH6jpyRWyYzA==
dependencies:
"@typescript-eslint/types" "5.62.0"
"@typescript-eslint/visitor-keys" "5.62.0"
debug "^4.3.4"
globby "^11.1.0"
is-glob "^4.0.3"
semver "^7.3.7"
tsutils "^3.21.0"

"@typescript-eslint/[email protected]":
version "5.30.7"
resolved "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.30.7.tgz"
Expand All @@ -2650,6 +2688,20 @@
eslint-scope "^5.1.1"
eslint-utils "^3.0.0"

"@typescript-eslint/utils@^5.10.0":
version "5.62.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.62.0.tgz#141e809c71636e4a75daa39faed2fb5f4b10df86"
integrity sha512-n8oxjeb5aIbPFEtmQxQYOLI0i9n5ySBEY/ZEHHZqKQSFnxio1rv6dthascc9dLuwrL0RC5mPCxB7vnAVGAYWAQ==
dependencies:
"@eslint-community/eslint-utils" "^4.2.0"
"@types/json-schema" "^7.0.9"
"@types/semver" "^7.3.12"
"@typescript-eslint/scope-manager" "5.62.0"
"@typescript-eslint/types" "5.62.0"
"@typescript-eslint/typescript-estree" "5.62.0"
eslint-scope "^5.1.1"
semver "^7.3.7"

"@typescript-eslint/[email protected]":
version "5.30.7"
resolved "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.30.7.tgz"
Expand All @@ -2658,6 +2710,14 @@
"@typescript-eslint/types" "5.30.7"
eslint-visitor-keys "^3.3.0"

"@typescript-eslint/[email protected]":
version "5.62.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.62.0.tgz#2174011917ce582875954ffe2f6912d5931e353e"
integrity sha512-07ny+LHRzQXepkGg6w0mFY41fVUNBrL2Roj/++7V1txKugfjm/Ci/qSND03r2RhlJhJYMcTn9AhhSSqQp0Ysyw==
dependencies:
"@typescript-eslint/types" "5.62.0"
eslint-visitor-keys "^3.3.0"

"@urql/[email protected]":
version "2.3.6"
resolved "https://registry.npmjs.org/@urql/core/-/core-2.3.6.tgz"
Expand Down Expand Up @@ -4252,13 +4312,25 @@ eslint-config-prettier@^8.5.0:
resolved "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.5.0.tgz"
integrity sha512-obmWKLUNCnhtQRKc+tmnYuQl0pFU1ibYJQ5BGhTVB08bHe9wC8qUeG7c08dj9XX+AuPj1YSGSQIHl1pnDHZR0Q==

eslint-plugin-jest@^27.2.3:
version "27.2.3"
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-27.2.3.tgz#6f8a4bb2ca82c0c5d481d1b3be256ab001f5a3ec"
integrity sha512-sRLlSCpICzWuje66Gl9zvdF6mwD5X86I4u55hJyFBsxYOsBCmT5+kSUjf+fkFWVMMgpzNEupjW8WzUqi83hJAQ==
dependencies:
"@typescript-eslint/utils" "^5.10.0"

eslint-plugin-prettier@^4.2.1:
version "4.2.1"
resolved "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-4.2.1.tgz"
integrity sha512-f/0rXLXUt0oFYs8ra4w49wYZBG5GKZpAYsJSm6rnYL5uVDjd+zowwMwVZHnAjf4edNrKpCDYfXDgmRE/Ak7QyQ==
dependencies:
prettier-linter-helpers "^1.0.0"

eslint-plugin-react-hooks@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.0.tgz#4c3e697ad95b77e93f8646aaa1630c1ba607edd3"
integrity sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==

eslint-plugin-react@^7.30.1:
version "7.30.1"
resolved "https://registry.npmjs.org/eslint-plugin-react/-/eslint-plugin-react-7.30.1.tgz"
Expand Down
Loading