]>
luflow.net public git repositories - flow-web.git/blob - static/highlight/es/languages/javascript.js
1 /*! `javascript` grammar compiled for Highlight.js 11.11.1 */
2 var hljsGrammar
= (function () {
5 const IDENT_RE
= '[A-Za-z$_][0-9A-Za-z$_]*';
37 // JS handles these with a special rule
48 // It's reached stage 3, which is "recommended for implementation":
60 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
62 // Fundamental objects
75 // Indexed collections
99 // Control abstraction objects
107 // Internationalization
113 const ERROR_TYPES
= [
124 const BUILT_IN_GLOBALS
= [
139 "decodeURIComponent",
141 "encodeURIComponent",
146 const BUILT_IN_VARIABLES
= [
159 const BUILT_INS
= [].concat(
167 Description: JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions.
168 Category: common, scripting, web
169 Website: https://developer.mozilla.org/en-US/docs/Web/JavaScript
173 /** @type LanguageFn */
174 function javascript(hljs
) {
175 const regex
= hljs
.regex
;
177 * Takes a string like "<Booger" and checks to see
178 * if we can find a matching "</Booger" later in the
180 * @param {RegExpMatchArray} match
181 * @param {{after:number}} param1
183 const hasClosingTag
= (match
, { after
}) => {
184 const tag
= "</" + match
[0].slice(1);
185 const pos
= match
.input
.indexOf(tag
, after
);
189 const IDENT_RE
$1 = IDENT_RE
;
194 // to avoid some special cases inside isTrulyOpeningTag
195 const XML_SELF_CLOSING
= /<[A-Za-z0-9\\._:-]+\s*\/>/;
197 begin: /<[A-Za-z0-9\\._:-]+/,
198 end: /\/[A-Za-z0-9\\._:-]+>|\/>/,
200 * @param {RegExpMatchArray} match
201 * @param {CallbackResponse} response
203 isTrulyOpeningTag: (match
, response
) => {
204 const afterMatchIndex
= match
[0].length
+ match
.index
;
205 const nextChar
= match
.input
[afterMatchIndex
];
207 // HTML should not include another raw `<` inside a tag
209 // `<Array<Array<number>>`, etc.
211 // the , gives away that this is not HTML
212 // `<T, A extends keyof T, V>`
215 response
.ignoreMatch();
220 // Quite possibly a tag, lets look for a matching closing tag...
221 if (nextChar
=== ">") {
222 // if we cannot find a matching closing tag, then we
224 if (!hasClosingTag(match
, { after: afterMatchIndex
})) {
225 response
.ignoreMatch();
229 // `<blah />` (self-closing)
230 // handled by simpleSelfClosing rule
233 const afterMatch
= match
.input
.substring(afterMatchIndex
);
235 // some more template typing stuff
236 // <T = any>(key?: string) => Modify<
237 if ((m
= afterMatch
.match(/^\s*=/))) {
238 response
.ignoreMatch();
242 // `<From extends string>`
243 // technically this could be HTML, but it smells like a type
244 // NOTE: This is ugh, but added specifically for https://github.com/highlightjs/highlight.js/issues/3276
245 if ((m
= afterMatch
.match(/^\s+extends\s+/))) {
247 response
.ignoreMatch();
248 // eslint-disable-next-line no-useless-return
259 "variable.language": BUILT_IN_VARIABLES
262 // https://tc39.es/ecma262/#sec-literals-numeric-literals
263 const decimalDigits
= '[0-9](_?[0-9])*';
264 const frac
= `\\.(${decimalDigits})`;
265 // DecimalIntegerLiteral, including Annex B NonOctalDecimalIntegerLiteral
266 // https://tc39.es/ecma262/#sec-additional-syntax-numeric-literals
267 const decimalInteger
= `0|[1-9](_?[0-9])*|0[0-7]*[89][0-9]*`;
272 { begin: `(\\b(${decimalInteger})((${frac})|\\.)?|(${frac}))` +
273 `[eE][+-]?(${decimalDigits})\\b` },
274 { begin: `\\b(${decimalInteger})\\b((${frac})\\b|\\.)?|(${frac})\\b` },
276 // DecimalBigIntegerLiteral
277 { begin: `\\b(0|[1-9](_?[0-9])*)n\\b` },
279 // NonDecimalIntegerLiteral
280 { begin: "\\b0[xX][0-9a-fA-F](_?[0-9a-fA-F])*n?\\b" },
281 { begin: "\\b0[bB][0-1](_?[0-1])*n?\\b" },
282 { begin: "\\b0[oO][0-7](_?[0-7])*n?\\b" },
284 // LegacyOctalIntegerLiteral (does not include underscore separators)
285 // https://tc39.es/ecma262/#sec-additional-syntax-numeric-literals
286 { begin: "\\b0[0-7]+n?\\b" },
295 keywords: KEYWORDS
$1,
296 contains: [] // defined later
298 const HTML_TEMPLATE
= {
305 hljs
.BACKSLASH_ESCAPE
,
311 const CSS_TEMPLATE
= {
318 hljs
.BACKSLASH_ESCAPE
,
324 const GRAPHQL_TEMPLATE
= {
331 hljs
.BACKSLASH_ESCAPE
,
334 subLanguage: 'graphql'
337 const TEMPLATE_STRING
= {
342 hljs
.BACKSLASH_ESCAPE
,
346 const JSDOC_COMMENT
= hljs
.COMMENT(
353 begin: '(?=@[A-Za-z]+)',
369 className: 'variable',
370 begin: IDENT_RE
$1 + '(?=\\s*(-)|$)',
374 // eat spaces (not newlines) so we can find
375 // types or variables
377 begin: /(?=[^\n])\s/,
386 className: "comment",
389 hljs
.C_BLOCK_COMMENT_MODE
,
390 hljs
.C_LINE_COMMENT_MODE
393 const SUBST_INTERNALS
= [
394 hljs
.APOS_STRING_MODE
,
395 hljs
.QUOTE_STRING_MODE
,
400 // Skip numbers when they are part of a variable name
403 // This is intentional:
404 // See https://github.com/highlightjs/highlight.js/issues/3288
407 SUBST
.contains
= SUBST_INTERNALS
409 // we need to pair up {} inside our subst to prevent
410 // it from ending too early by matching another }
413 keywords: KEYWORDS
$1,
416 ].concat(SUBST_INTERNALS
)
418 const SUBST_AND_COMMENTS
= [].concat(COMMENT
, SUBST
.contains
);
419 const PARAMS_CONTAINS
= SUBST_AND_COMMENTS
.concat([
420 // eat recursive parens in sub expressions
424 keywords: KEYWORDS
$1,
425 contains: ["self"].concat(SUBST_AND_COMMENTS
)
430 // convert this to negative lookbehind in v12
431 begin: /(\s*)\(/, // to match the parms with
435 keywords: KEYWORDS
$1,
436 contains: PARAMS_CONTAINS
440 const CLASS_OR_EXTENDS
= {
442 // class Car extends vehicle
451 regex
.concat(IDENT_RE
$1, "(", regex
.concat(/\./, IDENT_RE
$1), ")*")
457 7: "title.class.inherited"
476 const CLASS_REFERENCE
= {
480 // Hard coded exceptions
482 // Float32Array, OutT
483 /\b[A-Z][a-z]+([A-Z][a-z]*|\d)*/,
484 // CSSFactory, CSSFactoryT
485 /\b[A-Z]{2,}([A-Z][a-z]+|\d)+([A-Z][a-z]*)*/,
487 /\b[A-Z]{2,}[a-z]+([A-Z][a-z]+|\d)*([A-Z][a-z]*)*/,
489 // single letters are not highlighted
491 // this will be flagged as a UPPER_CASE_CONSTANT instead
493 className: "title.class",
496 // se we still get relevance credit for JS library classes
507 begin: /^\s
*['"]use (strict|asm)['"]/
510 const FUNCTION_DEFINITION = {
520 // anonymous function
533 contains: [ PARAMS ],
537 const UPPER_CASE_CONSTANT = {
539 match: /\b[A-Z][A-Z_0-9]+\b/,
540 className: "variable
.constant
"
543 function noneOf(list) {
544 return regex.concat("(?!", list.join("|"), ")");
547 const FUNCTION_CALL = {
554 ].map(x => `${x}\\s*\\(`)),
555 IDENT_RE$1, regex.lookahead(/\s*\(/)),
556 className: "title
.function",
560 const PROPERTY_ACCESS = {
561 begin: regex.concat(/\./, regex.lookahead(
562 regex.concat(IDENT_RE$1, /(?![0-9A-Za-z$_(])/)
566 keywords: "prototype",
567 className: "property
",
571 const GETTER_OR_SETTER = {
583 { // eat to avoid empty params
590 const FUNC_LEAD_IN_RE = '(\\(' +
596 '\\)|' + hljs.UNDERSCORE_IDENT_RE + ')\\s*=>';
598 const FUNCTION_VARIABLE = {
600 /const|var|let/, /\s+/,
603 /(async\s*)?/, // async is optional
604 regex.lookahead(FUNC_LEAD_IN_RE)
618 aliases: ['js', 'jsx', 'mjs', 'cjs'],
619 keywords: KEYWORDS$1,
620 // this will be extended by TypeScript
621 exports: { PARAMS_CONTAINS, CLASS_REFERENCE },
622 illegal: /#(?![$_A-z])/,
630 hljs.APOS_STRING_MODE,
631 hljs.QUOTE_STRING_MODE,
637 // Skip numbers when they are part of a variable name
643 match: IDENT_RE$1 + regex.lookahead(':'),
647 { // "value
" container
648 begin: '(' + hljs.RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*',
649 keywords: 'return throw case',
655 className: 'function',
656 // we have to count the parens to make sure we actually have the
657 // correct bounding ( ) before the =>. There could be any number of
658 // sub-expressions inside also surrounded by parens.
659 begin: FUNC_LEAD_IN_RE,
667 begin: hljs.UNDERSCORE_IDENT_RE,
680 keywords: KEYWORDS$1,
681 contains: PARAMS_CONTAINS
687 { // could be a comma delimited list of params to a function call
697 { begin: FRAGMENT.begin, end: FRAGMENT.end },
698 { match: XML_SELF_CLOSING },
700 begin: XML_TAG.begin,
701 // we carefully check the opening tag to see if it truly
702 // is a tag and not a false positive
703 'on:begin': XML_TAG.isTrulyOpeningTag,
710 begin: XML_TAG.begin,
721 // prevent this from getting swallowed up by function
722 // since they appear "function like
"
723 beginKeywords: "while if switch catch for"
726 // we have to count the parens to make sure we actually have the correct
727 // bounding ( ). There could be any number of sub-expressions inside
728 // also surrounded by parens.
729 begin: '\\b(?!function)' + hljs.UNDERSCORE_IDENT_RE +
730 '\\(' + // first parens
736 '\\)\\s*\\{', // end parens
741 hljs.inherit(hljs.TITLE_MODE, { begin: IDENT_RE$1, className: "title
.function" })
744 // catch ... so it won't trigger the property rule below
750 // hack: prevents detection of keywords in some circumstances
754 match: '\\$' + IDENT_RE$1,
758 match: [ /\bconstructor(?=\s*\()/ ],
759 className: { 1: "title
.function" },
767 match: /\$[(.]/ // relevance booster for a pattern common to JS libs: `$(something)` and `$.something`
777 export default hljsGrammar;