Skip to content

Commit 3f87259

Browse files
Merge pull request #3291 from Microsoft/jsDocParsing2
Parsing support for jsDocComments.
2 parents 118d87c + 9aab984 commit 3f87259

13 files changed

+3323
-69
lines changed

Jakefile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ var harnessSources = [
123123
return path.join(harnessDirectory, f);
124124
}).concat([
125125
"incrementalParser.ts",
126+
"jsDocParsing.ts",
126127
"services/colorization.ts",
127128
"services/documentRegistry.ts",
128129
"services/preProcessFile.ts",

src/compiler/core.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,11 @@ module ts {
312312

313313
Debug.assert(start >= 0, "start must be non-negative, is " + start);
314314
Debug.assert(length >= 0, "length must be non-negative, is " + length);
315-
Debug.assert(start <= file.text.length, `start must be within the bounds of the file. ${ start } > ${ file.text.length }`);
316-
Debug.assert(end <= file.text.length, `end must be the bounds of the file. ${ end } > ${ file.text.length }`);
315+
316+
if (file) {
317+
Debug.assert(start <= file.text.length, `start must be within the bounds of the file. ${ start } > ${ file.text.length }`);
318+
Debug.assert(end <= file.text.length, `end must be the bounds of the file. ${ end } > ${ file.text.length }`);
319+
}
317320

318321
let text = getLocaleSpecificMessage(message.key);
319322

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ module ts {
174174
Type_expected_0_is_a_reserved_word_in_strict_mode: { code: 1215, category: DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode" },
175175
Type_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode: { code: 1216, category: DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode. Class definitions are automatically in strict mode." },
176176
Export_assignment_is_not_supported_when_module_flag_is_system: { code: 1218, category: DiagnosticCategory.Error, key: "Export assignment is not supported when '--module' flag is 'system'." },
177+
_0_tag_already_specified: { code: 1219, category: DiagnosticCategory.Error, key: "'{0}' tag already specified." },
177178
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
178179
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
179180
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },
@@ -535,7 +536,6 @@ module ts {
535536
types_can_only_be_used_in_a_ts_file: { code: 8010, category: DiagnosticCategory.Error, key: "'types' can only be used in a .ts file." },
536537
type_arguments_can_only_be_used_in_a_ts_file: { code: 8011, category: DiagnosticCategory.Error, key: "'type arguments' can only be used in a .ts file." },
537538
parameter_modifiers_can_only_be_used_in_a_ts_file: { code: 8012, category: DiagnosticCategory.Error, key: "'parameter modifiers' can only be used in a .ts file." },
538-
can_only_be_used_in_a_ts_file: { code: 8013, category: DiagnosticCategory.Error, key: "'?' can only be used in a .ts file." },
539539
property_declarations_can_only_be_used_in_a_ts_file: { code: 8014, category: DiagnosticCategory.Error, key: "'property declarations' can only be used in a .ts file." },
540540
enum_declarations_can_only_be_used_in_a_ts_file: { code: 8015, category: DiagnosticCategory.Error, key: "'enum declarations' can only be used in a .ts file." },
541541
type_assertion_expressions_can_only_be_used_in_a_ts_file: { code: 8016, category: DiagnosticCategory.Error, key: "'type assertion expressions' can only be used in a .ts file." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,10 @@
683683
"category": "Error",
684684
"code": 1218
685685
},
686-
686+
"'{0}' tag already specified.": {
687+
"category": "Error",
688+
"code": 1219
689+
},
687690
"Duplicate identifier '{0}'.": {
688691
"category": "Error",
689692
"code": 2300
@@ -2132,10 +2135,6 @@
21322135
"category": "Error",
21332136
"code": 8012
21342137
},
2135-
"'?' can only be used in a .ts file.": {
2136-
"category": "Error",
2137-
"code": 8013
2138-
},
21392138
"'property declarations' can only be used in a .ts file.": {
21402139
"category": "Error",
21412140
"code": 8014

src/compiler/parser.ts

Lines changed: 837 additions & 35 deletions
Large diffs are not rendered by default.

src/compiler/scanner.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,26 @@ module ts {
598598
ch > CharacterCodes.maxAsciiCharacter && isUnicodeIdentifierPart(ch, languageVersion);
599599
}
600600

601-
/** Creates a scanner over a (possibly unspecified) range of a piece of text. */
602-
export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback, start?: number, length?: number): Scanner {
603-
let pos: number; // Current position (end position of text of current token)
604-
let end: number; // end of text
605-
let startPos: number; // Start position of whitespace before current token
606-
let tokenPos: number; // Start position of text of current token
601+
/* @internal */
602+
// Creates a scanner over a (possibly unspecified) range of a piece of text.
603+
export function createScanner(languageVersion: ScriptTarget,
604+
skipTrivia: boolean,
605+
text?: string,
606+
onError?: ErrorCallback,
607+
start?: number,
608+
length?: number): Scanner {
609+
// Current position (end position of text of current token)
610+
let pos: number;
611+
612+
// end of text
613+
let end: number;
614+
615+
// Start position of whitespace before current token
616+
let startPos: number;
617+
618+
// Start position of text of current token
619+
let tokenPos: number;
620+
607621
let token: SyntaxKind;
608622
let tokenValue: string;
609623
let precedingLineBreak: boolean;

src/compiler/types.ts

Lines changed: 143 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,32 @@ module ts {
270270
// Top-level nodes
271271
SourceFile,
272272

273+
// JSDoc nodes.
274+
JSDocTypeExpression,
275+
// The * type.
276+
JSDocAllType,
277+
// The ? type.
278+
JSDocUnknownType,
279+
JSDocArrayType,
280+
JSDocUnionType,
281+
JSDocTupleType,
282+
JSDocNullableType,
283+
JSDocNonNullableType,
284+
JSDocRecordType,
285+
JSDocRecordMember,
286+
JSDocTypeReference,
287+
JSDocOptionalType,
288+
JSDocFunctionType,
289+
JSDocVariadicType,
290+
JSDocConstructorType,
291+
JSDocThisType,
292+
JSDocComment,
293+
JSDocTag,
294+
JSDocParameterTag,
295+
JSDocReturnTag,
296+
JSDocTypeTag,
297+
JSDocTemplateTag,
298+
273299
// Synthesized list
274300
SyntaxList,
275301
// Enum value count
@@ -324,6 +350,8 @@ module ts {
324350

325351
/* @internal */
326352
export const enum ParserContextFlags {
353+
None = 0,
354+
327355
// Set if this node was parsed in strict mode. Used for grammar error checks, as well as
328356
// checking if the node can be reused in incremental settings.
329357
StrictMode = 1 << 0,
@@ -345,17 +373,21 @@ module ts {
345373
// error.
346374
ThisNodeHasError = 1 << 5,
347375

376+
// This node was parsed in a JavaScript file and can be processed differently. For example
377+
// its type can be specified usign a JSDoc comment.
378+
JavaScriptFile = 1 << 6,
379+
348380
// Context flags set directly by the parser.
349381
ParserGeneratedFlags = StrictMode | DisallowIn | Yield | GeneratorParameter | Decorator | ThisNodeHasError,
350382

351383
// Context flags computed by aggregating child flags upwards.
352384

353385
// Used during incremental parsing to determine if this node or any of its children had an
354386
// error. Computed only once and then cached.
355-
ThisNodeOrAnySubNodesHasError = 1 << 6,
387+
ThisNodeOrAnySubNodesHasError = 1 << 7,
356388

357389
// Used to know if we've computed data from children and cached it in this node.
358-
HasAggregatedChildData = 1 << 7
390+
HasAggregatedChildData = 1 << 8
359391
}
360392

361393
/* @internal */
@@ -371,14 +403,15 @@ module ts {
371403
// Specific context the parser was in when this node was created. Normally undefined.
372404
// Only set when the parser was in some interesting context (like async/yield).
373405
/* @internal */ parserContextFlags?: ParserContextFlags;
374-
decorators?: NodeArray<Decorator>; // Array of decorators (in document order)
375-
modifiers?: ModifiersArray; // Array of modifiers
376-
/* @internal */ id?: number; // Unique id (used to look up NodeLinks)
377-
parent?: Node; // Parent node (initialized by binding)
378-
/* @internal */ symbol?: Symbol; // Symbol declared by node (initialized by binding)
379-
/* @internal */ locals?: SymbolTable; // Locals associated with node (initialized by binding)
380-
/* @internal */ nextContainer?: Node; // Next container in declaration order (initialized by binding)
381-
/* @internal */ localSymbol?: Symbol; // Local symbol declared by node (initialized by binding only for exported nodes)
406+
decorators?: NodeArray<Decorator>; // Array of decorators (in document order)
407+
modifiers?: ModifiersArray; // Array of modifiers
408+
/* @internal */ id?: number; // Unique id (used to look up NodeLinks)
409+
parent?: Node; // Parent node (initialized by binding
410+
/* @internal */ jsDocComment?: JSDocComment; // JSDoc for the node, if it has any. Only for .js files.
411+
/* @internal */ symbol?: Symbol; // Symbol declared by node (initialized by binding)
412+
/* @internal */ locals?: SymbolTable; // Locals associated with node (initialized by binding)
413+
/* @internal */ nextContainer?: Node; // Next container in declaration order (initialized by binding)
414+
/* @internal */ localSymbol?: Symbol; // Local symbol declared by node (initialized by binding only for exported nodes)
382415
}
383416

384417
export interface NodeArray<T> extends Array<T>, TextRange {
@@ -992,6 +1025,106 @@ module ts {
9921025
kind: SyntaxKind;
9931026
}
9941027

1028+
// represents a top level: { type } expression in a JSDoc comment.
1029+
export interface JSDocTypeExpression extends Node {
1030+
type: JSDocType;
1031+
}
1032+
1033+
export interface JSDocType extends TypeNode {
1034+
_jsDocTypeBrand: any;
1035+
}
1036+
1037+
export interface JSDocAllType extends JSDocType {
1038+
_JSDocAllTypeBrand: any;
1039+
}
1040+
1041+
export interface JSDocUnknownType extends JSDocType {
1042+
_JSDocUnknownTypeBrand: any;
1043+
}
1044+
1045+
export interface JSDocArrayType extends JSDocType {
1046+
elementType: JSDocType;
1047+
}
1048+
1049+
export interface JSDocUnionType extends JSDocType {
1050+
types: NodeArray<JSDocType>;
1051+
}
1052+
1053+
export interface JSDocTupleType extends JSDocType {
1054+
types: NodeArray<JSDocType>;
1055+
}
1056+
1057+
export interface JSDocNonNullableType extends JSDocType {
1058+
type: JSDocType;
1059+
}
1060+
1061+
export interface JSDocNullableType extends JSDocType {
1062+
type: JSDocType;
1063+
}
1064+
1065+
export interface JSDocRecordType extends JSDocType, TypeLiteralNode {
1066+
members: NodeArray<JSDocRecordMember>;
1067+
}
1068+
1069+
export interface JSDocTypeReference extends JSDocType {
1070+
name: EntityName;
1071+
typeArguments: NodeArray<JSDocType>
1072+
}
1073+
1074+
export interface JSDocOptionalType extends JSDocType {
1075+
type: JSDocType;
1076+
}
1077+
1078+
export interface JSDocFunctionType extends JSDocType, SignatureDeclaration {
1079+
parameters: NodeArray<ParameterDeclaration>;
1080+
type: JSDocType;
1081+
}
1082+
1083+
export interface JSDocVariadicType extends JSDocType {
1084+
type: JSDocType;
1085+
}
1086+
1087+
export interface JSDocConstructorType extends JSDocType {
1088+
type: JSDocType;
1089+
}
1090+
1091+
export interface JSDocThisType extends JSDocType {
1092+
type: JSDocType;
1093+
}
1094+
1095+
export interface JSDocRecordMember extends PropertyDeclaration {
1096+
name: Identifier | LiteralExpression,
1097+
type?: JSDocType
1098+
}
1099+
1100+
export interface JSDocComment extends Node {
1101+
tags: NodeArray<JSDocTag>;
1102+
}
1103+
1104+
export interface JSDocTag extends Node {
1105+
atToken: Node;
1106+
tagName: Identifier;
1107+
}
1108+
1109+
export interface JSDocTemplateTag extends JSDocTag {
1110+
typeParameters: NodeArray<TypeParameterDeclaration>;
1111+
}
1112+
1113+
export interface JSDocReturnTag extends JSDocTag {
1114+
typeExpression: JSDocTypeExpression;
1115+
}
1116+
1117+
export interface JSDocTypeTag extends JSDocTag {
1118+
typeExpression: JSDocTypeExpression;
1119+
}
1120+
1121+
export interface JSDocParameterTag extends JSDocTag {
1122+
preParameterName?: Identifier;
1123+
typeExpression?: JSDocTypeExpression;
1124+
postParameterName?: Identifier;
1125+
isBracketed: boolean;
1126+
}
1127+
9951128
// Source files are declarations when they are external modules.
9961129
export interface SourceFile extends Declaration {
9971130
statements: NodeArray<ModuleElement>;

0 commit comments

Comments
 (0)