Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @noflow
*/

'use strict';

const {transform} = require('../__mocks__/test-helpers');
const fixHermesV1AsyncArrowNonSimpleParams = require('../fix-hermes-v1-async-arrow-non-simple-params');

test('rewrites destructured object param with default to simple identifier', () => {
const code = `
const fn = async ({a = 1, b} = {}) => {
return await fetch(a + b);
};
`;

expect(transform(code, [fixHermesV1AsyncArrowNonSimpleParams]))
.toMatchInlineSnapshot(`
"const fn = async _p => {
var {
a = 1,
b
} = _p === undefined ? {} : _p;
return await fetch(a + b);
};"
`);
});

test('rewrites destructured array param to simple identifier', () => {
const code = `
const fn = async ([a, b]) => await Promise.resolve(a + b);
`;

expect(transform(code, [fixHermesV1AsyncArrowNonSimpleParams]))
.toMatchInlineSnapshot(`
"const fn = async _p => {
var [a, b] = _p;
return await Promise.resolve(a + b);
};"
`);
});

test('rewrites assignment-pattern param without enclosing destructure', () => {
const code = `
const fn = async (x = 5) => await use(x);
`;

expect(transform(code, [fixHermesV1AsyncArrowNonSimpleParams]))
.toMatchInlineSnapshot(`
"const fn = async _p => {
var x = _p === undefined ? 5 : _p;
return await use(x);
};"
`);
});

test('wraps body in inner async arrow when rest param is present', () => {
const code = `
const fn = async (...args) => await handle(args);
`;

expect(transform(code, [fixHermesV1AsyncArrowNonSimpleParams]))
.toMatchInlineSnapshot(`
"const fn = (...args) => (async () => {
return await handle(args);
})();"
`);
});

test('leaves async arrow with only simple identifier params alone', () => {
const code = `
const fn = async (a, b) => await fetch(a + b);
`;

expect(
transform(code, [fixHermesV1AsyncArrowNonSimpleParams]),
).toMatchInlineSnapshot(`"const fn = async (a, b) => await fetch(a + b);"`);
});

test('leaves non-async arrow alone', () => {
const code = `
const fn = ({a = 1, b} = {}) => a + b;
`;

expect(transform(code, [fixHermesV1AsyncArrowNonSimpleParams]))
.toMatchInlineSnapshot(`
"const fn = ({
a = 1,
b
} = {}) => a + b;"
`);
});

test('handles multiple params mixing simple and complex', () => {
const code = `
const fn = async (a, {b}, c = 1) => await all(a, b, c);
`;

expect(transform(code, [fixHermesV1AsyncArrowNonSimpleParams]))
.toMatchInlineSnapshot(`
"const fn = async (a, _p, _p2) => {
var {
b
} = _p;
var c = _p2 === undefined ? 1 : _p2;
return await all(a, b, c);
};"
`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @noflow
*/

'use strict';

const {transform} = require('../__mocks__/test-helpers');
const fixHermesV1ClassInFinally = require('../fix-hermes-v1-class-in-finally');

test('wraps class declaration in finally block in IIFE', () => {
const code = `
function run() {
try {
risky();
} finally {
class Logger {
log() { console.log('done'); }
}
new Logger().log();
}
}
`;

expect(transform(code, [fixHermesV1ClassInFinally])).toMatchInlineSnapshot(`
"function run() {
try {
risky();
} finally {
var Logger = (() => {
class Logger {
log() {
console.log('done');
}
}
return Logger;
})();
new Logger().log();
}
}"
`);
});

test('wraps class expression in finally block in IIFE', () => {
const code = `
function run() {
try {
risky();
} finally {
const Logger = class {
log() {}
};
new Logger().log();
}
}
`;

expect(transform(code, [fixHermesV1ClassInFinally])).toMatchInlineSnapshot(`
"function run() {
try {
risky();
} finally {
const Logger = (() => class Logger {
log() {}
})();
new Logger().log();
}
}"
`);
});

test('leaves class outside finally block alone', () => {
const code = `
function run() {
try {
class Inside {}
return new Inside();
} catch (e) {
class Caught {}
return new Caught();
}
}
`;

expect(transform(code, [fixHermesV1ClassInFinally])).toMatchInlineSnapshot(`
"function run() {
try {
class Inside {}
return new Inside();
} catch (e) {
class Caught {}
return new Caught();
}
}"
`);
});

test('leaves class declared at module scope alone', () => {
const code = `
class Module {}
new Module();
`;

expect(transform(code, [fixHermesV1ClassInFinally])).toMatchInlineSnapshot(`
"class Module {}
new Module();"
`);
});

test('does not enter nested function scope', () => {
const code = `
try {} finally {
function inner() {
class NestedFn {}
return new NestedFn();
}
inner();
}
`;

expect(transform(code, [fixHermesV1ClassInFinally])).toMatchInlineSnapshot(`
"try {} finally {
function inner() {
class NestedFn {}
return new NestedFn();
}
inner();
}"
`);
});

test('preserves inferred name when wrapping a named-binding class expression', () => {
const code = `
function run() {
try {} finally {
const Service = class {
ping() {}
};
return new Service();
}
}
`;

expect(transform(code, [fixHermesV1ClassInFinally])).toMatchInlineSnapshot(`
"function run() {
try {} finally {
const Service = (() => class Service {
ping() {}
})();
return new Service();
}
}"
`);
});

test('leaves an unbound class expression in finally anonymous', () => {
const code = `
function run() {
try {} finally {
return register(class {
run() {}
});
}
}
`;

expect(transform(code, [fixHermesV1ClassInFinally])).toMatchInlineSnapshot(`
"function run() {
try {} finally {
return register((() => class {
run() {}
})());
}
}"
`);
});
Loading