Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

visit flow types - fixes #108 #109

Merged
merged 8 commits into from
Jun 2, 2015
Merged

visit flow types - fixes #108 #109

merged 8 commits into from
Jun 2, 2015

Conversation

hzoo
Copy link
Member

@hzoo hzoo commented May 21, 2015

@@ -187,10 +187,6 @@ var astTransformVisitor = {
return node.expression;
}

if (t.isFlow(node)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

So then we need to remove this line? #89 (comment)

@sebmck
Copy link
Contributor

sebmck commented May 21, 2015

This should do the trick. At least for very simple cases, although you can have deeply nested type annotations that this wont cover.

@hzoo
Copy link
Member Author

hzoo commented May 21, 2015

So then we would need to do the same for say variable declaration type, parameter return type, etc? Yeah I guess cover the common cases?

// fails at the moment
it("flow type var declaration", function () {
      verifyAndAssertMessages([
          "import type Foo from 'foo';",
          "var x: Foo = 1;",
          "x;"
        ].join("\n"),
        { "no-unused-vars": 1 },
        []
      );
    });

@sebmck
Copy link
Contributor

sebmck commented May 21, 2015

escope really is terrible for scope tracking. Yeah, those all will have to be visited too.

@hzoo
Copy link
Member Author

hzoo commented May 21, 2015

Ok this is kind of crazy - didn't cover everything but yeah. Are there any github projects using flow to test this stuff on?

@wincent
Copy link

wincent commented May 21, 2015

@hzoo: I'll ask in the internal Flow group at FB to see if anybody has any good open source examples.

@wincent
Copy link

wincent commented May 21, 2015

Somebody suggested the React Native example apps (eg. https://github.com/facebook/react-native/tree/master/Examples/2048 etc).

@hzoo
Copy link
Member Author

hzoo commented May 21, 2015

Ok cool I'l check it out - nice that it already has a .eslintrc too! (although rather old version) - maybe I'l do a PR.

@hzoo
Copy link
Member Author

hzoo commented May 22, 2015

Maybe we should just push a simple change (already done in this PR) to fix just the function a(foo: Foo) { (the original issue) and work on supporting everything else in another commit?

@hzoo hzoo force-pushed the i-108 branch 2 times, most recently from 1e88544 to c89a0e9 Compare May 23, 2015 00:35
@hzoo
Copy link
Member Author

hzoo commented May 23, 2015

Ok rewrote most of it since there were a lot of random issues. Should of got most of the types in Other https://gist.github.com/hzoo/e3a7db9be7a3b0c2094e


To fix still: #66 again because t.isFlow(node) is added back.

// have to visit TypeAlias?
// no-undef ExampleClass
type ExampleClass = {
  Component: ReactClass<any, any, any>,
  title: string,
  description: string,
};
var exampleClasses: Array<ExampleClass> = [];

Seems like you either get no-undef on the TypeAlias or no-unused-vars on the flow type annotation.


// don't visit declare or remove nodes?
// no-undef uri, isStatic
declare module 'image!story-background' {
  declare var uri: string;
  declare var isStatic: boolean;
}

Should we be removing the declares? Then you would add all variables to globals in .eslintrc?

if (t.isDeclareModule(node)) {
      return this.remove();
    }

Ok removing the declares.

@hzoo hzoo force-pushed the i-108 branch 2 times, most recently from bfb5ae5 to 5e94b81 Compare May 23, 2015 01:25
@hzoo hzoo changed the title visit parameter flow types - fixes #108 visit flow types - fixes #108 May 23, 2015
@hzoo
Copy link
Member Author

hzoo commented May 24, 2015

Ok so I guess we want type x = any; to act like a variable declaration? Ok so found that escope just has a scope.variables and looked for scope.variables.push(), which lead me to currentScope().__define which creates a variable.

From https://github.com/estools/escope/blob/master/src/variable.js#L72-L79 I found different variable types so I'm just using Variable.Variable which is just "Variable"

And then found out you can just do referencer.prototype.TypeAlias after removing the old code to not visit it to create a new variable definition!

Still need to add all the tests as well as visit the TypeAlias.right...

It looks like there is use of type x = mixed, or _search(text: mixed) { - is that supposed to be a built in type like MixedTypeAnnotation? @sebmck

@sebmck
Copy link
Contributor

sebmck commented May 24, 2015

@hzoo Nope. The mixed intype x = mixed;is just a normalGenericTypeAnnotation`.

@hzoo
Copy link
Member Author

hzoo commented May 24, 2015

Ok I guess there's just a lot of types that aren't imported explicitly in each file itself like ReactComponent, ReactElement, ReactPropsCheckType, ReactPropsChainableTypeChecker, SyntheticEvent in the react-native examples/libraries so eslint will give an error of no-undef (I'm assuming flow itself has all of these globally or from a file outside).

Examples of no-undef

// $Enum
{
  property: $Enum<typeof Properties>;
}
// mixed
  _search(text: mixed) {
// D, P, S
function renderApplication<D, P, S>(
  RootComponent: ReactClass<D, P, S>,
  initialProps: P,
  rootTag: any
) {
// T
getNode: function<T>(id: T): T {
    return id;
  }

@hzoo
Copy link
Member Author

hzoo commented May 24, 2015

I'm just going to add tests for everything next - do you think I need add/change anything at the moment @sebmck?

@sebmck
Copy link
Contributor

sebmck commented May 25, 2015

Doesn't look too bad. Only comment would be on the use of .bind(this) which is a bit inefficient.

@hzoo
Copy link
Member Author

hzoo commented May 25, 2015

Yeah ok, I should replace all those will just regular for loops.
And I guess update the PR based on the API changes you just did in #113?

Maybe the code would be better by refactoring it to use the visitor-keys data: https://github.com/babel/babel/blob/master/src/babel/types/visitor-keys.json#L79-L111?

@hzoo
Copy link
Member Author

hzoo commented May 25, 2015

Ok refactored the visitTypeAnnotation method in https://gist.github.com/hzoo/15675611b9c7a65d3bb0. Ended up still being kind of long so don't know if that's a better idea or not since it's not as straightforward as before?

@sebmck
Copy link
Contributor

sebmck commented May 25, 2015

@hzoo Looks nicer to me, easier to add additional types later too.

@hzoo
Copy link
Member Author

hzoo commented May 25, 2015

Ok I'l go ahead and use that then. Is there a way to use t.VISITOR_KEYS but only for the flow types and ones with typeAnnotation? Or is it better to just make a copy like I already did?

@sebmck
Copy link
Contributor

sebmck commented May 25, 2015

I guess you could loop over and filter them. require("babel-core").types.FLIPPED_ALIAS_KEYS.Flow contains all the flow nodes.

@hzoo hzoo force-pushed the i-108 branch 2 times, most recently from 33c7b58 to 00f0b76 Compare May 25, 2015 16:19
@hzoo
Copy link
Member Author

hzoo commented May 25, 2015

Yeah don't know if it's that great

  var visitorKeysMap = pick(t.VISITOR_KEYS, function(k) {
    return t.FLIPPED_ALIAS_KEYS.Flow.concat([
      "ArrayPattern",
      "ClassDeclaration",
      "ClassExpression",
      "FunctionDeclaration",
      "FunctionExpression",
      "Identifier",
      "ObjectPattern",
      "RestElement"
    ]).indexOf(k) === -1;
  });

  // don't check "id" to prevent "no-undef" for 'Component' with: 'let x: React.Component'
  visitorKeysMap.QualifiedTypeIdentifier = ["qualification"];

@hzoo
Copy link
Member Author

hzoo commented Jun 1, 2015

I added Type Annotations, Array Types, Tuples from the acorn flow tests https://gist.github.com/hzoo/c0ec600494025de9c192 and found/fixed a few issues because of them. Don't know if we need to do all of them but I can. @sebmck


For react-native, some variables aren't undefined so to fix I would add them to the .eslintrc globals or actual define them (so no issues there):

    "ReactClass": false,
    "ReactElement": false

Not sure about x: mixed;, easing: ($Enum<typeof AnimationUtils.Defaults> | EasingFunction);, or

function renderApplication<D, P, S>(
  RootComponent: ReactClass<D, P, S>,
 ...

I don't know if those are built-ins/temps for flow or something but it will also create an error (intended).

@sebmck
Copy link
Contributor

sebmck commented Jun 1, 2015

Unsure if they all needed to be added, just do whatever you feel comfortable with. Also, it might be best to reuse as much as you can from babel.types.VISITOR_KEYS so if extensions are done to the node types in the future then they'll automatically work in babel-eslint.

@hzoo
Copy link
Member Author

hzoo commented Jun 1, 2015

Ok I can add back something like the previous comment #109 (comment)?

@sebmck
Copy link
Contributor

sebmck commented Jun 1, 2015

Sure, something like that would work.

@hzoo
Copy link
Member Author

hzoo commented Jun 2, 2015

Ok added babel.types.VISITOR_KEYS, removed more .bind(this), and also trying to visit type casts
by removing change in #102 which fixes all the apparent issues I'm finding in react-native.

I think I'm ready to merge other than my confusion of if we need to do anything about var x: mixed since it will say it's not defined? @sebmck @wincent

@sebmck
Copy link
Contributor

sebmck commented Jun 2, 2015

I think I'm ready to merge other than my confusion of if we need to do anything about var x: mixed since it will say it's not defined?

Seems fine to me. If this is actually an issue then it should be fixed by parsing it as a MixedTypeAnnotation node so there's nothing that needs to be handled on the babel-eslint side.

@hzoo
Copy link
Member Author

hzoo commented Jun 2, 2015

Ok makes sense... so good to merge then? And would this still be a patch version?

@sebmck
Copy link
Contributor

sebmck commented Jun 2, 2015

Looks GTM to me. Doesn't really matter what version it's released as since it's not breaking or "adding features" really.

hzoo added a commit that referenced this pull request Jun 2, 2015
visit flow types - fixes #108
@hzoo hzoo merged commit f57158f into babel:master Jun 2, 2015
@jwangnz
Copy link

jwangnz commented Jun 2, 2015

Hi, I tried the 3.1.10, it seems that it will got no-undef on polymorphic functions.

/* @flow */

export function foo<X>(x: X): X {
  return x;
}

the code above will got three no-undef errors:

  3:20  error  "X" is not defined  no-undef
  3:26  error  "X" is not defined  no-undef
  3:30  error  "X" is not defined  no-undef

@hzoo
Copy link
Member Author

hzoo commented Jun 2, 2015

I didn't figure out how to get polymorphic functions to work yet (stuff like <T>) - so, sorry I knew about those but still need to work on it.

@jwangnz
Copy link

jwangnz commented Jun 2, 2015

Could bable-eslint remove this <T> stuff such as it did in the previous versions until it can properly get the polymorphic functions to work ?

@hzoo
Copy link
Member Author

hzoo commented Jun 2, 2015

The previous versions removed all flow types entirely but then you would get (no-unused-vars).

Since they aren't going to refer to any types (Foo, React), we might as well remove them. Not sure how we can do this other than checking if the Identifier is a single capital letter (which really sounds like a hack) and deleting it (or not visiting based on that)? @sebmck

} else if (node.type === "Identifier") {
      // check if a polymorphic type: <T>, <A>, etc
      if (node.name.length === 1 && node.name === node.name.toUpperCase()) {
        return;
      }
      this.visit(node);
    } 

Could do a similar thing for mixed at the moment.

hzoo added a commit to hzoo/babel-eslint that referenced this pull request Jun 2, 2015
hzoo referenced this pull request in wincent/corpus Jun 2, 2015
hzoo added a commit to hzoo/babel-eslint that referenced this pull request Jun 2, 2015
hzoo added a commit that referenced this pull request Jun 3, 2015
add flow exceptions for polymorphic types (<A>) - Ref #109
@hzoo
Copy link
Member Author

hzoo commented Jun 3, 2015

Ok @Tsing see if 3.1.11 fixes things for you.

@jwangnz
Copy link

jwangnz commented Jun 4, 2015

@hzoo Thanks, it works well!

@hzoo hzoo mentioned this pull request Jun 8, 2015
nicolo-ribaudo pushed a commit to babel/babel that referenced this pull request Nov 14, 2019
nicolo-ribaudo pushed a commit to babel/babel that referenced this pull request Nov 14, 2019
add flow exceptions for polymorphic types (<A>) - Ref babel/babel-eslint#109
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants