Typescript function destructured params (#9431)

* fix typescript funtion destructured params for array

* update type name
This commit is contained in:
Downpooooour 2019-02-08 05:59:50 +08:00 committed by Nicolò Ribaudo
parent fdb65ab8b1
commit d1514f57bd
4 changed files with 173 additions and 10 deletions

View File

@ -342,17 +342,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
tsParseBindingListForSignature(): $ReadOnlyArray< tsParseBindingListForSignature(): $ReadOnlyArray<
N.Identifier | N.RestElement | N.ObjectPattern, N.Identifier | N.RestElement | N.ObjectPattern | N.ArrayPattern,
> { > {
return this.parseBindingList(tt.parenR).map(pattern => { return this.parseBindingList(tt.parenR).map(pattern => {
if ( if (
pattern.type !== "Identifier" && pattern.type !== "Identifier" &&
pattern.type !== "RestElement" && pattern.type !== "RestElement" &&
pattern.type !== "ObjectPattern" pattern.type !== "ObjectPattern" &&
pattern.type !== "ArrayPattern"
) { ) {
throw this.unexpected( throw this.unexpected(
pattern.start, pattern.start,
`Name in a signature must be an Identifier or ObjectPattern, instead got ${ `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${
pattern.type pattern.type
}`, }`,
); );
@ -794,6 +795,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return true; return true;
} }
if (this.match(tt.bracketL)) {
let braceStackCounter = 1;
this.next();
while (braceStackCounter > 0) {
if (this.match(tt.bracketL)) {
++braceStackCounter;
} else if (this.match(tt.bracketR)) {
--braceStackCounter;
}
this.next();
}
return true;
}
return false; return false;
} }

View File

@ -1076,7 +1076,9 @@ export type TsSignatureDeclaration =
export type TsSignatureDeclarationOrIndexSignatureBase = NodeBase & { export type TsSignatureDeclarationOrIndexSignatureBase = NodeBase & {
// Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions. // Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions.
parameters: $ReadOnlyArray<Identifier | RestElement | ObjectPattern>, parameters: $ReadOnlyArray<
Identifier | RestElement | ObjectPattern | ArrayPattern,
>,
typeAnnotation: ?TsTypeAnnotation, typeAnnotation: ?TsTypeAnnotation,
}; };

View File

@ -1 +1,3 @@
type MyType = ({ theme }: any) => any type MyType = ({ theme }: any) => any
type AnotherType = ([a]: any) => any

View File

@ -1,29 +1,29 @@
{ {
"type": "File", "type": "File",
"start": 0, "start": 0,
"end": 37, "end": 75,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 1, "line": 3,
"column": 37 "column": 36
} }
}, },
"program": { "program": {
"type": "Program", "type": "Program",
"start": 0, "start": 0,
"end": 37, "end": 75,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 1, "line": 3,
"column": 37 "column": 36
} }
}, },
"sourceType": "module", "sourceType": "module",
@ -209,6 +209,149 @@
} }
} }
} }
},
{
"type": "TSTypeAliasDeclaration",
"start": 39,
"end": 75,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 36
}
},
"id": {
"type": "Identifier",
"start": 44,
"end": 55,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 16
},
"identifierName": "AnotherType"
},
"name": "AnotherType"
},
"typeAnnotation": {
"type": "TSFunctionType",
"start": 58,
"end": 75,
"loc": {
"start": {
"line": 3,
"column": 19
},
"end": {
"line": 3,
"column": 36
}
},
"parameters": [
{
"type": "ArrayPattern",
"start": 59,
"end": 67,
"loc": {
"start": {
"line": 3,
"column": 20
},
"end": {
"line": 3,
"column": 28
}
},
"elements": [
{
"type": "Identifier",
"start": 60,
"end": 61,
"loc": {
"start": {
"line": 3,
"column": 21
},
"end": {
"line": 3,
"column": 22
},
"identifierName": "a"
},
"name": "a"
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 62,
"end": 67,
"loc": {
"start": {
"line": 3,
"column": 23
},
"end": {
"line": 3,
"column": 28
}
},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start": 64,
"end": 67,
"loc": {
"start": {
"line": 3,
"column": 25
},
"end": {
"line": 3,
"column": 28
}
}
}
}
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 69,
"end": 75,
"loc": {
"start": {
"line": 3,
"column": 30
},
"end": {
"line": 3,
"column": 36
}
},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start": 72,
"end": 75,
"loc": {
"start": {
"line": 3,
"column": 33
},
"end": {
"line": 3,
"column": 36
}
}
}
}
}
} }
], ],
"directives": [] "directives": []