@atlaskit/eslint-plugin-ui-styling-standard
Version:
The eslint plugin to enforce and educate on Atlassian's UI Styling Standard
23 lines (22 loc) • 838 B
JavaScript
import cssSelectorParser from 'postcss-selector-parser';
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
const cssSelectorProcessor = cssSelectorParser();
const selectorCache = new WeakMap();
/**
* Parse each selector once per linted source file. Consumers treat the returned AST as read-only.
*/
export function parseSelector(context, selectorText) {
const sourceCode = getSourceCode(context);
let sourceFileCache = selectorCache.get(sourceCode);
if (!sourceFileCache) {
sourceFileCache = new Map();
selectorCache.set(sourceCode, sourceFileCache);
}
const cachedSelector = sourceFileCache.get(selectorText);
if (cachedSelector) {
return cachedSelector;
}
const selector = cssSelectorProcessor.astSync(selectorText);
sourceFileCache.set(selectorText, selector);
return selector;
}