Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 8698de7

Browse files
committed
Repair compiler output from Solidity <0.4.20
1 parent fe15aae commit 8698de7

File tree

1 file changed

+42
-2
lines changed
  • packages/compile-solidity

1 file changed

+42
-2
lines changed

packages/compile-solidity/run.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,15 @@ function processContracts({
335335
originalSourcePaths,
336336
solcVersion
337337
}) {
338-
if (!compilerOutput.contracts) return [];
338+
let { contracts } = compilerOutput;
339+
if (!contracts) return [];
340+
//HACK: versions of Solidity prior to 0.4.20 are confused by our "project:/"
341+
//prefix (or, more generally, by paths containing colons)
342+
//and put contracts in a weird form as a result. we detect
343+
//this case and repair it.
344+
contracts = repairOldContracts(contracts);
339345
return (
340-
Object.entries(compilerOutput.contracts)
346+
Object.entries(contracts)
341347
// map to [[{ source, contractName, contract }]]
342348
.map(([sourcePath, sourceContracts]) =>
343349
Object.entries(sourceContracts).map(([contractName, contract]) => ({
@@ -418,6 +424,40 @@ function processContracts({
418424
);
419425
}
420426

427+
function repairOldContracts(contracts) {
428+
const contractNames = [].concat(
429+
...Object.values(contracts).map(source => Object.keys(source))
430+
);
431+
if (contractNames.some(name => name.includes(":"))) {
432+
//if any of the "contract names" contains a colon... hack invoked!
433+
//(notionally we could always apply this hack but let's skip it most of the
434+
//time please :P )
435+
let repairedContracts = {};
436+
for (const [sourcePrefix, sourceContracts] of Object.entries(contracts)) {
437+
for (const [mixedPath, contract] of Object.entries(sourceContracts)) {
438+
let sourcePath, contractName;
439+
const lastColonIndex = mixedPath.lastIndexOf(":");
440+
if (lastColonIndex === -1) { //if there is none
441+
sourcePath = sourcePrefix;
442+
contractName = mixedPath;
443+
} else {
444+
contractName = mixedPath.slice(lastColonIndex + 1); //take the part after the final colon
445+
sourcePath = sourcePrefix + ":" + mixedPath.slice(0, lastColonIndex); //the part before the final colon
446+
}
447+
if (!repairedContracts[sourcePath]) {
448+
repairedContracts[sourcePath] = {};
449+
}
450+
repairedContracts[sourcePath][contractName] = contract;
451+
}
452+
}
453+
debug("repaired contracts: %O", repairedContracts);
454+
return repairedContracts;
455+
} else {
456+
//otherwise just return contracts as-is rather than processing
457+
return contracts;
458+
}
459+
}
460+
421461
function formatLinkReferences(linkReferences) {
422462
if (!linkReferences) {
423463
return [];

0 commit comments

Comments
 (0)