Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Dec 13, 2024
1 parent da9940e commit fd6bfff
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 81 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-onions-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": minor
---

expose the `BingingGraph` API to allow querying definitions/references between source files.
106 changes: 25 additions & 81 deletions crates/solidity/outputs/npm/tests/src/compilation/builder.test.mts
Original file line number Diff line number Diff line change
@@ -1,100 +1,44 @@
import assert from "node:assert";

import { CompilationBuilder } from "@nomicfoundation/slang/compilation";
import { NonterminalKind } from "@nomicfoundation/slang/cst";
import { createBuilder } from "./common.mjs";

describe("can resolve imports", () => {
test("empty builder", async () => {
const builder = createBuilder();

assertFiles(builder, []);
});

test("single file without imports", async () => {
const builder = createBuilder();
test("empty builder", async () => {
const builder = await createBuilder();

await builder.addFile("foo.sol");

assertFiles(builder, ["foo.sol"]);
});
assertFiles(builder, []);
});

test("one file and one import", async () => {
const builder = createBuilder();
test("parent file without imports", async () => {
const builder = await createBuilder();

await builder.addFile("bar.sol");
await builder.addFile("parent.sol");

assertFiles(builder, ["foo.sol", "bar.sol"]);
});
assertFiles(builder, ["parent.sol"]);
});

test("transitive imports", async () => {
const builder = createBuilder();
test("child file with one import", async () => {
const builder = await createBuilder();

await builder.addFile("baz.sol");
await builder.addFile("child.sol");

assertFiles(builder, ["foo.sol", "bar.sol", "baz.sol"]);
});
assertFiles(builder, ["parent.sol", "child.sol"]);
});

function createBuilder(): CompilationBuilder {
return CompilationBuilder.create({
languageVersion: "0.8.0",

readFile: async (fileId) => {
switch (fileId) {
case "foo.sol":
return `
contract Foo {}
`;
case "bar.sol":
return `
import {Foo} from "foo.sol";
`;
case "baz.sol":
return `
import {Bar} from "bar.sol";
`;
default:
return undefined;
}
},
function assertFiles(builder: CompilationBuilder, expected: string[]) {
const unit = builder.build();

resolveImport: async (sourceFileId, importPath) => {
const importString = importPath.node.unparse();
const actual = unit.files().map((file) => {
assert.strictEqual(file.tree.kind, NonterminalKind.SourceUnit);
assert.strictEqual(file.tree.id, file.createTreeCursor().node.id);

switch (sourceFileId) {
case "foo.sol": {
switch (importString) {
default:
return undefined;
}
}
case "bar.sol": {
switch (importString) {
case `"foo.sol"`:
return "foo.sol";
default:
return undefined;
}
}
case "baz.sol": {
switch (importString) {
case `"bar.sol"`:
return "bar.sol";
default:
return undefined;
}
}
default:
return undefined;
}
},
return file.id;
});
}

function assertFiles(builder: CompilationBuilder, expected: string[]) {
const actual = builder
.build()
.files()
.map((file) => file.id);
for (const id of actual) {
const file = unit.file(id)!;
assert.strictEqual(file.id, id);
}

assert.deepEqual(actual.sort(), expected.sort());
}
30 changes: 30 additions & 0 deletions crates/solidity/outputs/npm/tests/src/compilation/common.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import fs from "node:fs/promises";
import path from "node:path";
import assert from "node:assert";

import { CompilationBuilder } from "@nomicfoundation/slang/compilation";
import { readRepoFile } from "../utils/files.mjs";

export async function createBuilder(): Promise<CompilationBuilder> {
const inputsDir = await readRepoFile("crates/solidity/outputs/npm/tests/src/compilation/inputs");

const builder = CompilationBuilder.create({
languageVersion: "0.8.0",

readFile: async (fileId) => {
const filePath = path.resolve(inputsDir, fileId);
return fs.readFile(filePath, "utf8");
},

resolveImport: async (sourceFileId, importPath) => {
const importLiteral = importPath.node.unparse();
assert(importLiteral.startsWith('"'));
assert(importLiteral.endsWith('"'));

const importString = importLiteral.slice(1, -1);
return path.resolve(inputsDir, sourceFileId, "..", importString);
},
});

return builder;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity x.x.x;

import { Parent } from "./parent.sol";

contract Child is Parent {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity x.x.x;

contract Parent {}

0 comments on commit fd6bfff

Please sign in to comment.