|
1 | | -// @ts-expect-error walk is not in d.ts |
2 | 1 | import { walk } from 'svelte/compiler'; |
3 | 2 | import type { Ast, TemplateNode } from 'svelte/types/compiler/interfaces.d'; |
4 | 3 | import type { PluginOptions } from '../types'; |
@@ -32,44 +31,55 @@ const updateSelectorBoundaries = ( |
32 | 31 | * @param processor The CSS Module Processor |
33 | 32 | */ |
34 | 33 | const parser = (processor: Processor): void => { |
35 | | - walk(processor.ast, { |
36 | | - enter(node: TemplateNode) { |
| 34 | + const ast = (processor.ast as unknown) as TemplateNode; |
| 35 | + walk(ast, { |
| 36 | + enter(baseNode) { |
| 37 | + const node = baseNode as TemplateNode; |
37 | 38 | if (node.type === 'Script' || node.type === 'Fragment') { |
38 | 39 | this.skip(); |
39 | 40 | } |
40 | 41 |
|
41 | 42 | if (node.type === 'Selector') { |
42 | | - const classSelectors = node.children.filter((item) => item.type === 'ClassSelector'); |
| 43 | + const classSelectors = node.children |
| 44 | + ? node.children.filter((item: { type: string }) => item.type === 'ClassSelector') |
| 45 | + : []; |
43 | 46 | if (classSelectors.length > 0) { |
44 | 47 | let selectorBoundaries: Array<Boundaries> = []; |
45 | 48 | let start = 0; |
46 | 49 | let end = 0; |
47 | 50 |
|
48 | | - node.children.forEach((item, index) => { |
49 | | - if (!item.start && start > 0) { |
50 | | - selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end); |
51 | | - start = 0; |
52 | | - end = 0; |
53 | | - } else { |
54 | | - let hasPushed = false; |
55 | | - if (end !== item.start) { |
56 | | - start = item.start; |
57 | | - end = item.end; |
58 | | - } else { |
59 | | - selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, item.end); |
60 | | - hasPushed = true; |
| 51 | + if (node.children) { |
| 52 | + node.children.forEach((item: { start: number; end: number }, index: number) => { |
| 53 | + if (!item.start && start > 0) { |
| 54 | + selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end); |
61 | 55 | start = 0; |
62 | 56 | end = 0; |
| 57 | + } else { |
| 58 | + let hasPushed = false; |
| 59 | + if (end !== item.start) { |
| 60 | + start = item.start; |
| 61 | + end = item.end; |
| 62 | + } else { |
| 63 | + selectorBoundaries = updateSelectorBoundaries( |
| 64 | + selectorBoundaries, |
| 65 | + start, |
| 66 | + item.end |
| 67 | + ); |
| 68 | + hasPushed = true; |
| 69 | + start = 0; |
| 70 | + end = 0; |
| 71 | + } |
| 72 | + if (hasPushed === false && node.children && index === node.children.length - 1) { |
| 73 | + selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end); |
| 74 | + } |
63 | 75 | } |
64 | | - if (hasPushed === false && index === node.children.length - 1) { |
65 | | - selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end); |
66 | | - } |
67 | | - } |
68 | | - }); |
| 76 | + }); |
| 77 | + } |
69 | 78 |
|
70 | 79 | selectorBoundaries.forEach((boundary) => { |
71 | 80 | const hasClassSelector = classSelectors.filter( |
72 | | - (item) => boundary.start <= item.start && boundary.end >= item.end |
| 81 | + (item: { start: number; end: number }) => |
| 82 | + boundary.start <= item.start && boundary.end >= item.end |
73 | 83 | ); |
74 | 84 | if (hasClassSelector.length > 0) { |
75 | 85 | processor.magicContent.appendLeft(boundary.start, ':global('); |
|
0 commit comments