Skip to content

Commit

Permalink
Remove file level functions and variable from namespace rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Nov 8, 2023
1 parent 1be0edd commit f88e8e5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 46 deletions.
10 changes: 10 additions & 0 deletions packages/core/contracts/test/NamespacedToModify.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ library Lib {
}

contract Consumer {
bytes4 public variableFromConstant = CONSTANT_USING_SELECTOR;

function usingFreeFunction() pure public returns (bytes4) {
return FreeFunctionUsingSelector();
}
Expand All @@ -100,6 +102,14 @@ function plusTwo(uint x) pure returns (uint) {
return x + 2;
}

function plusThree(uint x) pure returns (uint) {
return x + 3;
}

function plusThree(uint x, uint y) pure returns (uint) {
return x + y + 3;
}

using {plusTwo} for uint;

contract UsingForDirectives {
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/utils/make-namespaced.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ Generated by [AVA](https://avajs.dev).
}␊
uint256 constant FreeFunctionUsingSelector = 0;␊
uint256 constant CONSTANT_USING_SELECTOR = 0;␊
library Lib {␊
Expand All @@ -98,9 +98,15 @@ Generated by [AVA](https://avajs.dev).
}␊
uint256 constant plusTwo = 0;␊
Expand Down
Binary file modified packages/core/src/utils/make-namespaced.test.ts.snap
Binary file not shown.
103 changes: 60 additions & 43 deletions packages/core/src/utils/make-namespaced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,69 @@ export function makeNamespacedInput(input: SolcInput, output: SolcOutput): SolcI
const modifications: Modification[] = [];

for (const node of output.sources[sourcePath].ast.nodes) {
if (isNodeType('ContractDefinition', node)) {
const contractDef = node;

// Remove any calls to parent constructors from the inheritance list
const inherits = contractDef.baseContracts;
for (const inherit of inherits) {
if (Array.isArray(inherit.arguments)) {
assert(inherit.baseName.name !== undefined);
modifications.push(makeReplace(inherit, orig, inherit.baseName.name));
}
}

const contractNodes = contractDef.nodes;
for (const contractNode of contractNodes) {
if (
isNodeType('FunctionDefinition', contractNode) ||
isNodeType('ModifierDefinition', contractNode) ||
isNodeType('VariableDeclaration', contractNode)
) {
if (contractNode.documentation) {
modifications.push(makeDelete(contractNode.documentation, orig));
switch (node.nodeType) {
case 'ContractDefinition':
const contractDef = node;

Check failure on line 49 in packages/core/src/utils/make-namespaced.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected lexical declaration in case block

// Remove any calls to parent constructors from the inheritance list
const inherits = contractDef.baseContracts;

Check failure on line 52 in packages/core/src/utils/make-namespaced.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected lexical declaration in case block
for (const inherit of inherits) {
if (Array.isArray(inherit.arguments)) {
assert(inherit.baseName.name !== undefined);
modifications.push(makeReplace(inherit, orig, inherit.baseName.name));
}
modifications.push(makeDelete(contractNode, orig));
} else if (isNodeType('UsingForDirective', contractNode)) {
modifications.push(makeDelete(contractNode, orig));
} else if (isNodeType('StructDefinition', contractNode)) {
const storageLocation = getStorageLocationAnnotation(contractNode);
if (storageLocation !== undefined) {
const structName = contractNode.name;
const variableName = `$${structName}_${(Math.random() * 1e6).toFixed(0)}`;
const insertText = ` ${structName} ${variableName};`;

modifications.push(makeInsertAfter(contractNode, insertText));
}

const contractNodes = contractDef.nodes;

Check failure on line 60 in packages/core/src/utils/make-namespaced.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected lexical declaration in case block
for (const contractNode of contractNodes) {
switch(contractNode.nodeType) {

Check warning on line 62 in packages/core/src/utils/make-namespaced.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
case 'FunctionDefinition':
case 'ModifierDefinition':
case 'VariableDeclaration':
if (contractNode.documentation) {
modifications.push(makeDelete(contractNode.documentation, orig));
}
modifications.push(makeDelete(contractNode, orig));
break;

case 'UsingForDirective':
modifications.push(makeDelete(contractNode, orig));
break;

case 'StructDefinition':
const storageLocation = getStorageLocationAnnotation(contractNode);

Check failure on line 77 in packages/core/src/utils/make-namespaced.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected lexical declaration in case block
if (storageLocation !== undefined) {
const structName = contractNode.name;
const variableName = `$${structName}_${(Math.random() * 1e6).toFixed(0)}`;
const insertText = ` ${structName} ${variableName};`;

modifications.push(makeInsertAfter(contractNode, insertText));
}
break;

case 'EnumDefinition':
case 'ErrorDefinition':
case 'EventDefinition':
case 'UserDefinedValueTypeDefinition':
// nothing
break;
}
}
}
} else if (isNodeType('FunctionDefinition', node) || isNodeType('VariableDeclaration', node)) {
if (node.documentation) {
modifications.push(makeDelete(node.documentation, orig));
}
// Replace with a dummy variable of arbitrary type
const name = node.name;
const insertText = `uint256 constant ${name} = 0;`;
modifications.push(makeReplace(node, orig, insertText));
} else if (isNodeType('UsingForDirective', node)) {
modifications.push(makeDelete(node, orig));
break;

case 'FunctionDefinition':
case 'UsingForDirective':
case 'VariableDeclaration':
modifications.push(makeDelete(node, orig));
break;

case 'EnumDefinition':
case 'ErrorDefinition':
case 'ImportDirective':
case 'PragmaDirective':
case 'StructDefinition':
case 'UserDefinedValueTypeDefinition':
break;
}
}

Expand Down

0 comments on commit f88e8e5

Please sign in to comment.