Skip to content

Commit

Permalink
add further type checks to catching errors, and then immediately ts-i…
Browse files Browse the repository at this point in the history
…gnore the issues.add because thats poorly typed. Update Deno.run to Deno.Command. Removes stubfile validators/isBidsy.ts.
  • Loading branch information
rwblair committed Oct 14, 2024
1 parent 80b2789 commit 9d18828
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bids-validator/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@bids/schema": "jsr:@bids/[email protected]+d9c23eb0",
"@cliffy/command": "jsr:@cliffy/[email protected]",
"@cliffy/table": "jsr:@cliffy/[email protected]",
"@hed/validator": "npm:[email protected].4",
"@hed/validator": "npm:[email protected].5",
"@ignore": "npm:[email protected]",
"@libs/xml": "jsr:@libs/[email protected]",
"@mango/nifti": "npm:[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion bids-validator/src/files/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals, assertObjectMatch } from '@std/assert'
import { BIDSFileBrowser, fileListToTree } from './browser.ts'

class TestFile extends File {
webkitRelativePath: string
override webkitRelativePath: string
constructor(
fileBits: BlobPart[],
fileName: string,
Expand Down
4 changes: 2 additions & 2 deletions bids-validator/src/files/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class BIDSFileDeno implements BIDSFile {
try {
this.#fileInfo = Deno.statSync(this._getPath())
} catch (error) {
if (error.code === 'ENOENT') {
if (error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT') {
this.#fileInfo = Deno.lstatSync(this._getPath())
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ export async function readFileTree(rootPath: string): Promise<FileTree> {
)
ignore.add(await readBidsIgnore(ignoreFile))
} catch (err) {
if (!Object.hasOwn(err, 'code') || err.code !== 'ENOENT') {
if (err && typeof err === 'object' && !('code' in err && err.code !== 'ENOENT')) {
logger.error(`Failed to read '.bidsignore' file with the following error:\n${err}`)
}
}
Expand Down
5 changes: 4 additions & 1 deletion bids-validator/src/files/inheritance.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, assertThrows } from '@std/assert'
import { assert, assertEquals, assertThrows } from '@std/assert'
import { pathsToTree } from './filetree.ts'
import { walkBack } from './inheritance.ts'

Expand All @@ -17,6 +17,9 @@ Deno.test('walkback inheritance tests', async (t) => {
continue
}
} catch (error) {
assert(error)
assert(typeof error === 'object')
assert('code' in error)
assertEquals(error.code, 'MULTIPLE_INHERITABLE_FILES')
throw error
}
Expand Down
3 changes: 2 additions & 1 deletion bids-validator/src/schema/associations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export async function buildAssociations(
try {
file = walkBack(source, inherit, extensions, suffix).next().value
} catch (error) {
if (error.code === 'MULTIPLE_INHERITABLE_FILES') {
if (error && typeof error === 'object' && 'code' in error && error.code === 'MULTIPLE_INHERITABLE_FILES') {
// @ts-expect-error
issues.add(error)
break
} else {
Expand Down
3 changes: 2 additions & 1 deletion bids-validator/src/schema/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ export class BIDSContext implements Context {
try {
sidecars = [...walkBack(this.file)]
} catch (error) {
if (error.code === 'MULTIPLE_INHERITABLE_FILES') {
if (error && typeof error === 'object' && 'code' in error && error.code === 'MULTIPLE_INHERITABLE_FILES') {
// @ts-expect-error
this.dataset.issues.add(error)
} else {
throw error
Expand Down
3 changes: 2 additions & 1 deletion bids-validator/src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export class SchemaStructureError extends Error {
constructor(schemaPath) {
schemaPath: string
constructor(schemaPath: string) {
super(`Validator attempted to access ${schemaPath}, but it wasn't there.`)
this.name = 'SchemaStructureError'
this.schemaPath = schemaPath
Expand Down
2 changes: 1 addition & 1 deletion bids-validator/src/validators/hed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function hedValidate(
context.dataset.issues.add({
code: 'HED_ERROR',
location: context.path,
issueMessage: error,
issueMessage: error as string,
})
}

Expand Down
14 changes: 0 additions & 14 deletions bids-validator/src/validators/isBidsy.ts

This file was deleted.

11 changes: 5 additions & 6 deletions bids-validator/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ export async function getVersion(): Promise<string> {
}

async function getLocalVersion(path: string): Promise<string> {
const p = Deno.run({
// safe.directory setting so we could still operate from another user
cmd: ['git', '-C', path, '-c', 'safe.directory=*', 'describe', '--tags', '--always'],
stdout: 'piped',
// safe.directory setting so we could still operate from another user
const command = new Deno.Command("git", {
args: ['git', '-C', path, '-c', 'safe.directory=*', 'describe', '--tags', '--always'],
})
const description = new TextDecoder().decode(await p.output()).trim()
p.close()
const { success, stdout } = await command.output();
const description = new TextDecoder().decode(stdout).trim()
return description
}

Expand Down

0 comments on commit 9d18828

Please sign in to comment.