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