UNPKG

5.66 kBJavaScriptView Raw
1Prism.languages.javascript = Prism.languages.extend('clike', {
2 'class-name': [
3 Prism.languages.clike['class-name'],
4 {
5 pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$A-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\.(?:constructor|prototype))/,
6 lookbehind: true
7 }
8 ],
9 'keyword': [
10 {
11 pattern: /((?:^|\})\s*)catch\b/,
12 lookbehind: true
13 },
14 {
15 pattern: /(^|[^.]|\.\.\.\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/,
16 lookbehind: true
17 },
18 ],
19 // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
20 'function': /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
21 'number': {
22 pattern: RegExp(
23 /(^|[^\w$])/.source +
24 '(?:' +
25 (
26 // constant
27 /NaN|Infinity/.source +
28 '|' +
29 // binary integer
30 /0[bB][01]+(?:_[01]+)*n?/.source +
31 '|' +
32 // octal integer
33 /0[oO][0-7]+(?:_[0-7]+)*n?/.source +
34 '|' +
35 // hexadecimal integer
36 /0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
37 '|' +
38 // decimal bigint
39 /\d+(?:_\d+)*n/.source +
40 '|' +
41 // decimal number (integer or float) but no bigint
42 /(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/.source
43 ) +
44 ')' +
45 /(?![\w$])/.source
46 ),
47 lookbehind: true
48 },
49 'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
50});
51
52Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|extends|implements|instanceof|interface|new)\s+)[\w.\\]+/;
53
54Prism.languages.insertBefore('javascript', 'keyword', {
55 'regex': {
56 // eslint-disable-next-line regexp/no-dupe-characters-character-class
57 pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/,
58 lookbehind: true,
59 greedy: true,
60 inside: {
61 'regex-source': {
62 pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
63 lookbehind: true,
64 alias: 'language-regex',
65 inside: Prism.languages.regex
66 },
67 'regex-delimiter': /^\/|\/$/,
68 'regex-flags': /^[a-z]+$/,
69 }
70 },
71 // This must be declared before keyword because we use "function" inside the look-forward
72 'function-variable': {
73 pattern: /#?(?!\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*=>))/,
74 alias: 'function'
75 },
76 'parameter': [
77 {
78 pattern: /(function(?:\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)?\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\))/,
79 lookbehind: true,
80 inside: Prism.languages.javascript
81 },
82 {
83 pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$a-z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*=>)/i,
84 lookbehind: true,
85 inside: Prism.languages.javascript
86 },
87 {
88 pattern: /(\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*=>)/,
89 lookbehind: true,
90 inside: Prism.languages.javascript
91 },
92 {
93 pattern: /((?:\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*\{)/,
94 lookbehind: true,
95 inside: Prism.languages.javascript
96 }
97 ],
98 'constant': /\b[A-Z](?:[A-Z_]|\dx?)*\b/
99});
100
101Prism.languages.insertBefore('javascript', 'string', {
102 'hashbang': {
103 pattern: /^#!.*/,
104 greedy: true,
105 alias: 'comment'
106 },
107 'template-string': {
108 pattern: /`(?:\\[\s\S]|\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}|(?!\$\{)[^\\`])*`/,
109 greedy: true,
110 inside: {
111 'template-punctuation': {
112 pattern: /^`|`$/,
113 alias: 'string'
114 },
115 'interpolation': {
116 pattern: /((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}/,
117 lookbehind: true,
118 inside: {
119 'interpolation-punctuation': {
120 pattern: /^\$\{|\}$/,
121 alias: 'punctuation'
122 },
123 rest: Prism.languages.javascript
124 }
125 },
126 'string': /[\s\S]+/
127 }
128 },
129 'string-property': {
130 pattern: /((?:^|[,{])[ \t]*)(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2(?=\s*:)/m,
131 lookbehind: true,
132 greedy: true,
133 alias: 'property'
134 }
135});
136
137Prism.languages.insertBefore('javascript', 'operator', {
138 'literal-property': {
139 pattern: /((?:^|[,{])[ \t]*)(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*:)/m,
140 lookbehind: true,
141 alias: 'property'
142 },
143});
144
145if (Prism.languages.markup) {
146 Prism.languages.markup.tag.addInlined('script', 'javascript');
147
148 // add attribute support for all DOM events.
149 // https://developer.mozilla.org/en-US/docs/Web/Events#Standard_events
150 Prism.languages.markup.tag.addAttribute(
151 /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)/.source,
152 'javascript'
153 );
154}
155
156Prism.languages.js = Prism.languages.javascript;