Skip to content

Commit

Permalink
Use instanceOfNodeError
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Oct 23, 2024
1 parent 063437f commit 6246a03
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/components/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ExternalPromise } from '../utils/externalpromise.js'
import type { Logger } from './logger.js'
import type { Viewer } from './viewer.js'
import { inspectCompact, inspectReadable } from '../utils/inspect.js'
import { instanceOfNodeError } from '../utils/utils.js'

class WsServer extends ws.Server {
private readonly validOrigin: string
Expand Down Expand Up @@ -284,9 +285,8 @@ export class Server {
this.sendOkResponse(response, content, contentType)
}
} catch (err: unknown) {
if (err instanceof vscode.FileSystemError || err instanceof Error) {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
const code = (err as any)?.code as unknown
if (err instanceof vscode.FileSystemError || instanceOfNodeError(err, Error)) {
const code = err.code
if (code === 'FileNotFound' || code === 'ENOENT') {
response.writeHead(404)
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/providers/atsuggestionlib/atsuggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export class AtSuggestion implements IProvider {
const filteredSuggestions = suggestions.filter(item => item.label === result[0])
if (filteredSuggestions.length > 0) {
return filteredSuggestions.map(item => {
/* eslint-disable-next-line @typescript-eslint/no-base-to-string */
item.range = new vscode.Range(args.position.translate(undefined, -item.label.toString().length), args.position)
const length = typeof item.label === 'string' ? item.label.length : item.label.label.length
item.range = new vscode.Range(args.position.translate(undefined, -length), args.position)
return item
})
}
Expand Down
9 changes: 6 additions & 3 deletions src/providers/completionlib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ export interface CmdSignature {
readonly args: string // {} for mandatory args and [] for optional args
}

function isCmdItemEntry(obj: any): obj is CmdItemEntry {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
return (typeof obj.command === 'string') && (typeof obj.snippet === 'string')
function isCmdItemEntry(obj: CmdItemEntry | undefined) {
if (obj) {
return (typeof obj.command === 'string') && (typeof obj.snippet === 'string')
} else {
return false
}
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/providers/completionlib/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ export interface EnvItemEntry {
readonly detail?: string
}

function isEnvItemEntry(obj: any): obj is EnvItemEntry {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
return (typeof obj.name === 'string')
function isEnvItemEntry(obj: EnvItemEntry | undefined) {
if (obj) {
return (typeof obj.name === 'string')
} else {
return false
}
}

export enum EnvSnippetType {
Expand Down Expand Up @@ -73,7 +76,7 @@ export class Environment implements IProvider {
const content = await lwfs.readFile(filePathUri)
const envs: Record<string, EnvItemEntry> = JSON.parse(content) as DataEnvsJsonType
Object.keys(envs).forEach(key => {
if (! isEnvItemEntry(envs[key])) {
if (!isEnvItemEntry(envs[key])) {
this.extension.logger.info(`Cannot parse intellisense file: ${filePathUri}`)
this.extension.logger.info(`Missing field in entry: "${key}": ${inspectCompact(envs[key])}`)
delete envs[key]
Expand Down
12 changes: 12 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ export function escapeRegExp(str: string) {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
}

/**
* A typeguarded version of `instanceof Error` for NodeJS.
* @author Joseph JDBar Barron
* @link https://dev.to/jdbar/the-problem-with-handling-node-js-errors-in-typescript-and-the-workaround-m64
*/
export function instanceOfNodeError<T extends new(...args: any) => Error>(
value: unknown,
errorType: T
): value is InstanceType<T> & NodeJS.ErrnoException {
return value instanceof errorType;
}

/**
* Resolve a relative file path to an absolute path using the prefixes `dirs`.
*
Expand Down
2 changes: 1 addition & 1 deletion test/completion.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function run(): Promise<void> {
})
} catch (e) {
console.error(e)
/* eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors */
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(e)
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/multiroot-ws.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function run(): Promise<void> {
})
} catch (e) {
console.error(e)
/* eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors */
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(e)
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/rootfile.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function run(): Promise<void> {
})
} catch (e) {
console.error(e)
/* eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors */
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(e)
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/unittest.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function run(): Promise<void> {
})
} catch (e) {
console.error(e)
/* eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors */
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(e)
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/viewer.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function run(): Promise<void> {
})
} catch (e) {
console.error(e)
/* eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors */
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(e)
}
})
Expand Down
2 changes: 1 addition & 1 deletion viewer/components/extensionconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ConnectionPort {
const server = `${scheme}://${window.location.hostname}:${window.location.port}`
const sock = new WebSocket(server)
sock.addEventListener('open', () => this.socketPromise.resolve(sock) )
/* eslint-disable-next-line @typescript-eslint/no-base-to-string */
// eslint-disable-next-line @typescript-eslint/no-base-to-string
sock.addEventListener('error', (ev) => this.socketPromise.reject(new Error(`Failed to connect to ${server}: ${ev}`)) )
this.startConnectionKeeper()
}
Expand Down

0 comments on commit 6246a03

Please sign in to comment.