forked from NomicFoundation/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da9940e
commit fd6bfff
Showing
5 changed files
with
70 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
crates/solidity/outputs/npm/tests/src/compilation/builder.test.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
crates/solidity/outputs/npm/tests/src/compilation/common.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
6 changes: 6 additions & 0 deletions
6
crates/solidity/outputs/npm/tests/src/compilation/inputs/child.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
4 changes: 4 additions & 0 deletions
4
crates/solidity/outputs/npm/tests/src/compilation/inputs/parent.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |