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

Commit ad9dd09

Browse files
committed
feat: simplify code to support diff ignore only
1 parent d9ebf15 commit ad9dd09

5 files changed

Lines changed: 230 additions & 403 deletions

File tree

packages/discovery/src/discovery/interactive/InteractiveOverridesManager.ts renamed to packages/discovery/src/discovery/config/DiscoveryOverridesBuilder.ts

Lines changed: 16 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ import { ContractParameters, DiscoveryOutput } from '@l2beat/discovery-types'
22
import { assign, parse, stringify } from 'comment-json'
33
import * as fs from 'fs/promises'
44

5-
import { ContractOverrides } from '../config/DiscoveryOverrides'
5+
import { ContractOverrides } from './DiscoveryOverrides'
66
import {
77
MutableDiscoveryOverrides,
88
MutableOverride,
9-
} from '../config/MutableDiscoveryOverrides'
10-
import { RawDiscoveryConfig } from '../config/RawDiscoveryConfig'
9+
} from './MutableDiscoveryOverrides'
10+
import { RawDiscoveryConfig } from './RawDiscoveryConfig'
1111

12-
interface IgnoreResult {
13-
possible: string[]
14-
ignored: string[]
15-
}
16-
17-
export class InteractiveOverridesManager {
12+
export class DiscoveryOverridesBuilder {
1813
private readonly mutableOverrides: MutableDiscoveryOverrides
1914

2015
constructor(
@@ -30,93 +25,33 @@ export class InteractiveOverridesManager {
3025
return [...this.output.contracts]
3126
}
3227

33-
getWatchMode(contract: ContractParameters): {
34-
all: string[]
35-
ignored: string[]
36-
} {
28+
getWatchMode(contract: ContractParameters): string[] {
3729
const isDiscoveryIgnored = this.getIgnoreDiscovery(contract)
38-
const ignoredMethods = this.getIgnoredMethods(contract)
3930

4031
if (isDiscoveryIgnored) {
41-
return {
42-
all: [],
43-
ignored: [],
44-
}
32+
return []
4533
}
4634

4735
const overrides = this.getSafeOverride(contract)
4836

49-
const allProperties = Object.keys(contract.values ?? {})
50-
5137
const ignoredInWatchMode = overrides?.ignoreInWatchMode ?? []
5238

53-
// All discovered keys + look ahead for all ignored methods
54-
const possibleMethods = [
55-
...new Set([...allProperties, ...ignoredMethods.possible]),
56-
]
57-
.filter((method) => !this.isCustomHandler(contract, method))
58-
.filter((method) => !ignoredMethods.ignored.includes(method))
59-
60-
return {
61-
all: possibleMethods,
62-
ignored: ignoredInWatchMode,
63-
}
39+
return ignoredInWatchMode
6440
}
6541

66-
getIgnoredRelatives(contract: ContractParameters): IgnoreResult {
67-
const isDiscoveryIgnored = this.getIgnoreDiscovery(contract)
68-
const ignoredMethods = this.getIgnoredMethods(contract)
69-
70-
if (isDiscoveryIgnored) {
71-
return {
72-
possible: [],
73-
ignored: [],
74-
}
75-
}
76-
42+
getIgnoredRelatives(contract: ContractParameters): string[] {
7743
const overrides = this.getSafeOverride(contract)
7844

79-
const allProperties = Object.keys(contract.values ?? {})
80-
8145
const ignoredRelatives = overrides?.ignoreRelatives ?? []
8246

83-
// All discovered keys + look ahead for all ignored methods
84-
const possibleMethods = [
85-
...new Set([...allProperties, ...ignoredMethods.possible]),
86-
]
87-
.filter((method) => !this.isCustomHandler(contract, method))
88-
.filter((method) => !ignoredMethods.ignored.includes(method))
89-
90-
return {
91-
possible: possibleMethods,
92-
ignored: ignoredRelatives,
93-
}
47+
return ignoredRelatives
9448
}
9549

96-
getIgnoredMethods(contract: ContractParameters): IgnoreResult {
97-
const isDiscoveryIgnored = this.getIgnoreDiscovery(contract)
98-
99-
if (isDiscoveryIgnored) {
100-
return {
101-
possible: [],
102-
ignored: [],
103-
}
104-
}
105-
50+
getIgnoredMethods(contract: ContractParameters): string[] {
10651
const overrides = this.getSafeOverride(contract)
107-
10852
const ignoredMethods = overrides?.ignoreMethods ?? []
10953

110-
const allProperties = Object.keys(contract.values ?? {})
111-
112-
const possibleMethods = [
113-
...new Set([...allProperties, ...ignoredMethods]),
114-
].filter((method) => !this.isCustomHandler(contract, method))
115-
116-
return {
117-
possible: possibleMethods,
118-
ignored: ignoredMethods,
119-
}
54+
return ignoredMethods
12055
}
12156

12257
getIgnoreDiscovery(contract: ContractParameters): boolean {
@@ -146,12 +81,12 @@ export class InteractiveOverridesManager {
14681
}
14782

14883
// Exclude ignoreMethods from watch mode and relatives completely
149-
const validWatchMode = ignoredInWatchMode.ignored.filter(
150-
(method) => !ignoredMethods.ignored.includes(method),
84+
const validWatchMode = ignoredInWatchMode.filter(
85+
(method) => !ignoredMethods.includes(method),
15186
)
15287

153-
const validRelatives = ignoredRelatives.ignored.filter(
154-
(method) => !ignoredMethods.ignored.includes(method),
88+
const validRelatives = ignoredRelatives.filter(
89+
(method) => !ignoredMethods.includes(method),
15590
)
15691

15792
this.mutableOverrides.set(contract, {
@@ -205,7 +140,7 @@ export class InteractiveOverridesManager {
205140
}
206141
}
207142

208-
private isCustomHandler(
143+
public isCustomHandler(
209144
contract: ContractParameters,
210145
property: string,
211146
): boolean {
Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ContractParameters } from '@l2beat/discovery-types'
22
import { assign } from 'comment-json'
33

4-
import { EthereumAddress } from '../../utils/EthereumAddress'
54
import { ContractOverrides, DiscoveryOverrides } from './DiscoveryOverrides'
65

76
export type MutableOverride = Pick<
@@ -10,17 +9,16 @@ export type MutableOverride = Pick<
109
>
1110

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

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

23-
const originalOverride = this.config.overrides?.[identifier] ?? {}
21+
const originalOverride = this.config.overrides?.[nameOrAddress] ?? {}
2422

2523
if (override.ignoreInWatchMode !== undefined) {
2624
if (override.ignoreInWatchMode.length === 0) {
@@ -64,49 +62,12 @@ export class MutableDiscoveryOverrides extends DiscoveryOverrides {
6462
// Set override only if it is not empty
6563
if (Object.keys(originalOverride).length > 0) {
6664
assign(this.config.overrides, {
67-
[identifier]: originalOverride,
65+
[nameOrAddress]: originalOverride,
6866
})
6967
// Remove override if it is empty
7068
} else {
7169
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
72-
delete this.config.overrides[identifier]
73-
}
74-
}
75-
76-
private getIdentifier(nameOrAddress: string | EthereumAddress): string {
77-
let name: string | undefined
78-
let address: EthereumAddress | undefined
79-
80-
if (EthereumAddress.check(nameOrAddress.toString())) {
81-
address = EthereumAddress(nameOrAddress.toString())
82-
name = this.config.names?.[address.toString()]
83-
} else {
84-
address = this.nameToAddress.get(nameOrAddress.toString())
85-
name = nameOrAddress.toString()
70+
delete this.config.overrides[nameOrAddress]
8671
}
87-
88-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
89-
return name ?? address!.toString()
90-
}
91-
92-
// Naive update without checks
93-
private updateNameToAddress(contract: ContractParameters): string {
94-
const hasName = Boolean(contract.name)
95-
96-
if (hasName) {
97-
this.nameToAddress.set(contract.name, contract.address)
98-
99-
if (this.config.names === undefined) {
100-
this.config.names = {
101-
[contract.address.toString()]: contract.name,
102-
}
103-
} else {
104-
this.config.names[contract.address.toString()] = contract.name
105-
}
106-
}
107-
108-
const addressOrName = hasName ? contract.name : contract.address.toString()
109-
110-
return addressOrName
11172
}
11273
}

0 commit comments

Comments
 (0)