Skip to content

Commit

Permalink
Replace using-for directives with dummy enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Nov 24, 2023
1 parent 0105e41 commit 15526b8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
10 changes: 6 additions & 4 deletions packages/core/src/utils/make-namespaced.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function testMakeNamespaced(
const origInput = JSON.parse(JSON.stringify(origBuildInfo.input));

const modifiedInput = makeNamespacedInput(origBuildInfo.input, origBuildInfo.output);
normalizeStateVariableNames(modifiedInput);
normalizeIdentifiers(modifiedInput);
t.snapshot(modifiedInput);

t.deepEqual(origBuildInfo.input, origInput);
Expand All @@ -46,12 +46,14 @@ async function testMakeNamespaced(
t.is(modifiedOutput.errors, undefined);
}

function normalizeStateVariableNames(input: SolcInput): void {
function normalizeIdentifiers(input: SolcInput): void {
for (const source of Object.values(input.sources)) {
if (source.content !== undefined) {
source.content = source.content
.replace(/\$MainStorage_\d{1,6};/g, '$MainStorage_random;')
.replace(/\$SecondaryStorage_\d{1,6}/g, '$SecondaryStorage_random');
.replace(/\$MainStorage_\d{1,6}/g, '$MainStorage_random')
.replace(/\$SecondaryStorage_\d{1,6}/g, '$SecondaryStorage_random')
.replace(/\$UsingForDirective_\d+_\d{1,6}/, '$UsingForDirective_1_random')
.replace(/\$UsingForDirective_\d+_\d{1,6}/, '$UsingForDirective_2_random');
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/make-namespaced.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ Generated by [AVA](https://avajs.dev).
/**␊
* @param foo foo␊
*/␊
enum $UsingForDirective_1_random { dummy }
contract UsingForDirectives {␊
enum $UsingForDirective_2_random { dummy }
}␊
Expand Down
Binary file modified packages/core/src/utils/make-namespaced.test.ts.snap
Binary file not shown.
15 changes: 12 additions & 3 deletions packages/core/src/utils/make-namespaced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ export function makeNamespacedInput(input: SolcInput, output: SolcOutput): SolcI
modifications.push(makeDelete(contractNode, orig));
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(makeDelete(contractNode, orig));
const insertText = getUsingForReplacement(contractNode.id);
modifications.push(makeReplace(contractNode, orig, insertText));
break;
}
case 'StructDefinition': {
Expand All @@ -99,9 +102,11 @@ export function makeNamespacedInput(input: SolcInput, output: SolcOutput): SolcI
}
break;
}
// - UsingFor can be deleted since its result only takes effect in functions
// - 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(makeDelete(node, orig));
const insertText = getUsingForReplacement(node.id);
modifications.push(makeReplace(node, orig, insertText));
break;
}
// - ErrorDefinition, FunctionDefinition, and VariableDeclaration might be imported by other files, so they cannot be deleted.
Expand Down Expand Up @@ -157,6 +162,10 @@ interface Modification {
text?: string;
}

function getUsingForReplacement(astId: number) {
return `enum $UsingForDirective_${astId}_${(Math.random() * 1e6).toFixed(0)} { dummy }`;
}

function getPositions(node: Node) {
const [start, length] = node.src.split(':').map(Number);
const end = start + length;
Expand Down

0 comments on commit 15526b8

Please sign in to comment.