Skip to content
Open
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,135 @@
/**
* 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 {
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();
}"
`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* 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 fixHermesV1SuperInObjectAccessor = require('../fix-hermes-v1-super-in-object-accessor');

test('rewrites identifier-keyed object getter using super.x to computed string key', () => {
const code = `
const obj = {
get name() {
return super.name;
},
};
`;

expect(transform(code, [fixHermesV1SuperInObjectAccessor]))
.toMatchInlineSnapshot(`
"const obj = {
get [\\"name\\"]() {
return super.name;
}
};"
`);
});

test('rewrites identifier-keyed object setter using super.x to computed string key', () => {
const code = `
const obj = {
set value(v) {
super.value = v;
},
};
`;

expect(transform(code, [fixHermesV1SuperInObjectAccessor]))
.toMatchInlineSnapshot(`
"const obj = {
set [\\"value\\"](v) {
super.value = v;
}
};"
`);
});

test('leaves super inside class method alone', () => {
const code = `
class Child extends Parent {
get name() {
return super.name;
}
}
`;

expect(transform(code, [fixHermesV1SuperInObjectAccessor]))
.toMatchInlineSnapshot(`
"class Child extends Parent {
get name() {
return super.name;
}
}"
`);
});

test('leaves super inside regular object method alone', () => {
const code = `
const obj = {
run() {
return super.run();
},
};
`;

expect(transform(code, [fixHermesV1SuperInObjectAccessor]))
.toMatchInlineSnapshot(`
"const obj = {
run() {
return super.run();
}
};"
`);
});

test('leaves super() call alone', () => {
const code = `
class Child extends Parent {
constructor() {
super();
}
}
`;

expect(transform(code, [fixHermesV1SuperInObjectAccessor]))
.toMatchInlineSnapshot(`
"class Child extends Parent {
constructor() {
super();
}
}"
`);
});

test('skips already-computed accessor', () => {
const code = `
const obj = {
get [keyName]() {
return super.value;
},
};
`;

expect(transform(code, [fixHermesV1SuperInObjectAccessor]))
.toMatchInlineSnapshot(`
"const obj = {
get [keyName]() {
return super.value;
}
};"
`);
});
Loading