Skip to content

Commit e676135

Browse files
authored
Merge pull request #74 from andrewbranch/more-types
Improve some types and fix tokenTypes transformation
2 parents aca519a + b1d4e3f commit e676135

File tree

7 files changed

+121
-57
lines changed

7 files changed

+121
-57
lines changed

src/getPossibleThemes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const {
88

99
/**
1010
* @param {ThemeOption} themeOption
11-
* @param {object} themeCache
11+
* @param {ThemeCache} themeCache
1212
* @param {string | undefined} contextDirectory
1313
* @param {MarkdownNode} markdownNode
14-
* @param {object} codeFenceNode
14+
* @param {MDASTNode} codeFenceNode
1515
* @param {string} languageName
1616
* @param {object} meta
1717
* @returns {Promise<ConditionalTheme[]>}

src/graphql/getThemes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const parseThemeCondition = require('./parseThemeCondition');
55

66
/**
77
* @param {grvsc.gql.GRVSCThemeArgument} theme
8-
* @param {any} themeCache
8+
* @param {ThemeCache} themeCache
99
* @returns {Promise<ConditionalTheme>}
1010
*/
1111
async function convertThemeArgument(theme, themeCache) {
@@ -19,7 +19,7 @@ async function convertThemeArgument(theme, themeCache) {
1919
/**
2020
* @param {ThemeOption} themeOption
2121
* @param {grvsc.gql.CSSArgs} args
22-
* @param {any} themeCache
22+
* @param {ThemeCache} themeCache
2323
* @returns {Promise<ConditionalTheme[]>}
2424
*/
2525
async function getThemes(themeOption, args, themeCache) {

src/processExtension.js

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,64 @@ const requireMain = createRequire(require.main.filename);
1919
*/
2020
async 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
*/
144145
async 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+
155190
module.exports = { processExtension, processExtensions };

src/storeUtils.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ const { exists } = require('./utils');
66
let grammarManifest;
77
/** @type {void} */
88
let themeManifest;
9+
10+
/** @returns {GrammarCache} */
911
function getGrammarManifest() {
1012
// @ts-ignore
1113
return grammarManifest || (grammarManifest = require('../lib/grammars/manifest.json'));
1214
}
15+
16+
/** @returns {ThemeCache} */
1317
function getThemeManifest() {
1418
// @ts-ignore
1519
return themeManifest || (themeManifest = require('../lib/themes/manifest.json'));
@@ -25,7 +29,7 @@ function resolveAlias(language, languageAliases) {
2529

2630
/**
2731
* @param {string} language
28-
* @param {*} grammarCache
32+
* @param {GrammarCache} grammarCache
2933
* @param {Record<string, string>} languageAliases
3034
*/
3135
function getScope(language, grammarCache, languageAliases) {
@@ -40,7 +44,7 @@ function getScope(language, grammarCache, languageAliases) {
4044
}
4145

4246
/**
43-
* @param {*} grammar
47+
* @param {GrammarData} grammar
4448
*/
4549
function getGrammarLocation(grammar) {
4650
return path.isAbsolute(grammar.path) ? grammar.path : path.resolve(__dirname, '../lib/grammars', grammar.path);
@@ -49,7 +53,7 @@ function getGrammarLocation(grammar) {
4953
/**
5054
*
5155
* @param {string} themeNameOrId
52-
* @param {object} themeCache
56+
* @param {ThemeCache} themeCache
5357
* @param {string=} contextDirectory
5458
* @returns {Promise<string>}
5559
*/
@@ -91,14 +95,15 @@ function getHighestBuiltinLanguageId() {
9195

9296
/**
9397
* @param {string} scopeName
94-
* @param {*} grammarCache
98+
* @param {GrammarCache} grammarCache
9599
*/
96100
function getGrammar(scopeName, grammarCache) {
97101
return getAllGrammars(grammarCache)[scopeName];
98102
}
99103

100104
/**
101-
* @param {*} grammarCache
105+
* @param {GrammarCache} grammarCache
106+
* @returns {GrammarCache}
102107
*/
103108
function getAllGrammars(grammarCache) {
104109
return { ...getGrammarManifest(), ...grammarCache };

src/themeUtils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function concatConditionalThemes(arr1, arr2) {
7373

7474
/**
7575
* @param {string} identifier
76-
* @param {any} themeCache
76+
* @param {ThemeCache} themeCache
7777
* @param {string=} contextDirectory
7878
* @returns {Promise<ConditionalTheme>}
7979
*/
@@ -88,7 +88,7 @@ async function createDefaultTheme(identifier, themeCache, contextDirectory) {
8888
/**
8989
* @param {string} identifier
9090
* @param {string} match
91-
* @param {any} themeCache
91+
* @param {ThemeCache} themeCache
9292
* @param {string=} contextDirectory
9393
* @returns {Promise<ConditionalTheme>}
9494
*/
@@ -103,7 +103,7 @@ async function createMatchMediaTheme(identifier, match, themeCache, contextDirec
103103
/**
104104
* @param {string} identifier
105105
* @param {string} parentSelector
106-
* @param {any} themeCache
106+
* @param {ThemeCache} themeCache
107107
* @param {string=} contextDirectory
108108
* @returns {Promise<ConditionalTheme>}
109109
*/

src/types.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,31 @@ interface PluginOptions {
6666
getLineTransformers?: (pluginOptions: PluginOptions, cache: GatsbyCache) => LineTransformer[];
6767
}
6868

69+
interface GrammarData {
70+
scopeName: string;
71+
languageId: number;
72+
path: string;
73+
tokenTypes: import('vscode-textmate').ITokenTypeMap | undefined;
74+
embeddedLanguages: Record<string, string> | undefined;
75+
injectTo: string[] | undefined;
76+
languageNames: string[];
77+
}
78+
79+
interface ThemeData {
80+
id: string;
81+
path: string;
82+
label: string;
83+
include: string | undefined;
84+
packageName: string;
85+
isOnlyThemeInPackage: boolean;
86+
}
87+
88+
type GrammarCache = Record<string, GrammarData>;
89+
type ThemeCache = Record<string, ThemeData>;
90+
6991
interface GatsbyCache {
92+
get(key: 'grammars'): Promise<GrammarCache>;
93+
get(key: 'themes'): Promise<ThemeCache>;
7094
get(key: string): Promise<any>;
7195
set(key: string, data: any): Promise<void>;
7296
}

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ function getMetadataForToken(token, binaryTokens) {
9393
}
9494

9595
/**
96-
* @param {*} cache
96+
* @param {GatsbyCache} cache
9797
* @param {string} key
98-
* @param {object} value
98+
* @param {Record<string, any>} value
9999
*/
100100
async function mergeCache(cache, key, value) {
101101
await cache.set(key, { ...(await cache.get(key)), ...value });

0 commit comments

Comments
 (0)