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

Remove some @ts-ignore directives. #288

Merged
merged 1 commit into from
Jul 6, 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
26 changes: 10 additions & 16 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ describe('CookieJar', () => {
})

it('should set a timestamp when storing or retrieving a cookie', async () => {
// @ts-ignore
cookie = Cookie.parse('a=b; Domain=example.com; Path=/')
// We know that we're passing a valid cookie, so we can use the non-null assertion
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
cookie = Cookie.parse('a=b; Domain=example.com; Path=/')!
const t0 = new Date()

expect(cookie).toEqual(
Expand Down Expand Up @@ -1007,7 +1008,6 @@ describe('CookieJar', () => {
})

it('should remove all from matching domain', async () => {
// @ts-ignore
await cookieJar.store.removeCookies('example.com', null)

const exampleCookies = await cookieJar.store.findCookies(
Expand Down Expand Up @@ -1145,11 +1145,8 @@ describe('loose mode', () => {
it('should fix issue #132', async () => {
const cookieJar = new CookieJar()
await expect(
cookieJar.setCookie(
// @ts-ignore
{ key: 'x', value: 'y' },
'http://example.com/',
),
// @ts-expect-error test case is explicitly testing invalid input
colincasey marked this conversation as resolved.
Show resolved Hide resolved
cookieJar.setCookie({ key: 'x', value: 'y' }, 'http://example.com/'),
).rejects.toThrowError(
'First argument to setCookie must be a Cookie object or string',
)
Expand Down Expand Up @@ -1177,15 +1174,12 @@ it('should fix issue #144', async () => {
])
})

it('should fix issue #145 - missing 2nd url parameter', async () => {
expect.assertions(1)
it('should fix issue #145 - missing 2nd url parameter', () => {
const cookieJar = new CookieJar()
try {
// @ts-ignore
await cookieJar.setCookie('x=y; Domain=example.com; Path=/')
} catch (e) {
expect(e).toBeInstanceOf(ParameterError)
}
expect(
// @ts-expect-error test case explicitly violates the expected function signature
() => cookieJar.setCookie('x=y; Domain=example.com; Path=/'),
).toThrow(ParameterError)
})

it('should fix issue #197 - CookieJar().setCookie throws an error when empty cookie is passed', async () => {
Expand Down
1 change: 0 additions & 1 deletion lib/__tests__/defaultPath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('defaultPath', () => {
output: '/',
},
])('defaultPath("$input") => $output', ({ input, output }) => {
// @ts-ignore
expect(defaultPath(input)).toBe(output)
})
})
1 change: 0 additions & 1 deletion lib/__tests__/domainMatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ describe('domainMatch', () => {
['com', 'com', true], // "are identical" rule
['NOTATLD', 'notaTLD', true], // "are identical" rule (after canonicalization)
])('domainMatch(%s, %s) => %s', (string, domain, expectedValue) => {
// @ts-ignore
expect(domainMatch(string, domain)).toBe(expectedValue)
})
})
22 changes: 8 additions & 14 deletions lib/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ function canonicalDomain(str: string | null) {

// S5.1.3 Domain Matching
function domainMatch(
str?: string,
domStr?: string,
str?: string | null,
domStr?: string | null,
colincasey marked this conversation as resolved.
Show resolved Hide resolved
canonicalize?: boolean,
): boolean | null {
if (str == null || domStr == null) {
Expand Down Expand Up @@ -456,7 +456,7 @@ function domainMatch(
*
* Assumption: the path (and not query part or absolute uri) is passed in.
*/
function defaultPath(path?: string): string {
function defaultPath(path?: string | null): string {
// "2. If the uri-path is empty or if the first character of the uri-path is not
// a %x2F ("/") character, output %x2F ("/") and skip the remaining steps.
if (!path || path.substr(0, 1) !== '/') {
Expand Down Expand Up @@ -757,9 +757,7 @@ function fromJSON(str: string | SerializedCookie | null | undefined) {
}

const c = new Cookie()
for (let i = 0; i < Cookie.serializableProperties.length; i++) {
const prop = Cookie.serializableProperties[i]
// @ts-ignore
for (const prop of Cookie.serializableProperties) {
if (obj[prop] === undefined || obj[prop] === cookieDefaults[prop]) {
continue // leave as prototype default
}
Expand All @@ -771,7 +769,6 @@ function fromJSON(str: string | SerializedCookie | null | undefined) {
c[prop] = obj[prop] == 'Infinity' ? 'Infinity' : new Date(obj[prop])
}
} else {
// @ts-ignore
c[prop] = obj[prop]
}
}
Expand Down Expand Up @@ -926,7 +923,6 @@ export class Cookie {
const obj: SerializedCookie = {}

for (const prop of Cookie.serializableProperties) {
// @ts-ignore
if (this[prop] === cookieDefaults[prop]) {
continue // leave as prototype default
}
Expand Down Expand Up @@ -957,9 +953,7 @@ export class Cookie {
: maxAge
}
} else {
// @ts-ignore
if (this[prop] !== cookieDefaults[prop]) {
// @ts-ignore
obj[prop] = this[prop]
}
}
Expand All @@ -983,6 +977,7 @@ export class Cookie {
) {
return false
}
// @ts-ignore
if (this.maxAge != null && this.maxAge <= 0) {
return false // "Max-Age=" non-zero-digit *DIGIT
}
Expand Down Expand Up @@ -1105,7 +1100,6 @@ export class Cookie {
return Infinity
}

// @ts-ignore
if (typeof expires === 'string') {
expires = parseDate(expires)
}
Expand Down Expand Up @@ -1181,12 +1175,12 @@ export class Cookie {
strict: 3,
lax: 2,
none: 1,
}
} as const

static sameSiteCanonical = {
strict: 'Strict',
lax: 'Lax',
}
} as const

static serializableProperties = [
'key',
Expand All @@ -1203,7 +1197,7 @@ export class Cookie {
'creation',
'lastAccessed',
'sameSite',
]
] as const
}

function getNormalizedPrefixSecurity(prefixSecurity: string) {
Expand Down
10 changes: 7 additions & 3 deletions lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ export class Store {
throw new Error('removeCookie is not implemented')
}

removeCookies(domain: string, path: string): Promise<void>
removeCookies(domain: string, path: string, callback: Callback<void>): void
removeCookies(domain: string, path: string | null): Promise<void>
removeCookies(
domain: string,
path: string | null,
callback: Callback<void>,
): void
removeCookies(
_domain: string,
_path: string,
_path: string | null,
_callback?: Callback<void>,
): unknown {
throw new Error('removeCookies is not implemented')
Expand Down