class

Obelisk::Lexers::JavaScript

Inherits Obelisk::RegexLexer < Obelisk::Lexer < Reference < Object

JavaScript/TypeScript language lexer Optimized for performance using Chroma-like simple patterns

Constants

BACKTICK = /`/
BLOCK_COMMENT_END = /\*\//
BLOCK_COMMENT_START = /\/\*/
BUILTIN_OBJECTS = /\b(?:Array|Boolean|Date|Error|Function|Math|Number|Object|RegExp|String|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|eval|isFinite|isNaN|parseFloat|parseInt|document|this|window)\b/

Builtin objects (simplified to ~24 names like Chroma, vs Obelisk's 40+) Note: Chroma includes: Array, Boolean, Date, Error, Function, Math, Number, Object, RegExp, String, decodeURI, decodeURIComponent, encodeURI, encodeURIComponent, eval, isFinite, isNaN, parseFloat, parseInt, document, this, window

CONSTANTS = /\b(?:true|false|null|NaN|Infinity|undefined)\b/

Constants (simplified from Obelisk's pattern)

DOUBLE_QUOTE = /"/

String delimiters

ESCAPE_SEQUENCE = /\\[\\\"'nrtbfv]|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\u\{[0-9a-fA-F]+\}|\\[0-7]{1,3}|\\./

Escape sequences (shared across string states)

IDENTIFIER = /[a-zA-Z_$][a-zA-Z0-9_$]*/

Generic identifier (Chroma approach - most identifiers are just "names")

INTERPOLATION_END = /\}/
INTERPOLATION_START = /\$\{/

Template literal interpolation

KEYWORDS = /\b(?:break|case|catch|class|const|continue|debugger|default|delete|do|else|export|extends|finally|for|function|if|import|in|instanceof|let|new|return|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/

Keywords (simplified - one pattern vs Chroma's multiple patterns)

KEYWORDS_RESERVED = /\b(?:abstract|async|boolean|class|const|debugger|enum|export|extends|from|get|global|goto|implements|import|interface|package|private|protected|public|readonly|require|set|static|super|type)\b/

Reserved/declaration keywords

LINE_COMMENT = /\/\/.*?(?=\n|$)/
LONE_DOLLAR = /\$/
NUMBER_FLOAT = /[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?/

Numbers (simplified from 7 to 3 patterns, matching Chroma)

NUMBER_HEX = /0x[0-9a-fA-F]+/
NUMBER_INT = /[0-9]+/
OPERATORS = /\+\+|--|\*\*|\.\?|\?\?|\?\?=|&&|\|\||<<|>>>?|===|!==|==|!=|<=|>=|\+=|-=|\*=|\/=|%=|<<=|>>=|>>>=|&=|\|=|\^=|=>|[+\-*\/%<>=!&|^~.]/

Operators - must include compound operators before single chars Note: ? is NOT in single-char class as it's only used in ??, ??= operators Order matters: longer patterns first to avoid splitting

PUNCTUATION = /[,;:()\[\]{}|]/

Punctuation (dot is handled separately to support optional chaining ?. operator)

REGEX_LITERAL = /\/(?:[^\/\\\n]|\\.)+\/[gimsuvy]*/

Regular expression literal (simplified - not context-aware like a real parser) This may have false positives but is much faster than full context tracking

SINGLE_QUOTE = /'/
STRING_BACKTICK_CONTENT = /[^`\\$]+/
STRING_DOUBLE_CONTENT = /[^"\\]+/

String content patterns (excluding escape sequences and quotes)

STRING_SINGLE_CONTENT = /[^'\\]+/
TYPESCRIPT_KEYWORDS = /\b(?:abstract|async|await|constructor|declare|enum|from|get|implements|interface|namespace|readonly|set)\b/

TypeScript keywords

WHITESPACE = /\s+/

Whitespace and comments

Instance methods

analyze(text : String) : Float32

Analyze text to determine if this lexer can handle it Returns a score from 0.0 to 1.0

Source
config
Source
rules
Source