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's generic types #489

Open
GeneZharov opened this issue Mar 17, 2018 · 1 comment
Open

Flow's generic types #489

GeneZharov opened this issue Mar 17, 2018 · 1 comment

Comments

@GeneZharov
Copy link

It seems that import-js does not recognize flow's generic types:

function Fn<T>(x: T): T { return x; }

It cause import-js to insert:

import { T } from "lodash/fp";

Though T is not a function, and it is already defined in this code snippet. Here is the generic's doc: https://flow.org/en/docs/types/generics/

I tried to fix this, but it is a too complex task. I see that "babylon" parses generic type definitions correctly. I think we need to add generic type declarations to the context so that we can filter this names in "lib/findUndefinedIdentifiers.js". But we must treat js names and flow names in the context differently. If T is used as a js name then we need to import it. If T is used as a flow type then it is already defined.

This are the tests I wrote for this task:

describe('should recognize flow\'s generic type', () => {
  test('function statement', () => {
    expect(findUndefinedIdentifiers(parse(`
      function Fn<T>(x: T): T {
        return x;
      }
    `))).toEqual(new Set([]));
  });

  test('function expression', () => {
    expect(findUndefinedIdentifiers(parse(`
      const Fn = function Fn<T>(x: T): T {
        return x;
      };
    `))).toEqual(new Set([]));
  });

  test('arrow function', () => {
    expect(findUndefinedIdentifiers(parse(`
      const Fn = <T>(x: T): T => x;
    `))).toEqual(new Set([]));
  });

  test('type statement', () => {
    expect(findUndefinedIdentifiers(parse(`
      type Some<T> = T[];
    `))).toEqual(new Set([]));
  });

  test('class', () => {
    expect(findUndefinedIdentifiers(parse(`
      class Some<T> {
        prop: T;
        constructor(param: T) {
          this.prop = param;
        }
      }
    `))).toEqual(new Set([]));
  });

  test('interface', () => {
    expect(findUndefinedIdentifiers(parse(`
      interface Some<T> {
        foo: T,
        bar: T
      }
    `))).toEqual(new Set([]));
  });
});
@trotzig
Copy link
Collaborator

trotzig commented Mar 19, 2018

Thanks for the report and the reproduction steps in the form of unit tests! Let's see if someone else is willing to take this on. I don't have much investment in flowscript unfortunately, the only project using it is (ironically) import-js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants