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

Enable strict type checked rules #392

Merged
merged 47 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
06e5d59
Make usage of null/undefined more consistent.
wjhsf Nov 18, 2023
842e129
add explicit return type to canonicalDomain
wjhsf Mar 1, 2024
d21c0da
add note for weird parse behavior
wjhsf Mar 1, 2024
2dbd3fa
use @typescript-eslint/strict-type-checked
wjhsf Mar 13, 2024
f7a9c64
eslint auto fixes
wjhsf Mar 13, 2024
ce6bc76
remove unnecessary polyfill
wjhsf Mar 13, 2024
32684bc
manual fixes for strict rules
wjhsf Mar 13, 2024
c303233
Update lib/cookie/cookie.ts
wjhsf Mar 14, 2024
56b696a
Merge branch 'master' into strict-type-checked
wjhsf Mar 14, 2024
4ba4af0
Merge branch 'master' into nullable
wjhsf Mar 15, 2024
8a83cf8
Replace `Nullable<T>` with more precise `T | undefined`
wjhsf Mar 15, 2024
523ebb9
Merge branch 'master' into nullable
wjhsf Mar 15, 2024
681537d
add a bit of breathing room to the file size
wjhsf Mar 15, 2024
214dcb0
Change `parse` to only return undefined on failure, nut null | undefi…
wjhsf Mar 18, 2024
4d0b46e
Merge branch 'master' into nullable
wjhsf Mar 19, 2024
4a97015
standardize helpers on returning undefined
wjhsf Mar 19, 2024
8f614cd
update doc for return type
wjhsf Mar 19, 2024
2f97b58
Merge branch 'master' into strict-type-checked
wjhsf Mar 19, 2024
53b377c
Fix a few more errors.
wjhsf Mar 19, 2024
7b2aaaa
Merge branch 'nullable' into strict-type-checked
wjhsf Mar 19, 2024
f91576b
appease linter
wjhsf Mar 19, 2024
ca2be2f
change fromJSON to return undefined instead of null
wjhsf Mar 20, 2024
9a54e84
fix incorrect comparison
wjhsf Mar 20, 2024
3fd718e
change date helpers to avoid null, for consistency
wjhsf Mar 20, 2024
407233c
update fromJSON tests for new return type
wjhsf Mar 20, 2024
2a04015
Merge branch 'nullable' into strict-type-checked
wjhsf Mar 20, 2024
66d2ef1
change internal null value to undefined
wjhsf Mar 20, 2024
a9f72e7
add type annotations
wjhsf Mar 20, 2024
a9e3f41
move from .eslintrc to flat config
wjhsf Mar 21, 2024
e2eae4e
linter fixes for vows tests
wjhsf Mar 21, 2024
803d2c7
fixes for restrict-template-expressions
wjhsf Mar 21, 2024
4c941c9
Avoid type linting eslint.config.mjs
wjhsf Mar 21, 2024
94e82c8
Merge branch 'master' into strict-type-checked
wjhsf Mar 21, 2024
dd666b9
fix package-lock.json
wjhsf Mar 22, 2024
391de34
bump deps
wjhsf Mar 22, 2024
1bacc2c
fix package-lock.json again
wjhsf Mar 22, 2024
849bac8
restore missing dependency
wjhsf Mar 22, 2024
540102c
revert changes to test files
wjhsf Apr 22, 2024
7216e61
disable no-unused-vars lint rule
wjhsf Apr 22, 2024
1e26cb9
fix no-case-declarations violation
wjhsf Apr 22, 2024
62603a1
Merge branch 'master' into strict-type-checked
wjhsf Apr 22, 2024
76b0616
Merge branch 'master' into strict-type-checked
wjhsf May 2, 2024
b098c22
remove max-lines enforcement
wjhsf May 2, 2024
8920b03
fix types and disable false positive eslint errors
wjhsf May 2, 2024
eb873a1
fix package-lock.json
wjhsf May 2, 2024
6d24b89
don't need to disable a rule that's never enabled
wjhsf May 3, 2024
5451403
Merge branch 'master' into strict-type-checked
wjhsf May 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

19 changes: 0 additions & 19 deletions .eslintrc.json

This file was deleted.

41 changes: 41 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-check

import eslint from '@eslint/js'
import prettierRecommended from 'eslint-plugin-prettier/recommended'
import tseslint from 'typescript-eslint'
import globals from 'globals'

export default tseslint.config(
{
ignores: ['dist', 'jest.config.ts'],
},
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
prettierRecommended,
{
languageOptions: {
globals: globals.node,
parserOptions: {
project: './tsconfig.json',
tsconfigDirName: import.meta.dirname,
},
},
linterOptions: {
reportUnusedDisableDirectives: 'warn',
},
rules: {
'@typescript-eslint/explicit-function-return-type': 'error',
},
},
{
// Once we remove the legacy vows tests in ./test, we can remove these JS-specific rules
files: ['test/**/*.js', 'eslint.config.mjs'],
...tseslint.configs.disableTypeChecked,
rules: {
...tseslint.configs.disableTypeChecked.rules,
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-unused-vars': 'off',
},
},
)
24 changes: 14 additions & 10 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,9 @@ describe('CookieJar', () => {
'should remove all the stored cookies',
{
callbackStyle(done) {
cookieJar.removeAllCookies(() => done())
cookieJar.removeAllCookies(() => {
done()
})
},
async asyncStyle() {
await cookieJar.removeAllCookies()
Expand Down Expand Up @@ -1348,7 +1350,7 @@ describe.each(['local', 'example', 'invalid', 'localhost', 'test'])(
expect.objectContaining({
key: 'settingThisShouldPass',
value: 'true',
domain: `${specialUseDomain}`,
domain: specialUseDomain,
}),
)
const cookies = await cookieJar.getCookies(
Expand Down Expand Up @@ -1393,7 +1395,7 @@ describe.each(['local', 'example', 'invalid', 'localhost', 'test'])(
expect.objectContaining({
key: 'settingThisShouldPass',
value: 'true',
domain: `${specialUseDomain}`,
domain: specialUseDomain,
}),
)
const cookies = await cookieJar.getCookies(
Expand Down Expand Up @@ -1480,9 +1482,9 @@ describe('Synchronous API on async CookieJar', () => {

it('should throw an error when calling `removeAllCookiesSync` if store is not synchronous', () => {
const cookieJar = new CookieJar(store)
expect(() => cookieJar.removeAllCookiesSync()).toThrow(
'CookieJar store is not synchronous; use async API instead.',
)
expect(() => {
cookieJar.removeAllCookiesSync()
}).toThrow('CookieJar store is not synchronous; use async API instead.')
})
})

Expand All @@ -1496,7 +1498,7 @@ function createCookie(
if (!cookie) {
throw new Error('This should not be undefined')
}
if (options?.hostOnly) {
if (options.hostOnly) {
cookie.hostOnly = options.hostOnly
}
return cookie
Expand All @@ -1508,9 +1510,11 @@ function apiVariants(
assertions: () => void,
): void {
it(`${testName} (callback)`, async () => {
await new Promise((resolve) =>
apiVariants.callbackStyle(() => resolve(undefined)),
)
await new Promise((resolve) => {
apiVariants.callbackStyle(() => {
resolve(undefined)
})
})
assertions()
})

Expand Down
1 change: 0 additions & 1 deletion lib/__tests__/data/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// This file just contains test data, so we don't care about the number of lines.
export default [
{
test: '0001',
Expand Down
17 changes: 4 additions & 13 deletions lib/__tests__/jarSerialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ function expectDataToMatchSerializationSchema(
expect(serializedJar.storeType).toBe('MemoryCookieStore')
expect(serializedJar.rejectPublicSuffixes).toBe(true)
expect(serializedJar.cookies).toBeInstanceOf(Array)
serializedJar.cookies.forEach((cookie) => validateSerializedCookie(cookie))
serializedJar.cookies.forEach((cookie) => {
validateSerializedCookie(cookie)
})
}

const serializedCookiePropTypes: { [key: string]: string } = {
Expand Down Expand Up @@ -345,7 +347,7 @@ function validateSerializedCookie(cookie: SerializedCookie): void {

case 'intOrInf':
if (cookie[prop] !== 'Infinity' && cookie[prop] !== '-Infinity') {
expect(isInteger(cookie[prop])).toBe(true)
expect(Number.isInteger(cookie[prop])).toBe(true)
}
break

Expand All @@ -361,14 +363,3 @@ function validateSerializedCookie(cookie: SerializedCookie): void {
}
})
}

function isInteger(value: unknown): boolean {
if (Number.isInteger) {
return Number.isInteger(value)
}
// Node 0.10 (still supported) doesn't have Number.isInteger
// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
return (
typeof value === 'number' && isFinite(value) && Math.floor(value) === value
)
}
55 changes: 32 additions & 23 deletions lib/__tests__/removeAll.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ describe('store removeAllCookies API', () => {
// replace remove cookie behavior to throw an error on the 4th invocation
const _removeCookie = store.removeCookie.bind(store)
const spy = jest.spyOn(store, 'removeCookie')
spy.mockImplementationOnce((domain, path, key, callback) =>
_removeCookie.call(store, domain, path, key, callback),
)
spy.mockImplementationOnce((domain, path, key, callback) =>
_removeCookie.call(store, domain, path, key, callback),
)
spy.mockImplementationOnce((domain, path, key, callback) =>
_removeCookie.call(store, domain, path, key, callback),
)
spy.mockImplementationOnce((_domain, _path, _key, callback) =>
callback(new Error('something happened 4')),
)
spy.mockImplementationOnce((domain, path, key, callback) => {
_removeCookie.call(store, domain, path, key, callback)
})
spy.mockImplementationOnce((domain, path, key, callback) => {
_removeCookie.call(store, domain, path, key, callback)
})
spy.mockImplementationOnce((domain, path, key, callback) => {
_removeCookie.call(store, domain, path, key, callback)
})
spy.mockImplementationOnce((_domain, _path, _key, callback) => {
callback(new Error('something happened 4'))
})

await expect(jar.removeAllCookies()).rejects.toThrow(
'something happened 4',
Expand All @@ -71,11 +71,12 @@ describe('store removeAllCookies API', () => {
const spy = jest.spyOn(store, 'removeCookie')
spy.mockImplementation((domain, path, key, callback) => {
if (spy.mock.calls.length % 2 === 1) {
return callback(
new Error(`something happened ${spy.mock.calls.length}`),
callback(
new Error(`something happened ${String(spy.mock.calls.length)}`),
)
return
}
return _removeCookie.call(store, domain, path, key, callback)
_removeCookie.call(store, domain, path, key, callback)
})

await expect(jar.removeAllCookies()).rejects.toThrowError(
Expand Down Expand Up @@ -143,7 +144,8 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(null, undefined)
callback(null, undefined)
return
}

override findCookies(
Expand All @@ -166,7 +168,8 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(null, [])
callback(null, [])
return
}

override putCookie(cookie: Cookie): Promise<void>
Expand All @@ -177,7 +180,8 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(null)
callback(null)
return
}

override getAllCookies(): Promise<Cookie[]>
Expand All @@ -187,7 +191,8 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(null, this.cookies.slice())
callback(null, this.cookies.slice())
return
}

override removeCookie(
Expand All @@ -211,7 +216,8 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(null)
callback(null)
return
}
}

Expand All @@ -234,7 +240,8 @@ class MemoryStoreExtension extends MemoryCookieStore {
if (!callback) {
throw new Error('This should not be undefined')
}
return super.getAllCookies(callback)
super.getAllCookies(callback)
return
}

override removeCookie(
Expand All @@ -258,7 +265,8 @@ class MemoryStoreExtension extends MemoryCookieStore {
if (!callback) {
throw new Error('This should not be undefined')
}
return super.removeCookie(domain, path, key, callback)
super.removeCookie(domain, path, key, callback)
return
}

override removeAllCookies(): Promise<void>
Expand All @@ -268,6 +276,7 @@ class MemoryStoreExtension extends MemoryCookieStore {
if (!callback) {
throw new Error('This should not be undefined')
}
return super.removeAllCookies(callback)
super.removeAllCookies(callback)
return
}
}
12 changes: 6 additions & 6 deletions lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function parse(str: string, options?: ParseCookieOptions): Cookie | undefined {
continue
}
const av_sep = av.indexOf('=')
let av_key, av_value
let av_key: string, av_value: string | null

if (av_sep === -1) {
av_key = av
Expand Down Expand Up @@ -551,11 +551,11 @@ export class Cookie {
const hostOnly = this.hostOnly != null ? this.hostOnly.toString() : '?'
const createAge =
this.creation && this.creation !== 'Infinity'
? `${now - this.creation.getTime()}ms`
? `${String(now - this.creation.getTime())}ms`
: '?'
const accessAge =
this.lastAccessed && this.lastAccessed !== 'Infinity'
? `${now - this.lastAccessed.getTime()}ms`
? `${String(now - this.lastAccessed.getTime())}ms`
: '?'
return `Cookie="${this.toString()}; hostOnly=${hostOnly}; aAge=${accessAge}; cAge=${createAge}"`
}
Expand Down Expand Up @@ -657,7 +657,7 @@ export class Cookie {
* @beta
*/
validate(): boolean {
if (this.value == null || !COOKIE_OCTETS.test(this.value)) {
if (!this.value || !COOKIE_OCTETS.test(this.value)) {
return false
}
if (
Expand Down Expand Up @@ -732,7 +732,7 @@ export class Cookie {
* @public
*/
cookieString(): string {
const val = this.value ?? ''
const val = this.value || ''
if (this.key) {
return `${this.key}=${val}`
}
Expand All @@ -753,7 +753,7 @@ export class Cookie {
}

if (this.maxAge != null && this.maxAge != Infinity) {
str += `; Max-Age=${this.maxAge}`
str += `; Max-Age=${String(this.maxAge)}`
}

if (this.domain && !this.hostOnly) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cookie/cookieCompare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function cookieCompare(a: Cookie, b: Cookie): number {
}

// break ties for the same millisecond (precision of JavaScript's clock)
cmp = (a.creationIndex ?? 0) - (b.creationIndex ?? 0)
cmp = (a.creationIndex || 0) - (b.creationIndex || 0)

return cmp
}