Overview
TheScanner performs lexical analysis (tokenization) of TypeScript source code. It breaks the source text into a stream of tokens that can be used by the parser or for direct analysis.
Creating a Scanner
ts.createScanner()
Creates a new Scanner instance.The ECMAScript target version (ES5, ES2015, ES2020, etc.)
Whether to skip whitespace and comments
Standard or JSX variant (defaults to Standard)
Initial text to scan
Error callback function
Starting position in the text
Length of text to scan
A Scanner instance
Example
Scanner Interface Methods
Scanning Methods
scan()
Scans the next token from the input.The syntax kind of the scanned token
getText()
Returns the full text being scanned.The full source text
setText()
Sets new text for the scanner to scan.The text to scan
Starting position (defaults to 0)
Length to scan (defaults to entire text)
Token Information Methods
getToken()
Returns the current token’s syntax kind.The current token’s syntax kind
getTokenText()
Returns the text of the current token.The token’s text
getTokenValue()
Returns the processed value of the current token (for strings and numbers).The token’s processed value
getTokenStart()
Returns the starting position of the current token (excluding leading trivia).The token’s start position
getTokenEnd()
Returns the ending position of the current token.The token’s end position
getTokenFullStart()
Returns the starting position of the current token (including leading trivia).The token’s full start position
Token State Methods
isIdentifier()
Returns true if the current token is an identifier.True if the token is an identifier
isReservedWord()
Returns true if the current token is a reserved keyword.True if the token is a reserved word
isUnterminated()
Returns true if the current token is unterminated (e.g., unterminated string).True if the token is unterminated
hasPrecedingLineBreak()
Returns true if there’s a line break before the current token.True if there’s a preceding line break
hasUnicodeEscape()
Returns true if the current identifier contains a Unicode escape sequence.True if the token has Unicode escapes
hasExtendedUnicodeEscape()
Returns true if the current identifier contains an extended Unicode escape sequence.True if the token has extended Unicode escapes
Advanced Scanning Methods
reScanGreaterToken()
Re-scans a greater-than token in JSX or type contexts.The re-scanned token kind
reScanSlashToken()
Re-scans a slash token (could be division or regex).The re-scanned token kind (SlashToken or RegularExpressionLiteral)
reScanTemplateToken()
Re-scans template literal tokens.Whether this is a tagged template literal
The re-scanned template token kind
scanJsxIdentifier()
Scans a JSX identifier (allows hyphens).The JSX identifier token
scanJsxToken()
Scans the next token in JSX mode.The JSX token kind
scanJsxAttributeValue()
Scans a JSX attribute value.The attribute value token
State Management Methods
resetTokenState()
Resets the scanner to a specific position.The position to reset to
lookAhead()
Invokes a callback while saving/restoring scanner state.Function to call with lookahead
The result of the callback
tryScan()
Tries a scan operation, only committing if the callback returns truthy.Function to try
The result of the callback
scanRange()
Scans a specific range of text.Start position
Length to scan
Function to call while scanning the range
The result of the callback
Configuration Methods
setScriptTarget()
Sets the ECMAScript target version.The target version
setLanguageVariant()
Sets the language variant (Standard or JSX).The language variant
setScriptKind()
Sets the script kind (TS, JS, JSX, etc.).The script kind
setOnError()
Sets the error callback function.The error callback