Skip to content

Commit

Permalink
Fix missing types due to aliased imports
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio committed Sep 30, 2023
1 parent 81b89e4 commit b7084b3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.3.14

- Fix missing types when project uses aliased imports.

## 0.3.13

- Interfaces are not longer exposed.
Expand Down
1 change: 1 addition & 0 deletions src/core.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ Generated by [AVA](https://avajs.dev).
import "../contracts/Test.sol";␊
import "../contracts/Imported.sol";␊
import "../contracts/Imported2.sol";␊
import "@openzeppelin/contracts/proxy/Clones.sol";␊
contract $Foo is Foo {␊
bytes32 public constant __hh_exposed_bytecode_marker = "hardhat-exposed";␊
Expand Down
Binary file modified src/core.test.ts.snap
Binary file not shown.
34 changes: 21 additions & 13 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,7 @@ function getExposedContent(ast: SourceUnit, relativizePath: (p: string) => strin

const contractPrefix = prefix.replace(/^./, c => c.toUpperCase());

const neededAbsoluteImports = [ast.absolutePath].concat(
[...findAll('ImportDirective', ast)]
.filter(i => i.symbolAliases.length > 0)
.map(i => i.absolutePath),
[...findAll('ContractDefinition', ast)]
.flatMap(c => c.linearizedBaseContracts.map(p => {
const sourceId = deref('ContractDefinition', p).scope;
return deref('SourceUnit', sourceId).absolutePath;
})),
);

// remove duplicates and relativize
const imports = Array.from(new Set(neededAbsoluteImports), relativizePath);
const imports = Array.from(getNeededImports(ast, deref), u => relativizePath(u.absolutePath));

const contracts = [...findAll('ContractDefinition', ast)].filter(c => filter?.(c) !== false && c.contractKind !== 'interface');

Expand Down Expand Up @@ -575,6 +563,26 @@ function getFunctions(contract: ContractDefinition, deref: ASTDereferencer, subs
return res;
}

function* getNeededImports(ast: SourceUnit, deref: ASTDereferencer): Iterable<SourceUnit> {
const needed = new Set<SourceUnit>([ast].concat(
[...findAll('ContractDefinition', ast)]
.flatMap(c => c.linearizedBaseContracts.map(p => {
const { sourceUnit } = deref.withSourceUnit('ContractDefinition', p)
return sourceUnit;
})),
));

for (const n of needed) {
yield n;

for (const imp of findAll('ImportDirective', n)) {
if (imp.symbolAliases.length > 0) {
needed.add(deref('SourceUnit', imp.sourceUnit));
}
}
}
}

function mustGet<K, V>(map: Map<K, V>, key: K): V {
const value = map.get(key);
if (value === undefined) {
Expand Down

0 comments on commit b7084b3

Please sign in to comment.