Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(babel-plugin-export-metadata): add case for export default memo() #1417

Merged
merged 1 commit into from
Mar 23, 2020
Merged
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
31 changes: 31 additions & 0 deletions other-packages/babel-plugin-export-metadata/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -130,6 +155,12 @@ const insertNodeExportDefault = t => (path, state) => {
}
break
}

case 'CallExpression': {
// case for: export default React.memo(Component).
replaceDefaultCallAddFileMetaProperties(t, path, filename)
break
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* ExportDefaultDeclaration with Call expression */
const foo = v => v
export default foo(5)
12 changes: 12 additions & 0 deletions other-packages/babel-plugin-export-metadata/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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', () => {
Expand Down