1 | 'use strict'
|
---|
2 |
|
---|
3 | module.exports = jsExtras
|
---|
4 | jsExtras.displayName = 'jsExtras'
|
---|
5 | jsExtras.aliases = []
|
---|
6 | function jsExtras(Prism) {
|
---|
7 | ;(function (Prism) {
|
---|
8 | Prism.languages.insertBefore('javascript', 'function-variable', {
|
---|
9 | 'method-variable': {
|
---|
10 | pattern: RegExp(
|
---|
11 | '(\\.\\s*)' +
|
---|
12 | Prism.languages.javascript['function-variable'].pattern.source
|
---|
13 | ),
|
---|
14 | lookbehind: true,
|
---|
15 | alias: ['function-variable', 'method', 'function', 'property-access']
|
---|
16 | }
|
---|
17 | })
|
---|
18 | Prism.languages.insertBefore('javascript', 'function', {
|
---|
19 | method: {
|
---|
20 | pattern: RegExp(
|
---|
21 | '(\\.\\s*)' + Prism.languages.javascript['function'].source
|
---|
22 | ),
|
---|
23 | lookbehind: true,
|
---|
24 | alias: ['function', 'property-access']
|
---|
25 | }
|
---|
26 | })
|
---|
27 | Prism.languages.insertBefore('javascript', 'constant', {
|
---|
28 | 'known-class-name': [
|
---|
29 | {
|
---|
30 | // standard built-ins
|
---|
31 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
|
---|
32 | pattern:
|
---|
33 | /\b(?:(?:Float(?:32|64)|(?:Int|Uint)(?:8|16|32)|Uint8Clamped)?Array|ArrayBuffer|BigInt|Boolean|DataView|Date|Error|Function|Intl|JSON|(?:Weak)?(?:Map|Set)|Math|Number|Object|Promise|Proxy|Reflect|RegExp|String|Symbol|WebAssembly)\b/,
|
---|
34 | alias: 'class-name'
|
---|
35 | },
|
---|
36 | {
|
---|
37 | // errors
|
---|
38 | pattern: /\b(?:[A-Z]\w*)Error\b/,
|
---|
39 | alias: 'class-name'
|
---|
40 | }
|
---|
41 | ]
|
---|
42 | })
|
---|
43 | /**
|
---|
44 | * Replaces the `<ID>` placeholder in the given pattern with a pattern for general JS identifiers.
|
---|
45 | *
|
---|
46 | * @param {string} source
|
---|
47 | * @param {string} [flags]
|
---|
48 | * @returns {RegExp}
|
---|
49 | */
|
---|
50 | function withId(source, flags) {
|
---|
51 | return RegExp(
|
---|
52 | source.replace(/<ID>/g, function () {
|
---|
53 | return /(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*/.source
|
---|
54 | }),
|
---|
55 | flags
|
---|
56 | )
|
---|
57 | }
|
---|
58 | Prism.languages.insertBefore('javascript', 'keyword', {
|
---|
59 | imports: {
|
---|
60 | // https://tc39.es/ecma262/#sec-imports
|
---|
61 | pattern: withId(
|
---|
62 | /(\bimport\b\s*)(?:<ID>(?:\s*,\s*(?:\*\s*as\s+<ID>|\{[^{}]*\}))?|\*\s*as\s+<ID>|\{[^{}]*\})(?=\s*\bfrom\b)/
|
---|
63 | .source
|
---|
64 | ),
|
---|
65 | lookbehind: true,
|
---|
66 | inside: Prism.languages.javascript
|
---|
67 | },
|
---|
68 | exports: {
|
---|
69 | // https://tc39.es/ecma262/#sec-exports
|
---|
70 | pattern: withId(
|
---|
71 | /(\bexport\b\s*)(?:\*(?:\s*as\s+<ID>)?(?=\s*\bfrom\b)|\{[^{}]*\})/
|
---|
72 | .source
|
---|
73 | ),
|
---|
74 | lookbehind: true,
|
---|
75 | inside: Prism.languages.javascript
|
---|
76 | }
|
---|
77 | })
|
---|
78 | Prism.languages.javascript['keyword'].unshift(
|
---|
79 | {
|
---|
80 | pattern: /\b(?:as|default|export|from|import)\b/,
|
---|
81 | alias: 'module'
|
---|
82 | },
|
---|
83 | {
|
---|
84 | pattern:
|
---|
85 | /\b(?:await|break|catch|continue|do|else|finally|for|if|return|switch|throw|try|while|yield)\b/,
|
---|
86 | alias: 'control-flow'
|
---|
87 | },
|
---|
88 | {
|
---|
89 | pattern: /\bnull\b/,
|
---|
90 | alias: ['null', 'nil']
|
---|
91 | },
|
---|
92 | {
|
---|
93 | pattern: /\bundefined\b/,
|
---|
94 | alias: 'nil'
|
---|
95 | }
|
---|
96 | )
|
---|
97 | Prism.languages.insertBefore('javascript', 'operator', {
|
---|
98 | spread: {
|
---|
99 | pattern: /\.{3}/,
|
---|
100 | alias: 'operator'
|
---|
101 | },
|
---|
102 | arrow: {
|
---|
103 | pattern: /=>/,
|
---|
104 | alias: 'operator'
|
---|
105 | }
|
---|
106 | })
|
---|
107 | Prism.languages.insertBefore('javascript', 'punctuation', {
|
---|
108 | 'property-access': {
|
---|
109 | pattern: withId(/(\.\s*)#?<ID>/.source),
|
---|
110 | lookbehind: true
|
---|
111 | },
|
---|
112 | 'maybe-class-name': {
|
---|
113 | pattern: /(^|[^$\w\xA0-\uFFFF])[A-Z][$\w\xA0-\uFFFF]+/,
|
---|
114 | lookbehind: true
|
---|
115 | },
|
---|
116 | dom: {
|
---|
117 | // this contains only a few commonly used DOM variables
|
---|
118 | pattern:
|
---|
119 | /\b(?:document|(?:local|session)Storage|location|navigator|performance|window)\b/,
|
---|
120 | alias: 'variable'
|
---|
121 | },
|
---|
122 | console: {
|
---|
123 | pattern: /\bconsole(?=\s*\.)/,
|
---|
124 | alias: 'class-name'
|
---|
125 | }
|
---|
126 | }) // add 'maybe-class-name' to tokens which might be a class name
|
---|
127 | var maybeClassNameTokens = [
|
---|
128 | 'function',
|
---|
129 | 'function-variable',
|
---|
130 | 'method',
|
---|
131 | 'method-variable',
|
---|
132 | 'property-access'
|
---|
133 | ]
|
---|
134 | for (var i = 0; i < maybeClassNameTokens.length; i++) {
|
---|
135 | var token = maybeClassNameTokens[i]
|
---|
136 | var value = Prism.languages.javascript[token] // convert regex to object
|
---|
137 | if (Prism.util.type(value) === 'RegExp') {
|
---|
138 | value = Prism.languages.javascript[token] = {
|
---|
139 | pattern: value
|
---|
140 | }
|
---|
141 | } // keep in mind that we don't support arrays
|
---|
142 | var inside = value.inside || {}
|
---|
143 | value.inside = inside
|
---|
144 | inside['maybe-class-name'] = /^[A-Z][\s\S]*/
|
---|
145 | }
|
---|
146 | })(Prism)
|
---|
147 | }
|
---|