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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@babel/plugin-transform-template-literals": "^7.10.5",
"@babel/preset-flow": "^7.10.4",
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.15.0",
"@babel/traverse": "^7.11.0",
"web-streams-polyfill": "^3.1.1",
"abort-controller": "^3.0.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/react-refresh/src/ReactFreshBabelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,10 @@ export default function(babel, opts = {}) {
path.traverse(HookCallsVisitor);
},
exit(path) {
const registrations = registrationsByProgramPath.get(path);
if (registrations === undefined) {
const registrations = Array.from(
registrationsByProgramPath.values(),
).flatMap(p => p);
if (registrations === undefined || registrations.length === 0) {
return;
}

Expand Down
50 changes: 50 additions & 0 deletions packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,56 @@ describe('ReactFreshIntegration', () => {
expect(el.textContent).toBe('CXY');
}
});

it('does not crash with typescript namespaces', () => {
if (__DEV__) {
const twoStepTransform = function(source) {
const firstStep = babel.transformSync(source, {
configFile: false,
parserOpts: {
plugins: ['jsx', 'typescript'],
},
plugins: ['react-refresh/babel.js'],
}).code;
const secondStep = babel.transformSync(firstStep, {
presets: ['@babel/preset-typescript'],
filename: 'file.tsx',
}).code;
return secondStep;
};

const code = twoStepTransform(`
namespace NS {
export const Comp = () => {
return <div>A</div>
};
}

function App() {
return <NS.Comp />
}

export default App;
`);
render(code);
expect(container.firstChild.textContent).toBe('A');
const patchCode = twoStepTransform(`
namespace NS {
export const Comp = () => {
return <div>B</div>
};
}

function App() {
return <NS.Comp />
}

export default App;
`);
patch(patchCode);
expect(container.firstChild.textContent).toBe('B');
}
});
});
}
});
Loading