Skip to content

Commit

Permalink
fix: ts root pathing distinctions (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Feb 7, 2024
1 parent 1d778d7 commit 09d7eb9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
24 changes: 14 additions & 10 deletions crates/js-component-bindgen/src/ts_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct TsBindgen {
/// typescript definitions.
struct TsInterface<'a> {
src: Source,
is_root: bool,
resolve: &'a Resolve,
needs_ty_option: bool,
needs_ty_result: bool,
Expand Down Expand Up @@ -92,7 +93,7 @@ pub fn ts_bindgen(

let name = ty.name.as_ref().unwrap();

let mut gen = bindgen.ts_interface(resolve);
let mut gen = bindgen.ts_interface(resolve, true);
gen.docs(&ty.docs);
match &ty.kind {
TypeDefKind::Record(record) => {
Expand Down Expand Up @@ -349,7 +350,7 @@ impl TsBindgen {
_files: &mut Files,
) {
uwriteln!(self.import_object, "{}: {{", maybe_quote_id(import_name));
let mut gen = self.ts_interface(resolve);
let mut gen = self.ts_interface(resolve, false);
gen.ts_func(func, true, false);
let src = gen.finish();
self.import_object.push_str(&src);
Expand Down Expand Up @@ -393,7 +394,7 @@ impl TsBindgen {
_files: &mut Files,
declaration: bool,
) {
let mut gen = self.ts_interface(resolve);
let mut gen = self.ts_interface(resolve, false);
for (_, func) in funcs {
gen.ts_func(func, false, declaration);
}
Expand Down Expand Up @@ -436,7 +437,7 @@ impl TsBindgen {
return local_name;
}

let mut gen = self.ts_interface(resolve);
let mut gen = self.ts_interface(resolve, false);

uwriteln!(gen.src, "export namespace {camel} {{");
for (_, func) in resolve.interfaces[id].functions.iter() {
Expand All @@ -449,7 +450,7 @@ impl TsBindgen {
if !gen.resources.contains_key(resource) {
uwriteln!(gen.src, "export {{ {} }};", resource.to_upper_camel_case());
gen.resources
.insert(resource.to_string(), TsInterface::new(resolve));
.insert(resource.to_string(), TsInterface::new(resolve, false));
}
}
}
Expand All @@ -465,8 +466,9 @@ impl TsBindgen {
local_name
}

fn ts_interface<'b>(&'b mut self, resolve: &'b Resolve) -> TsInterface<'b> {
fn ts_interface<'b>(&'b mut self, resolve: &'b Resolve, is_root: bool) -> TsInterface<'b> {
TsInterface {
is_root,
src: Source::default(),
resources: HashMap::new(),
local_names: LocalNames::default(),
Expand All @@ -478,8 +480,9 @@ impl TsBindgen {
}

impl<'a> TsInterface<'a> {
fn new(resolve: &'a Resolve) -> Self {
fn new(resolve: &'a Resolve, is_root: bool) -> Self {
TsInterface {
is_root,
src: Source::default(),
resources: HashMap::new(),
local_names: LocalNames::default(),
Expand Down Expand Up @@ -646,7 +649,7 @@ impl<'a> TsInterface<'a> {
if !self.resources.contains_key(resource) {
uwriteln!(self.src, "export {{ {} }};", resource.to_upper_camel_case());
self.resources
.insert(resource.to_string(), TsInterface::new(self.resolve));
.insert(resource.to_string(), TsInterface::new(self.resolve, false));
}
self.resources.get_mut(resource).unwrap()
} else {
Expand Down Expand Up @@ -936,6 +939,7 @@ impl<'a> TsInterface<'a> {
}
_ => None,
};
let path_prefix = if self.is_root { "./interfaces/" } else { "./" };
let type_name = name.to_upper_camel_case();
match owner_not_parent {
Some(owned_interface_name) => {
Expand All @@ -948,12 +952,12 @@ impl<'a> TsInterface<'a> {
if orig_name == type_name {
uwriteln!(
self.src,
"import type {{ {type_name} }} from '../interfaces/{owned_interface_name}.js';",
"import type {{ {type_name} }} from '{path_prefix}{owned_interface_name}.js';",
);
} else {
uwriteln!(
self.src,
"import type {{ {orig_name} as {type_name} }} from '../interfaces/{owned_interface_name}.js';",
"import type {{ {orig_name} as {type_name} }} from '{path_prefix}{owned_interface_name}.js';",
);
}
self.src.push_str(&format!("export {{ {} }};\n", type_name));
Expand Down
1 change: 0 additions & 1 deletion test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
opt,
print,
parse,
componentWit,
componentNew,
componentEmbed,
metadataShow,
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/wits/issue-365/issue-365.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test:issue;

interface types {
record bar {
x: u32
}
}

world issue {
use types.{bar};
export foo: func() -> bar;
}
49 changes: 39 additions & 10 deletions test/typescript.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
import { exec } from './helpers.js';
import { strictEqual } from 'node:assert';
import { exec } from "./helpers.js";
import { strictEqual } from "node:assert";
import {
transpile,
componentNew,
componentEmbed,
} from "../src/api.js";
import { readFile } from "node:fs/promises";
import { ok } from "node:assert";

const tscPath = 'node_modules/typescript/bin/tsc';
const tscPath = "node_modules/typescript/bin/tsc";

// always do TS generation
let promise;
export function tsGenerationPromise() {
if (promise) return promise;
return promise = (async () => {
var { stderr } = await exec(tscPath, '-p', 'test/tsconfig.json');
strictEqual(stderr, '');
})();
return (promise = (async () => {
var { stderr } = await exec(tscPath, "-p", "test/tsconfig.json");
strictEqual(stderr, "");
})());
}

// TypeScript tests _must_ run after all codegen to complete successfully
// This is due to type checking against generated bindings
export function tsTest () {
export function tsTest() {
suite(`TypeScript`, () => {
test('Verify Typescript output', async () => {
test("Verify Typescript output", async () => {
await tsGenerationPromise();
});

test(`TS aliasing`, async () => {
const component = await componentNew(
await componentEmbed({
witSource: await readFile(
`test/fixtures/wits/issue-365/issue-365.wit`,
"utf8"
),
dummy: true,
})
);

const { files } = await transpile(component, { name: "issue" });

const dtsSource = new TextDecoder().decode(files["issue.d.ts"]);

ok(
dtsSource.includes(
`import type { Bar } from './interfaces/test-issue-types.js';`
)
);
});
});
}
}

0 comments on commit 09d7eb9

Please sign in to comment.