Parse more types in JSDoc function() syntax

Also some cleanup from PR comments
This commit is contained in:
Nathan Shively-Sanders
2017-07-14 14:34:32 -07:00
parent bdc3f1f3f7
commit 172db13306
10 changed files with 30 additions and 86 deletions
+4
View File
@@ -6866,6 +6866,10 @@ namespace ts {
}
}
function isJSDocTypeReference(node: TypeReferenceType): node is TypeReferenceNode {
return node.flags & NodeFlags.JSDoc && node.kind === SyntaxKind.TypeReference;
}
function getPrimitiveTypeFromJSDocTypeReference(node: TypeReferenceNode): Type {
if (isIdentifier(node.typeName)) {
switch (node.typeName.text) {
+5 -1
View File
@@ -2199,7 +2199,11 @@ namespace ts {
}
function isStartOfParameter(): boolean {
return token() === SyntaxKind.DotDotDotToken || isIdentifierOrPattern() || isModifierKind(token()) || token() === SyntaxKind.AtToken || token() === SyntaxKind.ThisKeyword || token() === SyntaxKind.NewKeyword;
return token() === SyntaxKind.DotDotDotToken ||
isIdentifierOrPattern() ||
isModifierKind(token()) ||
token() === SyntaxKind.AtToken || token() === SyntaxKind.ThisKeyword || token() === SyntaxKind.NewKeyword ||
token() === SyntaxKind.StringLiteral || token() === SyntaxKind.NumericLiteral;
}
function parseParameter(): ParameterDeclaration {
-4
View File
@@ -3302,10 +3302,6 @@ namespace ts {
return false;
}
export function isJSDocTypeReference(node: TypeReferenceType): node is TypeReferenceNode {
return node.flags & NodeFlags.JSDoc && node.kind === SyntaxKind.TypeReference;
}
/**
* Formats an enum value as a string for debugging and debug assertions.
*/
@@ -61,3 +61,9 @@ z.length;
>z : Symbol(z, Decl(functions.js, 26, 3))
>length : Symbol(length, Decl(functions.js, 12, 27))
/** @type {function ("a" | "b"): 1 | 2} */
var f = function (s) { return s === "a" ? 1 : 2; }
>f : Symbol(f, Decl(functions.js, 30, 3))
>s : Symbol(s, Decl(functions.js, 30, 18))
>s : Symbol(s, Decl(functions.js, 30, 18))
@@ -68,3 +68,15 @@ z.length;
>z : { length: number; }
>length : number
/** @type {function ("a" | "b"): 1 | 2} */
var f = function (s) { return s === "a" ? 1 : 2; }
>f : (arg0: "a" | "b") => 1 | 2
>function (s) { return s === "a" ? 1 : 2; } : (s: "a" | "b") => 1 | 2
>s : "a" | "b"
>s === "a" ? 1 : 2 : 1 | 2
>s === "a" : boolean
>s : "a" | "b"
>"a" : "a"
>1 : 1
>2 : 2
@@ -1,29 +0,0 @@
tests/cases/conformance/jsdoc/f.js(5,15): error TS2304: Cannot find name 'F'.
tests/cases/conformance/jsdoc/f.js(5,15): error TS8010: 'types' can only be used in a .ts file.
tests/cases/conformance/jsdoc/normal.ts(4,12): error TS7006: Parameter 'c' implicitly has an 'any' type.
==== tests/cases/conformance/jsdoc/node.d.ts (0 errors) ====
declare function require(id: string): any;
declare var module: any, exports: any;
==== tests/cases/conformance/jsdoc/f.js (2 errors) ====
var F = function () {
this.x = 1;
};
function f(p: F) { p.x; }
~
!!! error TS2304: Cannot find name 'F'.
~
!!! error TS8010: 'types' can only be used in a .ts file.
==== tests/cases/conformance/jsdoc/normal.ts (1 errors) ====
class C { p: number }
/** @param {C} p */
function g(c) { return c.p }
~
!!! error TS7006: Parameter 'c' implicitly has an 'any' type.
-14
View File
@@ -1,14 +0,0 @@
=== tests/cases/conformance/jsdoc/crash.js ===
/**
* @param {function(new:number, string)} c crashes with correct syntax too
* @return {number}
*/
function sub4(c) {
>sub4 : Symbol(sub4, Decl(crash.js, 0, 0))
>c : Symbol(c, Decl(crash.js, 4, 14))
return new c('hi')
>c : Symbol(c, Decl(crash.js, 4, 14))
}
-16
View File
@@ -1,16 +0,0 @@
=== tests/cases/conformance/jsdoc/crash.js ===
/**
* @param {function(new:number, string)} c crashes with correct syntax too
* @return {number}
*/
function sub4(c) {
>sub4 : (c: new (arg1: string) => number) => number
>c : new (arg1: string) => number
return new c('hi')
>new c('hi') : number
>c : new (arg1: string) => number
>'hi' : "hi"
}
@@ -33,3 +33,6 @@ class C {
var y = id2(C);
var z = new y(12);
z.length;
/** @type {function ("a" | "b"): 1 | 2} */
var f = function (s) { return s === "a" ? 1 : 2; }
@@ -1,22 +0,0 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @module: commonjs
// @filename: node.d.ts
// @noImplicitAny: true
declare function require(id: string): any;
declare var module: any, exports: any;
// @filename: f.js
var F = function () {
this.x = 1;
};
function f(p: F) { p.x; }
// @filename: normal.ts
class C { p: number }
/** @param {C} p */
function g(c) { return c.p }