Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
feat: simplify code to support diff ignore only
Browse files Browse the repository at this point in the history
  • Loading branch information
sdlyy committed Feb 7, 2024
1 parent d9ebf15 commit ad9dd09
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 403 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@ import { ContractParameters, DiscoveryOutput } from '@l2beat/discovery-types'
import { assign, parse, stringify } from 'comment-json'
import * as fs from 'fs/promises'

import { ContractOverrides } from '../config/DiscoveryOverrides'
import { ContractOverrides } from './DiscoveryOverrides'
import {
MutableDiscoveryOverrides,
MutableOverride,
} from '../config/MutableDiscoveryOverrides'
import { RawDiscoveryConfig } from '../config/RawDiscoveryConfig'
} from './MutableDiscoveryOverrides'
import { RawDiscoveryConfig } from './RawDiscoveryConfig'

interface IgnoreResult {
possible: string[]
ignored: string[]
}

export class InteractiveOverridesManager {
export class DiscoveryOverridesBuilder {
private readonly mutableOverrides: MutableDiscoveryOverrides

constructor(
Expand All @@ -30,93 +25,33 @@ export class InteractiveOverridesManager {
return [...this.output.contracts]
}

getWatchMode(contract: ContractParameters): {
all: string[]
ignored: string[]
} {
getWatchMode(contract: ContractParameters): string[] {
const isDiscoveryIgnored = this.getIgnoreDiscovery(contract)
const ignoredMethods = this.getIgnoredMethods(contract)

if (isDiscoveryIgnored) {
return {
all: [],
ignored: [],
}
return []
}

const overrides = this.getSafeOverride(contract)

const allProperties = Object.keys(contract.values ?? {})

const ignoredInWatchMode = overrides?.ignoreInWatchMode ?? []

// All discovered keys + look ahead for all ignored methods
const possibleMethods = [
...new Set([...allProperties, ...ignoredMethods.possible]),
]
.filter((method) => !this.isCustomHandler(contract, method))
.filter((method) => !ignoredMethods.ignored.includes(method))

return {
all: possibleMethods,
ignored: ignoredInWatchMode,
}
return ignoredInWatchMode
}

getIgnoredRelatives(contract: ContractParameters): IgnoreResult {
const isDiscoveryIgnored = this.getIgnoreDiscovery(contract)
const ignoredMethods = this.getIgnoredMethods(contract)

if (isDiscoveryIgnored) {
return {
possible: [],
ignored: [],
}
}

getIgnoredRelatives(contract: ContractParameters): string[] {
const overrides = this.getSafeOverride(contract)

const allProperties = Object.keys(contract.values ?? {})

const ignoredRelatives = overrides?.ignoreRelatives ?? []

// All discovered keys + look ahead for all ignored methods
const possibleMethods = [
...new Set([...allProperties, ...ignoredMethods.possible]),
]
.filter((method) => !this.isCustomHandler(contract, method))
.filter((method) => !ignoredMethods.ignored.includes(method))

return {
possible: possibleMethods,
ignored: ignoredRelatives,
}
return ignoredRelatives
}

getIgnoredMethods(contract: ContractParameters): IgnoreResult {
const isDiscoveryIgnored = this.getIgnoreDiscovery(contract)

if (isDiscoveryIgnored) {
return {
possible: [],
ignored: [],
}
}

getIgnoredMethods(contract: ContractParameters): string[] {
const overrides = this.getSafeOverride(contract)

const ignoredMethods = overrides?.ignoreMethods ?? []

const allProperties = Object.keys(contract.values ?? {})

const possibleMethods = [
...new Set([...allProperties, ...ignoredMethods]),
].filter((method) => !this.isCustomHandler(contract, method))

return {
possible: possibleMethods,
ignored: ignoredMethods,
}
return ignoredMethods
}

getIgnoreDiscovery(contract: ContractParameters): boolean {
Expand Down Expand Up @@ -146,12 +81,12 @@ export class InteractiveOverridesManager {
}

// Exclude ignoreMethods from watch mode and relatives completely
const validWatchMode = ignoredInWatchMode.ignored.filter(
(method) => !ignoredMethods.ignored.includes(method),
const validWatchMode = ignoredInWatchMode.filter(
(method) => !ignoredMethods.includes(method),
)

const validRelatives = ignoredRelatives.ignored.filter(
(method) => !ignoredMethods.ignored.includes(method),
const validRelatives = ignoredRelatives.filter(
(method) => !ignoredMethods.includes(method),
)

this.mutableOverrides.set(contract, {
Expand Down Expand Up @@ -205,7 +140,7 @@ export class InteractiveOverridesManager {
}
}

private isCustomHandler(
public isCustomHandler(
contract: ContractParameters,
property: string,
): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ContractParameters } from '@l2beat/discovery-types'
import { assign } from 'comment-json'

import { EthereumAddress } from '../../utils/EthereumAddress'
import { ContractOverrides, DiscoveryOverrides } from './DiscoveryOverrides'

export type MutableOverride = Pick<
Expand All @@ -10,17 +9,16 @@ export type MutableOverride = Pick<
>

/**
* In-place overrides map with intention to be mutable
* since it is easier to do that this way instead of modification squash
* @notice Re-assignments made via comments-json `assign` which supports both entries with comments (JSONC) and with out them.
*/
export class MutableDiscoveryOverrides extends DiscoveryOverrides {
public set(contract: ContractParameters, override: MutableOverride): void {
const nameOrAddress = this.updateNameToAddress(contract)
const hasName = Boolean(contract.name)

const identifier = this.getIdentifier(nameOrAddress)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const nameOrAddress = hasName ? contract.name : contract.address.toString()!

const originalOverride = this.config.overrides?.[identifier] ?? {}
const originalOverride = this.config.overrides?.[nameOrAddress] ?? {}

if (override.ignoreInWatchMode !== undefined) {
if (override.ignoreInWatchMode.length === 0) {
Expand Down Expand Up @@ -64,49 +62,12 @@ export class MutableDiscoveryOverrides extends DiscoveryOverrides {
// Set override only if it is not empty
if (Object.keys(originalOverride).length > 0) {
assign(this.config.overrides, {
[identifier]: originalOverride,
[nameOrAddress]: originalOverride,
})
// Remove override if it is empty
} else {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.config.overrides[identifier]
}
}

private getIdentifier(nameOrAddress: string | EthereumAddress): string {
let name: string | undefined
let address: EthereumAddress | undefined

if (EthereumAddress.check(nameOrAddress.toString())) {
address = EthereumAddress(nameOrAddress.toString())
name = this.config.names?.[address.toString()]
} else {
address = this.nameToAddress.get(nameOrAddress.toString())
name = nameOrAddress.toString()
delete this.config.overrides[nameOrAddress]
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return name ?? address!.toString()
}

// Naive update without checks
private updateNameToAddress(contract: ContractParameters): string {
const hasName = Boolean(contract.name)

if (hasName) {
this.nameToAddress.set(contract.name, contract.address)

if (this.config.names === undefined) {
this.config.names = {
[contract.address.toString()]: contract.name,
}
} else {
this.config.names[contract.address.toString()] = contract.name
}
}

const addressOrName = hasName ? contract.name : contract.address.toString()

return addressOrName
}
}
Loading

0 comments on commit ad9dd09

Please sign in to comment.