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 visibleName with dots #1675

Merged
merged 3 commits into from Sep 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/loaders/__tests__/examples-loader.spec.ts
Expand Up @@ -9,7 +9,17 @@ const query = {
shouldShowDefaultExample: false,
};

const subComponentQuery = {
file: '../fooSub.js',
displayName: 'FooComponent.SubComponent',
shouldShowDefaultExample: false,
};


const getQuery = (options = {}) => encode({ ...query, ...options }, '?');
const getSubComponentQuery = (options = {}) => encode({ ...subComponentQuery, ...options }, '?');



it('should return valid, parsable JS', () => {
const exampleMarkdown = `
Expand Down Expand Up @@ -201,6 +211,23 @@ it('should prepend example code with component require()', () => {
);
});

it('should prepend example code with root component require() for sub components', () => {
const exampleMarkdown = `<X/>`;
const result = examplesLoader.call(
{
query: getSubComponentQuery(),
_styleguidist: {},
} as any,
exampleMarkdown
);

expect(result).toBeTruthy();
expect(() => new Function(result)).not.toThrowError(SyntaxError);
expect(result).toMatch(
`const FooComponentSubComponent$0 = require('../fooSub.js');\\nconst FooComponentSubComponent = FooComponentSubComponent$0.default || (FooComponentSubComponent$0['FooComponentSubComponent'] || FooComponentSubComponent$0);`
);
});

it('should allow explicit import of React and component module', () => {
const exampleMarkdown = `
import React from 'react';
Expand Down
40 changes: 23 additions & 17 deletions src/loaders/utils/resolveESModule.ts
Expand Up @@ -8,20 +8,26 @@ import requireIt from './requireIt';
* @param name the name of the resulting variable
* @returns AST
*/
export default (requireRequest: string, name: string) => [
// const name$0 = require(path);
b.variableDeclaration('const', [
b.variableDeclarator(b.identifier(`${name}$0`), requireIt(requireRequest).toAST() as any),
]),
// const name = name$0.default || name$0[name] || name$0;
b.variableDeclaration('const', [
b.variableDeclarator(
b.identifier(name),
b.logicalExpression(
'||',
b.identifier(`${name}$0.default`),
b.logicalExpression('||', b.identifier(`${name}$0['${name}']`), b.identifier(`${name}$0`))
)
),
]),
];
export default (requireRequest: string, name: string) => {
// The name could possibly contain invalid characters for a JS variable name
// such as "." or "-".
const safeName = name ? name.replace(/\W/, '') : name;

return [
// const safeName$0 = require(path);
b.variableDeclaration('const', [
b.variableDeclarator(b.identifier(`${safeName}$0`), requireIt(requireRequest).toAST() as any),
]),
// const safeName = safeName$0.default || safeName$0[safeName] || safeName$0;
b.variableDeclaration('const', [
b.variableDeclarator(
b.identifier(safeName),
b.logicalExpression(
'||',
b.identifier(`${safeName}$0.default`),
b.logicalExpression('||', b.identifier(`${safeName}$0['${safeName}']`), b.identifier(`${safeName}$0`))
)
),
]),
]
};