diff --git a/other-packages/babel-plugin-export-metadata/src/index.js b/other-packages/babel-plugin-export-metadata/src/index.js index c85ab6d99..ff125ed2f 100644 --- a/other-packages/babel-plugin-export-metadata/src/index.js +++ b/other-packages/babel-plugin-export-metadata/src/index.js @@ -19,6 +19,11 @@ const replaceExportDefault = template(` export default NAME `) +const replaceExportDefaultCall = template(` + const NAME = DECLARATION + export default NAME +`) + const getFilename = state => { const rootDir = get(state, 'opts.root', process.cwd()) const filename = get(state, 'file.opts.filename') @@ -68,6 +73,26 @@ const renameDefaultAddFileMetaProperties = (t, path, filename, name) => { pathToInsert.replaceWithMultiple(nameExport) } +const replaceDefaultCallAddFileMetaProperties = (t, path, filename) => { + if (!filename) { + return + } + + const declaration = get(path, 'node.declaration') + const pathToInsert = findPathToInsert(path) + + const fallbackName = '__DOCZ_DUMMY_EXPORT_DEFAULT' + + // replace + const nameExport = replaceExportDefaultCall({ + NAME: fallbackName, + DECLARATION: declaration, + }) + + const [declPath] = pathToInsert.replaceWithMultiple(nameExport) + path.scope.registerDeclaration(declPath) +} + const insertNodeExport = t => (path, state) => { const filename = getFilename(state) if (/(\.cache|\.docz).+/.test(filename)) return @@ -130,6 +155,12 @@ const insertNodeExportDefault = t => (path, state) => { } break } + + case 'CallExpression': { + // case for: export default React.memo(Component). + replaceDefaultCallAddFileMetaProperties(t, path, filename) + break + } } } diff --git a/other-packages/babel-plugin-export-metadata/tests/__snapshots__/index.test.js.snap b/other-packages/babel-plugin-export-metadata/tests/__snapshots__/index.test.js.snap index 05c40eab3..6d8d67780 100644 --- a/other-packages/babel-plugin-export-metadata/tests/__snapshots__/index.test.js.snap +++ b/other-packages/babel-plugin-export-metadata/tests/__snapshots__/index.test.js.snap @@ -29,6 +29,25 @@ if (typeof foo5 !== 'undefined' && foo5 && foo5 === Object(foo5) && Object.isExt }" `; +exports[`export-metadata export default works with Call expression 1`] = ` +"/* ExportDefaultDeclaration with Call expression */ +const foo = v => v; + +const __DOCZ_DUMMY_EXPORT_DEFAULT = foo(5); + +export default __DOCZ_DUMMY_EXPORT_DEFAULT; + +if (typeof __DOCZ_DUMMY_EXPORT_DEFAULT !== 'undefined' && __DOCZ_DUMMY_EXPORT_DEFAULT && __DOCZ_DUMMY_EXPORT_DEFAULT === Object(__DOCZ_DUMMY_EXPORT_DEFAULT) && Object.isExtensible(__DOCZ_DUMMY_EXPORT_DEFAULT) && !__DOCZ_DUMMY_EXPORT_DEFAULT.hasOwnProperty('__filemeta')) { + Object.defineProperty(__DOCZ_DUMMY_EXPORT_DEFAULT, '__filemeta', { + configurable: true, + value: { + name: \\"__DOCZ_DUMMY_EXPORT_DEFAULT\\", + filename: \\"tests/fixtures/export-default/with-identifier.js\\" + } + }); +}" +`; + exports[`export-metadata export default works with Class declaration 1`] = ` "/* ExportDefaultDeclaration with Class declaration */ export default class Bar6 {} diff --git a/other-packages/babel-plugin-export-metadata/tests/fixtures/export-default/with-call-expression.js b/other-packages/babel-plugin-export-metadata/tests/fixtures/export-default/with-call-expression.js new file mode 100644 index 000000000..c39e59556 --- /dev/null +++ b/other-packages/babel-plugin-export-metadata/tests/fixtures/export-default/with-call-expression.js @@ -0,0 +1,3 @@ +/* ExportDefaultDeclaration with Call expression */ +const foo = v => v +export default foo(5) diff --git a/other-packages/babel-plugin-export-metadata/tests/index.test.js b/other-packages/babel-plugin-export-metadata/tests/index.test.js index 6b471c957..b060ae1dd 100644 --- a/other-packages/babel-plugin-export-metadata/tests/index.test.js +++ b/other-packages/babel-plugin-export-metadata/tests/index.test.js @@ -23,6 +23,9 @@ const exportDefaultFixtures = { withObjExpression: path.resolve( './tests/fixtures/export-default/with-obj-expression.js' ), + withCallExpression: path.resolve( + './tests/fixtures/export-default/with-call-expression.js' + ), } const reExportsFixtures = { @@ -113,6 +116,15 @@ describe('export-metadata', () => { expect(result.code).toMatchSnapshot() }) + + it('works with Call expression', () => { + const result = transformSync(exportDefaultCode.withCallExpression, { + plugins: [plugin], + filename: exportDefaultFixtures.withIdentifier, + }) + + expect(result.code).toMatchSnapshot() + }) }) describe('export named', () => {