refactor: Prevent negated conditions

This commit is contained in:
Joachim Van Herwegen 2023-10-30 17:04:20 +01:00
parent c9e4c7041c
commit 98f5d8fb73
9 changed files with 28 additions and 30 deletions

View File

@ -236,6 +236,7 @@ const configs = antfu(
'unicorn/no-for-loop': 'error',
'unicorn/no-invalid-remove-event-listener': 'error',
'unicorn/no-lonely-if': 'error',
'unicorn/no-negated-condition': 'error',
'unicorn/no-nested-ternary': 'error',
'unicorn/no-process-exit': 'error',
'unicorn/no-thenable': 'error',
@ -269,8 +270,6 @@ const configs = antfu(
'unicorn/require-number-to-fixed-digits-argument': 'error',
// Might want to enable these
'unicorn/no-await-expression-member': 'off',
'unicorn/no-negated-condition': 'off',
'unicorn/no-object-as-default-parameter': 'off',
'unused-imports/no-unused-vars': [

View File

@ -44,14 +44,14 @@ class WebSocketListener extends WebSocketListenerEmitter {
// Verify the WebSocket protocol version
const protocolHeader = headers['sec-websocket-protocol'];
if (!protocolHeader) {
this.sendMessage('warning', `Missing Sec-WebSocket-Protocol header, expected value '${VERSION}'`);
} else {
if (protocolHeader) {
const supportedProtocols = splitCommaSeparated(protocolHeader);
if (!supportedProtocols.includes(VERSION)) {
this.sendMessage('error', `Client does not support protocol ${VERSION}`);
this.stop();
}
} else {
this.sendMessage('warning', `Missing Sec-WebSocket-Protocol header, expected value '${VERSION}'`);
}
// Store the HTTP host and protocol

View File

@ -76,17 +76,17 @@ export class WacAllowHttpHandler extends OperationHttpHandler {
if (permissionSet) {
const user: AclPermissionSet = permissionSet;
let everyone: AclPermissionSet;
if (!credentials.agent?.webId) {
// User is not authenticated so public permissions are the same as agent permissions
this.logger.debug('User is not authenticated so has public permissions');
everyone = user;
} else {
if (credentials.agent?.webId) {
// Need to determine public permissions
this.logger.debug('Determining public permissions');
// Note that this call can potentially create a new lock on a resource that is already locked,
// so a locker that allows multiple read locks on the same resource is required.
const permissionMap = await this.permissionReader.handleSafe({ credentials: {}, requestedModes });
everyone = permissionMap.get(operation.target) ?? {};
} else {
// User is not authenticated so public permissions are the same as agent permissions
this.logger.debug('User is not authenticated so has public permissions');
everyone = user;
}
this.logger.debug('Adding WAC-Allow metadata');

View File

@ -383,11 +383,7 @@ export class WrappedIndexedStorage<T extends IndexTypeCollection<T>> implements
let oldObj: VirtualObject;
let newObj: VirtualObject;
const relation = this.getParentRelation(type);
if (!relation) {
oldObj = root;
newObj = (replace ? { ...partial } : { ...oldObj, ...partial }) as VirtualObject;
root = newObj;
} else {
if (relation) {
const objs = this.getContainingRecord(root, type, id);
if (partial[relation.child.key] && objs[id][relation.child.key] !== partial[relation.child.key]) {
// eslint-disable-next-line ts/restrict-template-expressions
@ -397,6 +393,10 @@ export class WrappedIndexedStorage<T extends IndexTypeCollection<T>> implements
oldObj = objs[id];
newObj = (replace ? { ...partial } : { ...oldObj, ...partial }) as VirtualObject;
objs[id] = newObj;
} else {
oldObj = root;
newObj = (replace ? { ...partial } : { ...oldObj, ...partial }) as VirtualObject;
root = newObj;
}
// Copy over the child relations

View File

@ -88,14 +88,14 @@ function emitStoredErrors(this: Guarded, event: string, func: (error: Error) =>
*/
export function guardStream<T extends NodeJS.EventEmitter>(stream: T): Guarded<T> {
const guarded = stream as Guarded<T>;
if (!isGuarded(stream)) {
guarded[guardedErrors] = [];
guarded.on('error', guardingErrorListener);
guarded.on('newListener', emitStoredErrors);
} else {
if (isGuarded(stream)) {
// This makes sure the guarding error listener is the last one in the list again
guarded.removeListener('error', guardingErrorListener);
guarded.on('error', guardingErrorListener);
} else {
guarded[guardedErrors] = [];
guarded.on('error', guardingErrorListener);
guarded.on('newListener', emitStoredErrors);
}
return guarded;
}

View File

@ -139,7 +139,7 @@ function transformPathComponents(path: string, transform: (part: string) => stri
.map((part, index): string =>
index % 2 === 0 ? transform(part) : part.toUpperCase())
.join('');
return !queryString ? transformed : `${transformed}${queryString}`;
return queryString ? `${transformed}${queryString}` : transformed;
}
/**

View File

@ -72,8 +72,8 @@ export class FilterPattern {
* @param object - Optionally filter based on a specific object.
*/
public constructor(subject?: string, predicate?: string, object?: string) {
this.subject = typeof subject !== 'undefined' ? toNamedTerm(subject) : null;
this.predicate = typeof predicate !== 'undefined' ? toNamedTerm(predicate) : null;
this.object = typeof object !== 'undefined' ? toNamedTerm(object) : null;
this.subject = typeof subject === 'string' ? toNamedTerm(subject) : null;
this.predicate = typeof predicate === 'string' ? toNamedTerm(predicate) : null;
this.object = typeof object === 'string' ? toNamedTerm(object) : null;
}
}

View File

@ -31,11 +31,11 @@ export class SliceStream extends Transform {
let start = options.start;
const end = options.end ?? Number.POSITIVE_INFINITY;
if (options.start < 0) {
if (typeof options.size !== 'number') {
throw new RangeNotSatisfiedHttpError('Slicing data at the end of a stream requires a known size.');
} else {
if (typeof options.size === 'number') {
// `start` is a negative number here so need to add
start = options.size + start;
} else {
throw new RangeNotSatisfiedHttpError('Slicing data at the end of a stream requires a known size.');
}
}

View File

@ -14,13 +14,12 @@ class MemoryLocker implements ResourceLocker {
public async acquire(identifier: ResourceIdentifier): Promise<void> {
const { path } = identifier;
if (!this.locks[path]) {
this.locks[path] = [];
} else {
if (this.locks[path]) {
return new Promise((resolve): void => {
this.locks[path].push(resolve);
});
}
this.locks[path] = [];
}
public async release(identifier: ResourceIdentifier): Promise<void> {