@atlaskit/eslint-plugin-ui-styling-standard
Version:
The eslint plugin to enforce and educate on Atlassian's UI Styling Standard
29 lines (26 loc) • 1.02 kB
JavaScript
import cssSelectorParser from 'postcss-selector-parser';
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
const cssSelectorProcessor = cssSelectorParser();
/**
* Structural stand-in for postcss-selector-parser's Root AST. Avoid returning the
* library's Selector/Container types — under TSRS they expand into an infinite cycle.
*/
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;
}