Skip to content
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ The plugin takes three options, `toTitle` (required), `ifTitleFound` (optional),

- `ifTitleFound` is an optional string value that may either be set to:
- `'skip'` - skips adding a title if it has already been manually specified in the code
- `'transform'` - replaces the existing title with the value from `toTitle`
- `undefined` (or any other value) - raise an error if processing a file that already defines a title

- `renameDefaultExportsTo` is an optional string value that controls scenario 3 as described above. It is `undefined` by default.
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,37 @@ tester({
babelOptions: {
plugins: ["@babel/plugin-syntax-jsx"],
}
});

tester({
plugin,
tests: [
{
title: "Default export object with title and ifTitleFound: transform",
code: outdent`
import React from 'react';
import { Component } from './index';
export default {
title: "existing"
};
export const Default = () => <Component />;
`,
output: outdent`
import React from 'react';
import { Component } from './index';
export default {
title: "existingbar"
};
export const Default = () => <Component />;
`
}
],
pluginOptions: {
title: 'bar',
toTitle: (state) => state.title + state.opts.title,
ifTitleFound: 'transform'
},
babelOptions: {
plugins: ["@babel/plugin-syntax-jsx"],
}
});
32 changes: 25 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const TITLE_KEY = 'title';

const fixObjectDefaultExport = (path, t, title, ifTitleFound) => {
const fixObjectDefaultExport = (path, t, title, ifTitleFound, titlePropertyIndex) => {
if (path.node.declaration.properties) {
const titleProperty = path.node.declaration.properties.find(node =>
node.key && node.key.name === TITLE_KEY
);
if (titleProperty) {
const newTitleNode = t.objectProperty(t.identifier(TITLE_KEY), t.stringLiteral(title))
if (titlePropertyIndex !== -1) {
switch (ifTitleFound) {
case 'skip':
return;
case 'transform':
const newNode = t.cloneNode(path.node);
newNode.declaration.properties.splice(titlePropertyIndex, 1, newTitleNode);
path.replaceWith(newNode);
return;
default:
throw new Error(
`Default export object has a '${TITLE_KEY}' property; the title should, however, be generated. Please remove '${TITLE_KEY}'.`
Expand All @@ -17,7 +20,7 @@ const fixObjectDefaultExport = (path, t, title, ifTitleFound) => {
}
path.get('declaration').pushContainer(
'properties',
t.objectProperty(t.identifier(TITLE_KEY), t.stringLiteral(title))
newTitleNode
);
} else {
throw new Error('Default export object does not have properties.');
Expand Down Expand Up @@ -61,6 +64,21 @@ const plugin = babel => {
return;
}
state.defaultExportPath = path;
state.titleIndex = -1;
state.title = '';

const properties = path.node.declaration.properties;
if (properties) {
const titlePropertyIndex = properties.findIndex(node =>
node.key && node.key.name === TITLE_KEY
);
if (titlePropertyIndex !== -1) {
if (properties[titlePropertyIndex].value.type === 'StringLiteral') {
state.title = properties[titlePropertyIndex].value.value;
state.titleIndex = titlePropertyIndex;
}
}
}
},
ExportNamedDeclaration: (path, state) => {
if (state.handled) {
Expand Down Expand Up @@ -93,7 +111,7 @@ const plugin = babel => {

if (state.defaultExportPath) {
if (state.defaultExportPath.node.declaration.type === 'ObjectExpression') {
fixObjectDefaultExport(state.defaultExportPath, t, title, state.opts.ifTitleFound);
fixObjectDefaultExport(state.defaultExportPath, t, title, state.opts.ifTitleFound, state.titleIndex);
} else {
if (renameDefaultExportsTo) {
if (!state.namedDefaultExportPath) {
Expand Down