@@ -42,6 +42,7 @@ import { cloneIdentifier, type Undone } from "../../parser/node";
4242import type { Pattern } from "../../types" ;
4343import type { Expression } from "../../types" ;
4444import type { IJSXParserMixin } from "../jsx" ;
45+ import { ParseBindingListFlags } from "../../parser/lval" ;
4546
4647const getOwn = < T extends { } > ( object : T , key : keyof T ) =>
4748 Object . hasOwnProperty . call ( object , key ) && object [ key ] ;
@@ -755,7 +756,11 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
755756 N . Identifier | N . RestElement | N . ObjectPattern | N . ArrayPattern
756757 > {
757758 return super
758- . parseBindingList ( tt . parenR , charCodes . rightParenthesis )
759+ . parseBindingList (
760+ tt . parenR ,
761+ charCodes . rightParenthesis ,
762+ ParseBindingListFlags . IS_FUNCTION_PARAMS ,
763+ )
759764 . map ( pattern => {
760765 if (
761766 pattern . type !== "Identifier" &&
@@ -1422,7 +1427,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
14221427 super . parseBindingList (
14231428 tt . bracketR ,
14241429 charCodes . rightSquareBracket ,
1425- true ,
1430+ ParseBindingListFlags . ALLOW_EMPTY ,
14261431 ) ;
14271432 return errors . length === previousErrorCount ;
14281433 } catch {
@@ -2249,40 +2254,35 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
22492254 }
22502255
22512256 parseAssignableListItem (
2252- allowModifiers : boolean | undefined | null ,
2257+ flags : ParseBindingListFlags ,
22532258 decorators : N . Decorator [ ] ,
22542259 ) : N . Pattern | N . TSParameterProperty {
22552260 // Store original location to include modifiers in range
22562261 const startLoc = this . state . startLoc ;
22572262
2258- let accessibility : N . Accessibility | undefined | null ;
2259- let readonly = false ;
2260- let override = false ;
2261- if ( allowModifiers !== undefined ) {
2262- const modified : ModifierBase = { } ;
2263- this . tsParseModifiers ( {
2264- modified,
2265- allowedModifiers : [
2266- "public" ,
2267- "private" ,
2268- "protected" ,
2269- "override" ,
2270- "readonly" ,
2271- ] ,
2272- } ) ;
2273- accessibility = modified . accessibility ;
2274- override = modified . override ;
2275- readonly = modified . readonly ;
2276- if (
2277- allowModifiers === false &&
2278- ( accessibility || readonly || override )
2279- ) {
2280- this . raise ( TSErrors . UnexpectedParameterModifier , { at : startLoc } ) ;
2281- }
2263+ const modified : ModifierBase = { } ;
2264+ this . tsParseModifiers ( {
2265+ modified,
2266+ allowedModifiers : [
2267+ "public" ,
2268+ "private" ,
2269+ "protected" ,
2270+ "override" ,
2271+ "readonly" ,
2272+ ] ,
2273+ } ) ;
2274+ const accessibility = modified . accessibility ;
2275+ const override = modified . override ;
2276+ const readonly = modified . readonly ;
2277+ if (
2278+ ! ( flags & ParseBindingListFlags . IS_CONSTRUCTOR_PARAMS ) &&
2279+ ( accessibility || readonly || override )
2280+ ) {
2281+ this . raise ( TSErrors . UnexpectedParameterModifier , { at : startLoc } ) ;
22822282 }
22832283
22842284 const left = this . parseMaybeDefault ( ) ;
2285- this . parseAssignableListItemTypes ( left ) ;
2285+ this . parseAssignableListItemTypes ( left , flags ) ;
22862286 const elt = this . parseMaybeDefault ( left . loc . start , left ) ;
22872287 if ( accessibility || readonly || override ) {
22882288 const pp = this . startNodeAt < N . TSParameterProperty > ( startLoc ) ;
@@ -2314,6 +2314,27 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
23142314 ) ;
23152315 }
23162316
2317+ tsDisallowOptionalPattern ( node : Undone < N . Function > ) {
2318+ for ( const param of node . params ) {
2319+ if (
2320+ param . type !== "Identifier" &&
2321+ ( param as any ) . optional &&
2322+ ! this . state . isAmbientContext
2323+ ) {
2324+ this . raise ( TSErrors . PatternIsOptional , { at : param } ) ;
2325+ }
2326+ }
2327+ }
2328+
2329+ setArrowFunctionParameters (
2330+ node : Undone < N . ArrowFunctionExpression > ,
2331+ params : N . Expression [ ] ,
2332+ trailingCommaLoc ?: Position | null ,
2333+ ) : void {
2334+ super . setArrowFunctionParameters ( node , params , trailingCommaLoc ) ;
2335+ this . tsDisallowOptionalPattern ( node ) ;
2336+ }
2337+
23172338 parseFunctionBodyAndFinish <
23182339 T extends
23192340 | N . Function
@@ -2340,6 +2361,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
23402361 return super . parseFunctionBodyAndFinish ( node , bodilessType , isMethod ) ;
23412362 }
23422363 }
2364+ this . tsDisallowOptionalPattern ( node ) ;
23432365
23442366 return super . parseFunctionBodyAndFinish ( node , type , isMethod ) ;
23452367 }
@@ -3271,10 +3293,10 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
32713293 ) ;
32723294 }
32733295
3274- parseFunctionParams ( node : N . Function , allowModifiers ? : boolean ) : void {
3296+ parseFunctionParams ( node : N . Function , isConstructor : boolean ) : void {
32753297 const typeParameters = this . tsTryParseTypeParameters ( ) ;
32763298 if ( typeParameters ) node . typeParameters = typeParameters ;
3277- super . parseFunctionParams ( node , allowModifiers ) ;
3299+ super . parseFunctionParams ( node , isConstructor ) ;
32783300 }
32793301
32803302 // `let x: number;`
@@ -3504,16 +3526,13 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
35043526 }
35053527
35063528 // Allow type annotations inside of a parameter list.
3507- parseAssignableListItemTypes ( param : N . Pattern ) {
3508- if ( this . eat ( tt . question ) ) {
3509- if (
3510- param . type !== "Identifier" &&
3511- ! this . state . isAmbientContext &&
3512- ! this . state . inType
3513- ) {
3514- this . raise ( TSErrors . PatternIsOptional , { at : param } ) ;
3515- }
3529+ parseAssignableListItemTypes (
3530+ param : N . Pattern ,
3531+ flags : ParseBindingListFlags ,
3532+ ) {
3533+ if ( ! ( flags & ParseBindingListFlags . IS_FUNCTION_PARAMS ) ) return param ;
35163534
3535+ if ( this . eat ( tt . question ) ) {
35173536 ( param as any as N . Identifier ) . optional = true ;
35183537 }
35193538 const type = this . tsTryParseTypeAnnotation ( ) ;
0 commit comments