@@ -19,63 +19,64 @@ const requireMain = createRequire(require.main.filename);
1919 */
2020async function processExtension ( packageJsonPath ) {
2121 const packageJson = requireJson ( packageJsonPath ) ;
22+ /** @type {Record<string, GrammarData> } */
2223 let grammars = { } ;
24+ /** @type {Record<string, ThemeData> } */
2325 let themes = { } ;
2426 if ( packageJson . contributes && packageJson . contributes . grammars ) {
2527 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+ )
4550 ) ;
4651
4752 grammars = manifest . reduce (
4853 ( hash , grammar ) => ( {
4954 ...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
5756 } ) ,
5857 { }
5958 ) ;
6059 }
6160
6261 if ( packageJson . contributes && packageJson . contributes . themes ) {
6362 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+ )
7980 ) ;
8081
8182 themes = manifest . reduce (
@@ -91,7 +92,7 @@ async function processExtension(packageJsonPath) {
9192}
9293
9394/**
94- * @param {* } cache
95+ * @param {GatsbyCache } cache
9596 * @param {string } key
9697 * @param {object } value
9798 */
@@ -139,7 +140,7 @@ async function getExtensionPackageJsonPath(specifier, host) {
139140/**
140141 * @param {string[] } extensions
141142 * @param {Host } host
142- * @param {* } cache
143+ * @param {GatsbyCache } cache
143144 */
144145async function processExtensions ( extensions , host , cache ) {
145146 let languageId = getHighestBuiltinLanguageId ( ) + 1 ;
@@ -152,4 +153,38 @@ async function processExtensions(extensions, host, cache) {
152153 }
153154}
154155
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+
155190module . exports = { processExtension, processExtensions } ;
0 commit comments