diff --git a/internal/parser/reparser.go b/internal/parser/reparser.go index 45e422785e..041fd45f76 100644 --- a/internal/parser/reparser.go +++ b/internal/parser/reparser.go @@ -43,7 +43,7 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) { if tags == nil { continue } - for _, tag := range j.AsJSDoc().Tags.Nodes { + for _, tag := range tags.Nodes { switch tag.Kind { case ast.KindJSDocTypedefTag: // !!! Don't mark typedefs as exported if they are not in a module @@ -67,7 +67,14 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) { case ast.KindJSDocTypeLiteral: members := p.nodeSlicePool.NewSlice(0) for _, member := range typeExpression.AsJSDocTypeLiteral().JSDocPropertyTags { - prop := p.factory.NewPropertySignatureDeclaration(nil, member.Name(), nil /*postfixToken*/, member.Type(), nil /*initializer*/) + var questionToken *ast.TokenNode + if member.AsJSDocPropertyTag().IsBracketed || + member.AsJSDocPropertyTag().TypeExpression != nil && member.AsJSDocPropertyTag().TypeExpression.Type().Kind == ast.KindJSDocOptionalType { + questionToken = p.factory.NewToken(ast.KindQuestionToken) + questionToken.Loc = core.NewTextRange(member.Pos(), member.End()) + questionToken.Flags = p.contextFlags | ast.NodeFlagsReparsed + } + prop := p.factory.NewPropertySignatureDeclaration(nil, member.Name(), questionToken, member.Type(), nil /*initializer*/) prop.Loc = member.Loc prop.Flags = p.contextFlags | ast.NodeFlagsReparsed members = append(members, prop) diff --git a/testdata/baselines/reference/submodule/conformance/checkJsdocTypedefInParamTag1.errors.txt b/testdata/baselines/reference/submodule/conformance/checkJsdocTypedefInParamTag1.errors.txt deleted file mode 100644 index 5c9948e856..0000000000 --- a/testdata/baselines/reference/submodule/conformance/checkJsdocTypedefInParamTag1.errors.txt +++ /dev/null @@ -1,56 +0,0 @@ -0.js(15,5): error TS2739: Type '{ x: string; }' is missing the following properties from type 'Opts': y, z, w -0.js(28,6): error TS2741: Property 'anotherY' is missing in type '{ anotherX: string; }' but required in type 'AnotherOpts'. -0.js(42,6): error TS2739: Type '{ x: string; }' is missing the following properties from type 'Opts1': y, z, w - - -==== 0.js (3 errors) ==== - // @ts-check - /** - * @typedef {Object} Opts - * @property {string} x - * @property {string=} y - * @property {string} [z] - * @property {string} [w="hi"] - * - * @param {Opts} opts - */ - function foo(opts) { - opts.x; - } - - foo({x: 'abc'}); - ~~~~~~~~~~ -!!! error TS2739: Type '{ x: string; }' is missing the following properties from type 'Opts': y, z, w - - /** - * @typedef {Object} AnotherOpts - * @property anotherX {string} - * @property anotherY {string=} - * - * @param {AnotherOpts} opts - */ - function foo1(opts) { - opts.anotherX; - } - - foo1({anotherX: "world"}); - ~~~~~~~~~~~~~~~~~~~ -!!! error TS2741: Property 'anotherY' is missing in type '{ anotherX: string; }' but required in type 'AnotherOpts'. -!!! related TS2728 0.js:20:14: 'anotherY' is declared here. - - /** - * @typedef {object} Opts1 - * @property {string} x - * @property {string=} y - * @property {string} [z] - * @property {string} [w="hi"] - * - * @param {Opts1} opts - */ - function foo2(opts) { - opts.x; - } - foo2({x: 'abc'}); - ~~~~~~~~~~ -!!! error TS2739: Type '{ x: string; }' is missing the following properties from type 'Opts1': y, z, w - \ No newline at end of file diff --git a/testdata/baselines/reference/submoduleAccepted/conformance/checkJsdocTypedefInParamTag1.errors.txt.diff b/testdata/baselines/reference/submoduleAccepted/conformance/checkJsdocTypedefInParamTag1.errors.txt.diff deleted file mode 100644 index d1d0f17779..0000000000 --- a/testdata/baselines/reference/submoduleAccepted/conformance/checkJsdocTypedefInParamTag1.errors.txt.diff +++ /dev/null @@ -1,61 +0,0 @@ ---- old.checkJsdocTypedefInParamTag1.errors.txt -+++ new.checkJsdocTypedefInParamTag1.errors.txt -@@= skipped -0, +-1 lines =@@ -- -@@= skipped --1, +1 lines =@@ -+0.js(15,5): error TS2739: Type '{ x: string; }' is missing the following properties from type 'Opts': y, z, w -+0.js(28,6): error TS2741: Property 'anotherY' is missing in type '{ anotherX: string; }' but required in type 'AnotherOpts'. -+0.js(42,6): error TS2739: Type '{ x: string; }' is missing the following properties from type 'Opts1': y, z, w -+ -+ -+==== 0.js (3 errors) ==== -+ // @ts-check -+ /** -+ * @typedef {Object} Opts -+ * @property {string} x -+ * @property {string=} y -+ * @property {string} [z] -+ * @property {string} [w="hi"] -+ * -+ * @param {Opts} opts -+ */ -+ function foo(opts) { -+ opts.x; -+ } -+ -+ foo({x: 'abc'}); -+ ~~~~~~~~~~ -+!!! error TS2739: Type '{ x: string; }' is missing the following properties from type 'Opts': y, z, w -+ -+ /** -+ * @typedef {Object} AnotherOpts -+ * @property anotherX {string} -+ * @property anotherY {string=} -+ * -+ * @param {AnotherOpts} opts -+ */ -+ function foo1(opts) { -+ opts.anotherX; -+ } -+ -+ foo1({anotherX: "world"}); -+ ~~~~~~~~~~~~~~~~~~~ -+!!! error TS2741: Property 'anotherY' is missing in type '{ anotherX: string; }' but required in type 'AnotherOpts'. -+!!! related TS2728 0.js:20:14: 'anotherY' is declared here. -+ -+ /** -+ * @typedef {object} Opts1 -+ * @property {string} x -+ * @property {string=} y -+ * @property {string} [z] -+ * @property {string} [w="hi"] -+ * -+ * @param {Opts1} opts -+ */ -+ function foo2(opts) { -+ opts.x; -+ } -+ foo2({x: 'abc'}); -+ ~~~~~~~~~~ -+!!! error TS2739: Type '{ x: string; }' is missing the following properties from type 'Opts1': y, z, w -+