Skip to content

Commit c51abab

Browse files
authoredAug 27, 2020
JS Extras: Highlight import and export bindings (#2533)
1 parent bcef22a commit c51abab

File tree

4 files changed

+304
-2
lines changed

4 files changed

+304
-2
lines changed
 

‎components/prism-js-extras.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@
3232
]
3333
});
3434

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 /[_$a-zA-Z\xA0-\uFFFF][$\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+
3562
Prism.languages.javascript['keyword'].unshift(
3663
{
3764
pattern: /\b(?:as|default|export|from|import)\b/,
@@ -64,7 +91,7 @@
6491

6592
Prism.languages.insertBefore('javascript', 'punctuation', {
6693
'property-access': {
67-
pattern: /(\.\s*)#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
94+
pattern: withId(/(\.\s*)#?<ID>/.source),
6895
lookbehind: true
6996
},
7097
'maybe-class-name': {

‎components/prism-js-extras.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
export * from "mod";
2+
export * as Foo from "mod";
3+
export {} from "mod";
4+
export {x} from "mod";
5+
export {d} from "mod";
6+
export {d,} from "mod";
7+
export {d as Foo, b as Bar} from "mod";
8+
export {d as Foo, b as Bar,} from "mod";
9+
export {}
10+
export {x}
11+
export {d}
12+
export {d,}
13+
export {d as Foo, b as Bar}
14+
export {d as Foo, b as Bar,}
15+
16+
----------------------------------------------------
17+
18+
[
19+
["keyword", "export"],
20+
["exports", [
21+
["operator", "*"]
22+
]],
23+
["keyword", "from"],
24+
["string", "\"mod\""],
25+
["punctuation", ";"],
26+
["keyword", "export"],
27+
["exports", [
28+
["operator", "*"],
29+
["keyword", "as"],
30+
["maybe-class-name", "Foo"]
31+
]],
32+
["keyword", "from"],
33+
["string", "\"mod\""],
34+
["punctuation", ";"],
35+
["keyword", "export"],
36+
["exports", [
37+
["punctuation", "{"],
38+
["punctuation", "}"]
39+
]],
40+
["keyword", "from"],
41+
["string", "\"mod\""],
42+
["punctuation", ";"],
43+
["keyword", "export"],
44+
["exports", [
45+
["punctuation", "{"],
46+
"x",
47+
["punctuation", "}"]
48+
]],
49+
["keyword", "from"],
50+
["string", "\"mod\""],
51+
["punctuation", ";"],
52+
["keyword", "export"],
53+
["exports", [
54+
["punctuation", "{"],
55+
"d",
56+
["punctuation", "}"]
57+
]],
58+
["keyword", "from"],
59+
["string", "\"mod\""],
60+
["punctuation", ";"],
61+
["keyword", "export"],
62+
["exports", [
63+
["punctuation", "{"],
64+
"d",
65+
["punctuation", ","],
66+
["punctuation", "}"]
67+
]],
68+
["keyword", "from"],
69+
["string", "\"mod\""],
70+
["punctuation", ";"],
71+
["keyword", "export"],
72+
["exports", [
73+
["punctuation", "{"],
74+
"d ",
75+
["keyword", "as"],
76+
["maybe-class-name", "Foo"],
77+
["punctuation", ","],
78+
" b ",
79+
["keyword", "as"],
80+
["maybe-class-name", "Bar"],
81+
["punctuation", "}"]
82+
]],
83+
["keyword", "from"],
84+
["string", "\"mod\""],
85+
["punctuation", ";"],
86+
["keyword", "export"],
87+
["exports", [
88+
["punctuation", "{"],
89+
"d ",
90+
["keyword", "as"],
91+
["maybe-class-name", "Foo"],
92+
["punctuation", ","],
93+
" b ",
94+
["keyword", "as"],
95+
["maybe-class-name", "Bar"],
96+
["punctuation", ","],
97+
["punctuation", "}"]
98+
]],
99+
["keyword", "from"],
100+
["string", "\"mod\""],
101+
["punctuation", ";"],
102+
["keyword", "export"],
103+
["exports", [
104+
["punctuation", "{"],
105+
["punctuation", "}"]
106+
]],
107+
["keyword", "export"],
108+
["exports", [
109+
["punctuation", "{"],
110+
"x",
111+
["punctuation", "}"]
112+
]],
113+
["keyword", "export"],
114+
["exports", [
115+
["punctuation", "{"],
116+
"d",
117+
["punctuation", "}"]
118+
]],
119+
["keyword", "export"],
120+
["exports", [
121+
["punctuation", "{"],
122+
"d",
123+
["punctuation", ","],
124+
["punctuation", "}"]
125+
]],
126+
["keyword", "export"],
127+
["exports", [
128+
["punctuation", "{"],
129+
"d ",
130+
["keyword", "as"],
131+
["maybe-class-name", "Foo"],
132+
["punctuation", ","],
133+
" b ",
134+
["keyword", "as"],
135+
["maybe-class-name", "Bar"],
136+
["punctuation", "}"]
137+
]],
138+
["keyword", "export"],
139+
["exports", [
140+
["punctuation", "{"],
141+
"d ",
142+
["keyword", "as"],
143+
["maybe-class-name", "Foo"],
144+
["punctuation", ","],
145+
" b ",
146+
["keyword", "as"],
147+
["maybe-class-name", "Bar"],
148+
["punctuation", ","],
149+
["punctuation", "}"]
150+
]]
151+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import React from 'react';
2+
import x from "mod";
3+
import * as Foo from "mod";
4+
import {} from "mod";
5+
import {d} from "mod";
6+
import {d,} from "mod";
7+
import {d as Foo, b as Bar} from "mod";
8+
import {d as Foo, b as Bar,} from "mod";
9+
import Foo, { Bar } from "mod";
10+
import Foo, * as Bar from "mod";
11+
12+
import "mod";
13+
14+
----------------------------------------------------
15+
16+
[
17+
["keyword", "import"],
18+
["imports", [
19+
["maybe-class-name", "React"]
20+
]],
21+
["keyword", "from"],
22+
["string", "'react'"],
23+
["punctuation", ";"],
24+
["keyword", "import"],
25+
["imports", [
26+
"x"
27+
]],
28+
["keyword", "from"],
29+
["string", "\"mod\""],
30+
["punctuation", ";"],
31+
["keyword", "import"],
32+
["imports", [
33+
["operator", "*"],
34+
["keyword", "as"],
35+
["maybe-class-name", "Foo"]
36+
]],
37+
["keyword", "from"],
38+
["string", "\"mod\""],
39+
["punctuation", ";"],
40+
["keyword", "import"],
41+
["imports", [
42+
["punctuation", "{"],
43+
["punctuation", "}"]
44+
]],
45+
["keyword", "from"],
46+
["string", "\"mod\""],
47+
["punctuation", ";"],
48+
["keyword", "import"],
49+
["imports", [
50+
["punctuation", "{"],
51+
"d",
52+
["punctuation", "}"]
53+
]],
54+
["keyword", "from"],
55+
["string", "\"mod\""],
56+
["punctuation", ";"],
57+
["keyword", "import"],
58+
["imports", [
59+
["punctuation", "{"],
60+
"d",
61+
["punctuation", ","],
62+
["punctuation", "}"]
63+
]],
64+
["keyword", "from"],
65+
["string", "\"mod\""],
66+
["punctuation", ";"],
67+
["keyword", "import"],
68+
["imports", [
69+
["punctuation", "{"],
70+
"d ",
71+
["keyword", "as"],
72+
["maybe-class-name", "Foo"],
73+
["punctuation", ","],
74+
" b ",
75+
["keyword", "as"],
76+
["maybe-class-name", "Bar"],
77+
["punctuation", "}"]
78+
]],
79+
["keyword", "from"],
80+
["string", "\"mod\""],
81+
["punctuation", ";"],
82+
["keyword", "import"],
83+
["imports", [
84+
["punctuation", "{"],
85+
"d ",
86+
["keyword", "as"],
87+
["maybe-class-name", "Foo"],
88+
["punctuation", ","],
89+
" b ",
90+
["keyword", "as"],
91+
["maybe-class-name", "Bar"],
92+
["punctuation", ","],
93+
["punctuation", "}"]
94+
]],
95+
["keyword", "from"],
96+
["string", "\"mod\""],
97+
["punctuation", ";"],
98+
["keyword", "import"],
99+
["imports", [
100+
["maybe-class-name", "Foo"],
101+
["punctuation", ","],
102+
["punctuation", "{"],
103+
["maybe-class-name", "Bar"],
104+
["punctuation", "}"]
105+
]],
106+
["keyword", "from"],
107+
["string", "\"mod\""],
108+
["punctuation", ";"],
109+
["keyword", "import"],
110+
["imports", [
111+
["maybe-class-name", "Foo"],
112+
["punctuation", ","],
113+
["operator", "*"],
114+
["keyword", "as"],
115+
["maybe-class-name", "Bar"]
116+
]],
117+
["keyword", "from"],
118+
["string", "\"mod\""],
119+
["punctuation", ";"],
120+
121+
["keyword", "import"],
122+
["string", "\"mod\""],
123+
["punctuation", ";"]
124+
]

0 commit comments

Comments
 (0)
Please sign in to comment.