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