javascript.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. module.exports = function(hljs) {
  2. var IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
  3. var KEYWORDS = {
  4. keyword:
  5. 'in of if for while finally var new function do return void else break catch ' +
  6. 'instanceof with throw case default try this switch continue typeof delete ' +
  7. 'let yield const export super debugger as async await static ' +
  8. // ECMAScript 6 modules import
  9. 'import from as'
  10. ,
  11. literal:
  12. 'true false null undefined NaN Infinity',
  13. built_in:
  14. 'eval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent ' +
  15. 'encodeURI encodeURIComponent escape unescape Object Function Boolean Error ' +
  16. 'EvalError InternalError RangeError ReferenceError StopIteration SyntaxError ' +
  17. 'TypeError URIError Number Math Date String RegExp Array Float32Array ' +
  18. 'Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array ' +
  19. 'Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require ' +
  20. 'module console window document Symbol Set Map WeakSet WeakMap Proxy Reflect ' +
  21. 'Promise'
  22. };
  23. var NUMBER = {
  24. className: 'number',
  25. variants: [
  26. { begin: '\\b(0[bB][01]+)' },
  27. { begin: '\\b(0[oO][0-7]+)' },
  28. { begin: hljs.C_NUMBER_RE }
  29. ],
  30. relevance: 0
  31. };
  32. var SUBST = {
  33. className: 'subst',
  34. begin: '\\$\\{', end: '\\}',
  35. keywords: KEYWORDS,
  36. contains: [] // defined later
  37. };
  38. var TEMPLATE_STRING = {
  39. className: 'string',
  40. begin: '`', end: '`',
  41. contains: [
  42. hljs.BACKSLASH_ESCAPE,
  43. SUBST
  44. ]
  45. };
  46. SUBST.contains = [
  47. hljs.APOS_STRING_MODE,
  48. hljs.QUOTE_STRING_MODE,
  49. TEMPLATE_STRING,
  50. NUMBER,
  51. hljs.REGEXP_MODE
  52. ]
  53. var PARAMS_CONTAINS = SUBST.contains.concat([
  54. hljs.C_BLOCK_COMMENT_MODE,
  55. hljs.C_LINE_COMMENT_MODE
  56. ]);
  57. return {
  58. aliases: ['js', 'jsx'],
  59. keywords: KEYWORDS,
  60. contains: [
  61. {
  62. className: 'meta',
  63. relevance: 10,
  64. begin: /^\s*['"]use (strict|asm)['"]/
  65. },
  66. {
  67. className: 'meta',
  68. begin: /^#!/, end: /$/
  69. },
  70. hljs.APOS_STRING_MODE,
  71. hljs.QUOTE_STRING_MODE,
  72. TEMPLATE_STRING,
  73. hljs.C_LINE_COMMENT_MODE,
  74. hljs.C_BLOCK_COMMENT_MODE,
  75. NUMBER,
  76. { // object attr container
  77. begin: /[{,]\s*/, relevance: 0,
  78. contains: [
  79. {
  80. begin: IDENT_RE + '\\s*:', returnBegin: true,
  81. relevance: 0,
  82. contains: [{className: 'attr', begin: IDENT_RE, relevance: 0}]
  83. }
  84. ]
  85. },
  86. { // "value" container
  87. begin: '(' + hljs.RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*',
  88. keywords: 'return throw case',
  89. contains: [
  90. hljs.C_LINE_COMMENT_MODE,
  91. hljs.C_BLOCK_COMMENT_MODE,
  92. hljs.REGEXP_MODE,
  93. {
  94. className: 'function',
  95. begin: '(\\(.*?\\)|' + IDENT_RE + ')\\s*=>', returnBegin: true,
  96. end: '\\s*=>',
  97. contains: [
  98. {
  99. className: 'params',
  100. variants: [
  101. {
  102. begin: IDENT_RE
  103. },
  104. {
  105. begin: /\(\s*\)/,
  106. },
  107. {
  108. begin: /\(/, end: /\)/,
  109. excludeBegin: true, excludeEnd: true,
  110. keywords: KEYWORDS,
  111. contains: PARAMS_CONTAINS
  112. }
  113. ]
  114. }
  115. ]
  116. },
  117. { // E4X / JSX
  118. begin: /</, end: /(\/\w+|\w+\/)>/,
  119. subLanguage: 'xml',
  120. contains: [
  121. {begin: /<\w+\s*\/>/, skip: true},
  122. {
  123. begin: /<\w+/, end: /(\/\w+|\w+\/)>/, skip: true,
  124. contains: [
  125. {begin: /<\w+\s*\/>/, skip: true},
  126. 'self'
  127. ]
  128. }
  129. ]
  130. }
  131. ],
  132. relevance: 0
  133. },
  134. {
  135. className: 'function',
  136. beginKeywords: 'function', end: /\{/, excludeEnd: true,
  137. contains: [
  138. hljs.inherit(hljs.TITLE_MODE, {begin: IDENT_RE}),
  139. {
  140. className: 'params',
  141. begin: /\(/, end: /\)/,
  142. excludeBegin: true,
  143. excludeEnd: true,
  144. contains: PARAMS_CONTAINS
  145. }
  146. ],
  147. illegal: /\[|%/
  148. },
  149. {
  150. begin: /\$[(.]/ // relevance booster for a pattern common to JS libs: `$(something)` and `$.something`
  151. },
  152. hljs.METHOD_GUARD,
  153. { // ES6 class
  154. className: 'class',
  155. beginKeywords: 'class', end: /[{;=]/, excludeEnd: true,
  156. illegal: /[:"\[\]]/,
  157. contains: [
  158. {beginKeywords: 'extends'},
  159. hljs.UNDERSCORE_TITLE_MODE
  160. ]
  161. },
  162. {
  163. beginKeywords: 'constructor get set', end: /\{/, excludeEnd: true
  164. }
  165. ],
  166. illegal: /#(?!!)/
  167. };
  168. };