From 9d1882822bd7f0aa5ef4a32ee2537224c7d77927 Mon Sep 17 00:00:00 2001 From: Ross Blair Date: Mon, 14 Oct 2024 15:49:10 -0500 Subject: [PATCH] add further type checks to catching errors, and then immediately ts-ignore the issues.add because thats poorly typed. Update Deno.run to Deno.Command. Removes stubfile validators/isBidsy.ts. --- bids-validator/deno.json | 2 +- bids-validator/src/files/browser.test.ts | 2 +- bids-validator/src/files/deno.ts | 4 ++-- bids-validator/src/files/inheritance.test.ts | 5 ++++- bids-validator/src/schema/associations.ts | 3 ++- bids-validator/src/schema/context.ts | 3 ++- bids-validator/src/utils/errors.ts | 3 ++- bids-validator/src/validators/hed.ts | 2 +- bids-validator/src/validators/isBidsy.ts | 14 -------------- bids-validator/src/version.ts | 11 +++++------ 10 files changed, 20 insertions(+), 29 deletions(-) delete mode 100644 bids-validator/src/validators/isBidsy.ts diff --git a/bids-validator/deno.json b/bids-validator/deno.json index c1f9d14c4..3a5da9e39 100644 --- a/bids-validator/deno.json +++ b/bids-validator/deno.json @@ -31,7 +31,7 @@ "@bids/schema": "jsr:@bids/schema@0.11.4-dev.6+d9c23eb0", "@cliffy/command": "jsr:@cliffy/command@1.0.0-rc.5", "@cliffy/table": "jsr:@cliffy/table@1.0.0-rc.5", - "@hed/validator": "npm:hed-validator@3.15.4", + "@hed/validator": "npm:hed-validator@3.15.5", "@ignore": "npm:ignore@5.3.2", "@libs/xml": "jsr:@libs/xml@5.4.13", "@mango/nifti": "npm:nifti-reader-js@0.6.8", diff --git a/bids-validator/src/files/browser.test.ts b/bids-validator/src/files/browser.test.ts index 7486ff4a2..fca675b70 100644 --- a/bids-validator/src/files/browser.test.ts +++ b/bids-validator/src/files/browser.test.ts @@ -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, diff --git a/bids-validator/src/files/deno.ts b/bids-validator/src/files/deno.ts index 44f3c052a..c58173b79 100644 --- a/bids-validator/src/files/deno.ts +++ b/bids-validator/src/files/deno.ts @@ -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()) } } @@ -158,7 +158,7 @@ export async function readFileTree(rootPath: string): Promise { ) 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}`) } } diff --git a/bids-validator/src/files/inheritance.test.ts b/bids-validator/src/files/inheritance.test.ts index 1d84c6a8b..9e73b7d29 100644 --- a/bids-validator/src/files/inheritance.test.ts +++ b/bids-validator/src/files/inheritance.test.ts @@ -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' @@ -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 } diff --git a/bids-validator/src/schema/associations.ts b/bids-validator/src/schema/associations.ts index fea52a82e..ec1d17645 100644 --- a/bids-validator/src/schema/associations.ts +++ b/bids-validator/src/schema/associations.ts @@ -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 { diff --git a/bids-validator/src/schema/context.ts b/bids-validator/src/schema/context.ts index 9adca5d41..e4b5c83e5 100644 --- a/bids-validator/src/schema/context.ts +++ b/bids-validator/src/schema/context.ts @@ -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 diff --git a/bids-validator/src/utils/errors.ts b/bids-validator/src/utils/errors.ts index 66445113b..e5a99e934 100644 --- a/bids-validator/src/utils/errors.ts +++ b/bids-validator/src/utils/errors.ts @@ -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 diff --git a/bids-validator/src/validators/hed.ts b/bids-validator/src/validators/hed.ts index cd832f960..fc34ec0df 100644 --- a/bids-validator/src/validators/hed.ts +++ b/bids-validator/src/validators/hed.ts @@ -80,7 +80,7 @@ export async function hedValidate( context.dataset.issues.add({ code: 'HED_ERROR', location: context.path, - issueMessage: error, + issueMessage: error as string, }) } diff --git a/bids-validator/src/validators/isBidsy.ts b/bids-validator/src/validators/isBidsy.ts deleted file mode 100644 index 73e02ff0c..000000000 --- a/bids-validator/src/validators/isBidsy.ts +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Not sure if we want this yet. Would be to create issues for non standard - * derivatives to have the lowest common denomenator of bids like file names. - */ -// @ts-nocheck -import type { BIDSContext } from '../schema/context.ts' -import type { ContextCheckFunction } from '../../types/check.ts' -import type { BIDSFile } from '../types/filetree.ts' -import type { Schema } from '../types/schema.ts' - -export const isBidsyFilename: ContextCheckFunction = (schema, context) => { - // every '.', '-', '_' followed by an alnum - // only contains '.', '-', '_' and alnum -} diff --git a/bids-validator/src/version.ts b/bids-validator/src/version.ts index 10b25b372..c1739ea17 100644 --- a/bids-validator/src/version.ts +++ b/bids-validator/src/version.ts @@ -32,13 +32,12 @@ export async function getVersion(): Promise { } async function getLocalVersion(path: string): Promise { - 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 }