Skip to content

Commit

Permalink
Fix metadata serialization for invalid jsdoc types (#37836)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton authored Apr 8, 2020
1 parent 126c6ab commit 5a79169
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,18 @@ namespace ts {
case SyntaxKind.ImportType:
break;

// handle JSDoc types from an invalid parse
case SyntaxKind.JSDocAllType:
case SyntaxKind.JSDocUnknownType:
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.JSDocVariadicType:
case SyntaxKind.JSDocNamepathType:
break;

case SyntaxKind.JSDocNullableType:
case SyntaxKind.JSDocNonNullableType:
case SyntaxKind.JSDocOptionalType:
return serializeTypeNode((<JSDocNullableType | JSDocNonNullableType | JSDocOptionalType>node).type);

default:
return Debug.failBadSyntaxKind(node);
Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/decoratorMetadata-jsdoc.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts(5,9): error TS8020: JSDoc types can only be used inside documentation comments.
tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts(7,9): error TS8020: JSDoc types can only be used inside documentation comments.
tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts(9,9): error TS8020: JSDoc types can only be used inside documentation comments.


==== tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts (3 errors) ====
declare var decorator: any;

class X {
@decorator()
a?: string?;
~~~~~~~
!!! error TS8020: JSDoc types can only be used inside documentation comments.
@decorator()
b?: string!;
~~~~~~~
!!! error TS8020: JSDoc types can only be used inside documentation comments.
@decorator()
c?: *;
~
!!! error TS8020: JSDoc types can only be used inside documentation comments.
}
39 changes: 39 additions & 0 deletions tests/baselines/reference/decoratorMetadata-jsdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [decoratorMetadata-jsdoc.ts]
declare var decorator: any;

class X {
@decorator()
a?: string?;
@decorator()
b?: string!;
@decorator()
c?: *;
}

//// [decoratorMetadata-jsdoc.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var X = /** @class */ (function () {
function X() {
}
__decorate([
decorator(),
__metadata("design:type", String)
], X.prototype, "a", void 0);
__decorate([
decorator(),
__metadata("design:type", String)
], X.prototype, "b", void 0);
__decorate([
decorator(),
__metadata("design:type", Object)
], X.prototype, "c", void 0);
return X;
}());
25 changes: 25 additions & 0 deletions tests/baselines/reference/decoratorMetadata-jsdoc.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts ===
declare var decorator: any;
>decorator : Symbol(decorator, Decl(decoratorMetadata-jsdoc.ts, 0, 11))

class X {
>X : Symbol(X, Decl(decoratorMetadata-jsdoc.ts, 0, 27))

@decorator()
>decorator : Symbol(decorator, Decl(decoratorMetadata-jsdoc.ts, 0, 11))

a?: string?;
>a : Symbol(X.a, Decl(decoratorMetadata-jsdoc.ts, 2, 9))

@decorator()
>decorator : Symbol(decorator, Decl(decoratorMetadata-jsdoc.ts, 0, 11))

b?: string!;
>b : Symbol(X.b, Decl(decoratorMetadata-jsdoc.ts, 4, 16))

@decorator()
>decorator : Symbol(decorator, Decl(decoratorMetadata-jsdoc.ts, 0, 11))

c?: *;
>c : Symbol(X.c, Decl(decoratorMetadata-jsdoc.ts, 6, 16))
}
28 changes: 28 additions & 0 deletions tests/baselines/reference/decoratorMetadata-jsdoc.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts ===
declare var decorator: any;
>decorator : any

class X {
>X : X

@decorator()
>decorator() : any
>decorator : any

a?: string?;
>a : string

@decorator()
>decorator() : any
>decorator : any

b?: string!;
>b : string

@decorator()
>decorator() : any
>decorator : any

c?: *;
>c : any
}
14 changes: 14 additions & 0 deletions tests/cases/conformance/decorators/decoratorMetadata-jsdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @experimentalDecorators: true
// @emitDecoratorMetadata: true
// @target: es5
// @module: commonjs
declare var decorator: any;

class X {
@decorator()
a?: string?;
@decorator()
b?: string!;
@decorator()
c?: *;
}

0 comments on commit 5a79169

Please sign in to comment.