test(ts/compat): Migrate inline tests to fixture testing (#6475)

This commit is contained in:
Donny/강동윤 2022-11-28 13:54:38 +09:00 committed by GitHub
parent 3d58e37d86
commit cc4646a4cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
62 changed files with 388 additions and 617 deletions

View File

@ -0,0 +1,5 @@
function test() {
return function () {
return arguments[0];
};
}

View File

@ -0,0 +1,5 @@
function test() {
return function() {
return arguments[0];
};
}

View File

@ -0,0 +1,5 @@
function test() {
return (foo) => {
return foo.arguments;
}
}

View File

@ -0,0 +1,5 @@
function test() {
return function(foo) {
return foo.arguments;
};
}

View File

@ -0,0 +1,3 @@
function test() {
return () => arguments[0];
}

View File

@ -0,0 +1,6 @@
function test() {
var _arguments = arguments;
return function() {
return _arguments[0];
};
}

View File

@ -0,0 +1 @@
let echo = (bar) => bar

View File

@ -0,0 +1,3 @@
let echo = function(bar) {
return bar;
};

View File

@ -0,0 +1,3 @@
function foo() {
const a = (a) => new.target
}

View File

@ -0,0 +1,6 @@
function foo() {
var _newtarget = new.target;
const a = function(a) {
return _newtarget;
};
}

View File

@ -0,0 +1,3 @@
var a = {
[(() => this)()]: 123
}

View File

@ -0,0 +1,6 @@
var _this = this;
var a = {
[function() {
return _this;
}()]: 123
};

View File

@ -0,0 +1 @@
let foo = ({ bar }) => undefined;

View File

@ -0,0 +1,3 @@
let foo = function({ bar }) {
return undefined;
};

View File

@ -0,0 +1 @@
var t = () => 5 + 5;

View File

@ -0,0 +1,3 @@
var t = function() {
return 5 + 5;
};

View File

@ -0,0 +1 @@
arr.map(x => x * x);

View File

@ -0,0 +1,3 @@
arr.map(function(x) {
return x * x;
});

View File

@ -0,0 +1,9 @@
function fn() {
var foo = () => {
return arguments;
};
}
var bar = () => arguments;
var baz = () => () => arguments;

View File

@ -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;
};
};

View File

@ -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());
}
}
}

View File

@ -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());
}
}
}

View File

@ -0,0 +1,5 @@
const a = () => ({
get this() { this; arguments },
set arguments(a = this) { this; arguments },
get [this]() { this; arguments },
})

View File

@ -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;
}
};
};

View File

@ -0,0 +1 @@
arr.map(i => i + 1);

View File

@ -0,0 +1,3 @@
arr.map(function(i) {
return i + 1;
});

View File

@ -0,0 +1 @@
const foo = () => this

View File

@ -0,0 +1,4 @@
var _this = this;
const foo = function() {
return _this;
};

View File

@ -0,0 +1,3 @@
const foo = function () {
() => () => () => this
}

View File

@ -0,0 +1,10 @@
const foo = function() {
var _this = this;
(function() {
return function() {
return function() {
return _this;
};
};
});
};

View File

@ -0,0 +1 @@
const foo = () => ({ x, ...y }) => y

View File

@ -0,0 +1,5 @@
const foo = function() {
return function({ x , ...y }) {
return y;
};
};

View File

@ -0,0 +1,3 @@
export const getBadgeBorderRadius = (text, color) => {
return (text && style) || {}
}

View File

@ -0,0 +1,3 @@
export const getBadgeBorderRadius = function(text, color) {
return text && style || {};
};

View File

@ -0,0 +1,7 @@
const a = () => ({
[this](a = this) { this; arguments },
})
const b = () => class {
static [this]() { }
[arguments]() { }
}

View File

@ -0,0 +1,15 @@
var _this = this;
const a = function() {
return {
[_this] (a = this) {
this;
arguments;
}
};
};
const b = function() {
return class {
static [this]() {}
[arguments]() {}
};
};

View File

@ -0,0 +1 @@
var t = (i, x) => i * x;

View File

@ -0,0 +1,3 @@
var t = function(i, x) {
return i * x;
};

View File

@ -0,0 +1 @@
var t = i => i * 5;

View File

@ -0,0 +1,3 @@
var t = function(i) {
return i * 5;
};

View File

@ -0,0 +1 @@
var t = (i) => i * 5;

View File

@ -0,0 +1,3 @@
var t = function(i) {
return i * 5;
};

View File

@ -0,0 +1,5 @@
nums.forEach(v => {
if (v % 5 === 0) {
fives.push(v);
}
});

View File

@ -0,0 +1,5 @@
nums.forEach(function(v) {
if (v % 5 === 0) {
fives.push(v);
}
});

View File

@ -0,0 +1,3 @@
export const getBadgeBorderRadius = (text = this, color = arguments) => {
return (text && style) || {}
}

View File

@ -0,0 +1,4 @@
var _this = this, _arguments = arguments;
export const getBadgeBorderRadius = function(text = _this, color = _arguments) {
return text && style || {};
};

View File

@ -0,0 +1,4 @@
let foo = () => this;
let bar = () => this;
let foo1 = () => arguments;
let bar1 = () => arguments;

View File

@ -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;
};

View File

@ -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;
};
}"#
);
#[testing::fixture("tests/arrow/**/input.js")]
fn fixture(input: PathBuf) {
let output = input.with_file_name("output.js");
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;
test_fixture(
Default::default(),
&|_| {
let unresolved_mark = Mark::new();
chain!(
resolver(unresolved_mark, Mark::new(), false),
arrow(unresolved_mark)
)
},
&input,
&output,
Default::default(),
);
}
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]() {}
}
"#,
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;
};
}"
);

View File

@ -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)

View File

@ -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(

View File

@ -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!(

View File

@ -0,0 +1,2 @@
for (i of arr) {
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,6 @@
for (var i of foo) {
switch (i) {
case 1:
break;
}
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,3 @@
function foo() {
const a = () => new.target
}

View File

@ -0,0 +1,3 @@
function foo() {
const a = ()=>this instanceof foo ? this.constructor : void 0;
}

View File

@ -0,0 +1,9 @@
class A {
foo() {
return () => new.target
}
constructor() {
() => new.target
}
}

View File

@ -0,0 +1,8 @@
class A {
foo() {
return ()=>void 0;
}
constructor(){
()=>this.constructor;
}
}

View File

@ -0,0 +1,9 @@
const v0 = Symbol("Context#bar");
module.exports = {
get bar() {
if (new.target === "foo") {
return;
}
return new Proxy(this, v0);
}
};

View File

@ -0,0 +1,9 @@
const v0 = Symbol("Context#bar");
module.exports = {
get bar () {
if (void 0 === "foo") {
return;
}
return new Proxy(this, v0);
}
};