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
Fix parsing typescript function types with destructuring #9035
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -314,13 +314,19 @@ export default (superClass: Class<Parser>): Class<Parser> => | |||||
} | ||||||
|
||||||
tsParseBindingListForSignature(): $ReadOnlyArray< | ||||||
N.Identifier | N.RestElement, | ||||||
N.Identifier | N.RestElement | N.ObjectPattern, | ||||||
> { | ||||||
return this.parseBindingList(tt.parenR).map(pattern => { | ||||||
if (pattern.type !== "Identifier" && pattern.type !== "RestElement") { | ||||||
if ( | ||||||
pattern.type !== "Identifier" && | ||||||
pattern.type !== "RestElement" && | ||||||
pattern.type !== "ObjectPattern" | ||||||
) { | ||||||
throw this.unexpected( | ||||||
pattern.start, | ||||||
"Name in a signature must be an Identifier.", | ||||||
`Name in a signature must be an Identifier or ObjectPattern, instead got ${ | ||||||
pattern.type | ||||||
}`, | ||||||
); | ||||||
} | ||||||
return pattern; | ||||||
|
@@ -747,6 +753,22 @@ export default (superClass: Class<Parser>): Class<Parser> => | |||||
this.next(); | ||||||
return true; | ||||||
} | ||||||
|
||||||
if (this.match(tt.braceL)) { | ||||||
const braceStack = [tt.braceL]; | ||||||
this.next(); | ||||||
|
||||||
while (braceStack.length > 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it safe to skip the code between braces without parsing it? e.g., wouldn't this loop accept something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does ignore everything, even if its invalid, but this function is only used in a lookahead() and it's job is to try to skip the first param after it is used here:
and this in turn is used here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ok 👍 |
||||||
if (this.match(tt.braceL)) { | ||||||
braceStack.push(tt.braceL); | ||||||
} else if (this.match(tt.braceR)) { | ||||||
braceStack.pop(); | ||||||
} | ||||||
this.next(); | ||||||
} | ||||||
return true; | ||||||
} | ||||||
|
||||||
return false; | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type MyType = ({ theme }: any) => any |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 37, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 37 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 37, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 37 | ||
} | ||
}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start": 0, | ||
"end": 37, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 37 | ||
} | ||
}, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 5, | ||
"end": 11, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 5 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 11 | ||
}, | ||
"identifierName": "MyType" | ||
}, | ||
"name": "MyType" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSFunctionType", | ||
"start": 14, | ||
"end": 37, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 14 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 37 | ||
} | ||
}, | ||
"parameters": [ | ||
{ | ||
"type": "ObjectPattern", | ||
"start": 15, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 15 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 29 | ||
} | ||
}, | ||
"properties": [ | ||
{ | ||
"type": "ObjectProperty", | ||
"start": 17, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 17 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
} | ||
}, | ||
"method": false, | ||
"key": { | ||
"type": "Identifier", | ||
"start": 17, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 17 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
}, | ||
"identifierName": "theme" | ||
}, | ||
"name": "theme" | ||
}, | ||
"computed": false, | ||
"shorthand": true, | ||
"value": { | ||
"type": "Identifier", | ||
"start": 17, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 17 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
}, | ||
"identifierName": "theme" | ||
}, | ||
"name": "theme" | ||
}, | ||
"extra": { | ||
"shorthand": true | ||
} | ||
} | ||
], | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start": 24, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 24 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 29 | ||
} | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSAnyKeyword", | ||
"start": 26, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 26 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 29 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start": 31, | ||
"end": 37, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 31 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 37 | ||
} | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSAnyKeyword", | ||
"start": 34, | ||
"end": 37, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 34 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 37 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be an integer instead of an array, since the check only uses its length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed