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

Support for importing types #352

Closed
wants to merge 13 commits into from
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"commander": "^2.19.0",
"doctrine": "^3.0.0",
"node-dir": "^0.1.10",
"resolve": "^1.10.1",
"strip-indent": "^3.0.0"
},
"devDependencies": {
Expand Down Expand Up @@ -79,5 +80,8 @@
"src"
],
"testRegex": "/__tests__/.*-test\\.js$"
},
"browser": {
"./dist/utils/resolveImportedValue.js": "./dist/utils/resolveImportedValue.browser.js"
}
}
210 changes: 210 additions & 0 deletions src/__tests__/__snapshots__/main-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1446,3 +1446,213 @@ Object {
},
}
`;

exports[`main fixtures processes component "component_29.tsx" without errors 1`] = `
Object {
"description": "This is a typescript component with imported prop types",
"displayName": "ImportedExtendedComponent",
"methods": Array [],
}
`;

exports[`main fixtures processes component "component_30.js" without errors 1`] = `
Object {
"description": "",
"displayName": "CustomButton",
"methods": Array [],
"props": Object {
"children": Object {
"description": "",
"required": true,
"type": Object {
"name": "string",
},
},
"color": Object {
"description": "",
"required": false,
"type": Object {
"name": "string",
},
},
"onClick": Object {
"description": "",
"required": false,
"type": Object {
"name": "func",
},
},
"style": Object {
"description": "",
"required": false,
"type": Object {
"name": "object",
},
},
},
}
`;

exports[`main fixtures processes component "component_31.js" without errors 1`] = `
Object {
"description": "",
"displayName": "SuperCustomButton",
"methods": Array [],
"props": Object {
"children": Object {
"description": "",
"required": true,
"type": Object {
"name": "string",
},
},
"onClick": Object {
"description": "",
"required": false,
"type": Object {
"name": "func",
},
},
"style": Object {
"description": "",
"required": false,
"type": Object {
"name": "object",
},
},
},
}
`;

exports[`main fixtures processes component "component_32.js" without errors 1`] = `
Object {
"description": "",
"displayName": "SuperDuperCustomButton",
"methods": Array [],
"props": Object {
"children": Object {
"description": "",
"required": true,
"type": Object {
"name": "string",
},
},
"onClick": Object {
"description": "",
"required": false,
"type": Object {
"name": "func",
},
},
"style": Object {
"description": "",
"required": false,
"type": Object {
"name": "object",
},
},
},
}
`;

exports[`main fixtures processes component "component_33.js" without errors 1`] = `
Object {
"description": "",
"displayName": "SuperDuperCustomButton",
"methods": Array [],
"props": Object {
"children": Object {
"description": "",
"required": true,
"type": Object {
"name": "string",
},
},
"onClick": Object {
"description": "",
"required": false,
"type": Object {
"name": "func",
},
},
"style": Object {
"description": "",
"required": false,
"type": Object {
"name": "object",
},
},
},
}
`;

exports[`main fixtures processes component "component_34.js" without errors 1`] = `
Object {
"description": "",
"displayName": "SuperDuperCustomButton",
"methods": Array [],
"props": Object {
"children": Object {
"description": "",
"required": true,
"type": Object {
"name": "string",
},
},
"onClick": Object {
"description": "",
"required": false,
"type": Object {
"name": "func",
},
},
"style": Object {
"description": "",
"required": false,
"type": Object {
"name": "object",
},
},
},
}
`;

exports[`main fixtures processes component "component_35.tsx" without errors 1`] = `
Object {
"description": "This is a typescript component with imported prop types",
"displayName": "ImportedComponent",
"methods": Array [],
"props": Object {
"foo": Object {
"description": "",
"required": true,
"tsType": Object {
"name": "string",
},
},
},
}
`;

exports[`main fixtures processes component "component_36.js" without errors 1`] = `
Object {
"description": "",
"displayName": "SuperDuperCustomButton",
"methods": Array [],
"props": Object {
"size": Object {
"defaultValue": Object {
"computed": true,
"value": "Sizes.EXTRA_LARGE",
},
"description": "",
"required": false,
"type": Object {
"computed": true,
"name": "enum",
"value": "Object.values(Sizes)",
},
},
},
}
`;
2 changes: 1 addition & 1 deletion src/__tests__/fixtures/component_27.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React, { Component } from 'react';

interface Props {
export interface Props {
foo: string
}

Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/component_29.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import React, { Component } from 'react';
import ExtendedProps from './component_28';

/**
* This is a typescript component with imported prop types
*/
export function ImportedExtendedComponent(props: ExtendedProps) {
return <h1>Hello world</h1>;
}
13 changes: 13 additions & 0 deletions src/__tests__/fixtures/component_30.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Button from './component_6';
import PropTypes from 'prop-types';

export function CustomButton({color, ...otherProps}) {
return <Button {...otherProps} style={{color}} />;
}

CustomButton.propTypes = {
...Button.propTypes,
color: PropTypes.string
};

export const sharedProps = Button.propTypes;
10 changes: 10 additions & 0 deletions src/__tests__/fixtures/component_31.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {CustomButton, sharedProps} from './component_30';
import PropTypes from 'prop-types';

export function SuperCustomButton({color, ...otherProps}) {
return <CustomButton {...otherProps} style={{color}} />;
}

SuperCustomButton.propTypes = sharedProps;
export {sharedProps};
export * from './component_32';
9 changes: 9 additions & 0 deletions src/__tests__/fixtures/component_32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {SuperCustomButton, sharedProps} from './component_31';
import PropTypes from 'prop-types';

export function SuperDuperCustomButton({color, ...otherProps}) {
return <SuperCustomButton {...otherProps} style={{color}} />;
}

SuperDuperCustomButton.propTypes = sharedProps;
export * from './component_31';
9 changes: 9 additions & 0 deletions src/__tests__/fixtures/component_33.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as C31 from './component_32';
import PropTypes from 'prop-types';

export function SuperDuperCustomButton({color, ...otherProps}) {
return <C31.SuperCustomButton {...otherProps} style={{color}} />;
}

SuperDuperCustomButton.propTypes = C31.sharedProps;
export {SuperCustomButton} from './component_32';
8 changes: 8 additions & 0 deletions src/__tests__/fixtures/component_34.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {SuperCustomButton} from './component_33';
import PropTypes from 'prop-types';

export function SuperDuperCustomButton({color, ...otherProps}) {
return <SuperCustomButton {...otherProps} style={{color}} />;
}

SuperDuperCustomButton.propTypes = SuperCustomButton.propTypes;
21 changes: 21 additions & 0 deletions src/__tests__/fixtures/component_35.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import React, { Component } from 'react';
import { Props as ImportedProps } from './component_27';

export default interface ExtendedProps extends ImportedProps {
bar: number
}

/**
* This is a typescript component with imported prop types
*/
export function ImportedComponent(props: ImportedProps) {
return <h1>Hello world</h1>;
}
16 changes: 16 additions & 0 deletions src/__tests__/fixtures/component_36.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Sizes } from '../common';
import T from 'prop-types';

function SuperDuperCustomButton() {
return <div />;
}

SuperDuperCustomButton.defaultProps = {
size: Sizes.EXTRA_LARGE,
};

SuperDuperCustomButton.propTypes = {
size: T.oneOf(Object.values(Sizes)),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't looked at the implementation yet, but this snapshot reflects the current behaviour. Is it safe to assume this PR does not (yet) handle this behaviour, but it will given this test was checked in? I'm expecting both of these references to Sizes to resolve to a value rather than those string literals.

Also, ../common is not implemented - not sure if on purpose or not.

};

export default SuperDuperCustomButton;
12 changes: 8 additions & 4 deletions src/babelParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ function buildOptions(
export default function buildParse(options?: Options = {}): Parser {
const { parserOptions, ...babelOptions } = options;
const parserOpts = buildOptions(parserOptions, babelOptions);
const opts = {
parserOpts,
...babelOptions,
};

return {
parse(src: string): ASTNode {
return babel.parseSync(src, {
parserOpts,
...babelOptions,
});
const ast = babel.parseSync(src, opts);
// Attach options to the Program node, for use when processing imports.
ast.program.options = options;
return ast;
},
};
}
2 changes: 1 addition & 1 deletion src/handlers/__tests__/propDocblockHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('propDocBlockHandler', () => {
const definition = parse(
getSrc(
`{
...Foo.propTypes,
...Bar.propTypes,
/**
* Foo comment
*/
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getMemberValuePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const LOOKUP_METHOD = {
[t.ClassExpression.name]: getClassMemberValuePath,
};

function isSupportedDefinitionType({ node }) {
export function isSupportedDefinitionType({ node }: NodePath) {
return (
t.ObjectExpression.check(node) ||
t.ClassDeclaration.check(node) ||
Expand Down
2 changes: 1 addition & 1 deletion src/utils/isReactComponentClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function isReactComponentClass(path: NodePath): boolean {
}

// React.Component or React.PureComponent
const superClass = resolveToValue(path.get('superClass'));
const superClass = resolveToValue(path.get('superClass'), false);
if (
match(superClass.node, { property: { name: 'Component' } }) ||
match(superClass.node, { property: { name: 'PureComponent' } })
Expand Down