Skip to content

Commit 32c83df

Browse files
author
Wang Yilin
committed
[React Refresh] ignore typescript namespace syntax
1 parent 4298ddb commit 32c83df

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
1717
"@babel/plugin-syntax-import-meta": "^7.10.4",
1818
"@babel/plugin-syntax-jsx": "^7.10.4",
19+
"@babel/plugin-syntax-typescript": "^7.14.5",
1920
"@babel/plugin-transform-arrow-functions": "^7.10.4",
2021
"@babel/plugin-transform-async-to-generator": "^7.10.4",
2122
"@babel/plugin-transform-block-scoped-functions": "^7.10.4",
@@ -35,7 +36,6 @@
3536
"@babel/preset-flow": "^7.10.4",
3637
"@babel/preset-react": "^7.10.4",
3738
"@babel/traverse": "^7.11.0",
38-
"web-streams-polyfill": "^3.1.1",
3939
"abort-controller": "^3.0.0",
4040
"art": "0.10.1",
4141
"babel-eslint": "^10.0.3",
@@ -96,6 +96,7 @@
9696
"through2": "^3.0.1",
9797
"tmp": "^0.1.0",
9898
"typescript": "^3.7.5",
99+
"web-streams-polyfill": "^3.1.1",
99100
"webpack": "^4.41.2",
100101
"yargs": "^15.3.1"
101102
},

packages/react-refresh/src/ReactFreshBabelPlugin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ export default function(babel, opts = {}) {
494494
default:
495495
return;
496496
}
497+
498+
// This can happen with typescript namespace syntax
499+
// which allows named exports on non-top-level
500+
if (programPath.type !== 'Program') {
501+
return;
502+
}
503+
497504
const id = node.id;
498505
if (id === null) {
499506
// We don't currently handle anonymous default exports.
@@ -696,6 +703,12 @@ export default function(babel, opts = {}) {
696703
return;
697704
}
698705

706+
// This can happen with typescript namespace syntax
707+
// which allows named exports on non-top-level
708+
if (programPath.type !== 'Program') {
709+
return;
710+
}
711+
699712
// Make sure we're not mutating the same tree twice.
700713
// This can happen if another Babel plugin replaces parents.
701714
if (seenForRegistration.has(node)) {

packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,4 +536,18 @@ describe('ReactFreshBabelPlugin', () => {
536536
`),
537537
).toMatchSnapshot();
538538
});
539+
540+
it('ignores typescript namespace syntax', () => {
541+
expect(
542+
transform(
543+
`
544+
namespace NS {
545+
export const A = () => {};
546+
export function B() {};
547+
}
548+
`,
549+
{plugins: [['@babel/plugin-syntax-typescript', {isTSX: true}]]},
550+
),
551+
).toMatchSnapshot();
552+
});
539553
});

packages/react-refresh/src/__tests__/__snapshots__/ReactFreshBabelPlugin-test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ if (cond) {
254254
}
255255
`;
256256

257+
exports[`ReactFreshBabelPlugin ignores typescript namespace syntax 1`] = `
258+
namespace NS {
259+
export const A = () => {};
260+
export function B() {}
261+
;
262+
}
263+
`;
264+
257265
exports[`ReactFreshBabelPlugin ignores unnamed function declarations 1`] = `export default function () {}`;
258266

259267
exports[`ReactFreshBabelPlugin includes custom hooks into the signatures 1`] = `

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@
405405
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.13.0.tgz#806526ce125aed03373bc416a828321e3a6a33af"
406406
integrity sha512-ZPafIPSwzUlAoWT8DKs1W2VyF2gOWthGd5NGFMsBcMMol+ZhK+EQY/e6V96poa6PA/Bh+C9plWN0hXO1uB8AfQ==
407407

408+
"@babel/helper-plugin-utils@^7.14.5":
409+
version "7.14.5"
410+
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.14.5.tgz#5ac822ce97eec46741ab70a517971e443a70c5a9"
411+
integrity sha512-/37qQCE3K0vvZKwoK4XU/irIJQdIfCJuhU5eKnNxpFDsOkgFaUAwbv+RYw6eYgsC0E4hS7r5KqGULUogqui0fQ==
412+
408413
"@babel/helper-regex@^7.10.4":
409414
version "7.10.5"
410415
resolved "https://registry.yarnpkg.com/@babel/helper-regex/-/helper-regex-7.10.5.tgz#32dfbb79899073c415557053a19bd055aae50ae0"
@@ -847,6 +852,13 @@
847852
dependencies:
848853
"@babel/helper-plugin-utils" "^7.12.13"
849854

855+
"@babel/plugin-syntax-typescript@^7.14.5":
856+
version "7.14.5"
857+
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.14.5.tgz#b82c6ce471b165b5ce420cf92914d6fb46225716"
858+
integrity sha512-u6OXzDaIXjEstBRRoBCQ/uKQKlbuaeE5in0RvWdA4pN6AhqxTIwUsnHPU1CFZA/amYObMsuWhYfRl3Ch90HD0Q==
859+
dependencies:
860+
"@babel/helper-plugin-utils" "^7.14.5"
861+
850862
"@babel/plugin-transform-arrow-functions@^7.0.0":
851863
version "7.8.3"
852864
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.8.3.tgz#82776c2ed0cd9e1a49956daeb896024c9473b8b6"
@@ -6442,6 +6454,7 @@ eslint-plugin-no-unsanitized@3.1.2:
64426454

64436455
"eslint-plugin-react-internal@link:./scripts/eslint-rules":
64446456
version "0.0.0"
6457+
uid ""
64456458

64466459
eslint-plugin-react@^6.7.1:
64476460
version "6.10.3"

0 commit comments

Comments
 (0)