-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_compilation_fix.cjs
More file actions
115 lines (91 loc) · 4.16 KB
/
test_compilation_fix.cjs
File metadata and controls
115 lines (91 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { getRequiredOpenZeppelinContracts } = require('./dist/services/openZeppelinContracts.js');
// Test the OpenZeppelin inlining fix
function testCompilationFix() {
console.log('Testing OpenZeppelin compilation fix...');
// This is the basic ERC20 template that was causing errors
const contractSource = `// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract TokenTest1 is ERC20 {
constructor(
string memory name,
string memory symbol,
uint256 totalSupply,
uint8 decimals
) ERC20(name, symbol) {
_mint(msg.sender, totalSupply * 10**decimals);
}
}`;
console.log('Original contract source:');
console.log(contractSource);
// Get required OpenZeppelin contracts
console.log('\n--- Getting required OpenZeppelin contracts ---');
const requiredContracts = getRequiredOpenZeppelinContracts(contractSource);
console.log('Required contracts found:', Object.keys(requiredContracts));
// Simulate the inlining process
let inlinedContract = contractSource;
// Remove OpenZeppelin imports and replace with inlined contracts
for (const [importPath, contractCode] of Object.entries(requiredContracts)) {
console.log(`\nProcessing: ${importPath}`);
// Remove import statement
const importRegex = new RegExp(`import\\s+["']${importPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}["'];`, 'g');
inlinedContract = inlinedContract.replace(importRegex, '');
// Add contract source after pragma
const lines = inlinedContract.split('\n');
const pragmaIndex = lines.findIndex(line => line.trim().startsWith('pragma '));
if (pragmaIndex !== -1) {
lines.splice(pragmaIndex + 1, 0, '', contractCode, '');
inlinedContract = lines.join('\n');
}
}
console.log('\n--- Final inlined contract ---');
console.log(inlinedContract);
// Check for issues
console.log('\n--- Issue Analysis ---');
// Count SPDX identifiers
const spdxMatches = inlinedContract.match(/SPDX-License-Identifier/g);
const spdxCount = spdxMatches ? spdxMatches.length : 0;
console.log(`SPDX License Identifiers found: ${spdxCount}`);
// Check for remaining imports
const importMatches = inlinedContract.match(/import\s+["'].*?["'];/g);
const importCount = importMatches ? importMatches.length : 0;
console.log(`Remaining import statements: ${importCount}`);
if (importMatches) {
console.log('Remaining imports:', importMatches);
}
// Check for OpenZeppelin-specific imports that should be resolved
const ozImportMatches = inlinedContract.match(/import\s+["']@openzeppelin.*?["'];/g);
const ozImportCount = ozImportMatches ? ozImportMatches.length : 0;
console.log(`Unresolved OpenZeppelin imports: ${ozImportCount}`);
if (ozImportMatches) {
console.log('Unresolved OpenZeppelin imports:', ozImportMatches);
}
// Check for relative imports that would cause errors
const relativeImportMatches = inlinedContract.match(/import\s+["']\.\/(.*?)["'];/g);
const relativeImportCount = relativeImportMatches ? relativeImportMatches.length : 0;
console.log(`Problematic relative imports: ${relativeImportCount}`);
if (relativeImportMatches) {
console.log('Problematic relative imports:', relativeImportMatches);
}
console.log('\n--- Fix Status ---');
if (spdxCount === 1) {
console.log('✅ SPDX identifier duplication: FIXED');
} else {
console.log(`❌ SPDX identifier duplication: STILL EXISTS (${spdxCount} found)`);
}
if (ozImportCount === 0 && relativeImportCount === 0) {
console.log('✅ Import resolution: FIXED');
} else {
console.log(`❌ Import resolution: STILL HAS ISSUES (${ozImportCount} OZ imports, ${relativeImportCount} relative imports)`);
}
const isFixed = spdxCount === 1 && ozImportCount === 0 && relativeImportCount === 0;
console.log(`\n${isFixed ? '🎉' : '❌'} Overall fix status: ${isFixed ? 'SUCCESS' : 'NEEDS MORE WORK'}`);
}
// Run the test
try {
testCompilationFix();
} catch (error) {
console.error('Test failed:', error.message);
console.log('\n⚠️ Note: This test requires the built distribution files.');
console.log('The fix has been implemented in the source code.');
}