UNPKG

highlight.js

Version:

Syntax highlighting with language autodetection.

369 lines (357 loc) 7.33 kB
const IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*'; const KEYWORDS = [ "as", // for exports "in", "of", "if", "for", "while", "finally", "var", "new", "function", "do", "return", "void", "else", "break", "catch", "instanceof", "with", "throw", "case", "default", "try", "switch", "continue", "typeof", "delete", "let", "yield", "const", "class", // JS handles these with a special rule // "get", // "set", "debugger", "async", "await", "static", "import", "from", "export", "extends" ]; const LITERALS = [ "true", "false", "null", "undefined", "NaN", "Infinity" ]; const TYPES = [ "Intl", "DataView", "Number", "Math", "Date", "String", "RegExp", "Object", "Function", "Boolean", "Error", "Symbol", "Set", "Map", "WeakSet", "WeakMap", "Proxy", "Reflect", "JSON", "Promise", "Float64Array", "Int16Array", "Int32Array", "Int8Array", "Uint16Array", "Uint32Array", "Float32Array", "Array", "Uint8Array", "Uint8ClampedArray", "ArrayBuffer" ]; const ERROR_TYPES = [ "EvalError", "InternalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError" ]; const BUILT_IN_GLOBALS = [ "setInterval", "setTimeout", "clearInterval", "clearTimeout", "require", "exports", "eval", "isFinite", "isNaN", "parseFloat", "parseInt", "decodeURI", "decodeURIComponent", "encodeURI", "encodeURIComponent", "escape", "unescape" ]; const BUILT_IN_VARIABLES = [ "arguments", "this", "super", "console", "window", "document", "localStorage", "module", "global" // Node.js ]; const BUILT_INS = [].concat( BUILT_IN_GLOBALS, BUILT_IN_VARIABLES, TYPES, ERROR_TYPES ); /* Language: TypeScript Author: Panu Horsmalahti <panu.horsmalahti@iki.fi> Contributors: Ike Ku <dempfi@yahoo.com> Description: TypeScript is a strict superset of JavaScript Website: https://www.typescriptlang.org Category: common, scripting */ function typescript(hljs) { var IDENT_RE$1 = IDENT_RE; var TYPES = [ "any", "void", "number", "boolean", "string", "object", "never", "enum" ]; var TS_SPECIFIC_KEYWORDS = [ "type", "namespace", "typedef", "interface", "public", "private", "protected", "implements", "declare", "abstract", "readonly" ]; var KEYWORDS$1 = { $pattern: IDENT_RE, keyword: KEYWORDS.concat(TS_SPECIFIC_KEYWORDS).join(" "), literal: LITERALS.join(" "), built_in: BUILT_INS.concat(TYPES).join(" ") }; var DECORATOR = { className: 'meta', begin: '@' + IDENT_RE$1, }; var NUMBER = { className: 'number', variants: [ { begin: '\\b(0[bB][01]+)n?' }, { begin: '\\b(0[oO][0-7]+)n?' }, { begin: hljs.C_NUMBER_RE + 'n?' } ], relevance: 0 }; var SUBST = { className: 'subst', begin: '\\$\\{', end: '\\}', keywords: KEYWORDS$1, contains: [] // defined later }; var HTML_TEMPLATE = { begin: 'html`', end: '', starts: { end: '`', returnEnd: false, contains: [ hljs.BACKSLASH_ESCAPE, SUBST ], subLanguage: 'xml', } }; var CSS_TEMPLATE = { begin: 'css`', end: '', starts: { end: '`', returnEnd: false, contains: [ hljs.BACKSLASH_ESCAPE, SUBST ], subLanguage: 'css', } }; var TEMPLATE_STRING = { className: 'string', begin: '`', end: '`', contains: [ hljs.BACKSLASH_ESCAPE, SUBST ] }; SUBST.contains = [ hljs.APOS_STRING_MODE, hljs.QUOTE_STRING_MODE, HTML_TEMPLATE, CSS_TEMPLATE, TEMPLATE_STRING, NUMBER, hljs.REGEXP_MODE ]; var ARGUMENTS = { begin: '\\(', end: /\)/, keywords: KEYWORDS$1, contains: [ 'self', hljs.QUOTE_STRING_MODE, hljs.APOS_STRING_MODE, hljs.NUMBER_MODE ] }; var PARAMS = { className: 'params', begin: /\(/, end: /\)/, excludeBegin: true, excludeEnd: true, keywords: KEYWORDS$1, contains: [ hljs.C_LINE_COMMENT_MODE, hljs.C_BLOCK_COMMENT_MODE, DECORATOR, ARGUMENTS ] }; return { name: 'TypeScript', aliases: ['ts'], keywords: KEYWORDS$1, contains: [ hljs.SHEBANG(), { className: 'meta', begin: /^\s*['"]use strict['"]/ }, hljs.APOS_STRING_MODE, hljs.QUOTE_STRING_MODE, HTML_TEMPLATE, CSS_TEMPLATE, TEMPLATE_STRING, hljs.C_LINE_COMMENT_MODE, hljs.C_BLOCK_COMMENT_MODE, NUMBER, { // "value" container begin: '(' + hljs.RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*', keywords: 'return throw case', contains: [ hljs.C_LINE_COMMENT_MODE, hljs.C_BLOCK_COMMENT_MODE, hljs.REGEXP_MODE, { className: 'function', // we have to count the parens to make sure we actually have the // correct bounding ( ) before the =>. There could be any number of // sub-expressions inside also surrounded by parens. begin: '(\\([^(]*' + '(\\([^(]*' + '(\\([^(]*' + '\\))?' + '\\))?' + '\\)|' + hljs.UNDERSCORE_IDENT_RE + ')\\s*=>', returnBegin: true, end: '\\s*=>', contains: [ { className: 'params', variants: [ { begin: hljs.UNDERSCORE_IDENT_RE }, { className: null, begin: /\(\s*\)/, skip: true }, { begin: /\(/, end: /\)/, excludeBegin: true, excludeEnd: true, keywords: KEYWORDS$1, contains: ARGUMENTS.contains } ] } ] } ], relevance: 0 }, { className: 'function', beginKeywords: 'function', end: /[\{;]/, excludeEnd: true, keywords: KEYWORDS$1, contains: [ 'self', hljs.inherit(hljs.TITLE_MODE, { begin: IDENT_RE$1 }), PARAMS ], illegal: /%/, relevance: 0 // () => {} is more typical in TypeScript }, { beginKeywords: 'constructor', end: /[\{;]/, excludeEnd: true, contains: [ 'self', PARAMS ] }, { // prevent references like module.id from being higlighted as module definitions begin: /module\./, keywords: { built_in: 'module' }, relevance: 0 }, { beginKeywords: 'module', end: /\{/, excludeEnd: true }, { beginKeywords: 'interface', end: /\{/, excludeEnd: true, keywords: 'interface extends' }, { begin: /\$[(.]/ // relevance booster for a pattern common to JS libs: `$(something)` and `$.something` }, { begin: '\\.' + hljs.IDENT_RE, relevance: 0 // hack: prevents detection of keywords after dots }, DECORATOR, ARGUMENTS ] }; } module.exports = typescript;