Skip to content

Commit af4b391

Browse files
committed
Added a converter functions for ESTree-style range and location for PostCSS nodes
1 parent ac6a03c commit af4b391

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/parser/style-context.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import postcss from "postcss";
33
import { parse as SCSSparse } from "postcss-scss";
44

55
import type { Context } from "../context";
6-
import type { SvelteStyleElement } from "../ast";
6+
import type { SourceLocation, SvelteStyleElement } from "../ast";
77

88
export type StyleContext =
99
| StyleContextNoStyleElement
@@ -80,6 +80,46 @@ export function parseStyleContext(
8080
return { status: "success", sourceLang, sourceAst };
8181
}
8282

83+
/**
84+
* Extracts a node location (like that of any ESLint node) from a parsed svelte style node.
85+
*/
86+
export function styleNodeLoc(node: Node): Partial<SourceLocation> {
87+
if (node.source === undefined) {
88+
return {};
89+
}
90+
return {
91+
start:
92+
node.source.start !== undefined
93+
? {
94+
line: node.source.start.line,
95+
column: node.source.start.column - 1,
96+
}
97+
: undefined,
98+
end:
99+
node.source.end !== undefined
100+
? {
101+
line: node.source.end.line,
102+
column: node.source.end.column,
103+
}
104+
: undefined,
105+
};
106+
}
107+
108+
/**
109+
* Extracts a node range (like that of any ESLint node) from a parsed svelte style node.
110+
*/
111+
export function styleNodeRange(
112+
node: Node
113+
): [number | undefined, number | undefined] {
114+
if (node.source === undefined) {
115+
return [undefined, undefined];
116+
}
117+
return [
118+
node.source.start !== undefined ? node.source.start.offset - 2 : undefined,
119+
node.source.end !== undefined ? node.source.end.offset - 1 : undefined,
120+
];
121+
}
122+
83123
/**
84124
* Fixes PostCSS AST locations to be relative to the whole file instead of relative to the <style> element.
85125
*/

0 commit comments

Comments
 (0)