Clone a custom hook node before use (#16019)

This commit is contained in:
Anton Korzunov 2019-07-03 02:54:10 +10:00 committed by Dan Abramov
parent 6cf2234a57
commit a865e4a642
3 changed files with 58 additions and 2 deletions

View File

@ -245,7 +245,7 @@ export default function(babel) {
key: fnHookCalls.map(call => call.name + '{' + call.key + '}').join('\n'),
customHooks: fnHookCalls
.filter(call => !isBuiltinHook(call.name))
.map(call => call.callee),
.map(call => t.cloneDeep(call.callee)),
};
}

View File

@ -15,7 +15,12 @@ function transform(input, options = {}) {
return wrap(
babel.transform(input, {
babelrc: false,
plugins: ['syntax-jsx', 'syntax-dynamic-import', freshPlugin],
plugins: [
'syntax-jsx',
'syntax-dynamic-import',
freshPlugin,
...(options.plugins || []),
],
}).code,
);
}
@ -387,6 +392,26 @@ describe('ReactFreshBabelPlugin', () => {
).toMatchSnapshot();
});
it('includes custom hooks into the signatures when commonjs target is used', () => {
// this test is passing with Babel 6
// but would fail for Babel 7 _without_ custom hook node being cloned for signature
expect(
transform(
`
import {useFancyState} from './hooks';
export default function App() {
const bar = useFancyState();
return <h1>{bar}</h1>;
}
`,
{
plugins: ['transform-es2015-modules-commonjs'],
},
),
).toMatchSnapshot();
});
it('generates valid signature for exotic ways to call Hooks', () => {
expect(
transform(`

View File

@ -218,6 +218,37 @@ var _c;
$RefreshReg$(_c, "App");
`;
exports[`ReactFreshBabelPlugin includes custom hooks into the signatures when commonjs target is used 1`] = `
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _s = $RefreshSig$();
exports.default = App;
var _hooks = require('./hooks');
function App() {
_s();
const bar = (0, _hooks.useFancyState)();
return <h1>{bar}</h1>;
}
_s(App, 'useFancyState{bar}', false, function () {
return [_hooks.useFancyState];
});
_c = App;
var _c;
$RefreshReg$(_c, 'App');
`;
exports[`ReactFreshBabelPlugin only registers pascal case functions 1`] = `
function hello() {