Skip to content

Commit 89ebf40

Browse files
committed
fix error when importing non-contracts
1 parent f9651c6 commit 89ebf40

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

src/utils/map-values.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
export function mapValues<V, W>(obj: Record<string, V>, fn: (value: V) => W): Record<string, W> {
2-
const res: Record<string, W> = {};
3-
for (const k in obj) {
4-
res[k] = fn(obj[k]!);
1+
export function mapValues<T, U>(obj: Record<string, T>, fn: (value: T) => U): Record<string, U> {
2+
const res: Record<string, U> = {};
3+
for (const [k, v] of Object.entries(obj)) {
4+
res[k] = fn(v);
55
}
66
return res;
77
}
88

9+
export function filterValues<T, U extends T>(obj: Record<string, T>, fn: (value: T) => value is U): Record<string, U>;
10+
export function filterValues<T>(obj: Record<string, T>, fn: (value: T) => boolean): Record<string, T>;
11+
export function filterValues<T>(obj: Record<string, T>, fn: (value: T) => boolean): Record<string, T> {
12+
const res: Record<string, T> = {};
13+
for (const [k, v] of Object.entries(obj)) {
14+
if (fn(v)) {
15+
res[k] = v;
16+
}
17+
}
18+
return res;
19+
}

src/utils/scope.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
import { ContractDefinition, SourceUnit } from "solidity-ast";
2-
import { findAll } from "solidity-ast/utils";
2+
import { findAll, isNodeType } from "solidity-ast/utils";
33
import { DocItemWithContext } from "../site";
4-
import { mapValues } from './map-values';
4+
import { filterValues, mapValues } from './map-values';
55

66
export function getContractsInScope(item: DocItemWithContext) {
7-
const cache = new WeakMap<SourceUnit, Record<string, () => ContractDefinition>>();
8-
return mapValues(run(item.__item_context.file), fn => fn());
7+
const cache = new WeakMap<SourceUnit, Record<string, () => Definition>>();
98

10-
function run(
11-
file: SourceUnit,
12-
aliasedImport = false,
13-
): Record<string, () => ContractDefinition> {
9+
return filterValues(
10+
mapValues(run(item.__item_context.file), getDef => getDef()),
11+
isNodeType('ContractDefinition'),
12+
);
13+
14+
type Definition = SourceUnit['nodes'][number] & { name: string };
15+
16+
function run(file: SourceUnit): Record<string, () => Definition> {
1417
if (cache.has(file)) {
1518
return cache.get(file)!;
1619
}
1720

18-
const scope: Record<string, () => ContractDefinition> = {};
21+
const scope: Record<string, () => Definition> = {};
1922

2023
cache.set(file, scope);
2124

22-
for (const c of findAll('ContractDefinition', file)) {
23-
scope[c.name] = () => c;
25+
for (const c of file.nodes) {
26+
if ('name' in c) {
27+
scope[c.name] = () => c;
28+
}
2429
}
2530

2631
for (const i of findAll('ImportDirective', file)) {
2732
const importedFile = item.__item_context.build.deref('SourceUnit', i.sourceUnit);
28-
const importedScope = run(importedFile, aliasedImport || i.symbolAliases.length > 0);
33+
const importedScope = run(importedFile);
2934
if (i.symbolAliases.length === 0) {
3035
Object.assign(scope, importedScope);
3136
} else {

test-contracts/S08_C.sol

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.11;
33

4-
contract C {}
4+
import {I as J, E} from './S08_I.sol';
5+
6+
contract C is J {
7+
/// @inheritdoc J
8+
function foo() external override {}
9+
}

test-contracts/S08_E0.sol

Lines changed: 0 additions & 4 deletions
This file was deleted.

test-contracts/S08_I.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.11;
3+
4+
error E();
5+
6+
contract I {
7+
function foo() external virtual {}
8+
}

0 commit comments

Comments
 (0)