Skip to content

Commit

Permalink
fix: ts naming cases (bytecodealliance#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Jan 18, 2024
1 parent 8961e4f commit c12dca2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
59 changes: 46 additions & 13 deletions crates/js-component-bindgen/src/ts_bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::files::Files;
use crate::function_bindgen::{array_ty, as_nullable, maybe_null};
use crate::names::{maybe_quote_id, LocalNames, RESERVED_KEYWORDS};
use crate::names::{is_js_identifier, maybe_quote_id, LocalNames, RESERVED_KEYWORDS};
use crate::source::Source;
use crate::transpile_bindgen::{parse_world_key, InstantiationMode, TranspileOpts};
use crate::{dealias, uwrite, uwriteln};
Expand Down Expand Up @@ -36,6 +36,7 @@ struct TsInterface<'a> {
resolve: &'a Resolve,
needs_ty_option: bool,
needs_ty_result: bool,
local_names: LocalNames,
resources: HashMap<String, TsInterface<'a>>,
}

Expand Down Expand Up @@ -468,6 +469,7 @@ impl TsBindgen {
TsInterface {
src: Source::default(),
resources: HashMap::new(),
local_names: LocalNames::default(),
resolve,
needs_ty_option: false,
needs_ty_result: false,
Expand All @@ -480,6 +482,7 @@ impl<'a> TsInterface<'a> {
TsInterface {
src: Source::default(),
resources: HashMap::new(),
local_names: LocalNames::default(),
resolve,
needs_ty_option: false,
needs_ty_result: false,
Expand Down Expand Up @@ -650,23 +653,53 @@ impl<'a> TsInterface<'a> {
self
};

let end_character = if declaration {
iface.src.push_str(match func.kind {
FunctionKind::Freestanding => "export function ",
FunctionKind::Method(_) => "",
FunctionKind::Static(_) => "static ",
FunctionKind::Constructor(_) => "",
});
';'
let out_name = if default {
"default".to_string()
} else {
','
func.item_name().to_lower_camel_case()
};

if default {
iface.src.push_str("default");
if declaration {
match func.kind {
FunctionKind::Freestanding => {
if is_js_identifier(&out_name) {
iface.src.push_str(&format!("export function {out_name}"));
} else {
let (local_name, _) = iface.local_names.get_or_create(&out_name, &out_name);
iface
.src
.push_str(&format!("export {{ {local_name} as {out_name} }};\n"));
iface
.src
.push_str(&format!("declare function {local_name}"));
};
}
FunctionKind::Method(_) => {
if is_js_identifier(&out_name) {
iface.src.push_str(&out_name);
} else {
iface.src.push_str(&format!("'{out_name}'"));
}
}
FunctionKind::Static(_) => {
if is_js_identifier(&out_name) {
iface.src.push_str(&format!("static {out_name}"))
} else {
iface.src.push_str(&format!("static '{out_name}'"))
}
}
FunctionKind::Constructor(_) => iface.src.push_str("constructor"),
}
} else {
iface.src.push_str(&func.item_name().to_lower_camel_case());
if is_js_identifier(&out_name) {
iface.src.push_str(&out_name);
} else {
iface.src.push_str(&format!("'{out_name}'"));
}
}

let end_character = if declaration { ';' } else { ',' };

iface.src.push_str("(");

let param_start = match &func.kind {
Expand Down
15 changes: 15 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ export async function cliTest (fixtures) {
ok(source.includes('console.error(`[module="test:flavorful/test", function="list-of-variants"] return result=${toResultString(ret)}`);'));
});

test('TypeScript naming checks', async () => {
const { stderr } = await exec(jcoPath, 'transpile', `test/fixtures/wit/deps/ts-check/ts-check.wit`, '--stub', '-o', outDir);
strictEqual(stderr, '');
{
const source = await readFile(`${outDir}/ts-check.d.ts`);
ok(source.toString().includes('declare function _class(): void'));
ok(source.toString().includes('export { _class as class }'));
}
{
const source = await readFile(`${outDir}/interfaces/ts-naming-blah.d.ts`);
ok(source.toString().includes('declare function _class(): void'));
ok(source.toString().includes('export { _class as class }'));
}
});

test('Transpile to JS', async () => {
const name = 'flavorful';
const { stderr } = await exec(jcoPath, 'transpile', `test/fixtures/components/${name}.component.wasm`, '--name', name, '--map', 'testwasi=./wasi.js', '--valid-lifting-optimization', '--tla-compat', '--js', '--base64-cutoff=0', '-o', outDir);
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/wit/deps/ts-check/ts-check.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ts:naming;

interface blah {
class: func();
}

world test {
export class: func();
export blah;
}
8 changes: 7 additions & 1 deletion update-wasi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ wasm-tools metadata add \

# replace the existing fixtures with our newly created files
mv ../../test/fixtures/wit/deps/flavorful ../../test/fixtures/wit/
rm -r ../../test/fixtures/wit/deps
rm -r ../../test/fixtures/wit/deps/cli
rm -r ../../test/fixtures/wit/deps/clocks
rm -r ../../test/fixtures/wit/deps/filesystem
rm -r ../../test/fixtures/wit/deps/http
rm -r ../../test/fixtures/wit/deps/io
rm -r ../../test/fixtures/wit/deps/random
rm -r ../../test/fixtures/wit/deps/sockets
cp -r crates/wasi/wit/deps ../../test/fixtures/wit/
mv ../../test/fixtures/wit/flavorful ../../test/fixtures/wit/deps/

0 comments on commit c12dca2

Please sign in to comment.