l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

typescript.js (7330B)


      1 const IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
      2 const KEYWORDS = [
      3   "as", // for exports
      4   "in",
      5   "of",
      6   "if",
      7   "for",
      8   "while",
      9   "finally",
     10   "var",
     11   "new",
     12   "function",
     13   "do",
     14   "return",
     15   "void",
     16   "else",
     17   "break",
     18   "catch",
     19   "instanceof",
     20   "with",
     21   "throw",
     22   "case",
     23   "default",
     24   "try",
     25   "switch",
     26   "continue",
     27   "typeof",
     28   "delete",
     29   "let",
     30   "yield",
     31   "const",
     32   "class",
     33   // JS handles these with a special rule
     34   // "get",
     35   // "set",
     36   "debugger",
     37   "async",
     38   "await",
     39   "static",
     40   "import",
     41   "from",
     42   "export",
     43   "extends"
     44 ];
     45 const LITERALS = [
     46   "true",
     47   "false",
     48   "null",
     49   "undefined",
     50   "NaN",
     51   "Infinity"
     52 ];
     53 
     54 const TYPES = [
     55   "Intl",
     56   "DataView",
     57   "Number",
     58   "Math",
     59   "Date",
     60   "String",
     61   "RegExp",
     62   "Object",
     63   "Function",
     64   "Boolean",
     65   "Error",
     66   "Symbol",
     67   "Set",
     68   "Map",
     69   "WeakSet",
     70   "WeakMap",
     71   "Proxy",
     72   "Reflect",
     73   "JSON",
     74   "Promise",
     75   "Float64Array",
     76   "Int16Array",
     77   "Int32Array",
     78   "Int8Array",
     79   "Uint16Array",
     80   "Uint32Array",
     81   "Float32Array",
     82   "Array",
     83   "Uint8Array",
     84   "Uint8ClampedArray",
     85   "ArrayBuffer"
     86 ];
     87 
     88 const ERROR_TYPES = [
     89   "EvalError",
     90   "InternalError",
     91   "RangeError",
     92   "ReferenceError",
     93   "SyntaxError",
     94   "TypeError",
     95   "URIError"
     96 ];
     97 
     98 const BUILT_IN_GLOBALS = [
     99   "setInterval",
    100   "setTimeout",
    101   "clearInterval",
    102   "clearTimeout",
    103 
    104   "require",
    105   "exports",
    106 
    107   "eval",
    108   "isFinite",
    109   "isNaN",
    110   "parseFloat",
    111   "parseInt",
    112   "decodeURI",
    113   "decodeURIComponent",
    114   "encodeURI",
    115   "encodeURIComponent",
    116   "escape",
    117   "unescape"
    118 ];
    119 
    120 const BUILT_IN_VARIABLES = [
    121   "arguments",
    122   "this",
    123   "super",
    124   "console",
    125   "window",
    126   "document",
    127   "localStorage",
    128   "module",
    129   "global" // Node.js
    130 ];
    131 
    132 const BUILT_INS = [].concat(
    133   BUILT_IN_GLOBALS,
    134   BUILT_IN_VARIABLES,
    135   TYPES,
    136   ERROR_TYPES
    137 );
    138 
    139 /*
    140 Language: TypeScript
    141 Author: Panu Horsmalahti <panu.horsmalahti@iki.fi>
    142 Contributors: Ike Ku <dempfi@yahoo.com>
    143 Description: TypeScript is a strict superset of JavaScript
    144 Website: https://www.typescriptlang.org
    145 Category: common, scripting
    146 */
    147 
    148 function typescript(hljs) {
    149   var IDENT_RE$1 = IDENT_RE;
    150   var TYPES = [
    151     "any",
    152     "void",
    153     "number",
    154     "boolean",
    155     "string",
    156     "object",
    157     "never",
    158     "enum"
    159   ];
    160   var TS_SPECIFIC_KEYWORDS = [
    161     "type",
    162     "namespace",
    163     "typedef",
    164     "interface",
    165     "public",
    166     "private",
    167     "protected",
    168     "implements",
    169     "declare",
    170     "abstract",
    171     "readonly"
    172   ];
    173   var KEYWORDS$1 = {
    174     $pattern: IDENT_RE,
    175     keyword: KEYWORDS.concat(TS_SPECIFIC_KEYWORDS).join(" "),
    176     literal: LITERALS.join(" "),
    177     built_in: BUILT_INS.concat(TYPES).join(" ")
    178   };
    179   var DECORATOR = {
    180     className: 'meta',
    181     begin: '@' + IDENT_RE$1,
    182   };
    183   var NUMBER = {
    184     className: 'number',
    185     variants: [
    186       { begin: '\\b(0[bB][01]+)n?' },
    187       { begin: '\\b(0[oO][0-7]+)n?' },
    188       { begin: hljs.C_NUMBER_RE + 'n?' }
    189     ],
    190     relevance: 0
    191   };
    192   var SUBST = {
    193     className: 'subst',
    194     begin: '\\$\\{', end: '\\}',
    195     keywords: KEYWORDS$1,
    196     contains: [] // defined later
    197   };
    198   var HTML_TEMPLATE = {
    199     begin: 'html`', end: '',
    200     starts: {
    201       end: '`', returnEnd: false,
    202       contains: [
    203         hljs.BACKSLASH_ESCAPE,
    204         SUBST
    205       ],
    206       subLanguage: 'xml',
    207     }
    208   };
    209   var CSS_TEMPLATE = {
    210     begin: 'css`', end: '',
    211     starts: {
    212       end: '`', returnEnd: false,
    213       contains: [
    214         hljs.BACKSLASH_ESCAPE,
    215         SUBST
    216       ],
    217       subLanguage: 'css',
    218     }
    219   };
    220   var TEMPLATE_STRING = {
    221     className: 'string',
    222     begin: '`', end: '`',
    223     contains: [
    224       hljs.BACKSLASH_ESCAPE,
    225       SUBST
    226     ]
    227   };
    228   SUBST.contains = [
    229     hljs.APOS_STRING_MODE,
    230     hljs.QUOTE_STRING_MODE,
    231     HTML_TEMPLATE,
    232     CSS_TEMPLATE,
    233     TEMPLATE_STRING,
    234     NUMBER,
    235     hljs.REGEXP_MODE
    236   ];
    237   var ARGUMENTS =
    238   {
    239     begin: '\\(',
    240     end: /\)/,
    241     keywords: KEYWORDS$1,
    242     contains: [
    243       'self',
    244       hljs.QUOTE_STRING_MODE,
    245       hljs.APOS_STRING_MODE,
    246       hljs.NUMBER_MODE
    247     ]
    248   };
    249   var PARAMS = {
    250     className: 'params',
    251     begin: /\(/, end: /\)/,
    252     excludeBegin: true,
    253     excludeEnd: true,
    254     keywords: KEYWORDS$1,
    255     contains: [
    256       hljs.C_LINE_COMMENT_MODE,
    257       hljs.C_BLOCK_COMMENT_MODE,
    258       DECORATOR,
    259       ARGUMENTS
    260     ]
    261   };
    262 
    263   return {
    264     name: 'TypeScript',
    265     aliases: ['ts'],
    266     keywords: KEYWORDS$1,
    267     contains: [
    268       hljs.SHEBANG(),
    269       {
    270         className: 'meta',
    271         begin: /^\s*['"]use strict['"]/
    272       },
    273       hljs.APOS_STRING_MODE,
    274       hljs.QUOTE_STRING_MODE,
    275       HTML_TEMPLATE,
    276       CSS_TEMPLATE,
    277       TEMPLATE_STRING,
    278       hljs.C_LINE_COMMENT_MODE,
    279       hljs.C_BLOCK_COMMENT_MODE,
    280       NUMBER,
    281       { // "value" container
    282         begin: '(' + hljs.RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*',
    283         keywords: 'return throw case',
    284         contains: [
    285           hljs.C_LINE_COMMENT_MODE,
    286           hljs.C_BLOCK_COMMENT_MODE,
    287           hljs.REGEXP_MODE,
    288           {
    289             className: 'function',
    290             // we have to count the parens to make sure we actually have the
    291             // correct bounding ( ) before the =>.  There could be any number of
    292             // sub-expressions inside also surrounded by parens.
    293             begin: '(\\([^(]*' +
    294               '(\\([^(]*' +
    295                 '(\\([^(]*' +
    296                 '\\))?' +
    297               '\\))?' +
    298             '\\)|' + hljs.UNDERSCORE_IDENT_RE + ')\\s*=>', returnBegin: true,
    299             end: '\\s*=>',
    300             contains: [
    301               {
    302                 className: 'params',
    303                 variants: [
    304                   {
    305                     begin: hljs.UNDERSCORE_IDENT_RE
    306                   },
    307                   {
    308                     className: null,
    309                     begin: /\(\s*\)/,
    310                     skip: true
    311                   },
    312                   {
    313                     begin: /\(/, end: /\)/,
    314                     excludeBegin: true, excludeEnd: true,
    315                     keywords: KEYWORDS$1,
    316                     contains: ARGUMENTS.contains
    317                   }
    318                 ]
    319               }
    320             ]
    321           }
    322         ],
    323         relevance: 0
    324       },
    325       {
    326         className: 'function',
    327         beginKeywords: 'function', end: /[\{;]/, excludeEnd: true,
    328         keywords: KEYWORDS$1,
    329         contains: [
    330           'self',
    331           hljs.inherit(hljs.TITLE_MODE, { begin: IDENT_RE$1 }),
    332           PARAMS
    333         ],
    334         illegal: /%/,
    335         relevance: 0 // () => {} is more typical in TypeScript
    336       },
    337       {
    338         beginKeywords: 'constructor', end: /[\{;]/, excludeEnd: true,
    339         contains: [
    340           'self',
    341           PARAMS
    342         ]
    343       },
    344       { // prevent references like module.id from being highlighted as module definitions
    345         begin: /module\./,
    346         keywords: { built_in: 'module' },
    347         relevance: 0
    348       },
    349       {
    350         beginKeywords: 'module', end: /\{/, excludeEnd: true
    351       },
    352       {
    353         beginKeywords: 'interface', end: /\{/, excludeEnd: true,
    354         keywords: 'interface extends'
    355       },
    356       {
    357         begin: /\$[(.]/ // relevance booster for a pattern common to JS libs: `$(something)` and `$.something`
    358       },
    359       {
    360         begin: '\\.' + hljs.IDENT_RE, relevance: 0 // hack: prevents detection of keywords after dots
    361       },
    362       DECORATOR,
    363       ARGUMENTS
    364     ]
    365   };
    366 }
    367 
    368 module.exports = typescript;