From c7b25abc903bd38cf2f060e2c1a59695fcde00d5 Mon Sep 17 00:00:00 2001 From: Ankeet Maini Date: Sun, 24 May 2020 20:56:59 +0530 Subject: [PATCH 1/2] Add support for namespace import --- packages/babel-plugin-apply-mdx-type-props/index.js | 11 ++++++++++- packages/mdx/mdx-hast-to-jsx.js | 8 ++++++-- packages/mdx/test/fixtures/namespace-import.mdx | 8 ++++++++ packages/mdx/test/index.test.js | 9 +++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 packages/mdx/test/fixtures/namespace-import.mdx diff --git a/packages/babel-plugin-apply-mdx-type-props/index.js b/packages/babel-plugin-apply-mdx-type-props/index.js index d4fe24307..a4abeaf07 100644 --- a/packages/babel-plugin-apply-mdx-type-props/index.js +++ b/packages/babel-plugin-apply-mdx-type-props/index.js @@ -12,7 +12,7 @@ class BabelPluginApplyMdxTypeProp { return { visitor: { - JSXOpeningElement(path) { + JSXOpeningElement(path, state) { const jsxName = path.node.name.name if (startsWithCapitalLetter(jsxName)) { @@ -24,6 +24,15 @@ class BabelPluginApplyMdxTypeProp { t.stringLiteral(jsxName) ) ) + + const {namespacedImports} = state.opts + const isNamespaceImport = + namespacedImports && + namespacedImports.find(s => s.includes(jsxName)) + + if (isNamespaceImport) { + path.node.name.name += '.default' + } } } } diff --git a/packages/mdx/mdx-hast-to-jsx.js b/packages/mdx/mdx-hast-to-jsx.js index 7b8d75979..a965207ec 100644 --- a/packages/mdx/mdx-hast-to-jsx.js +++ b/packages/mdx/mdx-hast-to-jsx.js @@ -134,13 +134,17 @@ MDXContent.isMDXComponent = true` const babelPluginApplyMdxPropInstance = new BabelPluginApplyMdxProp() const babelPluginApplyMdxPropToExportsInstance = new BabelPluginApplyMdxProp() + const namespacedImports = importStatements + .split('\n') + .filter(i => i.includes('* as')) + const fnPostMdxTypeProp = transformSync(fn, { configFile: false, babelrc: false, plugins: [ require('@babel/plugin-syntax-jsx'), require('@babel/plugin-syntax-object-rest-spread'), - babelPluginApplyMdxPropInstance.plugin + [babelPluginApplyMdxPropInstance.plugin, {namespacedImports}] ] }).code @@ -150,7 +154,7 @@ MDXContent.isMDXComponent = true` plugins: [ require('@babel/plugin-syntax-jsx'), require('@babel/plugin-syntax-object-rest-spread'), - babelPluginApplyMdxPropToExportsInstance.plugin + [babelPluginApplyMdxPropToExportsInstance.plugin, {namespacedImports}] ] }).code diff --git a/packages/mdx/test/fixtures/namespace-import.mdx b/packages/mdx/test/fixtures/namespace-import.mdx new file mode 100644 index 000000000..cdcc5a398 --- /dev/null +++ b/packages/mdx/test/fixtures/namespace-import.mdx @@ -0,0 +1,8 @@ +import * as Foo from './baz' + +# hello world + + + + + diff --git a/packages/mdx/test/index.test.js b/packages/mdx/test/index.test.js index a5a1241d1..22c8b723c 100644 --- a/packages/mdx/test/index.test.js +++ b/packages/mdx/test/index.test.js @@ -16,6 +16,10 @@ const fixtureBlogPost = fs.readFileSync( path.join(__dirname, './fixtures/blog-post.mdx') ) +const fixtureNamespaceImport = fs.readFileSync( + path.join(__dirname, './fixtures/namespace-import.mdx') +) + it('Should output parseable JSX', async () => { const result = await mdx('Hello World') @@ -419,3 +423,8 @@ test('Should handle layout props', () => { MDXContent.isMDXComponent = true;" `) }) + +it('Should render namespaced element appropriately', async () => { + const result = await mdx(fixtureNamespaceImport) + expect(result).toContain(' Date: Sun, 24 May 2020 21:01:38 +0530 Subject: [PATCH 2/2] Add test for exported member. --- packages/mdx/test/index.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/mdx/test/index.test.js b/packages/mdx/test/index.test.js index 22c8b723c..ec29d0f2a 100644 --- a/packages/mdx/test/index.test.js +++ b/packages/mdx/test/index.test.js @@ -427,4 +427,5 @@ test('Should handle layout props', () => { it('Should render namespaced element appropriately', async () => { const result = await mdx(fixtureNamespaceImport) expect(result).toContain('