diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/arguments-fn-expr/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/arguments-fn-expr/input.js new file mode 100644 index 000000000000..33bc51ece156 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/arguments-fn-expr/input.js @@ -0,0 +1,5 @@ +function test() { + return function () { + return arguments[0]; + }; +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/arguments-fn-expr/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/arguments-fn-expr/output.js new file mode 100644 index 000000000000..58710ad951fa --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/arguments-fn-expr/output.js @@ -0,0 +1,5 @@ +function test() { + return function() { + return arguments[0]; + }; +} diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/arguments-member/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/arguments-member/input.js new file mode 100644 index 000000000000..95a359b78d93 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/arguments-member/input.js @@ -0,0 +1,5 @@ +function test() { + return (foo) => { + return foo.arguments; + } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/arguments-member/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/arguments-member/output.js new file mode 100644 index 000000000000..2324ee8e6a41 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/arguments-member/output.js @@ -0,0 +1,5 @@ +function test() { + return function(foo) { + return foo.arguments; + }; +} diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/arguments/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/arguments/input.js new file mode 100644 index 000000000000..3ed23c3b639d --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/arguments/input.js @@ -0,0 +1,3 @@ +function test() { + return () => arguments[0]; +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/arguments/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/arguments/output.js new file mode 100644 index 000000000000..6e382cbb7320 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/arguments/output.js @@ -0,0 +1,6 @@ +function test() { + var _arguments = arguments; + return function() { + return _arguments[0]; + }; +} diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/basic/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/basic/input.js new file mode 100644 index 000000000000..2f0f282ff646 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/basic/input.js @@ -0,0 +1 @@ +let echo = (bar) => bar \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/basic/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/basic/output.js new file mode 100644 index 000000000000..63235f7dbf85 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/basic/output.js @@ -0,0 +1,3 @@ +let echo = function(bar) { + return bar; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/chrome-46/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/chrome-46/input.js new file mode 100644 index 000000000000..bce42792e998 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/chrome-46/input.js @@ -0,0 +1,3 @@ +function foo() { + const a = (a) => new.target +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/chrome-46/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/chrome-46/output.js new file mode 100644 index 000000000000..22c41df1da7c --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/chrome-46/output.js @@ -0,0 +1,6 @@ +function foo() { + var _newtarget = new.target; + const a = function(a) { + return _newtarget; + }; +} diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/computed-props/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/computed-props/input.js new file mode 100644 index 000000000000..acf561f4e419 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/computed-props/input.js @@ -0,0 +1,3 @@ +var a = { + [(() => this)()]: 123 +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/computed-props/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/computed-props/output.js new file mode 100644 index 000000000000..3279d0cce111 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/computed-props/output.js @@ -0,0 +1,6 @@ +var _this = this; +var a = { + [function() { + return _this; + }()]: 123 +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/destructuring/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/destructuring/input.js new file mode 100644 index 000000000000..955854cf72b6 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/destructuring/input.js @@ -0,0 +1 @@ +let foo = ({ bar }) => undefined; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/destructuring/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/destructuring/output.js new file mode 100644 index 000000000000..75699c25cac5 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/destructuring/output.js @@ -0,0 +1,3 @@ +let foo = function({ bar }) { + return undefined; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/empty-arguments/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/empty-arguments/input.js new file mode 100644 index 000000000000..801aca71571b --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/empty-arguments/input.js @@ -0,0 +1 @@ +var t = () => 5 + 5; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/empty-arguments/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/empty-arguments/output.js new file mode 100644 index 000000000000..1aad3c29058d --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/empty-arguments/output.js @@ -0,0 +1,3 @@ +var t = function() { + return 5 + 5; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/expression/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/expression/input.js new file mode 100644 index 000000000000..126c6c136401 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/expression/input.js @@ -0,0 +1 @@ +arr.map(x => x * x); \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/expression/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/expression/output.js new file mode 100644 index 000000000000..85f87d989ff3 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/expression/output.js @@ -0,0 +1,3 @@ +arr.map(function(x) { + return x * x; +}); diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/fixture-arguments/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/fixture-arguments/input.js new file mode 100644 index 000000000000..6682461393f6 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/fixture-arguments/input.js @@ -0,0 +1,9 @@ +function fn() { + var foo = () => { + return arguments; + }; +} + +var bar = () => arguments; + +var baz = () => () => arguments; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/fixture-arguments/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/fixture-arguments/output.js new file mode 100644 index 000000000000..eb7fd52a4384 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/fixture-arguments/output.js @@ -0,0 +1,15 @@ +var _arguments = arguments; +function fn() { + var _arguments = arguments; + var foo = function() { + return _arguments; + }; +} +var bar = function() { + return _arguments; +}; +var baz = function() { + return function() { + return _arguments; + }; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/fixture-this/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/fixture-this/input.js new file mode 100644 index 000000000000..8136b60c5d29 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/fixture-this/input.js @@ -0,0 +1,16 @@ +function b() { + var t = x => this.x + x; +} + +class Foo extends (function () { }) { + constructor() { + var foo = () => this; + + if (true) { + console.log(super(), foo()); + } else { + super(); + console.log(foo()); + } + } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/fixture-this/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/fixture-this/output.js new file mode 100644 index 000000000000..fe4f41c291c2 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/fixture-this/output.js @@ -0,0 +1,20 @@ +function b() { + var _this = this; + var t = function(x) { + return _this.x + x; + }; +} +class Foo extends function() {} { + constructor(){ + var _this; + var foo = function() { + return _this; + }; + if (true) { + console.log((super(), _this = this), foo()); + } else { + super(), _this = this; + console.log(foo()); + } + } +} diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/getter-setter/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/getter-setter/input.js new file mode 100644 index 000000000000..c7f11d22484b --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/getter-setter/input.js @@ -0,0 +1,5 @@ +const a = () => ({ + get this() { this; arguments }, + set arguments(a = this) { this; arguments }, + get [this]() { this; arguments }, +}) \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/getter-setter/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/getter-setter/output.js new file mode 100644 index 000000000000..7022a56c94c2 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/getter-setter/output.js @@ -0,0 +1,17 @@ +var _this = this; +const a1 = function() { + return { + get this () { + this; + arguments; + }, + set arguments (a = this){ + this; + arguments; + }, + get [_this] () { + this; + arguments; + } + }; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/inside-call/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/inside-call/input.js new file mode 100644 index 000000000000..74cf708f7606 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/inside-call/input.js @@ -0,0 +1 @@ +arr.map(i => i + 1); \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/inside-call/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/inside-call/output.js new file mode 100644 index 000000000000..af30eb2ba358 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/inside-call/output.js @@ -0,0 +1,3 @@ +arr.map(function(i) { + return i + 1; +}); diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-1/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-1/input.js new file mode 100644 index 000000000000..fc8a6cd666f2 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-1/input.js @@ -0,0 +1 @@ +const foo = () => this \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-1/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-1/output.js new file mode 100644 index 000000000000..471f59ad0ab2 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-1/output.js @@ -0,0 +1,4 @@ +var _this = this; +const foo = function() { + return _this; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-2/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-2/input.js new file mode 100644 index 000000000000..6448cb07d95b --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-2/input.js @@ -0,0 +1,3 @@ +const foo = function () { + () => () => () => this +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-2/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-2/output.js new file mode 100644 index 000000000000..f170e04b2193 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/issue-2212-2/output.js @@ -0,0 +1,10 @@ +const foo = function() { + var _this = this; + (function() { + return function() { + return function() { + return _this; + }; + }; + }); +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/issue-233/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/issue-233/input.js new file mode 100644 index 000000000000..0b6de017d2f6 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/issue-233/input.js @@ -0,0 +1 @@ +const foo = () => ({ x, ...y }) => y \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/issue-233/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/issue-233/output.js new file mode 100644 index 000000000000..ebefea790833 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/issue-233/output.js @@ -0,0 +1,5 @@ +const foo = function() { + return function({ x , ...y }) { + return y; + }; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/issue-413/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/issue-413/input.js new file mode 100644 index 000000000000..41f466e759e1 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/issue-413/input.js @@ -0,0 +1,3 @@ +export const getBadgeBorderRadius = (text, color) => { + return (text && style) || {} +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/issue-413/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/issue-413/output.js new file mode 100644 index 000000000000..b281a498f5dd --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/issue-413/output.js @@ -0,0 +1,3 @@ +export const getBadgeBorderRadius = function(text, color) { + return text && style || {}; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/method-captured/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/method-captured/input.js new file mode 100644 index 000000000000..b26d2dd5981e --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/method-captured/input.js @@ -0,0 +1,7 @@ +const a = () => ({ + [this](a = this) { this; arguments }, +}) +const b = () => class { + static [this]() { } + [arguments]() { } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/method-captured/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/method-captured/output.js new file mode 100644 index 000000000000..f12198eb00b4 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/method-captured/output.js @@ -0,0 +1,15 @@ +var _this = this; +const a = function() { + return { + [_this] (a = this) { + this; + arguments; + } + }; +}; +const b = function() { + return class { + static [this]() {} + [arguments]() {} + }; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/multiple-arguments/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/multiple-arguments/input.js new file mode 100644 index 000000000000..4c408ad1f24a --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/multiple-arguments/input.js @@ -0,0 +1 @@ +var t = (i, x) => i * x; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/multiple-arguments/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/multiple-arguments/output.js new file mode 100644 index 000000000000..80dfef4ea6f6 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/multiple-arguments/output.js @@ -0,0 +1,3 @@ +var t = function(i, x) { + return i * x; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/paren-insertion/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/paren-insertion/input.js new file mode 100644 index 000000000000..f0b4aba4b4b7 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/paren-insertion/input.js @@ -0,0 +1 @@ +var t = i => i * 5; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/paren-insertion/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/paren-insertion/output.js new file mode 100644 index 000000000000..8c6195e0d3c7 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/paren-insertion/output.js @@ -0,0 +1,3 @@ +var t = function(i) { + return i * 5; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/single-argument/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/single-argument/input.js new file mode 100644 index 000000000000..d6eda13c89fe --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/single-argument/input.js @@ -0,0 +1 @@ +var t = (i) => i * 5; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/single-argument/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/single-argument/output.js new file mode 100644 index 000000000000..8c6195e0d3c7 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/single-argument/output.js @@ -0,0 +1,3 @@ +var t = function(i) { + return i * 5; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/statement/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/statement/input.js new file mode 100644 index 000000000000..fc21a6527577 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/statement/input.js @@ -0,0 +1,5 @@ +nums.forEach(v => { + if (v % 5 === 0) { + fives.push(v); + } +}); \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/statement/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/statement/output.js new file mode 100644 index 000000000000..f055880460d2 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/statement/output.js @@ -0,0 +1,5 @@ +nums.forEach(function(v) { + if (v % 5 === 0) { + fives.push(v); + } +}); diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/this-in-params/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/this-in-params/input.js new file mode 100644 index 000000000000..54f9bc7592b9 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/this-in-params/input.js @@ -0,0 +1,3 @@ +export const getBadgeBorderRadius = (text = this, color = arguments) => { + return (text && style) || {} +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/this-in-params/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/this-in-params/output.js new file mode 100644 index 000000000000..111a4a627cdd --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/this-in-params/output.js @@ -0,0 +1,4 @@ +var _this = this, _arguments = arguments; +export const getBadgeBorderRadius = function(text = _this, color = _arguments) { + return text && style || {}; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/two-arrow/input.js b/crates/swc_ecma_transforms_compat/tests/arrow/two-arrow/input.js new file mode 100644 index 000000000000..976ec5169d3f --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/two-arrow/input.js @@ -0,0 +1,4 @@ +let foo = () => this; +let bar = () => this; +let foo1 = () => arguments; +let bar1 = () => arguments; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/arrow/two-arrow/output.js b/crates/swc_ecma_transforms_compat/tests/arrow/two-arrow/output.js new file mode 100644 index 000000000000..7326ff079875 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/arrow/two-arrow/output.js @@ -0,0 +1,13 @@ +var _this = this, _arguments = arguments; +let foo = function() { + return _this; +}; +let bar = function() { + return _this; +}; +let foo1 = function() { + return _arguments; +}; +let bar1 = function() { + return _arguments; +}; diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs b/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs index 0df02227c067..edf525180c39 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs @@ -1,7 +1,9 @@ +use std::path::PathBuf; + use swc_common::{chain, Mark}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2015::arrow; -use swc_ecma_transforms_testing::{compare_stdout, test}; +use swc_ecma_transforms_testing::{compare_stdout, test, test_fixture}; use swc_ecma_visit::Fold; fn tr() -> impl Fold { @@ -10,181 +12,6 @@ fn tr() -> impl Fold { chain!(resolver(unresolved, global, false), arrow(unresolved)) } -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - issue_233, - "const foo = () => ({ x, ...y }) => y", - "const foo = function() { - return function({ x , ...y }) { - return y; - }; -};" -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - destructuring, - r#"let foo = ({bar}) => undefined;"#, - r#"let foo = function ({bar}) { - return undefined; -}"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - basic, - r#"let echo = (bar) => bar"#, - r#"let echo = function(bar) { - return bar; - }"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - empty_arguments, - r#"var t = () => 5 + 5;"#, - r#"var t = function () { - return 5 + 5; -};"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - expression, - r#"arr.map(x => x * x);"#, - r#"arr.map(function (x) { - return x * x; -});"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - inside_call, - r#"arr.map(i => i + 1);"#, - r#"arr.map(function (i) { - return i + 1; -});"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - multiple_arguments, - r#"var t = (i, x) => i * x;"#, - r#"var t = function (i, x) { - return i * x; -};"# -); - -// test!(::swc_ecma_parser::Syntax::default(), -// |_| arrow(Mark::new()), -// nested, -// r#"module.exports = { -// init: function () { -// return new Promise((resolve, reject) => { -// MongoClient.connect(config.mongodb, (err, db) => { -// if (err) { -// return reject(err); -// } -// this.db = db; -// resolve(this); -// }); -// }); -// } -// };"#, -// r#"module.exports = { -// init: function () { -// var _this = this; - -// return new Promise(function (resolve, reject) { -// MongoClient.connect(config.mongodb, function (err, db) { -// if (err) { -// return reject(err); -// } - -// _this.db = db; -// resolve(_this); -// }); -// }); -// } -// };"# -// ); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - paren_insertion, - r#"var t = i => i * 5;"#, - r#"var t = function (i) { - return i * 5; -};"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - single_argument, - r#"var t = (i) => i * 5;"#, - r#"var t = function (i) { - return i * 5; -};"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - statement, - r#"nums.forEach(v => { - if (v % 5 === 0) { - fives.push(v); - } -});"#, - r#"nums.forEach(function (v) { - if (v % 5 === 0) { - fives.push(v); - } -});"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - issue_413, - r#" -export const getBadgeBorderRadius = (text, color) => { - return (text && style) || {} -}"#, - r#" -export const getBadgeBorderRadius = function(text, color) { - return text && style || { - }; -}; -"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| tr(), - arguments, - r#" -function test() { - return () => arguments[0]; -}"#, - r#" - function test() { - var _arguments = arguments; - return function() { - return _arguments[0]; - } - }"# -); - compare_stdout!( ::swc_ecma_parser::Syntax::default(), |_| tr(), @@ -225,310 +52,21 @@ compare_stdout!( " ); -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - arguments_member, - r#" - function test() { - return (foo) => { - return foo.arguments; - } - }"#, - r#" - function test() { - return function(foo) { - return foo.arguments; - }; - }"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - arguments_fn_expr, - r#" - function test() { - return function() { - return arguments[0]; - }; - }"#, - r#" - function test() { - return function() { - return arguments[0]; - }; - }"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - issue_2212_1, - "const foo = () => this", - " - var _this = this; - const foo = function() { - return _this; - }; - " -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - issue_2212_2, - " - const foo = function (){ - () => () => () => this - } - ", - " - const foo = function() { - var _this = this; - (function() { - return function() { - return function() { - return _this; - }; - }; - }); - }; - " -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - fixture_this, - r#" -function b() { - var t = x => this.x + x; -} - -class Foo extends (function(){}) { - constructor(){ - var foo = () => this; - - if (true){ - console.log(super(), foo()); - } else { - super(); - console.log(foo()); - } - } -} -"#, - r#" -function b() { - var _this = this; - - var t = function (x) { - return _this.x + x; - }; -} - -class Foo extends function () {} { - constructor() { - var _this; - - var foo = function () { - return _this; - }; - - if (true) { - console.log((super(), _this = this), foo()); - } else { - super(), _this = this; - console.log(foo()); - } - } - -} -"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| tr(), - fixture_arguments, - r#" -function fn() { - var foo = () => { - return arguments; - }; -} - -var bar = () => arguments; - -var baz = () => () => arguments; -"#, - r#" -var _arguments = arguments; - -function fn() { - var _arguments = arguments; - - var foo = function () { - return _arguments; - }; -} - -var bar = function () { - return _arguments; -}; - -var baz = function () { - return function () { - return _arguments; - }; -}; -"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| tr(), - two_arrow, - r#" -let foo = () => this; -let bar = () => this; -let foo1 = () => arguments; -let bar1 = () => arguments; -"#, - r#" -var _this = this, _arguments = arguments; -let foo = function () { - return _this; -} -let bar = function () { - return _this; -} -let foo1 = function () { - return _arguments; -} -let bar1 = function () { - return _arguments; -} -"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - computed_props, - r#" -var a = { - [(() => this)()]: 123 -} -"#, - r#" -var _this = this; - -var a = { - [function () { - return _this; - }()]: 123 -}; -"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| tr(), - this_in_params, - r#" -export const getBadgeBorderRadius = (text = this, color = arguments) => { - return (text && style) || {} -}"#, - r#" -var _this = this, _arguments = arguments; -export const getBadgeBorderRadius = function(text = _this, color = _arguments) { - return text && style || { - }; -}; -"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - getter_setter, - r#" -const a = () => ({ - get this() { this;arguments }, - set arguments(a = this) { this;arguments }, - get [this]() { this;arguments }, -}) -"#, - r#" -var _this = this; -const a = function () { - return { - get this() { - this; - arguments; - }, - - set arguments(a = this) { - this; - arguments; - }, - - get [_this] () { - this; - arguments; - } - }; -}; -"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - method_computed, - r#" -const a = () => ({ - [this](a = this) { this;arguments }, -}) -const b = () => class { - static [this]() {} - [arguments]() {} +#[testing::fixture("tests/arrow/**/input.js")] +fn fixture(input: PathBuf) { + let output = input.with_file_name("output.js"); + + test_fixture( + Default::default(), + &|_| { + let unresolved_mark = Mark::new(); + chain!( + resolver(unresolved_mark, Mark::new(), false), + arrow(unresolved_mark) + ) + }, + &input, + &output, + Default::default(), + ); } -"#, - r#" -var _this = this; -const a = function () { - return { - [_this](a = this) { - this; - arguments; - } - }; -}; -const b = function() { - return class { - static [this]() {} - [arguments]() { - } - }; -}; -"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| arrow(Mark::new()), - chrome_46, - "function foo() { - const a = (a) => new.target - }", - "function foo() { - var _newtarget = new.target; - - const a = function (a) { - return _newtarget; - }; - }" -); diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs b/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs index 7c527c43c57e..eb1fa6b68c77 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs @@ -5319,7 +5319,7 @@ const obj = new Obj(); expect(() => { obj.call(); - // Asser that this throws, but that it's not + // Assert that this throws, but that it's not // a gobbledygook error that is thrown }).toThrowError(TypeError) diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_for_of.rs b/crates/swc_ecma_transforms_compat/tests/es2015_for_of.rs index d7b0572b8945..dc062423e0f6 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_for_of.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_for_of.rs @@ -7,81 +7,14 @@ use swc_ecma_transforms_compat::es2015::{ self, for_of::{for_of, Config}, }; -use swc_ecma_transforms_testing::{compare_stdout, test, test_exec}; +use swc_ecma_transforms_testing::{ + compare_stdout, test, test_exec, test_fixture, FixtureTestConfig, +}; fn syntax() -> Syntax { Default::default() } -test!( - syntax(), - |_| for_of(Default::default()), - spec_identifier, - r#"for (i of arr) { -}"#, - r#"var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; - -try { - for (var _iterator = arr[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = - _iterator.next()).done); _iteratorNormalCompletion = true) { - i = _step.value; - } -} catch (err) { - _didIteratorError = true; - _iteratorError = err; -} finally { - try { - if (!_iteratorNormalCompletion && _iterator.return != null) { - _iterator.return(); - } - } finally { - if (_didIteratorError) { - throw _iteratorError; - } - } -}"#, - ok_if_code_eq -); - -test!( - syntax(), - |_| for_of(Default::default()), - spec_ignore_cases, - r#"for (var i of foo) { - switch (i) { - case 1: - break; - } -}"#, - r#"var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; - -try { - for (var _iterator = foo[Symbol.iterator](), _step; !(_iteratorNormalCompletion = - (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { - var i = _step.value; - - switch (i) { - case 1: - break; - } - } -} catch (err) { - _didIteratorError = true; - _iteratorError = err; -} finally { - try { - if (!_iteratorNormalCompletion && _iterator.return != null) { - _iterator.return(); - } - } finally { - if (_didIteratorError) { - throw _iteratorError; - } - } -}"#, - ok_if_code_eq -); - test!( syntax(), |_| for_of(Default::default()), @@ -609,7 +542,7 @@ if (true) loop: for(let _i = 0, _iter = []; _i < _iter.length; _i++){ ); #[testing::fixture("tests/for-of/**/exec.js")] -fn fixture(input: PathBuf) { +fn exec(input: PathBuf) { let input = read_to_string(&input).unwrap(); compare_stdout( @@ -629,8 +562,33 @@ fn fixture(input: PathBuf) { ); } +#[testing::fixture("tests/for-of/**/input.js")] +fn fixture(input: PathBuf) { + let output = input.with_file_name("output.js"); + + test_fixture( + Syntax::default(), + &|_| { + let top_level_mark = Mark::new(); + + chain!( + resolver(Mark::new(), top_level_mark, false), + for_of(Config { + assume_array: false, + ..Default::default() + }) + ) + }, + &input, + &output, + FixtureTestConfig { + ..Default::default() + }, + ); +} + #[testing::fixture("tests/for-of/**/exec.js")] -fn fixture_es2015(input: PathBuf) { +fn exec_es2015(input: PathBuf) { let input = read_to_string(&input).unwrap(); compare_stdout( diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs b/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs index 01b30f8e08b0..7acf5ceb02f4 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs @@ -103,69 +103,6 @@ fn fixture(input: PathBuf) { ) } -test!( - ::swc_ecma_parser::Syntax::default(), - |_| new_target(), - edge_12, - r#"function foo() { - const a = () => new.target - }"#, - r#"function foo() { - const a = () => this instanceof foo ? this.constructor : void 0 - }"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| new_target(), - issue_4193, - r#"const v0 = Symbol("Context#bar"); - module.exports = { - get bar() { - if (new.target === "foo") { - return; - } - return new Proxy(this, v0); - } - };"#, - r#"const v0 = Symbol("Context#bar"); - module.exports = { - get bar() { - if (void 0 === "foo") { - return; - } - return new Proxy(this, v0); - } - };"# -); - -test!( - ::swc_ecma_parser::Syntax::default(), - |_| new_target(), - edge_13, - r#" -class A { - foo() { - return () => new.target - } - - constructor() { - () => new.target - } -} -"#, - r#" -class A { - foo() { - return ()=>void 0; - } - constructor(){ - ()=>this.constructor; - } -} -"# -); - test!( ::swc_ecma_parser::Syntax::default(), |t| chain!( diff --git a/crates/swc_ecma_transforms_compat/tests/for-of/spec-identifier/input.js b/crates/swc_ecma_transforms_compat/tests/for-of/spec-identifier/input.js new file mode 100644 index 000000000000..f7a368a40d3c --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/for-of/spec-identifier/input.js @@ -0,0 +1,2 @@ +for (i of arr) { +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/for-of/spec-identifier/output.js b/crates/swc_ecma_transforms_compat/tests/for-of/spec-identifier/output.js new file mode 100644 index 000000000000..7dd8bf4c7633 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/for-of/spec-identifier/output.js @@ -0,0 +1,19 @@ +var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; +try { + for(var _iterator = arr[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ + i = _step.value; + } +} catch (err) { + _didIteratorError = true; + _iteratorError = err; +} finally{ + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally{ + if (_didIteratorError) { + throw _iteratorError; + } + } +} diff --git a/crates/swc_ecma_transforms_compat/tests/for-of/spec-ignore-cases/input.js b/crates/swc_ecma_transforms_compat/tests/for-of/spec-ignore-cases/input.js new file mode 100644 index 000000000000..ef2f644919c5 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/for-of/spec-ignore-cases/input.js @@ -0,0 +1,6 @@ +for (var i of foo) { + switch (i) { + case 1: + break; + } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/for-of/spec-ignore-cases/output.js b/crates/swc_ecma_transforms_compat/tests/for-of/spec-ignore-cases/output.js new file mode 100644 index 000000000000..28086ba0bc5e --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/for-of/spec-ignore-cases/output.js @@ -0,0 +1,23 @@ +var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; +try { + for(var _iterator = foo[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ + var i = _step.value; + switch(i){ + case 1: + break; + } + } +} catch (err) { + _didIteratorError = true; + _iteratorError = err; +} finally{ + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally{ + if (_didIteratorError) { + throw _iteratorError; + } + } +} diff --git a/crates/swc_ecma_transforms_compat/tests/new-target/edge-12/input.js b/crates/swc_ecma_transforms_compat/tests/new-target/edge-12/input.js new file mode 100644 index 000000000000..0916684d717b --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/new-target/edge-12/input.js @@ -0,0 +1,3 @@ +function foo() { + const a = () => new.target +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/new-target/edge-12/output.js b/crates/swc_ecma_transforms_compat/tests/new-target/edge-12/output.js new file mode 100644 index 000000000000..14816e55bd58 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/new-target/edge-12/output.js @@ -0,0 +1,3 @@ +function foo() { + const a = ()=>this instanceof foo ? this.constructor : void 0; +} diff --git a/crates/swc_ecma_transforms_compat/tests/new-target/edge-13/input.js b/crates/swc_ecma_transforms_compat/tests/new-target/edge-13/input.js new file mode 100644 index 000000000000..cc73e4d1a4cd --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/new-target/edge-13/input.js @@ -0,0 +1,9 @@ +class A { + foo() { + return () => new.target + } + + constructor() { + () => new.target + } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/new-target/edge-13/output.js b/crates/swc_ecma_transforms_compat/tests/new-target/edge-13/output.js new file mode 100644 index 000000000000..73976d3fcd96 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/new-target/edge-13/output.js @@ -0,0 +1,8 @@ +class A { + foo() { + return ()=>void 0; + } + constructor(){ + ()=>this.constructor; + } +} diff --git a/crates/swc_ecma_transforms_compat/tests/new-target/issue-4193/input.js b/crates/swc_ecma_transforms_compat/tests/new-target/issue-4193/input.js new file mode 100644 index 000000000000..7523652edf99 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/new-target/issue-4193/input.js @@ -0,0 +1,9 @@ +const v0 = Symbol("Context#bar"); +module.exports = { + get bar() { + if (new.target === "foo") { + return; + } + return new Proxy(this, v0); + } +}; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/new-target/issue-4193/output.js b/crates/swc_ecma_transforms_compat/tests/new-target/issue-4193/output.js new file mode 100644 index 000000000000..15513ac4ca6f --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/new-target/issue-4193/output.js @@ -0,0 +1,9 @@ +const v0 = Symbol("Context#bar"); +module.exports = { + get bar () { + if (void 0 === "foo") { + return; + } + return new Proxy(this, v0); + } +};