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

extend genericSpacing to cover funtion annotations #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hsimah
Copy link

@hsimah hsimah commented Jan 5, 2022

I put this together - it's ended up being fairly complex, so if I missed the mark let me know.

It works if I call eslint . --fix on my code, but the tests are failing saying the expected rule is not throwing. If I deploy this branch to my project's package.json I get all the warnings and fixers.

Fixes #24

@Brianzchen
Copy link
Member

Thanks, looks like some good effort here initially. Some input here would be,

  • I thought we were fixing the rule genericSpacing instead of creating a new rule. I find the name typeAnnotationSpacing confusing because what is a type annotation, generics are actually what you're covering
  • You'll need to add to readme (if it's really meant to be a new rule) https://github.com/flow-typed/eslint-plugin-ft-flow/blob/master/CONTRIBUTING.md#adding-documentation
  • The tests should all pass and not be commented out, especially if you're adding a new rule they should be commented from the very beginning.
  • The fact that it works in your build but not project but not in testing is so dodgy... Can you give some insight into what eslint version you're running for my own reference 😅

@hsimah
Copy link
Author

hsimah commented Jan 6, 2022

  • I thought we were fixing the rule genericSpacing instead of creating a new rule. I find the name typeAnnotationSpacing confusing because what is a type annotation, generics are actually what you're covering

Yeah, agreed. I will merge the rules.

Can do.

  • The tests should all pass and not be commented out, especially if you're adding a new rule they should be commented from the very beginning.

Ah, forgot to mention there seems to be an issue in the underlying test runner. When I add <?string> it crashes. Interestingly enough the array long form tests (Array<?string>) don't have the same issue. I will fix that and uncomment those tests before we ship this. We can still hone the rule code itself while I work on that.

  • The fact that it works in your build but not project but not in testing is so dodgy... Can you give some insight into what eslint version you're running for my own reference

Yeah, scratching my head at that too. I will check my linting version, mostly I use CRA + Relay + Flow outside of work (where all this stuff magically works for me).

@Brianzchen
Copy link
Member

When I add <?string> it crashes

Amazing, that's so interesting that this use case was never tested. I'll try look into a fix too but if you find one feel free to contribute it

@hsimah hsimah changed the title create type-annotation-spacing extend genericSpacing to cover funtion annotations Jan 23, 2022
Copy link
Member

@Brianzchen Brianzchen left a comment

Choose a reason for hiding this comment

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

Also you can remove package-lock.json and why idea why there was a large yarn.lock diff? Not a biggie we can cleanup later

@@ -44,6 +44,10 @@
2,
"never"
],
"ft-flow/type-annotation-spacing": [
Copy link
Member

Choose a reason for hiding this comment

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

We should remove this now?

Copy link
Author

Choose a reason for hiding this comment

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

yep, forgot to stage that change

@hsimah hsimah force-pushed the feature/type-parameter-spacing branch from 8f1f4c8 to 5333a0c Compare January 23, 2022 23:51
@hsimah
Copy link
Author

hsimah commented Jan 23, 2022

I am still seeing failures when the lint tester tries to parse the ?. it's weird because the array rule contains question marks, though those are type declarations and not function calls. I'll keep fiddling with it.

@Brianzchen
Copy link
Member

Yeah I've just spent the better part of my day trying to figure this one out. I'm a bit confused.. I dug into the code and in the end RuleTester calls @babel/eslint-parser which is also what is used when in production but syntax like useState<?string>() doesn't cause eslint to crash in those instances.

At this rate if we can't solve it but your improvement works i'm ok to merge it as an alpha release initially whenever you think your change is ready

},
{
code: 'useState<string >(null)',
errors: [{ message: 'There must be no space at start of type annotations' }],
Copy link
Member

Choose a reason for hiding this comment

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

This should say end of type annotations

// Type annotations
{
code: 'const [state, setState] = useState<?string >(null)',
errors: [{ message: 'There must be no space at start of type annotations' }],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
errors: [{ message: 'There must be no space at start of type annotations' }],
errors: [{ message: 'There must be no space at end of type annotations' }],

},
{
code: 'const [state, setState] = useState<?string > (null)',
errors: [{ message: 'There must be no space at start of type annotations' }],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
errors: [{ message: 'There must be no space at start of type annotations' }],
errors: [{ message: 'There must be no space at end of type annotations' }],

code: 'useState< string >(null)',
errors: [{ message: 'There must be no space at start of type annotations' }],
output: 'useState<string>(null)',
},
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test that's something like the following if it doesn't already exist elsewhere I'm interested to know if it strips all spaces blindly or only when it's between maybe types ? and arrow brackets </>

    {
      code: 'useState< string | number >(null)',
      errors: [{ message: 'There must be no space at start of type annotations' }],
      output: 'useState<string | number>(null)',
    },

@Brianzchen
Copy link
Member

Ok found something. I believe this is unrelated to the test runner itself. If you go to any file in src and add useState<?string>(null) it will throw the same unexpected token error. So it must be the way the eslint/babel is configured on this project. At least there's a way to easily test and fix now

@Brianzchen
Copy link
Member

Right as it turns out, it's because it's not parsing the token correctly unless you have // @flow\n at the start of your code snippet, can you try that and let me know?

@Brianzchen
Copy link
Member

This is a guess but I'm imagining it has something to do with how TS doesn't have maybe types so flow token parsing doesn't actually run with babel unless the flow pragma is first detected.

@hsimah
Copy link
Author

hsimah commented Jan 26, 2022

I was looking at the TS side of things, so I think you are right. I will pull out my laptop tonight after work and action your comments here. Thanks for the time - it feels nice to use Flow at home :)

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

Successfully merging this pull request may close these issues.

Function type parameters spacing issues
2 participants