From df7072953e322fd4bd6ab7f969f9ed762736c550 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Sun, 28 Apr 2019 21:59:00 -0700 Subject: [PATCH] Initial support for importing prop types --- package.json | 3 +- src/__tests__/__snapshots__/main-test.js.snap | 141 ++++++++++++++++++ src/__tests__/fixtures/component_27.tsx | 2 +- src/__tests__/fixtures/component_28.tsx | 21 +++ src/__tests__/fixtures/component_29.tsx | 17 +++ src/__tests__/fixtures/component_30.js | 13 ++ src/__tests__/fixtures/component_31.js | 9 ++ src/__tests__/fixtures/component_32.js | 8 + src/babelParser.js | 12 +- .../__tests__/propDocblockHandler-test.js | 2 +- src/utils/getMemberValuePath.js | 2 +- src/utils/isReactComponentClass.js | 2 +- src/utils/resolveImportedValue.js | 113 ++++++++++++++ src/utils/resolveToModule.js | 2 +- src/utils/resolveToValue.js | 68 +++++++-- yarn.lock | 7 + 16 files changed, 399 insertions(+), 23 deletions(-) create mode 100644 src/__tests__/fixtures/component_28.tsx create mode 100644 src/__tests__/fixtures/component_29.tsx create mode 100644 src/__tests__/fixtures/component_30.js create mode 100644 src/__tests__/fixtures/component_31.js create mode 100644 src/__tests__/fixtures/component_32.js create mode 100644 src/utils/resolveImportedValue.js diff --git a/package.json b/package.json index 65b22b5c663..118b3e90f9f 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "commander": "^2.19.0", "doctrine": "^3.0.0", "node-dir": "^0.1.10", - "recast": "^0.17.6" + "recast": "^0.17.6", + "resolve": "^1.10.1" }, "devDependencies": { "@babel/cli": "^7.0.0", diff --git a/src/__tests__/__snapshots__/main-test.js.snap b/src/__tests__/__snapshots__/main-test.js.snap index 3652dd82b4f..6e9b91c15a2 100644 --- a/src/__tests__/__snapshots__/main-test.js.snap +++ b/src/__tests__/__snapshots__/main-test.js.snap @@ -1421,3 +1421,144 @@ Object { }, } `; + +exports[`main fixtures processes component "component_28.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_29.tsx" without errors 1`] = ` +Object { + "description": "This is a typescript component with imported prop types", + "displayName": "ImportedExtendedComponent", + "methods": Array [], + "props": Object { + "bar": Object { + "description": "", + "required": true, + "tsType": Object { + "name": "number", + }, + }, + "foo": Object { + "description": "", + "required": true, + "tsType": Object { + "name": "string", + }, + }, + }, +} +`; + +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", + }, + }, + }, +} +`; diff --git a/src/__tests__/fixtures/component_27.tsx b/src/__tests__/fixtures/component_27.tsx index 9cd7c4187ae..b7c84d7dfb5 100644 --- a/src/__tests__/fixtures/component_27.tsx +++ b/src/__tests__/fixtures/component_27.tsx @@ -8,7 +8,7 @@ import React, { Component } from 'react'; -interface Props { +export interface Props { foo: string } diff --git a/src/__tests__/fixtures/component_28.tsx b/src/__tests__/fixtures/component_28.tsx new file mode 100644 index 00000000000..69265650558 --- /dev/null +++ b/src/__tests__/fixtures/component_28.tsx @@ -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

Hello world

; +} diff --git a/src/__tests__/fixtures/component_29.tsx b/src/__tests__/fixtures/component_29.tsx new file mode 100644 index 00000000000..b0c7b95432a --- /dev/null +++ b/src/__tests__/fixtures/component_29.tsx @@ -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

Hello world

; +} diff --git a/src/__tests__/fixtures/component_30.js b/src/__tests__/fixtures/component_30.js new file mode 100644 index 00000000000..4b227a5bc62 --- /dev/null +++ b/src/__tests__/fixtures/component_30.js @@ -0,0 +1,13 @@ +import Button from './component_6'; +import PropTypes from 'prop-types'; + +export function CustomButton({color, ...otherProps}) { + return