Skip to content

Commit 27d039c

Browse files
committed
Update svelte and refine typing
1 parent c0a5c45 commit 27d039c

File tree

7 files changed

+86
-69
lines changed

7 files changed

+86
-69
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const markup = async ({ content, filename }: PreprocessorOptions): Promise<Prepr
6969
};
7070

7171
// eslint-disable-next-line no-multi-assign
72-
export default exports = module.exports = (options: Partial<PluginOptions>) => {
72+
export default module.exports = (options: Partial<PluginOptions>) => {
7373
pluginOptions = {
7474
...pluginOptions,
7575
...options,

src/parsers/importDeclaration.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
/* eslint-disable no-param-reassign */
2-
// @ts-expect-error walk is not in d.ts
3-
import { parse, walk } from 'svelte/compiler';
42
import path from 'path';
53
import fs, { constants } from 'fs';
64
import MagicString from 'magic-string';
5+
import { parse, walk } from 'svelte/compiler';
76
import type { TemplateNode } from 'svelte/types/compiler/interfaces.d';
8-
import Processor from '../processors/processor';
7+
import type Processor from '../processors/processor';
98

109
/**
1110
* Parse CssModules Imports
1211
*/
1312
export default (processor: Processor): void => {
13+
const ast = (processor.ast as unknown) as TemplateNode;
1414
const backup = {
1515
ast: processor.ast,
1616
magicContent: processor.magicContent,
1717
};
1818

1919
let importedContent = '';
2020

21-
walk(processor.ast, {
22-
enter(node: TemplateNode) {
21+
walk(ast, {
22+
enter(baseNode) {
23+
const node = baseNode as TemplateNode;
2324
if (node.type === 'Style' || node.type === 'Fragment') {
2425
this.skip();
2526
}
@@ -76,7 +77,7 @@ export default (processor: Processor): void => {
7677
} else {
7778
importedContent += content;
7879
}
79-
} catch (err) {
80+
} catch (err: any) {
8081
fs.access(nodeModulesPath, constants.F_OK, (error) => {
8182
if (error) {
8283
throw new Error(err); // not found in node_modules packages either, throw orignal error

src/parsers/template.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// @ts-expect-error walk is not in d.ts
21
import { walk } from 'svelte/compiler';
32
import type { TemplateNode } from 'svelte/types/compiler/interfaces.d';
4-
import Processor from '../processors/processor';
3+
import type Processor from '../processors/processor';
54

65
/**
76
* Update a string of multiple Classes
@@ -45,8 +44,9 @@ export default (processor: Processor): void => {
4544
const directiveLength: number = 'class:'.length;
4645
const allowedAttributes = ['class', ...processor.options.allowedAttributes];
4746

48-
walk(processor.ast, {
49-
enter(node: TemplateNode) {
47+
walk(processor.ast.html, {
48+
enter(baseNode) {
49+
const node = baseNode as TemplateNode;
5050
if (node.type === 'Script' || node.type === 'Style') {
5151
this.skip();
5252
}

src/processors/mixed.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-expect-error walk is not in d.ts
21
import { walk } from 'svelte/compiler';
32
import type { Ast, TemplateNode } from 'svelte/types/compiler/interfaces.d';
43
import type { PluginOptions } from '../types';
@@ -32,44 +31,55 @@ const updateSelectorBoundaries = (
3231
* @param processor The CSS Module Processor
3332
*/
3433
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;
3738
if (node.type === 'Script' || node.type === 'Fragment') {
3839
this.skip();
3940
}
4041

4142
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+
: [];
4346
if (classSelectors.length > 0) {
4447
let selectorBoundaries: Array<Boundaries> = [];
4548
let start = 0;
4649
let end = 0;
4750

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);
6155
start = 0;
6256
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+
}
6375
}
64-
if (hasPushed === false && index === node.children.length - 1) {
65-
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
66-
}
67-
}
68-
});
76+
});
77+
}
6978

7079
selectorBoundaries.forEach((boundary) => {
7180
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
7383
);
7484
if (hasClassSelector.length > 0) {
7585
processor.magicContent.appendLeft(boundary.start, ':global(');

src/processors/native.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-expect-error walk is not in d.ts
21
import { walk } from 'svelte/compiler';
32
import type { Ast, TemplateNode } from 'svelte/types/compiler/interfaces.d';
43
import type { PluginOptions } from '../types';
@@ -33,40 +32,44 @@ const updateSelectorBoundaries = (
3332
* @param processor The CSS Module Processor
3433
*/
3534
const parser = (processor: Processor): void => {
35+
const ast = (processor.ast as unknown) as TemplateNode;
3636
let selectorBoundaries: Array<Boundaries> = [];
3737

38-
walk(processor.ast, {
39-
enter(node: TemplateNode) {
38+
walk(ast, {
39+
enter(baseNode) {
40+
const node = baseNode as TemplateNode;
4041
if (node.type === 'Script' || node.type === 'Fragment') {
4142
this.skip();
4243
}
4344
if (node.type === 'Selector') {
4445
let start = 0;
4546
let end = 0;
4647

47-
node.children.forEach((item, index) => {
48-
let hasPushed = false;
49-
if (
50-
(item.name === 'global' || item.name === 'local') &&
51-
item.type === 'PseudoClassSelector'
52-
) {
53-
if (start > 0 && end > 0) {
54-
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
55-
hasPushed = true;
56-
}
57-
start = item.end + 1;
58-
end = 0;
59-
} else if (item.start && item.end) {
60-
if (start === 0) {
61-
start = item.start;
48+
if (node.children) {
49+
node.children.forEach((item, index) => {
50+
let hasPushed = false;
51+
if (
52+
(item.name === 'global' || item.name === 'local') &&
53+
item.type === 'PseudoClassSelector'
54+
) {
55+
if (start > 0 && end > 0) {
56+
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
57+
hasPushed = true;
58+
}
59+
start = item.end + 1;
60+
end = 0;
61+
} else if (item.start && item.end) {
62+
if (start === 0) {
63+
start = item.start;
64+
}
65+
end = item.end;
6266
}
63-
end = item.end;
64-
}
6567

66-
if (!hasPushed && index === node.children.length - 1 && end > 0) {
67-
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
68-
}
69-
});
68+
if (!hasPushed && node.children && index === node.children.length - 1 && end > 0) {
69+
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
70+
}
71+
});
72+
}
7073

7174
processor.parsePseudoLocalSelectors(node);
7275
}

src/processors/processor.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default class Processor {
1616
ast: Style;
1717
openTag: string;
1818
closeTag: string;
19-
} = { ast: null, openTag: '<style module>', closeTag: '</style>' };
19+
};
2020

2121
public magicContent: MagicString;
2222

@@ -37,10 +37,11 @@ export default class Processor {
3737
this.magicContent = new MagicString(content);
3838
this.styleParser = parser.bind(this);
3939

40-
if (ast.css) {
41-
this.style.ast = ast.css;
42-
this.style.openTag = content.substring(this.style.ast.start, this.style.ast.content.start);
43-
}
40+
this.style = {
41+
ast: ast.css,
42+
openTag: ast.css ? content.substring(ast.css.start, ast.css.content.start) : '<style module>',
43+
closeTag: '</style>',
44+
};
4445
}
4546

4647
/**
@@ -77,9 +78,10 @@ export default class Processor {
7778
* @param node The ast "Selector" node to parse
7879
*/
7980
public parsePseudoLocalSelectors = (node: TemplateNode): void => {
80-
const pseudoLocalSelectors = node.children.filter(
81-
(item) => item.type === 'PseudoClassSelector' && item.name === 'local'
82-
);
81+
const pseudoLocalSelectors =
82+
node.children?.filter(
83+
(item) => item.type === 'PseudoClassSelector' && item.name === 'local'
84+
) ?? [];
8385

8486
if (pseudoLocalSelectors.length > 0) {
8587
pseudoLocalSelectors.forEach((item) => {

src/processors/scoped.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-expect-error walk is not in d.ts
21
import { walk } from 'svelte/compiler';
32
import type { Ast, TemplateNode } from 'svelte/types/compiler/interfaces.d';
43
import type { PluginOptions } from '../types';
@@ -9,8 +8,10 @@ import Processor from './processor';
98
* @param processor The CSS Module Processor
109
*/
1110
const parser = (processor: Processor): void => {
12-
walk(processor.ast, {
13-
enter(node: TemplateNode) {
11+
const ast = (processor.ast as unknown) as TemplateNode;
12+
walk(ast, {
13+
enter(baseNode) {
14+
const node = baseNode as TemplateNode;
1415
if (node.type === 'Script' || node.type === 'Fragment') {
1516
this.skip();
1617
}

0 commit comments

Comments
 (0)