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

[flow] import type from another file does not resolve the type information #180

Closed
rosskevin opened this issue May 3, 2017 · 5 comments
Closed

Comments

@rosskevin
Copy link
Contributor

Problem

import type from another file does not resolve the underlying type information

breakpoints.js used throughout the project:

export type Breakpoints = 'xs' | 'sm' | 'md' | 'lg' | 'xl';

Hidden.js component:

import type { Breakpoints } from '../styles/breakpoints';

type Props = {
  /**
   * Hide the given breakpoint(s).
   */
  only?: Breakpoints | Array<Breakpoints>,
}

breakpoints_js_-material-ui-___projects_material-ui

Expected result

When I place the type Breakpoints locally in the file (no import type), I get the expected result:

build-api-docs_js_-material-ui-___projects_material-ui

Placing every type inside the file is a non-starter though, as it severely limits reuse of types throughout the application.

@fkling
Copy link
Member

fkling commented May 3, 2017

react-docgen only looks at files in isolation since it doesn't know how to resolve dependencies. This is basically a duplicate of #33.

A typical workaround is to process the type definition file separately to extract the type information and join the data yourself.

That said, I'm open to suggestions how we could solve this, bearing in mind that the module identifier resolution process must be pluggable/exchangeable.

@rosskevin
Copy link
Contributor Author

At the root, this does look like a duplicate of #33.

Regarding module resolution, I suspect it will have to be rather smart or reuse something from node or flow. I am unfamiliar with the code here, so not sure about the challenges, but let me just add some pertinent details.

Flow types are available from:

  1. libdefs - as set by the [libs] in the .flowconfig
  2. libdefs - as available in the <PROJECT_ROOT>/flow-typed directory
  3. shadow files - as available in any node_modules/* as *.js.flow files. e.g. flow-source-copy is script that helps distribute flow types with a package.
  4. localized in a single file in the project
  5. imported by a project file

libdefs are globally available or bound to a module namespace, whereas import type works just as you would import any other js function etc (either from a node_module or local file).

@rosskevin
Copy link
Contributor Author

I'm trying to venture down this path but I'm afraid I know little about this toolset.

I presume I would:

  1. Determine import type statements
  2. Gather exported flow types
  3. Recurse over all files found in number 1, until no more import type declarations found
  4. Do a normal parse
  5. Merge flow type definitions

First thing is to discover the types imported and from where. I determined how to get the information but not how to return it through the resolver/handler. I unclear if I'm on the right track or even using the full capability of this toolset.

Called from our api-docs/index.js

reactDocgen.parse(src, findFlowTypeImportDefinitions)
// @flow
import { utils } from 'react-docgen';
import type { NodePath } from './types';

/**
 * Given an AST, this function tries to find the exported flow type definition.
 */
export default function findFlowTypeImportDefinitions(
  ast: ASTNode,
  recast: Object,
): ?NodePath {

  function importTypeDeclaration(path: NodePath) {
    let definition
    if (path.node.importKind === 'type') {
      if (path.node.specifiers) {

        // to be done in the handler?
        //const specifiers = path.get('specifiers');
        //specifiers.each(
        //  (specifier) => {
        //    const local = specifier.get('local');
        //    types.push(local.value.name);
        //  },
        //);
        // structure return representing types import?
        //return {
        //  module: path.node.source.raw,
        //  types: types,
        //};


        definition = path;
      }
    }

//    if(!definition) {
//      return false;
//    }
    return false;
  }

  recast.visit(ast, {
    visitImportDeclaration: importTypeDeclaration
  });
}

I've got a branch started with these experiments.

Any guidance is appreciated. We need to solve this before our next major release.

@rosskevin
Copy link
Contributor Author

It seems that a new plugin inline's the flow types, this might solve this problem for me altogether.

https://twitter.com/thejameskyle/status/870764494917582850

https://github.com/thejameskyle/babel-plugin-react-flow-props-to-prop-types

@danez
Copy link
Collaborator

danez commented Jul 13, 2018

Duplicate of #33

@danez danez marked this as a duplicate of #33 Jul 13, 2018
@danez danez closed this as completed Jul 13, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants