Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/helpers): Fix decorators #3105

Merged
merged 10 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-1160/output/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-1278/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-1345/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-1362/case1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-1421/case1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-2428/1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
29 changes: 29 additions & 0 deletions crates/swc_ecma_transforms/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
feature = "swc_ecma_transforms_proposal",
))]

use std::{fs, path::PathBuf};

use swc_common::{chain, Mark};
use swc_ecma_parser::{EsConfig, Syntax, TsConfig};
use swc_ecma_transforms::resolver_with_mark;
Expand Down Expand Up @@ -6034,3 +6036,30 @@ test!(
initializer: void 0
}), _class);"
);

#[testing::fixture("tests/fixture/decorator/**/exec.ts")]
fn fixture(input: PathBuf) {
let code = fs::read_to_string(&input).expect("failed to read file");

swc_ecma_transforms_testing::exec_tr(
"decorator",
Syntax::Typescript(TsConfig {
decorators: true,
..Default::default()
}),
|_| {
let top_level_mark = Mark::fresh(Mark::root());

chain!(
resolver_with_mark(top_level_mark),
strip(top_level_mark),
decorators(Config {
legacy: true,
emit_metadata: true,
..Default::default()
})
)
},
&code,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const returnValue = "asdasd"
class TestClass {
public testProperty: Date;
}

Object.defineProperty(TestClass.prototype, "testProperty", {
get: function () { return returnValue },
enumerable: true,
configurable: true,
});
const instance = new TestClass()
expect(instance.testProperty).toBe(returnValue)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const returnValue = "asdasd"
class TestClass2 {
@deco public testProperty: Date;
}
function deco(target: any, key: string) {
console.log(target, key);
Object.defineProperty(target.constructor.prototype, key, {
get: function () { return returnValue },
enumerable: true,
configurable: true,
});
}
console.log(TestClass2.prototype.testProperty)
const instance = new TestClass2()
expect(instance.testProperty).toBe(returnValue)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const returnValue = "asdasd"
class TestClass2 {
@deco public testProperty: Date;
}
function deco(target: any, key: string) {
console.log(target, key);
Object.defineProperty(target.constructor.prototype, key, {
value: returnValue,
enumerable: true,
configurable: true,
});
}
console.log(TestClass2.prototype.testProperty)
const instance = new TestClass2()
expect(instance.testProperty).toBe(returnValue)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}

var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
// Prevent overriding
delete desc.writable;
delete desc.initializer;
}

if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
26 changes: 18 additions & 8 deletions crates/swc_ecma_transforms_testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ pub fn test_transform<F, P>(
return Ok(());
}

println!(">>>>> Orig <<<<<\n{}", input);
println!(">>>>> Code <<<<<\n{}", actual_src);
println!(">>>>> {} <<<<<\n{}", Color::Green.paint("Orig"), input);
println!(">>>>> {} <<<<<\n{}", Color::Green.paint("Code"), actual_src);
if actual_src != expected_src {
panic!(
r#"assertion failed: `(left == right)`
Expand Down Expand Up @@ -445,8 +445,11 @@ where
let src = tester.print(&module, &tester.comments.clone());

println!(
"\t>>>>> Orig <<<<<\n{}\n\t>>>>> Code <<<<<\n{}",
input, src_without_helpers
"\t>>>>> {} <<<<<\n{}\n\t>>>>> {} <<<<<\n{}",
Color::Green.paint("Orig"),
input,
Color::Green.paint("Code"),
src_without_helpers
);

exec_with_node_test_runner(test_name, &src)
Expand Down Expand Up @@ -504,12 +507,19 @@ fn exec_with_node_test_runner(test_name: &str, src: &str) -> Result<(), ()> {
Command::new(&test_runner_path)
};

let status = base_cmd
let output = base_cmd
.arg(&format!("{}", path.display()))
.arg("--color")
.current_dir(root)
.status()
.expect("failed to run jest");
if status.success() {
.output()
.expect("failed to run mocha");

println!(">>>>> {} <<<<<", Color::Red.paint("Stdout"));
println!("{}", String::from_utf8_lossy(&output.stdout));
println!(">>>>> {} <<<<<", Color::Red.paint("Stderr"));
println!("{}", String::from_utf8_lossy(&output.stderr));

if output.status.success() {
fs::write(&success_cache, "").unwrap();
return Ok(());
}
Expand Down