Skip to content

Commit 5a79169

Browse files
authored
Fix metadata serialization for invalid jsdoc types (microsoft#37836)
1 parent 126c6ab commit 5a79169

6 files changed

Lines changed: 140 additions & 0 deletions

File tree

src/compiler/transformers/ts.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,18 @@ namespace ts {
15901590
case SyntaxKind.ImportType:
15911591
break;
15921592

1593+
// handle JSDoc types from an invalid parse
1594+
case SyntaxKind.JSDocAllType:
1595+
case SyntaxKind.JSDocUnknownType:
1596+
case SyntaxKind.JSDocFunctionType:
1597+
case SyntaxKind.JSDocVariadicType:
1598+
case SyntaxKind.JSDocNamepathType:
1599+
break;
1600+
1601+
case SyntaxKind.JSDocNullableType:
1602+
case SyntaxKind.JSDocNonNullableType:
1603+
case SyntaxKind.JSDocOptionalType:
1604+
return serializeTypeNode((<JSDocNullableType | JSDocNonNullableType | JSDocOptionalType>node).type);
15931605

15941606
default:
15951607
return Debug.failBadSyntaxKind(node);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts(5,9): error TS8020: JSDoc types can only be used inside documentation comments.
2+
tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts(7,9): error TS8020: JSDoc types can only be used inside documentation comments.
3+
tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts(9,9): error TS8020: JSDoc types can only be used inside documentation comments.
4+
5+
6+
==== tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts (3 errors) ====
7+
declare var decorator: any;
8+
9+
class X {
10+
@decorator()
11+
a?: string?;
12+
~~~~~~~
13+
!!! error TS8020: JSDoc types can only be used inside documentation comments.
14+
@decorator()
15+
b?: string!;
16+
~~~~~~~
17+
!!! error TS8020: JSDoc types can only be used inside documentation comments.
18+
@decorator()
19+
c?: *;
20+
~
21+
!!! error TS8020: JSDoc types can only be used inside documentation comments.
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//// [decoratorMetadata-jsdoc.ts]
2+
declare var decorator: any;
3+
4+
class X {
5+
@decorator()
6+
a?: string?;
7+
@decorator()
8+
b?: string!;
9+
@decorator()
10+
c?: *;
11+
}
12+
13+
//// [decoratorMetadata-jsdoc.js]
14+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
15+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
16+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
17+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
18+
return c > 3 && r && Object.defineProperty(target, key, r), r;
19+
};
20+
var __metadata = (this && this.__metadata) || function (k, v) {
21+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
22+
};
23+
var X = /** @class */ (function () {
24+
function X() {
25+
}
26+
__decorate([
27+
decorator(),
28+
__metadata("design:type", String)
29+
], X.prototype, "a", void 0);
30+
__decorate([
31+
decorator(),
32+
__metadata("design:type", String)
33+
], X.prototype, "b", void 0);
34+
__decorate([
35+
decorator(),
36+
__metadata("design:type", Object)
37+
], X.prototype, "c", void 0);
38+
return X;
39+
}());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts ===
2+
declare var decorator: any;
3+
>decorator : Symbol(decorator, Decl(decoratorMetadata-jsdoc.ts, 0, 11))
4+
5+
class X {
6+
>X : Symbol(X, Decl(decoratorMetadata-jsdoc.ts, 0, 27))
7+
8+
@decorator()
9+
>decorator : Symbol(decorator, Decl(decoratorMetadata-jsdoc.ts, 0, 11))
10+
11+
a?: string?;
12+
>a : Symbol(X.a, Decl(decoratorMetadata-jsdoc.ts, 2, 9))
13+
14+
@decorator()
15+
>decorator : Symbol(decorator, Decl(decoratorMetadata-jsdoc.ts, 0, 11))
16+
17+
b?: string!;
18+
>b : Symbol(X.b, Decl(decoratorMetadata-jsdoc.ts, 4, 16))
19+
20+
@decorator()
21+
>decorator : Symbol(decorator, Decl(decoratorMetadata-jsdoc.ts, 0, 11))
22+
23+
c?: *;
24+
>c : Symbol(X.c, Decl(decoratorMetadata-jsdoc.ts, 6, 16))
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts ===
2+
declare var decorator: any;
3+
>decorator : any
4+
5+
class X {
6+
>X : X
7+
8+
@decorator()
9+
>decorator() : any
10+
>decorator : any
11+
12+
a?: string?;
13+
>a : string
14+
15+
@decorator()
16+
>decorator() : any
17+
>decorator : any
18+
19+
b?: string!;
20+
>b : string
21+
22+
@decorator()
23+
>decorator() : any
24+
>decorator : any
25+
26+
c?: *;
27+
>c : any
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @experimentalDecorators: true
2+
// @emitDecoratorMetadata: true
3+
// @target: es5
4+
// @module: commonjs
5+
declare var decorator: any;
6+
7+
class X {
8+
@decorator()
9+
a?: string?;
10+
@decorator()
11+
b?: string!;
12+
@decorator()
13+
c?: *;
14+
}

0 commit comments

Comments
 (0)