deno.land / x / deno_temme@v1.0.0 / temme / check.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import type { Qualifier, Section, TemmeSelector } from '../parser/mod.ts'
export const msg = { invalidFilter(name: string) { return `${name} is not a valid filter.` }, invalidModifier(name: string) { return `${name} is not a valid modifier.` }, invalidProcedure(name: string) { return `${name} is not a valid procedure.` }, hasLeadingAttributeCapture() { return 'Attribute capturing is only allowed in the last css section. Capture in leading css-selectors will be omitted.' }, parentRefSelectorAtTopLevel() { return `Parent-reference must not be at top level.` }, snippetAlreadyDefined(name: string) { return `Snippet \`${name}\` is already defined.` }, snippetDefineNotAtTopLevel(name: string) { return `The definition of snippet \`${name}\` must be at top level.` }, filterDefineNotAtTopLevel(name: string) { return `The definition of inline filter \`${name}\` must be at top level.` }, filterAlreadyDefined(name: string) { return `Filter \`${name}\` is already defined.` }, modifierAlreadyDefined(name: string) { return `Modifier ${name} is already defined.` }, procedureAlreadyDefined(name: string) { return `Procedure ${name} is already defined.` }, snippetNotDefined(name: string) { return `Snippet \`${name}\` is not defined.` }, valueCaptureWithOtherOperator() { return 'Value capture in attribute qualifier only works with `=` operator.' }, circularSnippetExpansion(loop: string[]) { return `Circular snippet expansion detected: ${loop.join(' -> ')}` }, arrayFilterAppliedToNonArrayValue(filterName: string) { return `Array-filter \`${filterName}\` can only be applied to an array.` },}
function isCaptureQualifier(qualifier: Qualifier) { return ( qualifier.type === 'attribute-qualifier' && qualifier.value && typeof qualifier.value === 'object' )}
function containsAnyCapture(sections: Section[]) { return sections.some(section => section.qualifiers.some(isCaptureQualifier))}
export function checkRootSelector(selector: TemmeSelector) { commonCheck(selector) if (selector.type === 'parent-ref-selector') { throw new Error(msg.parentRefSelectorAtTopLevel()) } else if (selector.type === 'normal-selector') { for (const child of selector.children) { checkChild(child) } } else if (selector.type === 'snippet-define') { for (const child of selector.selectors) { checkChild(child) } }}
function commonCheck(selector: TemmeSelector) { if (selector.type === 'normal-selector') { const sectionCount = selector.sections.length const leadingSections = selector.sections.slice(0, sectionCount - 1) const hasLeadingCapture = containsAnyCapture(leadingSections) if (hasLeadingCapture) { throw new Error(msg.hasLeadingAttributeCapture()) } }}
export function checkChild(selector: TemmeSelector) { commonCheck(selector) if (selector.type === 'snippet-define') { throw new Error(msg.snippetDefineNotAtTopLevel(selector.name)) } else if (selector.type === 'filter-define') { throw new Error(msg.filterDefineNotAtTopLevel(selector.name)) } else if (selector.type === 'normal-selector') { for (const child of selector.children) { checkChild(child) } }}
deno_temme

Version Info

Tagged at
a year ago