-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstyle-context.ts
146 lines (136 loc) · 4.01 KB
/
style-context.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import type { Node, Parser, Root } from "postcss";
import postcss from "postcss";
import { parse as SCSSparse } from "postcss-scss";
import type { Context } from "../context";
import type { SourceLocation, SvelteStyleElement } from "../ast";
export type StyleContext =
| StyleContextNoStyleElement
| StyleContextParseError
| StyleContextSuccess
| StyleContextUnknownLang;
export interface StyleContextNoStyleElement {
status: "no-style-element";
}
export interface StyleContextParseError {
status: "parse-error";
sourceLang: string;
error: any;
}
export interface StyleContextSuccess {
status: "success";
sourceLang: string;
sourceAst: Root;
}
export interface StyleContextUnknownLang {
status: "unknown-lang";
sourceLang: string;
}
/**
* Extracts style source from a SvelteStyleElement and parses it into a PostCSS AST.
*/
export function parseStyleContext(
styleElement: SvelteStyleElement | undefined,
ctx: Context,
): StyleContext {
if (!styleElement || !styleElement.endTag) {
return { status: "no-style-element" };
}
let sourceLang = "css";
for (const attribute of styleElement.startTag.attributes) {
if (
attribute.type === "SvelteAttribute" &&
attribute.key.name === "lang" &&
attribute.value.length > 0 &&
attribute.value[0].type === "SvelteLiteral"
) {
sourceLang = attribute.value[0].value;
}
}
let parseFn: Parser<Root>, sourceAst: Root;
switch (sourceLang) {
case "css":
case "postcss":
parseFn = postcss.parse;
break;
case "scss":
parseFn = SCSSparse;
break;
default:
return { status: "unknown-lang", sourceLang };
}
const styleCode = ctx.code.slice(
styleElement.startTag.range[1],
styleElement.endTag.range[0],
);
try {
sourceAst = parseFn(styleCode, {
from: ctx.parserOptions.filePath,
});
} catch (error) {
return { status: "parse-error", sourceLang, error };
}
fixPostCSSNodeLocation(sourceAst, styleElement);
sourceAst.walk((node) => fixPostCSSNodeLocation(node, styleElement));
return { status: "success", sourceLang, sourceAst };
}
/**
* Extracts a node location (like that of any ESLint node) from a parsed svelte style node.
*/
export function styleNodeLoc(node: Node): Partial<SourceLocation> {
if (node.source === undefined) {
return {};
}
return {
start:
node.source.start !== undefined
? {
line: node.source.start.line,
column: node.source.start.column - 1,
}
: undefined,
end:
node.source.end !== undefined
? {
line: node.source.end.line,
column: node.source.end.column,
}
: undefined,
};
}
/**
* Extracts a node range (like that of any ESLint node) from a parsed svelte style node.
*/
export function styleNodeRange(
node: Node,
): [number | undefined, number | undefined] {
if (node.source === undefined) {
return [undefined, undefined];
}
return [
node.source.start !== undefined ? node.source.start.offset : undefined,
node.source.end !== undefined ? node.source.end.offset + 1 : undefined,
];
}
/**
* Fixes PostCSS AST locations to be relative to the whole file instead of relative to the <style> element.
*/
function fixPostCSSNodeLocation(node: Node, styleElement: SvelteStyleElement) {
if (node.source?.start?.offset !== undefined) {
node.source.start.offset += styleElement.startTag.range[1];
}
if (node.source?.start?.line !== undefined) {
node.source.start.line += styleElement.loc.start.line - 1;
}
if (node.source?.end?.offset !== undefined) {
node.source.end.offset += styleElement.startTag.range[1];
}
if (node.source?.end?.line !== undefined) {
node.source.end.line += styleElement.loc.start.line - 1;
}
if (node.source?.start?.line === styleElement.loc.start.line) {
node.source.start.column += styleElement.startTag.loc.end.column;
}
if (node.source?.end?.line === styleElement.loc.start.line) {
node.source.end.column += styleElement.startTag.loc.end.column;
}
}