Path: blob/main/build/lib/stylelint/validateVariableNames.ts
5239 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { readFileSync } from 'fs';6import path from 'path';78const RE_VAR_PROP = /var\(\s*(--([\w\-\.]+))/g;910let knownVariables: Set<string> | undefined;11function getKnownVariableNames() {12if (!knownVariables) {13const knownVariablesFileContent = readFileSync(path.join(import.meta.dirname, './vscode-known-variables.json'), 'utf8').toString();14const knownVariablesInfo = JSON.parse(knownVariablesFileContent);15knownVariables = new Set([...knownVariablesInfo.colors, ...knownVariablesInfo.others, ...(knownVariablesInfo.sizes || [])] as string[]);16}17return knownVariables;18}1920const iconVariable = /^--vscode-icon-.+-(content|font-family)$/;2122export interface IValidator {23(value: string, report: (message: string) => void): void;24}2526export function getVariableNameValidator(): IValidator {27const allVariables = getKnownVariableNames();28return (value: string, report: (unknwnVariable: string) => void) => {29RE_VAR_PROP.lastIndex = 0; // reset lastIndex just to be sure30let match;31while (match = RE_VAR_PROP.exec(value)) {32const variableName = match[1];33if (variableName && !allVariables.has(variableName) && !iconVariable.test(variableName)) {34report(variableName);35}36}37};38}39404142