Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/stylelint/validateVariableNames.ts
5239 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { readFileSync } from 'fs';
7
import path from 'path';
8
9
const RE_VAR_PROP = /var\(\s*(--([\w\-\.]+))/g;
10
11
let knownVariables: Set<string> | undefined;
12
function getKnownVariableNames() {
13
if (!knownVariables) {
14
const knownVariablesFileContent = readFileSync(path.join(import.meta.dirname, './vscode-known-variables.json'), 'utf8').toString();
15
const knownVariablesInfo = JSON.parse(knownVariablesFileContent);
16
knownVariables = new Set([...knownVariablesInfo.colors, ...knownVariablesInfo.others, ...(knownVariablesInfo.sizes || [])] as string[]);
17
}
18
return knownVariables;
19
}
20
21
const iconVariable = /^--vscode-icon-.+-(content|font-family)$/;
22
23
export interface IValidator {
24
(value: string, report: (message: string) => void): void;
25
}
26
27
export function getVariableNameValidator(): IValidator {
28
const allVariables = getKnownVariableNames();
29
return (value: string, report: (unknwnVariable: string) => void) => {
30
RE_VAR_PROP.lastIndex = 0; // reset lastIndex just to be sure
31
let match;
32
while (match = RE_VAR_PROP.exec(value)) {
33
const variableName = match[1];
34
if (variableName && !allVariables.has(variableName) && !iconVariable.test(variableName)) {
35
report(variableName);
36
}
37
}
38
};
39
}
40
41
42