Skip to content

fix: Correctly support ClassDeclarations without identifier #475

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions src/__tests__/__snapshots__/main-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,22 @@ Object {
}
`;

exports[`main fixtures processes component "component_41.tsx" without errors 1`] = `
Object {
"description": "",
"methods": Array [],
"props": Object {
"value": Object {
"description": "",
"required": true,
"tsType": Object {
"name": "string",
},
},
},
}
`;

exports[`main fixtures processes component "flow-export-type.js" without errors 1`] = `
Object {
"description": "This is a Flow class component",
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/fixtures/component_41.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as React from 'react';

interface IProps {
value: string;
}

export default class extends React.Component<IProps> {
render() {
return <div className='s-c-z-example'/>;
}
}
11 changes: 11 additions & 0 deletions src/handlers/__tests__/displayNameHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ describe('defaultPropsHandler', () => {
});

describe('ClassDeclaration', () => {
it('ignores classes without name', () => {
const definition = statement(`
export default class {
}
`).get('declaration');
expect(() =>
displayNameHandler(documentation, definition, noopImporter),
).not.toThrow();
expect(documentation.displayName).not.toBeDefined();
});

it('considers the class name', () => {
const definition = statement(`
class Foo {
Expand Down
5 changes: 3 additions & 2 deletions src/handlers/displayNameHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export default function displayNameHandler(
// Function and class declarations need special treatment. The name of the
// function / class is the displayName
if (
t.ClassDeclaration.check(path.node) ||
t.FunctionDeclaration.check(path.node)
(t.ClassDeclaration.check(path.node) ||
t.FunctionDeclaration.check(path.node)) &&
path.node.id != null
) {
documentation.set('displayName', getNameOrValue(path.get('id')));
} else if (
Expand Down