Skip to content

Commit

Permalink
refactor: extract validation function
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros committed Sep 19, 2023
1 parent f29c5c7 commit e497e90
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions packages/core/src/helpers/custom-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import postcssValueParser, {
type FunctionNode,
} from 'postcss-value-parser';
import cssesc from 'cssesc';
import type { PseudoClass, SelectorNode } from '@tokey/css-selector-parser';
import type { PseudoClass, SelectorList, SelectorNode } from '@tokey/css-selector-parser';
import { createDiagnosticReporter, Diagnostics } from '../diagnostics';
import {
parseSelectorWithCache,
Expand Down Expand Up @@ -1075,30 +1075,57 @@ function transformMappedStateWithParam({
selectorNode?: postcss.Node;
diagnostics: Diagnostics;
}) {
const targetSelectorStr = template.replace(/\$0/g, param);
const selectorAst = parseSelectorWithCache(targetSelectorStr, { clone: true });
const selectorStr = template.replace(/\$0/g, param);
const selectorAst = parseSelectorWithCache(selectorStr, { clone: true });
if (
!validateTemplateSelector({
stateName,
selectorStr,
selectorAst,
cssNode: selectorNode,
diagnostics,
})
) {
return;
}
convertToSelector(node).nodes = selectorAst[0].nodes;
}

function validateTemplateSelector({
stateName,
selectorStr,
selectorAst,
cssNode,
diagnostics,
}: {
stateName: string;
selectorStr: string;
selectorAst: SelectorList;
cssNode?: postcss.Node;
diagnostics: Diagnostics;
}): boolean {
if (selectorAst.length > 1) {
if (selectorNode) {
if (cssNode) {
diagnostics.report(
stateDiagnostics.UNSUPPORTED_MULTI_SELECTOR(stateName, targetSelectorStr),
stateDiagnostics.UNSUPPORTED_MULTI_SELECTOR(stateName, selectorStr),
{
node: selectorNode,
node: cssNode,
}
);
}
return;
return false;
} else {
const firstSelector = selectorAst[0].nodes.find(({ type }) => type !== 'comment');
if (firstSelector?.type === 'type' || firstSelector?.type === 'universal') {
if (selectorNode) {
if (cssNode) {
diagnostics.report(
stateDiagnostics.UNSUPPORTED_INITIAL_SELECTOR(stateName, targetSelectorStr),
stateDiagnostics.UNSUPPORTED_INITIAL_SELECTOR(stateName, selectorStr),
{
node: selectorNode,
node: cssNode,
}
);
}
return;
return false;
}
let unexpectedSelector: undefined | SelectorNode = undefined;
for (const node of selectorAst[0].nodes) {
Expand All @@ -1108,33 +1135,30 @@ function transformMappedStateWithParam({
}
}
if (unexpectedSelector) {
if (selectorNode) {
if (cssNode) {
switch (unexpectedSelector.type) {
case 'combinator':
diagnostics.report(
stateDiagnostics.UNSUPPORTED_COMPLEX_SELECTOR(
stateName,
targetSelectorStr
),
stateDiagnostics.UNSUPPORTED_COMPLEX_SELECTOR(stateName, selectorStr),
{
node: selectorNode,
node: cssNode,
}
);
break;
case 'invalid':
diagnostics.report(
stateDiagnostics.INVALID_SELECTOR(stateName, targetSelectorStr),
stateDiagnostics.INVALID_SELECTOR(stateName, selectorStr),
{
node: selectorNode,
node: cssNode,
}
);
break;
}
}
return;
return false;
}
}
convertToSelector(node).nodes = selectorAst[0].nodes;
return true;
}

function resolveParam(
Expand Down

0 comments on commit e497e90

Please sign in to comment.