Skip to content

Commit e637f53

Browse files
authored
fix: Handle visibleName with non-alphanumeric characters (#1675)
Refs styleguidist/react-docgen-typescript#292
1 parent 4790f3f commit e637f53

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/loaders/__tests__/examples-loader.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ const query = {
99
shouldShowDefaultExample: false,
1010
};
1111

12+
const subComponentQuery = {
13+
file: '../fooSub.js',
14+
displayName: 'FooComponent.SubComponent',
15+
shouldShowDefaultExample: false,
16+
};
17+
18+
1219
const getQuery = (options = {}) => encode({ ...query, ...options }, '?');
20+
const getSubComponentQuery = (options = {}) => encode({ ...subComponentQuery, ...options }, '?');
21+
22+
1323

1424
it('should return valid, parsable JS', () => {
1525
const exampleMarkdown = `
@@ -201,6 +211,23 @@ it('should prepend example code with component require()', () => {
201211
);
202212
});
203213

214+
it('should prepend example code with root component require() for sub components', () => {
215+
const exampleMarkdown = `<X/>`;
216+
const result = examplesLoader.call(
217+
{
218+
query: getSubComponentQuery(),
219+
_styleguidist: {},
220+
} as any,
221+
exampleMarkdown
222+
);
223+
224+
expect(result).toBeTruthy();
225+
expect(() => new Function(result)).not.toThrowError(SyntaxError);
226+
expect(result).toMatch(
227+
`const FooComponentSubComponent$0 = require('../fooSub.js');\\nconst FooComponentSubComponent = FooComponentSubComponent$0.default || (FooComponentSubComponent$0['FooComponentSubComponent'] || FooComponentSubComponent$0);`
228+
);
229+
});
230+
204231
it('should allow explicit import of React and component module', () => {
205232
const exampleMarkdown = `
206233
import React from 'react';

src/loaders/utils/resolveESModule.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@ import requireIt from './requireIt';
88
* @param name the name of the resulting variable
99
* @returns AST
1010
*/
11-
export default (requireRequest: string, name: string) => [
12-
// const name$0 = require(path);
13-
b.variableDeclaration('const', [
14-
b.variableDeclarator(b.identifier(`${name}$0`), requireIt(requireRequest).toAST() as any),
15-
]),
16-
// const name = name$0.default || name$0[name] || name$0;
17-
b.variableDeclaration('const', [
18-
b.variableDeclarator(
19-
b.identifier(name),
20-
b.logicalExpression(
21-
'||',
22-
b.identifier(`${name}$0.default`),
23-
b.logicalExpression('||', b.identifier(`${name}$0['${name}']`), b.identifier(`${name}$0`))
24-
)
25-
),
26-
]),
27-
];
11+
export default (requireRequest: string, name: string) => {
12+
// The name could possibly contain invalid characters for a JS variable name
13+
// such as "." or "-".
14+
const safeName = name ? name.replace(/\W/, '') : name;
15+
16+
return [
17+
// const safeName$0 = require(path);
18+
b.variableDeclaration('const', [
19+
b.variableDeclarator(b.identifier(`${safeName}$0`), requireIt(requireRequest).toAST() as any),
20+
]),
21+
// const safeName = safeName$0.default || safeName$0[safeName] || safeName$0;
22+
b.variableDeclaration('const', [
23+
b.variableDeclarator(
24+
b.identifier(safeName),
25+
b.logicalExpression(
26+
'||',
27+
b.identifier(`${safeName}$0.default`),
28+
b.logicalExpression('||', b.identifier(`${safeName}$0['${safeName}']`), b.identifier(`${safeName}$0`))
29+
)
30+
),
31+
]),
32+
]
33+
};

0 commit comments

Comments
 (0)