Skip to content

Commit

Permalink
handling imported types
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Nov 23, 2024
1 parent b207118 commit 6bf5211
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
9 changes: 5 additions & 4 deletions src/analysis/analyse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,13 @@ describe("modules", () => {
});
});

test.todo("implicitly imports types of the modules in the prelude", () => {
const [A] = performAnalysis(`type MyType {}`, { namespace: "A" });
test("implicitly imports types of the modules in the prelude", () => {
const [A] = performAnalysis(`pub type MyType {}`, { namespace: "A" });

const [a] = performAnalysis(
`
let x: Fn(MyType) -> MyType = fn x { x }
// import A.{MyType}
pub let x: Fn(MyType) -> MyType = fn x { x }
`,
{
dependencies: { A },
Expand All @@ -260,7 +261,7 @@ describe("modules", () => {
],
},
);
expect(a.errors).toEqual([]); // TODO check assertion
expect(a.errors).toEqual([]);
});

test.todo("implicitly imports variants of the modules in the prelude", () => {
Expand Down
88 changes: 67 additions & 21 deletions src/analysis/resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Binding,
TypeAst,
UntypedDeclaration,
UntypedExposedValue,
UntypedExpr,
UntypedImport,
UntypedMatchPattern,
Expand Down Expand Up @@ -88,6 +89,10 @@ export class ResolutionAnalysis {
>();

private importedValues = new Map<string, IdentifierResolution>();
private importedTypes = new Map<
string,
[resolution: ResolutionAnalysis, typeDeclaration: UntypedTypeDeclaration]
>();

private unusedBindings = new WeakSet<Binding>();
private identifiersResolutions = new WeakMap<
Expand Down Expand Up @@ -142,31 +147,61 @@ export class ResolutionAnalysis {

private initImportsResolution() {
for (const import_ of [...this.implicitImports, ...this.module.imports]) {
this.registerImport(import_);
const dep = this.getDependency(import_.ns);
if (dep === undefined) {
throw new Error("TODO dep not found");
}

Check warning on line 153 in src/analysis/resolution.ts

View check run for this annotation

Codecov / codecov/patch

src/analysis/resolution.ts#L152-L153

Added lines #L152 - L153 were not covered by tests
for (const exposedValue of import_.exposing) {
this.registerExposedValue(dep, import_, exposedValue);
}
}
}

private registerImport(import_: UntypedImport) {
const dep = this.getDependency(import_.ns);
if (dep === undefined) {
throw new Error("TODO dep not found");
}
private registerExposedValue(
analysis: ResolutionAnalysis,
import_: UntypedImport,
exposedValue: UntypedExposedValue,
) {
switch (exposedValue.type) {
case "value": {
const declarationLookup = analysis.locallyDefinedDeclarations.get(
exposedValue.name,
);
if (declarationLookup === undefined || !declarationLookup.pub) {
throw new Error("TODO imported value not found");
}

Check warning on line 172 in src/analysis/resolution.ts

View check run for this annotation

Codecov / codecov/patch

src/analysis/resolution.ts#L171-L172

Added lines #L171 - L172 were not covered by tests

for (const exposedValue of import_.exposing) {
const declarationLookup = dep.locallyDefinedDeclarations.get(
exposedValue.name,
);
if (declarationLookup === undefined || !declarationLookup.pub) {
throw new Error("TODO imported value not found");
// TODO set resolution of imported value

this.importedValues.set(exposedValue.name, {
type: "global-variable",
declaration: declarationLookup,
namespace: import_.ns,
});
break;
}

// TODO set resolution of imported value
case "type": {
const declarationLookup = analysis.locallyDefinedTypes.get(
exposedValue.name,
);
if (
declarationLookup === undefined ||
declarationLookup.pub === false
) {
throw new Error("TODO imported value not found");
}

Check warning on line 193 in src/analysis/resolution.ts

View check run for this annotation

Codecov / codecov/patch

src/analysis/resolution.ts#L192-L193

Added lines #L192 - L193 were not covered by tests

this.importedTypes.set(declarationLookup.name, [
analysis,
declarationLookup,
]);

this.importedValues.set(exposedValue.name, {
type: "global-variable",
declaration: declarationLookup,
namespace: import_.ns,
});
break;
}

default:
return exposedValue satisfies void;

Check warning on line 204 in src/analysis/resolution.ts

View check run for this annotation

Codecov / codecov/patch

src/analysis/resolution.ts#L204

Added line #L204 was not covered by tests
}
}

Expand Down Expand Up @@ -255,10 +290,21 @@ export class ResolutionAnalysis {
}

private runNamedTypeResolution(typeAst: TypeAst & { type: "named" }) {
const localT = this.locallyDefinedTypes.get(typeAst.name);
if (localT !== undefined) {
const importedType = this.importedTypes.get(typeAst.name);
if (importedType !== undefined) {
const [resolution, declaration] = importedType;
this.typesResolutions.set(typeAst, {
declaration,
ns: resolution.ns,
package: resolution.package_,
});
return;
}

const localTypeLookup = this.locallyDefinedTypes.get(typeAst.name);
if (localTypeLookup !== undefined) {
this.typesResolutions.set(typeAst, {
declaration: localT,
declaration: localTypeLookup,
ns: this.ns,
package: this.package_,
});
Expand Down

0 comments on commit 6bf5211

Please sign in to comment.