[d24f17c] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | module.exports = javascript
|
---|
| 4 | javascript.displayName = 'javascript'
|
---|
| 5 | javascript.aliases = ['js']
|
---|
| 6 | function javascript(Prism) {
|
---|
| 7 | Prism.languages.javascript = Prism.languages.extend('clike', {
|
---|
| 8 | 'class-name': [
|
---|
| 9 | Prism.languages.clike['class-name'],
|
---|
| 10 | {
|
---|
| 11 | pattern:
|
---|
| 12 | /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$A-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\.(?:constructor|prototype))/,
|
---|
| 13 | lookbehind: true
|
---|
| 14 | }
|
---|
| 15 | ],
|
---|
| 16 | keyword: [
|
---|
| 17 | {
|
---|
| 18 | pattern: /((?:^|\})\s*)catch\b/,
|
---|
| 19 | lookbehind: true
|
---|
| 20 | },
|
---|
| 21 | {
|
---|
| 22 | pattern:
|
---|
| 23 | /(^|[^.]|\.\.\.\s*)\b(?:as|assert(?=\s*\{)|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|function|(?:get|set)(?=\s*(?:[#\[$\w\xA0-\uFFFF]|$))|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
|
---|
| 24 | lookbehind: true
|
---|
| 25 | }
|
---|
| 26 | ],
|
---|
| 27 | // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
|
---|
| 28 | function:
|
---|
| 29 | /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
|
---|
| 30 | number: {
|
---|
| 31 | pattern: RegExp(
|
---|
| 32 | /(^|[^\w$])/.source +
|
---|
| 33 | '(?:' + // constant
|
---|
| 34 | (/NaN|Infinity/.source +
|
---|
| 35 | '|' + // binary integer
|
---|
| 36 | /0[bB][01]+(?:_[01]+)*n?/.source +
|
---|
| 37 | '|' + // octal integer
|
---|
| 38 | /0[oO][0-7]+(?:_[0-7]+)*n?/.source +
|
---|
| 39 | '|' + // hexadecimal integer
|
---|
| 40 | /0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
|
---|
| 41 | '|' + // decimal bigint
|
---|
| 42 | /\d+(?:_\d+)*n/.source +
|
---|
| 43 | '|' + // decimal number (integer or float) but no bigint
|
---|
| 44 | /(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/
|
---|
| 45 | .source) +
|
---|
| 46 | ')' +
|
---|
| 47 | /(?![\w$])/.source
|
---|
| 48 | ),
|
---|
| 49 | lookbehind: true
|
---|
| 50 | },
|
---|
| 51 | operator:
|
---|
| 52 | /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
|
---|
| 53 | })
|
---|
| 54 | Prism.languages.javascript['class-name'][0].pattern =
|
---|
| 55 | /(\b(?:class|extends|implements|instanceof|interface|new)\s+)[\w.\\]+/
|
---|
| 56 | Prism.languages.insertBefore('javascript', 'keyword', {
|
---|
| 57 | regex: {
|
---|
| 58 | // eslint-disable-next-line regexp/no-dupe-characters-character-class
|
---|
| 59 | pattern:
|
---|
| 60 | /((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/,
|
---|
| 61 | lookbehind: true,
|
---|
| 62 | greedy: true,
|
---|
| 63 | inside: {
|
---|
| 64 | 'regex-source': {
|
---|
| 65 | pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
|
---|
| 66 | lookbehind: true,
|
---|
| 67 | alias: 'language-regex',
|
---|
| 68 | inside: Prism.languages.regex
|
---|
| 69 | },
|
---|
| 70 | 'regex-delimiter': /^\/|\/$/,
|
---|
| 71 | 'regex-flags': /^[a-z]+$/
|
---|
| 72 | }
|
---|
| 73 | },
|
---|
| 74 | // This must be declared before keyword because we use "function" inside the look-forward
|
---|
| 75 | 'function-variable': {
|
---|
| 76 | pattern:
|
---|
| 77 | /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)\s*=>))/,
|
---|
| 78 | alias: 'function'
|
---|
| 79 | },
|
---|
| 80 | parameter: [
|
---|
| 81 | {
|
---|
| 82 | pattern:
|
---|
| 83 | /(function(?:\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)?\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\))/,
|
---|
| 84 | lookbehind: true,
|
---|
| 85 | inside: Prism.languages.javascript
|
---|
| 86 | },
|
---|
| 87 | {
|
---|
| 88 | pattern:
|
---|
| 89 | /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$a-z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*=>)/i,
|
---|
| 90 | lookbehind: true,
|
---|
| 91 | inside: Prism.languages.javascript
|
---|
| 92 | },
|
---|
| 93 | {
|
---|
| 94 | pattern:
|
---|
| 95 | /(\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*=>)/,
|
---|
| 96 | lookbehind: true,
|
---|
| 97 | inside: Prism.languages.javascript
|
---|
| 98 | },
|
---|
| 99 | {
|
---|
| 100 | pattern:
|
---|
| 101 | /((?:\b|\s|^)(?!(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*\s*)\(\s*|\]\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*\{)/,
|
---|
| 102 | lookbehind: true,
|
---|
| 103 | inside: Prism.languages.javascript
|
---|
| 104 | }
|
---|
| 105 | ],
|
---|
| 106 | constant: /\b[A-Z](?:[A-Z_]|\dx?)*\b/
|
---|
| 107 | })
|
---|
| 108 | Prism.languages.insertBefore('javascript', 'string', {
|
---|
| 109 | hashbang: {
|
---|
| 110 | pattern: /^#!.*/,
|
---|
| 111 | greedy: true,
|
---|
| 112 | alias: 'comment'
|
---|
| 113 | },
|
---|
| 114 | 'template-string': {
|
---|
| 115 | pattern:
|
---|
| 116 | /`(?:\\[\s\S]|\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}|(?!\$\{)[^\\`])*`/,
|
---|
| 117 | greedy: true,
|
---|
| 118 | inside: {
|
---|
| 119 | 'template-punctuation': {
|
---|
| 120 | pattern: /^`|`$/,
|
---|
| 121 | alias: 'string'
|
---|
| 122 | },
|
---|
| 123 | interpolation: {
|
---|
| 124 | pattern:
|
---|
| 125 | /((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}/,
|
---|
| 126 | lookbehind: true,
|
---|
| 127 | inside: {
|
---|
| 128 | 'interpolation-punctuation': {
|
---|
| 129 | pattern: /^\$\{|\}$/,
|
---|
| 130 | alias: 'punctuation'
|
---|
| 131 | },
|
---|
| 132 | rest: Prism.languages.javascript
|
---|
| 133 | }
|
---|
| 134 | },
|
---|
| 135 | string: /[\s\S]+/
|
---|
| 136 | }
|
---|
| 137 | },
|
---|
| 138 | 'string-property': {
|
---|
| 139 | pattern:
|
---|
| 140 | /((?:^|[,{])[ \t]*)(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2(?=\s*:)/m,
|
---|
| 141 | lookbehind: true,
|
---|
| 142 | greedy: true,
|
---|
| 143 | alias: 'property'
|
---|
| 144 | }
|
---|
| 145 | })
|
---|
| 146 | Prism.languages.insertBefore('javascript', 'operator', {
|
---|
| 147 | 'literal-property': {
|
---|
| 148 | pattern:
|
---|
| 149 | /((?:^|[,{])[ \t]*)(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*:)/m,
|
---|
| 150 | lookbehind: true,
|
---|
| 151 | alias: 'property'
|
---|
| 152 | }
|
---|
| 153 | })
|
---|
| 154 | if (Prism.languages.markup) {
|
---|
| 155 | Prism.languages.markup.tag.addInlined('script', 'javascript') // add attribute support for all DOM events.
|
---|
| 156 | // https://developer.mozilla.org/en-US/docs/Web/Events#Standard_events
|
---|
| 157 | Prism.languages.markup.tag.addAttribute(
|
---|
| 158 | /on(?:abort|blur|change|click|composition(?:end|start|update)|dblclick|error|focus(?:in|out)?|key(?:down|up)|load|mouse(?:down|enter|leave|move|out|over|up)|reset|resize|scroll|select|slotchange|submit|unload|wheel)/
|
---|
| 159 | .source,
|
---|
| 160 | 'javascript'
|
---|
| 161 | )
|
---|
| 162 | }
|
---|
| 163 | Prism.languages.js = Prism.languages.javascript
|
---|
| 164 | }
|
---|