UNPKG

6.33 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 pattern: RegExp(
57 // lookbehind
58 // eslint-disable-next-line regexp/no-dupe-characters-character-class
59 /((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)/.source +
60 // Regex pattern:
61 // There are 2 regex patterns here. The RegExp set notation proposal added support for nested character
62 // classes if the `v` flag is present. Unfortunately, nested CCs are both context-free and incompatible
63 // with the only syntax, so we have to define 2 different regex patterns.
64 /\//.source +
65 '(?:' +
66 /(?:\[(?:[^\]\\\r\n]|\\.)*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}/.source +
67 '|' +
68 // `v` flag syntax. This supports 3 levels of nested character classes.
69 /(?:\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.)*\])*\])*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}v[dgimyus]{0,7}/.source +
70 ')' +
71 // lookahead
72 /(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/.source
73 ),
74 lookbehind: true,
75 greedy: true,
76 inside: {
77 'regex-source': {
78 pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
79 lookbehind: true,
80 alias: 'language-regex',
81 inside: Prism.languages.regex
82 },
83 'regex-delimiter': /^\/|\/$/,
84 'regex-flags': /^[a-z]+$/,
85 }
86 },
87 // This must be declared before keyword because we use "function" inside the look-forward
88 'function-variable': {
89 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*=>))/,
90 alias: 'function'
91 },
92 'parameter': [
93 {
94 pattern: /(function(?:\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)?\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\))/,
95 lookbehind: true,
96 inside: Prism.languages.javascript
97 },
98 {
99 pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$a-z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*=>)/i,
100 lookbehind: true,
101 inside: Prism.languages.javascript
102 },
103 {
104 pattern: /(\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*=>)/,
105 lookbehind: true,
106 inside: Prism.languages.javascript
107 },
108 {
109 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*\{)/,
110 lookbehind: true,
111 inside: Prism.languages.javascript
112 }
113 ],
114 'constant': /\b[A-Z](?:[A-Z_]|\dx?)*\b/
115});
116
117Prism.languages.insertBefore('javascript', 'string', {
118 'hashbang': {
119 pattern: /^#!.*/,
120 greedy: true,
121 alias: 'comment'
122 },
123 'template-string': {
124 pattern: /`(?:\\[\s\S]|\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}|(?!\$\{)[^\\`])*`/,
125 greedy: true,
126 inside: {
127 'template-punctuation': {
128 pattern: /^`|`$/,
129 alias: 'string'
130 },
131 'interpolation': {
132 pattern: /((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}/,
133 lookbehind: true,
134 inside: {
135 'interpolation-punctuation': {
136 pattern: /^\$\{|\}$/,
137 alias: 'punctuation'
138 },
139 rest: Prism.languages.javascript
140 }
141 },
142 'string': /[\s\S]+/
143 }
144 },
145 'string-property': {
146 pattern: /((?:^|[,{])[ \t]*)(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2(?=\s*:)/m,
147 lookbehind: true,
148 greedy: true,
149 alias: 'property'
150 }
151});
152
153Prism.languages.insertBefore('javascript', 'operator', {
154 'literal-property': {
155 pattern: /((?:^|[,{])[ \t]*)(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*:)/m,
156 lookbehind: true,
157 alias: 'property'
158 },
159});
160
161if (Prism.languages.markup) {
162 Prism.languages.markup.tag.addInlined('script', 'javascript');
163
164 // add attribute support for all DOM events.
165 // https://developer.mozilla.org/en-US/docs/Web/Events#Standard_events
166 Prism.languages.markup.tag.addAttribute(
167 /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,
168 'javascript'
169 );
170}
171
172Prism.languages.js = Prism.languages.javascript;