@@ -19,63 +19,64 @@ const requireMain = createRequire(require.main.filename);
19
19
*/
20
20
async function processExtension ( packageJsonPath ) {
21
21
const packageJson = requireJson ( packageJsonPath ) ;
22
+ /** @type {Record<string, GrammarData> } */
22
23
let grammars = { } ;
24
+ /** @type {Record<string, ThemeData> } */
23
25
let themes = { } ;
24
26
if ( packageJson . contributes && packageJson . contributes . grammars ) {
25
27
const manifest = await Promise . all (
26
- packageJson . contributes . grammars . map ( async grammar => {
27
- const sourcePath = path . resolve ( path . dirname ( packageJsonPath ) , grammar . path ) ;
28
- const content = await requirePlistOrJson ( sourcePath ) ;
29
- const { scopeName } = content ;
30
- const languageRegistration = packageJson . contributes . languages . find ( l => l . id === grammar . language ) ;
31
- const languageNames = languageRegistration ? getLanguageNames ( languageRegistration ) : [ ] ;
32
- logger . info (
33
- `Registering grammar '${ scopeName } ' from package ${ packageJson . name } with language names: ${ languageNames } `
34
- ) ;
35
-
36
- return {
37
- scopeName,
38
- path : sourcePath ,
39
- tokenTypes : grammar . tokenTypes ,
40
- embeddedLanguages : grammar . embeddedLanguages ,
41
- injectTo : grammar . injectTo ,
42
- languageNames
43
- } ;
44
- } )
28
+ packageJson . contributes . grammars . map (
29
+ /** @returns {Promise<GrammarData> } */ async grammar => {
30
+ const sourcePath = path . resolve ( path . dirname ( packageJsonPath ) , grammar . path ) ;
31
+ const content = await requirePlistOrJson ( sourcePath ) ;
32
+ const { scopeName } = content ;
33
+ const languageRegistration = packageJson . contributes . languages . find ( l => l . id === grammar . language ) ;
34
+ const languageNames = languageRegistration ? getLanguageNames ( languageRegistration ) : [ ] ;
35
+ logger . info (
36
+ `Registering grammar '${ scopeName } ' from package ${ packageJson . name } with language names: ${ languageNames } `
37
+ ) ;
38
+
39
+ return {
40
+ languageId : 0 , // Overwritten elsewhere
41
+ scopeName,
42
+ path : sourcePath ,
43
+ tokenTypes : toStandardTokenTypes ( grammar . tokenTypes ) ,
44
+ embeddedLanguages : grammar . embeddedLanguages ,
45
+ injectTo : grammar . injectTo ,
46
+ languageNames
47
+ } ;
48
+ }
49
+ )
45
50
) ;
46
51
47
52
grammars = manifest . reduce (
48
53
( hash , grammar ) => ( {
49
54
...hash ,
50
- [ grammar . scopeName ] : {
51
- path : grammar . path ,
52
- tokenTypes : grammar . tokenTypes ,
53
- embeddedLanguages : grammar . embeddedLanguages ,
54
- injectTo : grammar . injectTo ,
55
- languageNames : grammar . languageNames
56
- }
55
+ [ grammar . scopeName ] : grammar
57
56
} ) ,
58
57
{ }
59
58
) ;
60
59
}
61
60
62
61
if ( packageJson . contributes && packageJson . contributes . themes ) {
63
62
const manifest = await Promise . all (
64
- packageJson . contributes . themes . map ( async theme => {
65
- const sourcePath = path . resolve ( path . dirname ( packageJsonPath ) , theme . path ) ;
66
- const themeContents = await requirePlistOrJson ( sourcePath ) ;
67
- const id = theme . id || path . basename ( theme . path ) . split ( '.' ) [ 0 ] ;
68
- logger . info ( `Registering theme '${ theme . label || id } ' from package ${ packageJson . name } ` ) ;
69
-
70
- return {
71
- id,
72
- path : sourcePath ,
73
- label : theme . label ,
74
- include : themeContents . include ,
75
- packageName : packageJson . name ,
76
- isOnlyThemeInPackage : packageJson . contributes . themes . length === 1
77
- } ;
78
- } )
63
+ packageJson . contributes . themes . map (
64
+ /** @returns {Promise<ThemeData> } */ async theme => {
65
+ const sourcePath = path . resolve ( path . dirname ( packageJsonPath ) , theme . path ) ;
66
+ const themeContents = await requirePlistOrJson ( sourcePath ) ;
67
+ const id = theme . id || path . basename ( theme . path ) . split ( '.' ) [ 0 ] ;
68
+ logger . info ( `Registering theme '${ theme . label || id } ' from package ${ packageJson . name } ` ) ;
69
+
70
+ return {
71
+ id,
72
+ path : sourcePath ,
73
+ label : theme . label ,
74
+ include : themeContents . include ,
75
+ packageName : packageJson . name ,
76
+ isOnlyThemeInPackage : packageJson . contributes . themes . length === 1
77
+ } ;
78
+ }
79
+ )
79
80
) ;
80
81
81
82
themes = manifest . reduce (
@@ -91,7 +92,7 @@ async function processExtension(packageJsonPath) {
91
92
}
92
93
93
94
/**
94
- * @param {* } cache
95
+ * @param {GatsbyCache } cache
95
96
* @param {string } key
96
97
* @param {object } value
97
98
*/
@@ -139,7 +140,7 @@ async function getExtensionPackageJsonPath(specifier, host) {
139
140
/**
140
141
* @param {string[] } extensions
141
142
* @param {Host } host
142
- * @param {* } cache
143
+ * @param {GatsbyCache } cache
143
144
*/
144
145
async function processExtensions ( extensions , host , cache ) {
145
146
let languageId = getHighestBuiltinLanguageId ( ) + 1 ;
@@ -152,4 +153,38 @@ async function processExtensions(extensions, host, cache) {
152
153
}
153
154
}
154
155
156
+ /**
157
+ * @param {Record<string, string> | undefined } tokenTypes
158
+ * @returns {import('vscode-textmate').ITokenTypeMap }
159
+ */
160
+ function toStandardTokenTypes ( tokenTypes ) {
161
+ return (
162
+ tokenTypes &&
163
+ Object . keys ( tokenTypes ) . reduce (
164
+ ( map , selector ) => ( {
165
+ ...map ,
166
+ [ selector ] : toStandardTokenType ( tokenTypes [ selector ] )
167
+ } ) ,
168
+ { }
169
+ )
170
+ ) ;
171
+ }
172
+
173
+ /**
174
+ * @param {string } tokenType
175
+ * @returns {import('vscode-textmate').StandardTokenType }
176
+ */
177
+ function toStandardTokenType ( tokenType ) {
178
+ switch ( tokenType . toLowerCase ( ) ) {
179
+ case 'comment' :
180
+ return 1 ;
181
+ case 'string' :
182
+ return 2 ;
183
+ case 'regex' :
184
+ return 4 ;
185
+ default :
186
+ return 0 ;
187
+ }
188
+ }
189
+
155
190
module . exports = { processExtension, processExtensions } ;
0 commit comments