Skip to content

Commit 5739893

Browse files
committed
refactor: Remove dead code and hide unnecessarily public properties
This makes it easier to analyze which logic is actually used and makes the porting easier.
1 parent 6569b50 commit 5739893

File tree

2 files changed

+8
-79
lines changed

2 files changed

+8
-79
lines changed

packages/hardhat-core/src/internal/hardhat-network/stack-traces/compiler-to-model.ts

-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ function processContractAstNode(
157157
contractNode.linearizedBaseContracts
158158
);
159159

160-
file.addContract(contract);
161-
162160
for (const node of contractNode.nodes) {
163161
if (node.nodeType === "FunctionDefinition") {
164162
const functionAbis = contractAbi?.filter(

packages/hardhat-core/src/internal/hardhat-network/stack-traces/model.ts

+8-77
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,27 @@ export enum ContractFunctionVisibility {
3636
}
3737

3838
export class SourceFile {
39-
public readonly contracts: Contract[] = [];
40-
public readonly functions: ContractFunction[] = [];
39+
private readonly _functions: ContractFunction[] = [];
4140

4241
constructor(
4342
public readonly sourceName: string,
4443
public readonly content: string
4544
) {}
4645

47-
public addContract(contract: Contract) {
48-
if (contract.location.file !== this) {
49-
throw new Error("Trying to add a contract from another file");
50-
}
51-
52-
this.contracts.push(contract);
53-
}
54-
5546
public addFunction(func: ContractFunction) {
5647
if (func.location.file !== this) {
5748
throw new Error("Trying to add a function from another file");
5849
}
5950

60-
this.functions.push(func);
51+
this._functions.push(func);
6152
}
6253

6354
public getContainingFunction(
6455
location: SourceLocation
6556
): ContractFunction | undefined {
6657
// TODO: Optimize this with a binary search or an internal tree
6758

68-
for (const func of this.functions) {
59+
for (const func of this._functions) {
6960
if (func.location.contains(location)) {
7061
return func;
7162
}
@@ -124,12 +115,12 @@ export class SourceLocation {
124115
}
125116

126117
export class Contract {
127-
public readonly localFunctions: ContractFunction[] = [];
128118
public readonly customErrors: CustomError[] = [];
129119

130120
private _constructor: ContractFunction | undefined;
131121
private _fallback: ContractFunction | undefined;
132122
private _receive: ContractFunction | undefined;
123+
private readonly _localFunctions: ContractFunction[] = [];
133124
private readonly _selectorHexToFunction: Map<string, ContractFunction> =
134125
new Map();
135126

@@ -174,7 +165,7 @@ export class Contract {
174165
}
175166
}
176167

177-
this.localFunctions.push(func);
168+
this._localFunctions.push(func);
178169
}
179170

180171
public addCustomError(customError: CustomError) {
@@ -189,7 +180,7 @@ export class Contract {
189180
this._receive = baseContract._receive;
190181
}
191182

192-
for (const baseContractFunction of baseContract.localFunctions) {
183+
for (const baseContractFunction of baseContract._localFunctions) {
193184
if (
194185
baseContractFunction.type !== ContractFunctionType.GETTER &&
195186
baseContractFunction.type !== ContractFunctionType.FUNCTION
@@ -305,52 +296,10 @@ export class Instruction {
305296
public readonly pc: number,
306297
public readonly opcode: Opcode,
307298
public readonly jumpType: JumpType,
299+
// Used only for debugging
308300
public readonly pushData?: Buffer,
309301
public readonly location?: SourceLocation
310302
) {}
311-
312-
/**
313-
* Checks equality with another Instruction.
314-
*/
315-
public equals(other: Instruction): boolean {
316-
if (this.pc !== other.pc) {
317-
return false;
318-
}
319-
320-
if (this.opcode !== other.opcode) {
321-
return false;
322-
}
323-
324-
if (this.jumpType !== other.jumpType) {
325-
return false;
326-
}
327-
328-
if (this.pushData !== undefined) {
329-
if (other.pushData === undefined) {
330-
return false;
331-
}
332-
333-
if (!this.pushData.equals(other.pushData)) {
334-
return false;
335-
}
336-
} else if (other.pushData !== undefined) {
337-
return false;
338-
}
339-
340-
if (this.location !== undefined) {
341-
if (other.location === undefined) {
342-
return false;
343-
}
344-
345-
if (!this.location.equals(other.location)) {
346-
return false;
347-
}
348-
} else if (other.location !== undefined) {
349-
return false;
350-
}
351-
352-
return true;
353-
}
354303
}
355304

356305
interface ImmutableReference {
@@ -365,7 +314,7 @@ export class Bytecode {
365314
public readonly contract: Contract,
366315
public readonly isDeployment: boolean,
367316
public readonly normalizedCode: Buffer,
368-
public readonly instructions: Instruction[],
317+
instructions: Instruction[],
369318
public readonly libraryAddressPositions: number[],
370319
public readonly immutableReferences: ImmutableReference[],
371320
public readonly compilerVersion: string
@@ -388,22 +337,4 @@ export class Bytecode {
388337
public hasInstruction(pc: number): boolean {
389338
return this._pcToInstruction.has(pc);
390339
}
391-
392-
/**
393-
* Checks equality with another Bytecode.
394-
*/
395-
public equals(other: Bytecode): boolean {
396-
if (this._pcToInstruction.size !== other._pcToInstruction.size) {
397-
return false;
398-
}
399-
400-
for (const [key, val] of this._pcToInstruction) {
401-
const otherVal = other._pcToInstruction.get(key);
402-
if (otherVal === undefined || !val.equals(otherVal)) {
403-
return false;
404-
}
405-
}
406-
407-
return true;
408-
}
409340
}

0 commit comments

Comments
 (0)