Skip to content

Commit

Permalink
Simplify handling of custom errors and using for
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Jul 17, 2024
1 parent c37dfb5 commit 81b5eed
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions packages/core/src/utils/make-namespaced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Node } from 'solidity-ast/node';
import { SolcInput, SolcOutput } from '../solc-api';
import { getStorageLocationAnnotation } from '../storage/namespace';
import { assert } from './assert';
import { FunctionDefinition, VariableDeclaration } from 'solidity-ast';
import { FunctionDefinition } from 'solidity-ast';

const OUTPUT_SELECTION = {
'*': {
Expand Down Expand Up @@ -41,7 +41,6 @@ export function makeNamespacedInput(input: SolcInput, output: SolcOutput): SolcI

const orig = Buffer.from(source.content, 'utf8');

const replacedIdentifiers = new Set<string>();
const modifications: Modification[] = [];

for (const node of output.sources[sourcePath].ast.nodes) {
Expand Down Expand Up @@ -108,32 +107,26 @@ export function makeNamespacedInput(input: SolcInput, output: SolcOutput): SolcI
replaceFunction(node, orig, modifications);
break;
}
// - UsingForDirective isn't needed, but it might have NatSpec documentation which is not included in the AST.
// We convert it to a dummy enum to avoid orphaning any possible documentation.
// - ErrorDefinition and VariableDeclaration might be imported by other files, so they cannot be deleted.

// - VariableDeclaration and ErrorDefinition might be imported by other files, so they cannot be deleted.
// However, we need to remove their values to avoid referencing other deleted nodes.
// We do this by converting them to dummy enums, but avoiding duplicate names.
// We do this by converting them to dummy enums with the same name.
case 'VariableDeclaration': {
// If variable is a constant, keep it since it may be referenced in a struct
if (node.constant) {
break;
}
// Otherwise, fall through to convert to dummy enum
}
case 'UsingForDirective':
case 'ErrorDefinition': {
// If an identifier with the same name was not previously written, replace with a dummy enum using its name.
// Otherwise replace with an enum based on astId to avoid duplicate names, which can happen if there was overloading.
// This does not need to check all identifiers from the original contract, since the original compilation
// should have failed if there were conflicts in the first place.
if ('name' in node && !replacedIdentifiers.has(node.name)) {
modifications.push(makeReplace(node, orig, toDummyEnumWithName(node.name)));
replacedIdentifiers.add(node.name);
} else {
modifications.push(makeReplace(node, orig, toDummyEnumWithAstId(node.id)));
}
modifications.push(makeReplace(node, orig, toDummyEnumWithName(node.name)));
break;
}
// - UsingForDirective isn't needed, but it might have NatSpec documentation which is not included in the AST.
// We convert it to a dummy enum to avoid orphaning any possible documentation.
case 'UsingForDirective':
modifications.push(makeReplace(node, orig, toDummyEnumWithAstId(node.id)));
break;
case 'EnumDefinition':
case 'ImportDirective':
case 'PragmaDirective':
Expand Down

0 comments on commit 81b5eed

Please sign in to comment.