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

chore: enable useUnknownInCatchVariables TypeScript option #494

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/blob-store/live-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class DriveLiveDownload extends TypedEmitter {
const [blobs] = await once(this.#drive, 'blobs', { signal: this.#signal })
return blobs.core
} catch (e) {
if (e.name === 'AbortError') return
if (e instanceof Error && e.name === 'AbortError') return
throw e
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/config-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import yauzl from 'yauzl-promise'
import { validate, valueSchemas } from '@mapeo/schema'
import { json, buffer } from 'node:stream/consumers'
import path from 'node:path'
import { isRecord } from './lib/object.js'

// Throw error if a zipfile contains more than 10,000 entries
const MAX_ENTRIES = 10_000
Expand Down Expand Up @@ -95,7 +96,11 @@ export async function readConfig(configPath) {
}
}
} catch (err) {
warnings.push(err)
warnings.push(
err instanceof Error
? err
: new Error('Unknown error importing icon')
)
}
}
if (icon) {
Expand Down Expand Up @@ -253,14 +258,6 @@ function validatePresetsFile(presetsFile) {
}
}

/**
* @param {unknown} value
* @returns {value is Record<string, unknown>}
*/
function isRecord(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value)
}

/**
* @param {Record<string | symbol, unknown>} obj
* @param {string | symbol} prop
Expand Down
9 changes: 9 additions & 0 deletions src/lib/object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @ts-check

/**
* @param {unknown} value
* @returns {value is Record<string, unknown>}
*/
export function isRecord(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value)
}
achou11 marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 28 additions & 0 deletions tests/lib/object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check
import test from 'brittle'
import { isRecord } from '../../src/lib/object.js'

test('isRecord()', (t) => {
class Klass {}

const records = [
{},
{ foo: 'bar' },
Object.create(null),
new Klass(),
new Error(),
]
for (const value of records) t.ok(isRecord(value), 'value is record')

const others = [
undefined,
null,
0,
123,
'string',
Symbol('symbol'),
() => {},
[],
]
for (const value of others) t.ok(!isRecord(value), 'value is not a record')
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"strictNullChecks": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"useUnknownInCatchVariables": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"module": "Node16",
Expand Down
Loading