javascript.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. module.exports = function(hljs) {
  2. var FRAGMENT = {
  3. begin: '<>',
  4. end: '</>'
  5. };
  6. var XML_TAG = {
  7. begin: /<[A-Za-z0-9\\._:-]+/,
  8. end: /\/[A-Za-z0-9\\._:-]+>|\/>/
  9. };
  10. var IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
  11. var KEYWORDS = {
  12. keyword:
  13. 'in of if for while finally var new function do return void else break catch ' +
  14. 'instanceof with throw case default try this switch continue typeof delete ' +
  15. 'let yield const export super debugger as async await static ' +
  16. // ECMAScript 6 modules import
  17. 'import from as'
  18. ,
  19. literal:
  20. 'true false null undefined NaN Infinity',
  21. built_in:
  22. 'eval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent ' +
  23. 'encodeURI encodeURIComponent escape unescape Object Function Boolean Error ' +
  24. 'EvalError InternalError RangeError ReferenceError StopIteration SyntaxError ' +
  25. 'TypeError URIError Number Math Date String RegExp Array Float32Array ' +
  26. 'Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array ' +
  27. 'Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require ' +
  28. 'module console window document Symbol Set Map WeakSet WeakMap Proxy Reflect ' +
  29. 'Promise'
  30. };
  31. var NUMBER = {
  32. className: 'number',
  33. variants: [
  34. { begin: '\\b(0[bB][01]+)n?' },
  35. { begin: '\\b(0[oO][0-7]+)n?' },
  36. { begin: hljs.C_NUMBER_RE + 'n?' }
  37. ],
  38. relevance: 0
  39. };
  40. var SUBST = {
  41. className: 'subst',
  42. begin: '\\$\\{', end: '\\}',
  43. keywords: KEYWORDS,
  44. contains: [] // defined later
  45. };
  46. var HTML_TEMPLATE = {
  47. begin: 'html`', end: '',
  48. starts: {
  49. end: '`', returnEnd: false,
  50. contains: [
  51. hljs.BACKSLASH_ESCAPE,
  52. SUBST
  53. ],
  54. subLanguage: 'xml',
  55. }
  56. };
  57. var CSS_TEMPLATE = {
  58. begin: 'css`', end: '',
  59. starts: {
  60. end: '`', returnEnd: false,
  61. contains: [
  62. hljs.BACKSLASH_ESCAPE,
  63. SUBST
  64. ],
  65. subLanguage: 'css',
  66. }
  67. };
  68. var TEMPLATE_STRING = {
  69. className: 'string',
  70. begin: '`', end: '`',
  71. contains: [
  72. hljs.BACKSLASH_ESCAPE,
  73. SUBST
  74. ]
  75. };
  76. SUBST.contains = [
  77. hljs.APOS_STRING_MODE,
  78. hljs.QUOTE_STRING_MODE,
  79. HTML_TEMPLATE,
  80. CSS_TEMPLATE,
  81. TEMPLATE_STRING,
  82. NUMBER,
  83. hljs.REGEXP_MODE
  84. ];
  85. var PARAMS_CONTAINS = SUBST.contains.concat([
  86. hljs.C_BLOCK_COMMENT_MODE,
  87. hljs.C_LINE_COMMENT_MODE
  88. ]);
  89. return {
  90. aliases: ['js', 'jsx', 'mjs', 'cjs'],
  91. keywords: KEYWORDS,
  92. contains: [
  93. {
  94. className: 'meta',
  95. relevance: 10,
  96. begin: /^\s*['"]use (strict|asm)['"]/
  97. },
  98. {
  99. className: 'meta',
  100. begin: /^#!/, end: /$/
  101. },
  102. hljs.APOS_STRING_MODE,
  103. hljs.QUOTE_STRING_MODE,
  104. HTML_TEMPLATE,
  105. CSS_TEMPLATE,
  106. TEMPLATE_STRING,
  107. hljs.C_LINE_COMMENT_MODE,
  108. hljs.COMMENT(
  109. '/\\*\\*',
  110. '\\*/',
  111. {
  112. relevance : 0,
  113. contains : [
  114. {
  115. className : 'doctag',
  116. begin : '@[A-Za-z]+',
  117. contains : [
  118. {
  119. className: 'type',
  120. begin: '\\{',
  121. end: '\\}',
  122. relevance: 0
  123. },
  124. {
  125. className: 'variable',
  126. begin: IDENT_RE + '(?=\\s*(-)|$)',
  127. endsParent: true,
  128. relevance: 0
  129. },
  130. // eat spaces (not newlines) so we can find
  131. // types or variables
  132. {
  133. begin: /(?=[^\n])\s/,
  134. relevance: 0
  135. },
  136. ]
  137. }
  138. ]
  139. }
  140. ),
  141. hljs.C_BLOCK_COMMENT_MODE,
  142. NUMBER,
  143. { // object attr container
  144. begin: /[{,\n]\s*/, relevance: 0,
  145. contains: [
  146. {
  147. begin: IDENT_RE + '\\s*:', returnBegin: true,
  148. relevance: 0,
  149. contains: [{className: 'attr', begin: IDENT_RE, relevance: 0}]
  150. }
  151. ]
  152. },
  153. { // "value" container
  154. begin: '(' + hljs.RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*',
  155. keywords: 'return throw case',
  156. contains: [
  157. hljs.C_LINE_COMMENT_MODE,
  158. hljs.C_BLOCK_COMMENT_MODE,
  159. hljs.REGEXP_MODE,
  160. {
  161. className: 'function',
  162. begin: '(\\(.*?\\)|' + IDENT_RE + ')\\s*=>', returnBegin: true,
  163. end: '\\s*=>',
  164. contains: [
  165. {
  166. className: 'params',
  167. variants: [
  168. {
  169. begin: IDENT_RE
  170. },
  171. {
  172. begin: /\(\s*\)/,
  173. },
  174. {
  175. begin: /\(/, end: /\)/,
  176. excludeBegin: true, excludeEnd: true,
  177. keywords: KEYWORDS,
  178. contains: PARAMS_CONTAINS
  179. }
  180. ]
  181. }
  182. ]
  183. },
  184. {
  185. className: '',
  186. begin: /\s/,
  187. end: /\s*/,
  188. skip: true,
  189. },
  190. { // JSX
  191. variants: [
  192. { begin: FRAGMENT.begin, end: FRAGMENT.end },
  193. { begin: XML_TAG.begin, end: XML_TAG.end }
  194. ],
  195. subLanguage: 'xml',
  196. contains: [
  197. {
  198. begin: XML_TAG.begin, end: XML_TAG.end, skip: true,
  199. contains: ['self']
  200. }
  201. ]
  202. },
  203. ],
  204. relevance: 0
  205. },
  206. {
  207. className: 'function',
  208. beginKeywords: 'function', end: /\{/, excludeEnd: true,
  209. contains: [
  210. hljs.inherit(hljs.TITLE_MODE, {begin: IDENT_RE}),
  211. {
  212. className: 'params',
  213. begin: /\(/, end: /\)/,
  214. excludeBegin: true,
  215. excludeEnd: true,
  216. contains: PARAMS_CONTAINS
  217. }
  218. ],
  219. illegal: /\[|%/
  220. },
  221. {
  222. begin: /\$[(.]/ // relevance booster for a pattern common to JS libs: `$(something)` and `$.something`
  223. },
  224. hljs.METHOD_GUARD,
  225. { // ES6 class
  226. className: 'class',
  227. beginKeywords: 'class', end: /[{;=]/, excludeEnd: true,
  228. illegal: /[:"\[\]]/,
  229. contains: [
  230. {beginKeywords: 'extends'},
  231. hljs.UNDERSCORE_TITLE_MODE
  232. ]
  233. },
  234. {
  235. beginKeywords: 'constructor get set', end: /\{/, excludeEnd: true
  236. }
  237. ],
  238. illegal: /#(?!!)/
  239. };
  240. };