Skip to content

Commit 5a31703

Browse files
authored
Always reparse bracketed @param names into ? modifiers (#3660)
1 parent ce43ebc commit 5a31703

26 files changed

Lines changed: 18 additions & 157 deletions

CHANGES.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,6 @@ var f = (x) => x,
247247
g = (x) => x;
248248
```
249249

250-
#### Optional marking on parameter names now makes the parameter both optional and undefined:
251-
252-
```js
253-
/** @param {number} [x] */
254-
function f(x) {
255-
return x;
256-
}
257-
```
258-
259-
This behaves the same as TypeScript's `x?: number` syntax.
260-
Strada makes the parameter optional but does not add `undefined` to the type.
261-
262250
#### Type assertions with `@type` tags now prevent narrowing of the type.
263251

264252
```js

internal/checker/grammarchecks.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,9 @@ func (c *Checker) checkGrammarParameterList(parameters *ast.NodeList) bool {
705705
return c.grammarErrorOnNode(parameter.Name(), diagnostics.A_rest_parameter_cannot_have_an_initializer)
706706
}
707707
} else if isOptionalDeclaration(parameter.AsNode()) {
708-
// !!!
709-
// used to be hasEffectiveQuestionToken for JSDoc
710708
seenOptionalParameter = true
711-
if parameter.QuestionToken != nil && parameter.Initializer != nil {
709+
// A reparsed '?' token indicates a bracketed name in @param tag
710+
if parameter.QuestionToken != nil && parameter.QuestionToken.Flags&ast.NodeFlagsReparsed == 0 && parameter.Initializer != nil {
712711
return c.grammarErrorOnNode(parameter.Name(), diagnostics.Parameter_cannot_have_question_mark_and_initializer)
713712
}
714713
} else if seenOptionalParameter && parameter.Initializer == nil {

internal/parser/reparser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
404404
if param.Type == nil && parameterTag.TypeExpression != nil {
405405
param.AsParameterDeclaration().Type = p.reparseJSDocTypeLiteral(parameterTag.TypeExpression.Type())
406406
}
407-
if param.QuestionToken == nil && param.Initializer == nil {
407+
if param.QuestionToken == nil {
408408
if question := p.makeQuestionIfOptional(parameterTag); question != nil {
409409
param.QuestionToken = question
410410
}

testdata/baselines/reference/submodule/compiler/argumentsReferenceInConstructor1_Js.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class A {
1010
* @param {object} [foo={}]
1111
*/
1212
constructor(foo = {}) {
13-
>foo : object
13+
>foo : object | undefined
1414
>{} : {}
1515

1616
/**

testdata/baselines/reference/submodule/compiler/argumentsReferenceInConstructor2_Js.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class A {
1010
* @param {object} [foo={}]
1111
*/
1212
constructor(foo = {}) {
13-
>foo : object
13+
>foo : object | undefined
1414
>{} : {}
1515

1616
/**

testdata/baselines/reference/submodule/compiler/argumentsReferenceInConstructor3_Js.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class B extends A {
2424
* @param {object} [foo={}]
2525
*/
2626
constructor(foo = {}) {
27-
>foo : object
27+
>foo : object | undefined
2828
>{} : {}
2929

3030
super();

testdata/baselines/reference/submodule/compiler/argumentsReferenceInConstructor4_Js.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class A {
1010
* @param {object} [foo={}]
1111
*/
1212
constructor(foo = {}) {
13-
>foo : object
13+
>foo : object | undefined
1414
>{} : {}
1515

1616
const key = "bar";

testdata/baselines/reference/submodule/compiler/argumentsReferenceInConstructor5_Js.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class A {
1919
* @param {object} [foo={}]
2020
*/
2121
constructor(foo = {}) {
22-
>foo : object
22+
>foo : object | undefined
2323
>{} : {}
2424

2525
/**

testdata/baselines/reference/submodule/compiler/argumentsReferenceInMethod1_Js.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class A {
99
*/
1010
m(foo = {}) {
1111
>m : (foo?: object) => void
12-
>foo : object
12+
>foo : object | undefined
1313
>{} : {}
1414

1515
/**

testdata/baselines/reference/submodule/compiler/argumentsReferenceInMethod2_Js.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class A {
99
*/
1010
m(foo = {}) {
1111
>m : (foo?: object) => void
12-
>foo : object
12+
>foo : object | undefined
1313
>{} : {}
1414

1515
/**

0 commit comments

Comments
 (0)