diff --git a/Cargo.toml b/Cargo.toml index 513f10e9068..b95aea07828 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,9 +53,10 @@ wasm-bindgen-test-crate-b = { path = 'tests/crates/b', version = '0.1' } members = [ "crates/cli", "crates/js-sys", + "crates/macro/ui-tests", "crates/test", "crates/test/sample", - "crates/macro/ui-tests", + "crates/typescript-tests", "crates/web-sys", "crates/webidl", "crates/webidl-tests", @@ -64,8 +65,8 @@ members = [ "examples/char", "examples/closures", "examples/console_log", - "examples/duck-typed-interfaces", "examples/dom", + "examples/duck-typed-interfaces", "examples/fetch", "examples/guide-supported-types-examples", "examples/hello_world", diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 750cff9d33c..68f4632809f 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -119,6 +119,14 @@ jobs: echo "##vso[task.setvariable variable=PATH;]$PATH:$PWD" - script: cargo test -p wasm-bindgen-wasm-interpreter + - job: test_typescript_output + displayName: "Test TypeScript output of wasm-bindgen" + steps: + - template: ci/azure-install-rust.yml + - template: ci/azure-install-sccache.yml + - template: ci/azure-install-node.yml + - script: cd crates/typescript-tests && ./run.sh + - job: build_examples displayName: "Build almost all examples" steps: diff --git a/crates/typescript-tests/.gitignore b/crates/typescript-tests/.gitignore new file mode 100644 index 00000000000..2dfbe6f5bc4 --- /dev/null +++ b/crates/typescript-tests/.gitignore @@ -0,0 +1,2 @@ +pkg +dist diff --git a/crates/typescript-tests/Cargo.toml b/crates/typescript-tests/Cargo.toml new file mode 100644 index 00000000000..505c86217cf --- /dev/null +++ b/crates/typescript-tests/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "typescript-tests" +version = "0.1.0" +authors = ["The wasm-bindgen Developers"] +edition = "2018" + +[dependencies] +wasm-bindgen = { path = '../..' } + +[lib] +crate-type = ['cdylib'] diff --git a/crates/typescript-tests/index.ts b/crates/typescript-tests/index.ts new file mode 100644 index 00000000000..f36c5192936 --- /dev/null +++ b/crates/typescript-tests/index.ts @@ -0,0 +1,11 @@ +import * as wbg from './pkg/typescript_tests'; +import * as wasm from './pkg/typescript_tests_bg'; + +const a1: (a: string) => void = wbg.greet; +const a2: (a: number, b: number) => void = wasm.greet; +const a3: WebAssembly.Memory = wasm.memory; + +const c = new wbg.A(); +wbg.A.other(); +c.foo(); +c.free(); diff --git a/crates/typescript-tests/package.json b/crates/typescript-tests/package.json new file mode 100644 index 00000000000..f921d933d8d --- /dev/null +++ b/crates/typescript-tests/package.json @@ -0,0 +1,9 @@ +{ + "scripts": { + "tsc": "tsc" + }, + "devDependencies": { + "@types/webassembly-js-api": "0.0.2", + "typescript": "^3.3.3333" + } +} diff --git a/crates/typescript-tests/run.sh b/crates/typescript-tests/run.sh new file mode 100755 index 00000000000..d6b033988a1 --- /dev/null +++ b/crates/typescript-tests/run.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +set -ex + +cargo build --target wasm32-unknown-unknown + +rm -rf pkg +mkdir pkg +cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \ + ../../target/wasm32-unknown-unknown/debug/typescript_tests.wasm \ + --out-dir pkg \ + --typescript + +if [ ! -d node_modules ]; then + npm install +fi + +npm run tsc diff --git a/crates/typescript-tests/src/lib.rs b/crates/typescript-tests/src/lib.rs new file mode 100644 index 00000000000..ea10b7b2a7a --- /dev/null +++ b/crates/typescript-tests/src/lib.rs @@ -0,0 +1,20 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn greet(_: &str) {} + +#[wasm_bindgen] +struct A { +} + +#[wasm_bindgen] +impl A { + #[wasm_bindgen(constructor)] + pub fn new() -> A { + A {} + } + + pub fn other() {} + + pub fn foo(&self) {} +} diff --git a/crates/typescript-tests/tsconfig.json b/crates/typescript-tests/tsconfig.json new file mode 100644 index 00000000000..bdd0690736c --- /dev/null +++ b/crates/typescript-tests/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "commonjs", + "esModuleInterop": true, + "target": "es6", + "noImplicitAny": true, + "sourceMap": true, + "outDir": "dist", + "baseUrl": "." + }, + "include": [ + "index.ts" + ] +}