Skip to content

Commit

Permalink
style: reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Sep 22, 2024
1 parent 98ce643 commit 4ddfb0e
Show file tree
Hide file tree
Showing 37 changed files with 193 additions and 174 deletions.
8 changes: 4 additions & 4 deletions __tests__/common/utils/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ describe('merge', () => {
expect(res).toEqual({ nested: { a: 4 } }) // not linked
})
test('1. works with multiple levels | 2. overwrites entire object with null', () => {
const origin = { body: '', head: null, toes: { big: true }, fingers: { '12': false } }
const origin = { body: '', head: null, toes: { big: true }, fingers: { 12: false } }
const target = { body: {}, head: {}, toes: {}, fingers: null }
const res = merge(origin, target)
expect(res).toEqual({ body: {}, head: {}, toes: { big: true }, fingers: null })
})
test('origin and target are not AsAny', () => {
const origin = { body: '', head: null, toes: { big: true }, fingers: { '12': false } }
const origin = { body: '', head: null, toes: { big: true }, fingers: { 12: false } }
const target = { body: {}, head: {}, toes: {}, fingers: null }
const res = merge(origin, target)
expect(res).toEqual({ body: {}, head: {}, toes: { big: true }, fingers: null })
expect(origin).toEqual({ body: '', head: null, toes: { big: true }, fingers: { '12': false } })
expect(origin).toEqual({ body: '', head: null, toes: { big: true }, fingers: { 12: false } })
expect(target).toEqual({ body: {}, head: {}, toes: {}, fingers: null })
origin.body = 'a'
const originAsAny: any = origin
Expand All @@ -94,7 +94,7 @@ describe('merge', () => {
targetAsAny.toes = 'b'
targetAsAny.fingers = 'b'
expect(res).toEqual({ body: {}, head: {}, toes: { big: true }, fingers: null })
expect(originAsAny).toEqual({ body: 'a', head: 'a', toes: { big: 'a' }, fingers: { '12': 'a' } })
expect(originAsAny).toEqual({ body: 'a', head: 'a', toes: { big: 'a' }, fingers: { 12: 'a' } })
expect(targetAsAny).toEqual({ body: 'b', head: 'b', toes: 'b', fingers: 'b' })
})
test('Overwrite arrays', () => {
Expand Down
3 changes: 2 additions & 1 deletion __tests__/features/assembler/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('assembler', () => {
it('should throw instance of AssemblerError', () => {
try {
assemble('foo')
} catch (error) {
}
catch (error) {
expect(error).toBeInstanceOf(AssemblerError)
}
expect.assertions(1)
Expand Down
3 changes: 2 additions & 1 deletion __tests__/features/cpu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe('cpu', () => {
})
try {
step(memoryData, initialRegisters)
} catch (error) {
}
catch (error) {
expect(error).toBeInstanceOf(RuntimeError)
}
expect.assertions(1)
Expand Down
41 changes: 20 additions & 21 deletions __tests__/snapshotSerializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ type TypeofResult =
| 'function'

type TypeofResultToType<T extends TypeofResult> =
T extends 'string' ? string :
T extends 'number' ? number :
T extends 'bigint' ? bigint :
T extends 'boolean' ? boolean :
T extends 'symbol' ? symbol :
T extends 'undefined' ? undefined :
T extends 'object' ? object :
T extends 'function' ? Function :
never
T extends 'string' ? string :
T extends 'number' ? number :
T extends 'bigint' ? bigint :
T extends 'boolean' ? boolean :
T extends 'symbol' ? symbol :
T extends 'undefined' ? undefined :
T extends 'object' ? object :
T extends 'function' ? Function :
never

const isArrayOf =
<T extends TypeofResult>(...types: T[]) =>
const isArrayOf = <T extends TypeofResult>(...types: T[]) =>
(value: unknown): value is Array<TypeofResultToType<T>> =>
Array.isArray(value) &&
value.length > 0 &&
value.every((item) => (types as TypeofResult[]).includes(typeof item))
Array.isArray(value)
&& value.length > 0
&& value.every((item) => (types as TypeofResult[]).includes(typeof item))

export const shortArraySerializer: jest.SnapshotSerializerPlugin = {
test: (value) => isArrayOf('number', 'boolean')(value) && value.length <= 4,
Expand All @@ -39,12 +38,12 @@ export const memoryDataSerializer: jest.SnapshotSerializerPlugin = {
test: (value) => isArrayOf('number', 'string')(value) && value.length % 0x10 === 0,
serialize: (arr: Array<number | string>, _config, indentation) => `[
${chunk(0x10, arr)
.map(
(row) =>
`${indentation}${' '.repeat(2)}${row
.map((value) => (typeof value === 'number' ? decToHex(value) : value))
.join(SEPARATOR)}`,
)
.join(',\n')},
.map(
(row) =>
`${indentation}${' '.repeat(2)}${row
.map((value) => (typeof value === 'number' ? decToHex(value) : value))
.join(SEPARATOR)}`,
)
.join(',\n')},
${indentation}]`,
}
19 changes: 9 additions & 10 deletions src/app/store/enhancers/injectStoreExtension.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { Store, StoreEnhancer } from '@reduxjs/toolkit'

export const injectStoreExtension =
<StoreExtension extends {}>(
createExtension: (store: Store) => StoreExtension,
): StoreEnhancer<StoreExtension> =>
export const injectStoreExtension = <StoreExtension extends {}>(
createExtension: (store: Store) => StoreExtension,
): StoreEnhancer<StoreExtension> =>
(next) =>
(...args) => {
const store = next(...args)
return {
...store,
...createExtension(store),
(...args) => {
const store = next(...args)
return {
...store,
...createExtension(store),
}
}
}
3 changes: 2 additions & 1 deletion src/app/store/enhancers/subscribeChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const subscribeChange = injectStoreExtension<{ subscribeChange: Subscribe
snapshot = store.getState()
try {
return store.dispatch(action)
} finally {
}
finally {
snapshot = NIL
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/store/observers/actionObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ interface ActionObserver {
enhancer: StoreEnhancer<{ onAction: ObserveAction }>
}

const matchType =
<Payload>(actionCreator: PayloadActionCreator<Payload>) =>
const matchType = <Payload>(actionCreator: PayloadActionCreator<Payload>) =>
(action: Action): action is PayloadAction<Payload> =>
action.type === actionCreator.type

Expand Down
9 changes: 4 additions & 5 deletions src/app/store/persistence/combinedProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import type { PersistenceProvider, PersistenceProviderCreator, PersistenceValida

const defaultProviderCreators = [createLocalStorageProvider, createQueryParamProvider]

export const createCombinedProvider =
<State>(
combine: (a: State, b: State) => State,
providerCreators: PersistenceProviderCreator[] = defaultProviderCreators,
) =>
export const createCombinedProvider = <State>(
combine: (a: State, b: State) => State,
providerCreators: PersistenceProviderCreator[] = defaultProviderCreators,
) =>
(validate: PersistenceValidator<State>, fallback: State): PersistenceProvider<State> => {
const providers = providerCreators.map((createProvider) => createProvider(validate, fallback))
return {
Expand Down
6 changes: 4 additions & 2 deletions src/app/store/persistence/providers/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const createLocalStorageProvider: PersistenceProviderCreator = (validate,
return state
}
}
} catch {
}
catch {
// ignore error
}
return fallbackState
Expand All @@ -25,7 +26,8 @@ export const createLocalStorageProvider: PersistenceProviderCreator = (validate,
try {
const serializedState = JSON.stringify(state)
localStorage.setItem(LOCAL_STORAGE_KEY, serializedState)
} catch {
}
catch {
// ignore write error
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/app/store/persistence/providers/queryParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const createQueryParamProvider: PersistenceProviderCreator = (validate, f
if (validate(state)) {
return state
}
} catch {
}
catch {
// ignore error
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/components/ResizablePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ const ResizablePanel: FC<ResizablePanelProps> = ({
resetLeftChildWidth()
window.clearTimeout(clickTimeoutIdRef.current)
clickCountRef.current = 0
} else {
}
else {
clickTimeoutIdRef.current = window.setTimeout(() => {
clickCountRef.current = 0
}, DOUBLE_CLICK_DELAY_MS)
Expand Down
2 changes: 1 addition & 1 deletion src/common/components/icons/Close.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { IconProps } from './types'
// https://iconmonstr.com/x-mark-2-svg/
const Close: FC<IconProps> = (props) => (
<svg viewBox="0 0 24 24" width="1rem" xmlns="http://www.w3.org/2000/svg" {...props}>
<path d="M23.954 21.03l-9.184-9.095 9.092-9.174-2.832-2.807-9.09 9.179-9.176-9.088-2.81 2.81 9.186 9.105-9.095 9.184 2.81 2.81 9.112-9.192 9.18 9.1z" />{' '}
<path d="M23.954 21.03l-9.184-9.095 9.092-9.174-2.832-2.807-9.09 9.179-9.176-9.088-2.81 2.81 9.186 9.105-9.095 9.184 2.81 2.81 9.112-9.192 9.18 9.1z" />
</svg>
)

Expand Down
2 changes: 1 addition & 1 deletion src/common/components/icons/Undo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { IconProps } from './types'
// https://iconmonstr.com/undo-4-svg/
const Undo: FC<IconProps> = (props) => (
<svg viewBox="0 0 24 24" width="1rem" xmlns="http://www.w3.org/2000/svg" {...props}>
<path d="M17.026 22.957c10.957-11.421-2.326-20.865-10.384-13.309l2.464 2.352h-9.106v-8.947l2.232 2.229c14.794-13.203 31.51 7.051 14.794 17.675z" />{' '}
<path d="M17.026 22.957c10.957-11.421-2.326-20.865-10.384-13.309l2.464 2.352h-9.106v-8.947l2.232 2.229c14.794-13.203 31.51 7.051 14.794 17.675z" />
</svg>
)

Expand Down
9 changes: 5 additions & 4 deletions src/common/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export const useOutsideClick = <T extends Element = Element>(
}
const handleClick = (event: MouseEvent): void => {
if (
event.target instanceof Element &&
event.target !== current &&
!current.contains(event.target)
event.target instanceof Element
&& event.target !== current
&& !current.contains(event.target)
) {
handler(event)
}
Expand Down Expand Up @@ -89,7 +89,8 @@ export const useHover = <T extends Element = Element>(
if (delay === undefined) {
handler(/* isHovered: */ true)
mutableState.isHovered = true
} else {
}
else {
mutableState.timeoutId = window.setTimeout(() => {
handler(/* isHovered: */ true)
mutableState.isHovered = true
Expand Down
3 changes: 2 additions & 1 deletion src/common/utils/classNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const classNames = (...items: ClassItem[]): string => {
if (typeof item === 'string') {
className && (className += ' ')
className += item
} else {
}
else {
for (const key in item) {
if (item[key]) {
className && (className += ' ')
Expand Down
19 changes: 9 additions & 10 deletions src/common/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export const parseString = (str: string): string => parseStringRecursively(str.r
const parseStringRecursively = (str: string): string => {
try {
return JSON.parse(str)
} catch (error) {
}
catch (error) {
if (error instanceof SyntaxError) {
const charIndex = Number(error.message.split(' ').slice(-1)[0])
// invalid escape character or number
Expand Down Expand Up @@ -118,19 +119,16 @@ export const noop = (): void => {}

export const call = <T>(fn: () => T): T => fn()

export const ary =
<T extends unknown[], R>(fn: (...args: T) => R, arity: number) =>
export const ary = <T extends unknown[], R>(fn: (...args: T) => R, arity: number) =>
(...args: T): R =>
fn(...(<T>args.slice(0, arity)))

export const curryRight2 =
<T1, T2, R>(fn: (arg1: T1, arg2: T2) => R) =>
export const curryRight2 = <T1, T2, R>(fn: (arg1: T1, arg2: T2) => R) =>
(arg2: T2) =>
(arg1: T1): R =>
fn(arg1, arg2)
(arg1: T1): R =>
fn(arg1, arg2)

export const pipe =
<T>(...fns: ((value: T) => T)[]) =>
export const pipe = <T>(...fns: ((value: T) => T)[]) =>
(value: T): T =>
fns.reduce((acc, fn) => fn(acc), value)

Expand All @@ -148,7 +146,8 @@ export const throttle = <T extends unknown[]>(
if (lastTime === undefined || currentTime - lastTime >= wait) {
fn(...args)
lastTime = currentTime
} else {
}
else {
queuedTimeoutId = window.setTimeout(
() => {
invokeFn(...args)
Expand Down
7 changes: 4 additions & 3 deletions src/common/utils/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const mergeRec = (target: unknown, source: PlainObject): PlainObject => {
const assignTargetProperty = (key: string | symbol): void => {
const isKeySymbol = typeof key === 'symbol'
if (
(!isKeySymbol && !sourcePropertyNames.includes(key)) ||
(isKeySymbol && !sourcePropertySymbols.includes(key))
(!isKeySymbol && !sourcePropertyNames.includes(key))
|| (isKeySymbol && !sourcePropertySymbols.includes(key))
) {
Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(target, key)!)
}
Expand All @@ -35,7 +35,8 @@ const mergeRec = (target: unknown, source: PlainObject): PlainObject => {
...Object.getOwnPropertyDescriptor(source, key),
value: mergeRec(targetPropertyValue, sourcePropertyValue),
})
} else {
}
else {
Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(source, key)!)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/common/utils/mergeSafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const mergeSafeRec = <Target>(target: Target, source: unknown): Target => {
const assignTargetProperty = (key: string | symbol): void => {
const isKeySymbol = typeof key === 'symbol'
if (
(!isKeySymbol && !sourcePropertyNames.includes(key)) ||
(isKeySymbol && !sourcePropertySymbols.includes(key))
(!isKeySymbol && !sourcePropertyNames.includes(key))
|| (isKeySymbol && !sourcePropertySymbols.includes(key))
) {
Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(target, key)!)
}
Expand All @@ -27,8 +27,8 @@ const mergeSafeRec = <Target>(target: Target, source: unknown): Target => {
const assignSourceProperty = (key: string | symbol): void => {
const isKeySymbol = typeof key === 'symbol'
if (
(!isKeySymbol && targetPropertyNames.includes(key)) ||
(isKeySymbol && targetPropertySymbols.includes(key))
(!isKeySymbol && targetPropertyNames.includes(key))
|| (isKeySymbol && targetPropertySymbols.includes(key))
) {
const targetPropertyValue = target[key]
const sourcePropertyValue: unknown = source[key]
Expand Down
6 changes: 4 additions & 2 deletions src/features/assembler/assemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export const assemble = (source: string): void => {
let assembleResult: AssembleResult
try {
assembleResult = assemblePure(source)
} catch (exception) {
}
catch (exception) {
if (exception instanceof AssemblerError) {
const assemblerErrorObject = exception.toPlainObject()
store.dispatch(setAssemblerError(assemblerErrorObject))
} else {
}
else {
store.dispatch(setException(exception))
store.dispatch(resetAssemblerState())
}
Expand Down
3 changes: 2 additions & 1 deletion src/features/assembler/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const getLabelToAddressMap = (statements: Statement[]): LabelToAddressMap => {
if (instruction.mnemonic === Mnemonic.ORG) {
invariant(typeof firstOperand?.code === 'number')
address = firstOperand.code
} else {
}
else {
// label value has not been calculated yet
address += codes.length + (firstOperand?.type === OperandType.Label ? 1 : 0)
if (address > 0xff && statementIndex !== statementCount - 1) {
Expand Down
Loading

0 comments on commit 4ddfb0e

Please sign in to comment.