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