Skip to content

Commit

Permalink
fix error when importing non-contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio committed Nov 29, 2022
1 parent f9651c6 commit 89ebf40
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
19 changes: 15 additions & 4 deletions src/utils/map-values.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
export function mapValues<V, W>(obj: Record<string, V>, fn: (value: V) => W): Record<string, W> {
const res: Record<string, W> = {};
for (const k in obj) {
res[k] = fn(obj[k]!);
export function mapValues<T, U>(obj: Record<string, T>, fn: (value: T) => U): Record<string, U> {
const res: Record<string, U> = {};
for (const [k, v] of Object.entries(obj)) {
res[k] = fn(v);
}
return res;
}

export function filterValues<T, U extends T>(obj: Record<string, T>, fn: (value: T) => value is U): Record<string, U>;
export function filterValues<T>(obj: Record<string, T>, fn: (value: T) => boolean): Record<string, T>;
export function filterValues<T>(obj: Record<string, T>, fn: (value: T) => boolean): Record<string, T> {
const res: Record<string, T> = {};
for (const [k, v] of Object.entries(obj)) {
if (fn(v)) {
res[k] = v;
}
}
return res;
}
29 changes: 17 additions & 12 deletions src/utils/scope.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
import { ContractDefinition, SourceUnit } from "solidity-ast";
import { findAll } from "solidity-ast/utils";
import { findAll, isNodeType } from "solidity-ast/utils";
import { DocItemWithContext } from "../site";
import { mapValues } from './map-values';
import { filterValues, mapValues } from './map-values';

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

function run(
file: SourceUnit,
aliasedImport = false,
): Record<string, () => ContractDefinition> {
return filterValues(
mapValues(run(item.__item_context.file), getDef => getDef()),
isNodeType('ContractDefinition'),
);

type Definition = SourceUnit['nodes'][number] & { name: string };

function run(file: SourceUnit): Record<string, () => Definition> {
if (cache.has(file)) {
return cache.get(file)!;
}

const scope: Record<string, () => ContractDefinition> = {};
const scope: Record<string, () => Definition> = {};

cache.set(file, scope);

for (const c of findAll('ContractDefinition', file)) {
scope[c.name] = () => c;
for (const c of file.nodes) {
if ('name' in c) {
scope[c.name] = () => c;
}
}

for (const i of findAll('ImportDirective', file)) {
const importedFile = item.__item_context.build.deref('SourceUnit', i.sourceUnit);
const importedScope = run(importedFile, aliasedImport || i.symbolAliases.length > 0);
const importedScope = run(importedFile);
if (i.symbolAliases.length === 0) {
Object.assign(scope, importedScope);
} else {
Expand Down
7 changes: 6 additions & 1 deletion test-contracts/S08_C.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract C {}
import {I as J, E} from './S08_I.sol';

contract C is J {
/// @inheritdoc J
function foo() external override {}
}
4 changes: 0 additions & 4 deletions test-contracts/S08_E0.sol

This file was deleted.

8 changes: 8 additions & 0 deletions test-contracts/S08_I.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

error E();

contract I {
function foo() external virtual {}
}

0 comments on commit 89ebf40

Please sign in to comment.