From b66f5ed00e83927a976ffdbe45c2ace9641de086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 24 Jan 2024 14:16:23 +0100 Subject: feat: TC39 decorator proposal support (#22040) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds support for [TC39 Decorator Proposal](https://github.com/tc39/proposal-decorators). These decorators are only available in transpiled sources - ie. non-JavaScript files (because of lack of support in V8). This entails that "experimental TypeScript decorators" are not available by default and require to be configured, with a configuration like this: ``` { "compilerOptions": { "experimentalDecorators": true } } ``` Closes https://github.com/denoland/deno/issues/19160 --------- Signed-off-by: Bartek IwaƄczuk Co-authored-by: crowlkats Co-authored-by: Divy Srivastava --- cli/tests/integration/repl_tests.rs | 29 ++++++++++----- cli/tests/integration/run_tests.rs | 17 +++++---- .../testdata/run/decorators/experimental/deno.json | 5 +++ .../run/decorators/experimental/no_check/main.out | 3 ++ .../run/decorators/experimental/no_check/main.ts | 21 +++++++++++ .../run/decorators/experimental/runtime/main.out | 7 ++++ .../run/decorators/experimental/runtime/main.ts | 42 ++++++++++++++++++++++ .../run/decorators/experimental/ts/main.out | 2 ++ .../run/decorators/experimental/ts/main.ts | 14 ++++++++ .../testdata/run/decorators/tc39_proposal/main.out | 3 ++ .../testdata/run/decorators/tc39_proposal/main.ts | 21 +++++++++++ cli/tests/testdata/run/no_check_decorators.ts | 21 ----------- cli/tests/testdata/run/no_check_decorators.ts.out | 3 -- cli/tests/testdata/run/runtime_decorators.ts | 8 ++--- cli/tests/testdata/run/ts_decorators.ts | 14 -------- cli/tests/testdata/run/ts_decorators.ts.out | 2 -- 16 files changed, 153 insertions(+), 59 deletions(-) create mode 100644 cli/tests/testdata/run/decorators/experimental/deno.json create mode 100644 cli/tests/testdata/run/decorators/experimental/no_check/main.out create mode 100644 cli/tests/testdata/run/decorators/experimental/no_check/main.ts create mode 100644 cli/tests/testdata/run/decorators/experimental/runtime/main.out create mode 100644 cli/tests/testdata/run/decorators/experimental/runtime/main.ts create mode 100644 cli/tests/testdata/run/decorators/experimental/ts/main.out create mode 100644 cli/tests/testdata/run/decorators/experimental/ts/main.ts create mode 100644 cli/tests/testdata/run/decorators/tc39_proposal/main.out create mode 100644 cli/tests/testdata/run/decorators/tc39_proposal/main.ts delete mode 100644 cli/tests/testdata/run/no_check_decorators.ts delete mode 100644 cli/tests/testdata/run/no_check_decorators.ts.out delete mode 100644 cli/tests/testdata/run/ts_decorators.ts delete mode 100644 cli/tests/testdata/run/ts_decorators.ts.out (limited to 'cli/tests') diff --git a/cli/tests/integration/repl_tests.rs b/cli/tests/integration/repl_tests.rs index 3c5c3e85f..0e63f1589 100644 --- a/cli/tests/integration/repl_tests.rs +++ b/cli/tests/integration/repl_tests.rs @@ -358,15 +358,26 @@ fn typescript_declarations() { #[test] fn typescript_decorators() { - util::with_pty(&["repl"], |mut console| { - console - .write_line("function dec(target) { target.prototype.test = () => 2; }"); - console.expect("undefined"); - console.write_line("@dec class Test {}"); - console.expect("[class Test]"); - console.write_line("new Test().test()"); - console.expect("2"); - }); + let context = TestContextBuilder::default().use_temp_cwd().build(); + let temp_dir = context.temp_dir(); + temp_dir.write( + "./deno.json", + r#"{ "compilerOptions": { "experimentalDecorators": true } }"#, + ); + let config_path = temp_dir.target_path().join("./deno.json"); + util::with_pty( + &["repl", "--config", config_path.to_string_lossy().as_ref()], + |mut console| { + console.write_line( + "function dec(target) { target.prototype.test = () => 2; }", + ); + console.expect("undefined"); + console.write_line("@dec class Test {}"); + console.expect("[class Test]"); + console.write_line("new Test().test()"); + console.expect("2"); + }, + ); } #[test] diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 3a2b46133..6c82cfeec 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -1508,8 +1508,13 @@ itest!(no_check { }); itest!(no_check_decorators { - args: "run --quiet --reload --no-check run/no_check_decorators.ts", - output: "run/no_check_decorators.ts.out", + args: "run --quiet --reload --no-check run/decorators/experimental/no_check/main.ts", + output: "run/decorators/experimental/no_check/main.out", +}); + +itest!(decorators_tc39_proposal { + args: "run --quiet --reload --check run/decorators/tc39_proposal/main.ts", + output: "run/decorators/tc39_proposal/main.out", }); itest!(check_remote { @@ -1526,8 +1531,8 @@ itest!(no_check_remote { }); itest!(runtime_decorators { - args: "run --quiet --reload --no-check run/runtime_decorators.ts", - output: "run/runtime_decorators.ts.out", + args: "run --quiet --reload --no-check run/decorators/experimental/runtime/main.ts", + output: "run/decorators/experimental/runtime/main.out", }); itest!(seed_random { @@ -1591,8 +1596,8 @@ itest!(ts_type_imports { }); itest!(ts_decorators { - args: "run --reload --check run/ts_decorators.ts", - output: "run/ts_decorators.ts.out", + args: "run --reload --check run/decorators/experimental/ts/main.ts", + output: "run/decorators/experimental/ts/main.out", }); itest!(ts_type_only_import { diff --git a/cli/tests/testdata/run/decorators/experimental/deno.json b/cli/tests/testdata/run/decorators/experimental/deno.json new file mode 100644 index 000000000..504cd646e --- /dev/null +++ b/cli/tests/testdata/run/decorators/experimental/deno.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "experimentalDecorators": true + } +} diff --git a/cli/tests/testdata/run/decorators/experimental/no_check/main.out b/cli/tests/testdata/run/decorators/experimental/no_check/main.out new file mode 100644 index 000000000..015f7076e --- /dev/null +++ b/cli/tests/testdata/run/decorators/experimental/no_check/main.out @@ -0,0 +1,3 @@ +a(): evaluated +a(): called +method diff --git a/cli/tests/testdata/run/decorators/experimental/no_check/main.ts b/cli/tests/testdata/run/decorators/experimental/no_check/main.ts new file mode 100644 index 000000000..9f7ec550d --- /dev/null +++ b/cli/tests/testdata/run/decorators/experimental/no_check/main.ts @@ -0,0 +1,21 @@ +// deno-lint-ignore-file +function a() { + console.log("a(): evaluated"); + return ( + _target: any, + _propertyKey: string, + _descriptor: PropertyDescriptor, + ) => { + console.log("a(): called"); + }; +} + +class B { + @a() + method() { + console.log("method"); + } +} + +const b = new B(); +b.method(); diff --git a/cli/tests/testdata/run/decorators/experimental/runtime/main.out b/cli/tests/testdata/run/decorators/experimental/runtime/main.out new file mode 100644 index 000000000..0fc1d4590 --- /dev/null +++ b/cli/tests/testdata/run/decorators/experimental/runtime/main.out @@ -0,0 +1,7 @@ +@A evaluated +@B evaluated +@B called +@A called +fn() called from @A +fn() called from @B +C.test() called diff --git a/cli/tests/testdata/run/decorators/experimental/runtime/main.ts b/cli/tests/testdata/run/decorators/experimental/runtime/main.ts new file mode 100644 index 000000000..40a26bbd4 --- /dev/null +++ b/cli/tests/testdata/run/decorators/experimental/runtime/main.ts @@ -0,0 +1,42 @@ +// deno-lint-ignore-file +function a() { + console.log("@A evaluated"); + return function ( + target: any, + propertyKey: string, + descriptor: PropertyDescriptor, + ) { + console.log("@A called"); + const fn = descriptor.value; + descriptor.value = function () { + console.log("fn() called from @A"); + fn(); + }; + }; +} + +function b() { + console.log("@B evaluated"); + return function ( + target: any, + propertyKey: string, + descriptor: PropertyDescriptor, + ) { + console.log("@B called"); + const fn = descriptor.value; + descriptor.value = function () { + console.log("fn() called from @B"); + fn(); + }; + }; +} + +class C { + @a() + @b() + static test() { + console.log("C.test() called"); + } +} + +C.test(); diff --git a/cli/tests/testdata/run/decorators/experimental/ts/main.out b/cli/tests/testdata/run/decorators/experimental/ts/main.out new file mode 100644 index 000000000..ee77417cf --- /dev/null +++ b/cli/tests/testdata/run/decorators/experimental/ts/main.out @@ -0,0 +1,2 @@ +Check [WILDCARD] +SomeClass { someField: "asdf" } diff --git a/cli/tests/testdata/run/decorators/experimental/ts/main.ts b/cli/tests/testdata/run/decorators/experimental/ts/main.ts new file mode 100644 index 000000000..95fba6cd4 --- /dev/null +++ b/cli/tests/testdata/run/decorators/experimental/ts/main.ts @@ -0,0 +1,14 @@ +// deno-lint-ignore-file + +function Decorate() { + return function (constructor: any): any { + return class extends constructor { + protected someField: string = "asdf"; + }; + }; +} + +@Decorate() +class SomeClass {} + +console.log(new SomeClass()); diff --git a/cli/tests/testdata/run/decorators/tc39_proposal/main.out b/cli/tests/testdata/run/decorators/tc39_proposal/main.out new file mode 100644 index 000000000..39394952e --- /dev/null +++ b/cli/tests/testdata/run/decorators/tc39_proposal/main.out @@ -0,0 +1,3 @@ +starting m with arguments 1 +C.m 1 +ending m diff --git a/cli/tests/testdata/run/decorators/tc39_proposal/main.ts b/cli/tests/testdata/run/decorators/tc39_proposal/main.ts new file mode 100644 index 000000000..00c8a8502 --- /dev/null +++ b/cli/tests/testdata/run/decorators/tc39_proposal/main.ts @@ -0,0 +1,21 @@ +// deno-lint-ignore no-explicit-any +function logged(value: any, { kind, name }: { kind: string; name: string }) { + if (kind === "method") { + return function (...args: unknown[]) { + console.log(`starting ${name} with arguments ${args.join(", ")}`); + // @ts-ignore this has implicit any type + const ret = value.call(this, ...args); + console.log(`ending ${name}`); + return ret; + }; + } +} + +class C { + @logged + m(arg: number) { + console.log("C.m", arg); + } +} + +new C().m(1); diff --git a/cli/tests/testdata/run/no_check_decorators.ts b/cli/tests/testdata/run/no_check_decorators.ts deleted file mode 100644 index 9f7ec550d..000000000 --- a/cli/tests/testdata/run/no_check_decorators.ts +++ /dev/null @@ -1,21 +0,0 @@ -// deno-lint-ignore-file -function a() { - console.log("a(): evaluated"); - return ( - _target: any, - _propertyKey: string, - _descriptor: PropertyDescriptor, - ) => { - console.log("a(): called"); - }; -} - -class B { - @a() - method() { - console.log("method"); - } -} - -const b = new B(); -b.method(); diff --git a/cli/tests/testdata/run/no_check_decorators.ts.out b/cli/tests/testdata/run/no_check_decorators.ts.out deleted file mode 100644 index 015f7076e..000000000 --- a/cli/tests/testdata/run/no_check_decorators.ts.out +++ /dev/null @@ -1,3 +0,0 @@ -a(): evaluated -a(): called -method diff --git a/cli/tests/testdata/run/runtime_decorators.ts b/cli/tests/testdata/run/runtime_decorators.ts index 5da109110..40a26bbd4 100644 --- a/cli/tests/testdata/run/runtime_decorators.ts +++ b/cli/tests/testdata/run/runtime_decorators.ts @@ -1,5 +1,5 @@ // deno-lint-ignore-file -function A() { +function a() { console.log("@A evaluated"); return function ( target: any, @@ -15,7 +15,7 @@ function A() { }; } -function B() { +function b() { console.log("@B evaluated"); return function ( target: any, @@ -32,8 +32,8 @@ function B() { } class C { - @A() - @B() + @a() + @b() static test() { console.log("C.test() called"); } diff --git a/cli/tests/testdata/run/ts_decorators.ts b/cli/tests/testdata/run/ts_decorators.ts deleted file mode 100644 index 95fba6cd4..000000000 --- a/cli/tests/testdata/run/ts_decorators.ts +++ /dev/null @@ -1,14 +0,0 @@ -// deno-lint-ignore-file - -function Decorate() { - return function (constructor: any): any { - return class extends constructor { - protected someField: string = "asdf"; - }; - }; -} - -@Decorate() -class SomeClass {} - -console.log(new SomeClass()); diff --git a/cli/tests/testdata/run/ts_decorators.ts.out b/cli/tests/testdata/run/ts_decorators.ts.out deleted file mode 100644 index ee77417cf..000000000 --- a/cli/tests/testdata/run/ts_decorators.ts.out +++ /dev/null @@ -1,2 +0,0 @@ -Check [WILDCARD] -SomeClass { someField: "asdf" } -- cgit v1.2.3