Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Fix: Parameter with assignation provide type annotations (fixes #146) #147

Merged
merged 2 commits into from
Feb 5, 2017

Conversation

weirdpattern
Copy link
Contributor

Adds code to process the type annotations on parameters that have assignations

@eslintbot
Copy link

LGTM

@JamesHenry
Copy link
Member

Thanks so much for your efforts, @weirdpattern! I've been so swamped in January but things are set to improve from next week.

I will try my best to get to this at the weekend, but it will be by Monday at the latest.

@weirdpattern
Copy link
Contributor Author

Perfect, could you please also take a look at issue #146, just want your input on that before I submit the PR

@soda0289
Copy link
Member

soda0289 commented Feb 2, 2017

@weirdpattern I think there are some more cases that need to include type annotations

//Missing
function iii(...name: string): string {
        return name;
}

//Handled correctly
function jjj(name: string): string {
        return name;
}

//Fixed by this PR
function kkk(name: string = 'hello'): string {
        return name;
}

//Missing
function lll(...name: string = 'hh'): string {
        return name;
}

Maybe we can avoid the duplicate code by setting a variable to the argument and then setting typeAnnotation on that.

@weirdpattern
Copy link
Contributor Author

Hey @soda0289

//Missing
function iii(...name: Array<string>): Array<string> {
       return name;
}

I will look into this later today (I believe this was already being handled by the current code, but I can double check).

//Missing
function iii(...name: Array<string> = ['hi']): Array<string> {
       return name;
}

I don't think this even compiles, rest operators cannot have initializers here

Are you seeing something that I'm not?

@soda0289
Copy link
Member

soda0289 commented Feb 2, 2017

You are right @weirdpattern those examples I gave are invalid typescript. I was just reading the code and noticied rest paremeters were missing the type annotation conversion. I did not know initializers with the rest parameter were invalid, or that it had to be an array but that makes sense.

With the following code

function lll(...name: string[]): string {
        return name;
}

The output I get for function declation is

{ type: 'FunctionDeclaration',
  range: [ 203, 267 ],
  loc: { start: { line: 16, column: 0 }, end: { line: 18, column: 1 } },
  id:
   { type: 'Identifier',
     range: [ 212, 215 ],
     loc: { start: [Object], end: [Object] },
     name: 'lll' },
  generator: false,
  expression: false,
  async: false,
  params:
   [ { type: 'RestElement',
       range: [Object],
       loc: [Object],
       argument: [Object] } ],
  body:
   { type: 'BlockStatement',
     range: [ 243, 267 ],
     loc: { start: [Object], end: [Object] },
     body: [ [Object] ] },
  returnType:
   { type: 'TypeAnnotation',
     loc: { start: [Object], end: [Object] },
     range: [ 236, 242 ],
     typeAnnotation: { type: 'TSStringKeyword', range: [Object], loc: [Object] } } }

For the rest element

{ type: 'RestElement',
  range: [ 216, 233 ],
  loc:
   { start: { line: 16, column: 13 },
     end: { line: 16, column: 30 } },
  argument:
   { type: 'Identifier',
     range: [ 219, 223 ],
     loc: { start: [Object], end: [Object] },
     name: 'name' } }

I think type annotations are missing from the argument property of the RestElement node and would also need to have type annotations included.

@weirdpattern
Copy link
Contributor Author

@soda0289 I think you are right. I will fix this today. Thanks!

@eslintbot
Copy link

LGTM

@weirdpattern
Copy link
Contributor Author

@soda0289 ok, I've included rest arguments in this PR

Copy link
Member

@soda0289 soda0289 left a comment

Choose a reason for hiding this comment

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

LGTM Thanks!

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@nzakas nzakas merged commit fc1e6bb into eslint:master Feb 5, 2017
@weirdpattern weirdpattern deleted the function-types-assignation branch February 5, 2017 17:53
soda0289 pushed a commit to soda0289/typescript-eslint-parser that referenced this pull request Feb 7, 2017
…t#146) (eslint#147)

* Fix: Parameter with assignation provide type annotations (fixes eslint#146)

* Adding support for rest arguments
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.

5 participants