@@ -279,6 +279,15 @@ namespace ts {
279
279
*/
280
280
const commentDirectiveRegExMultiLine = / ^ \s * (?: \/ | \* ) * \s * @ ( t s - e x p e c t - e r r o r | t s - i g n o r e ) / ;
281
281
282
+ /**
283
+ * Rather than recording all the characters, we record the beginning and end of each paragraph, it saves memory.
284
+ *
285
+ * Put all start and end positions in the same `map`. Use odd bits to indicate the beginning of each paragraph, and even bits to indicate the end of each paragraph.
286
+ *
287
+ * When you need to find whether a `code` is an identifier, use the binary search algorithm to confirm whether it is a valid Unicode identifier.
288
+ * @param code
289
+ * @param map unicodeESNextIdentifierPart | unicodeESNextIdentifierStart
290
+ */
282
291
function lookupInUnicodeMap ( code : number , map : readonly number [ ] ) : boolean {
283
292
// Bail out quickly if it couldn't possibly be in the map.
284
293
if ( code < map [ 0 ] ) {
@@ -342,6 +351,7 @@ namespace ts {
342
351
}
343
352
344
353
/* @internal */
354
+ /** 计算出每一行是从哪一个Position开始的 */
345
355
export function computeLineStarts ( text : string ) : number [ ] {
346
356
const result : number [ ] = new Array ( ) ;
347
357
let pos = 0 ;
@@ -381,6 +391,7 @@ namespace ts {
381
391
}
382
392
383
393
/* @internal */
394
+ /** Query position from row and column number */
384
395
export function computePositionOfLineAndCharacter ( lineStarts : readonly number [ ] , line : number , character : number , debugText ?: string , allowEdits ?: true ) : number {
385
396
if ( line < 0 || line >= lineStarts . length ) {
386
397
if ( allowEdits ) {
@@ -414,6 +425,7 @@ namespace ts {
414
425
}
415
426
416
427
/* @internal */
428
+ /** Retrieve position table query row and column number, used such as show error postion */
417
429
export function computeLineAndCharacterOfPosition ( lineStarts : readonly number [ ] , position : number ) : LineAndCharacter {
418
430
const lineNumber = computeLineOfPosition ( lineStarts , position ) ;
419
431
return {
@@ -458,13 +470,14 @@ namespace ts {
458
470
return computeLineAndCharacterOfPosition ( getLineStarts ( sourceFile ) , position ) ;
459
471
}
460
472
473
+ /** Include line breaks and white space */
461
474
export function isWhiteSpaceLike ( ch : number ) : boolean {
462
475
return isWhiteSpaceSingleLine ( ch ) || isLineBreak ( ch ) ;
463
476
}
464
477
465
478
/** Does not include line breaks. For that, see isWhiteSpaceLike. */
466
479
export function isWhiteSpaceSingleLine ( ch : number ) : boolean {
467
- // Note: nextLine is in the Zs space, and should be considered to be a whitespace.
480
+ // Note: nextLine is in the [ Zs space](https://en.wikipedia.org/wiki/Template:Whitespace_(Unicode)) , and should be considered to be a whitespace.
468
481
// It is explicitly not a line-break as it isn't in the exact set specified by EcmaScript.
469
482
return ch === CharacterCodes . space ||
470
483
ch === CharacterCodes . tab ||
@@ -607,7 +620,7 @@ namespace ts {
607
620
break ;
608
621
609
622
case CharacterCodes . hash :
610
- if ( pos === 0 && isShebangTrivia ( text , pos ) ) {
623
+ if ( pos === 0 && isShebangTrivia ( text , pos ) ) { // #! Shebang - TC39_Stage 2
611
624
pos = scanShebangTrivia ( text , pos ) ;
612
625
continue ;
613
626
}
@@ -881,6 +894,12 @@ namespace ts {
881
894
}
882
895
883
896
/* @internal */
897
+ /**
898
+ * Determine whether a string is a legal identifier
899
+ * @param name The string to be searched
900
+ * @param languageVersion Compiled target ES version
901
+ * @param identifierVariant Because of JSX, we need to use this parameter to distinguish
902
+ */
884
903
export function isIdentifierText ( name : string , languageVersion : ScriptTarget | undefined , identifierVariant ?: LanguageVariant ) : boolean {
885
904
let ch = codePointAt ( name , 0 ) ;
886
905
if ( ! isIdentifierStart ( ch , languageVersion ) ) {
@@ -943,7 +962,7 @@ namespace ts {
943
962
isUnterminated : ( ) => ( tokenFlags & TokenFlags . Unterminated ) !== 0 ,
944
963
getCommentDirectives : ( ) => commentDirectives ,
945
964
getTokenFlags : ( ) => tokenFlags ,
946
- reScanGreaterToken,
965
+ reScanGreaterToken, // We have to reScan it and something below. Because some token is context-sensitive
947
966
reScanSlashToken,
948
967
reScanTemplateToken,
949
968
reScanTemplateHeadOrNoSubstitutionTemplate,
@@ -1644,15 +1663,15 @@ namespace ts {
1644
1663
}
1645
1664
return token = SyntaxKind . WhitespaceTrivia ;
1646
1665
}
1647
- case CharacterCodes . exclamation :
1666
+ case CharacterCodes . exclamation : // !
1648
1667
if ( text . charCodeAt ( pos + 1 ) === CharacterCodes . equals ) {
1649
1668
if ( text . charCodeAt ( pos + 2 ) === CharacterCodes . equals ) {
1650
- return pos += 3 , token = SyntaxKind . ExclamationEqualsEqualsToken ;
1669
+ return pos += 3 , token = SyntaxKind . ExclamationEqualsEqualsToken ; // !==
1651
1670
}
1652
- return pos += 2 , token = SyntaxKind . ExclamationEqualsToken ;
1671
+ return pos += 2 , token = SyntaxKind . ExclamationEqualsToken ; // !=
1653
1672
}
1654
1673
pos ++ ;
1655
- return token = SyntaxKind . ExclamationToken ;
1674
+ return token = SyntaxKind . ExclamationToken ; // !
1656
1675
case CharacterCodes . doubleQuote :
1657
1676
case CharacterCodes . singleQuote :
1658
1677
tokenValue = scanString ( ) ;
@@ -2444,10 +2463,24 @@ namespace ts {
2444
2463
return result ;
2445
2464
}
2446
2465
2466
+ /**
2467
+ * Sometimes we don’t need to look back, but we need to look ahead. For example, the keywords of TS: interface, enum etc.
2468
+ *
2469
+ * Unlike `tryScan`, It will unconditionally restore us to where we were.
2470
+ *
2471
+ * Also see: interface Scan
2472
+ */
2447
2473
function lookAhead < T > ( callback : ( ) => T ) : T {
2448
2474
return speculationHelper ( callback , /*isLookahead*/ true ) ;
2449
2475
}
2450
2476
2477
+ /**
2478
+ * Sometimes we don’t need to look back, but we need to look ahead. For example, the keywords of TS: interface, enum etc.
2479
+ *
2480
+ * Unlike `lookAhead`, It will not "unconditionally" restore us to where we were.
2481
+ *
2482
+ * Also see: interface Scan
2483
+ */
2451
2484
function tryScan < T > ( callback : ( ) => T ) : T {
2452
2485
return speculationHelper ( callback , /*isLookahead*/ false ) ;
2453
2486
}
0 commit comments