@@ -3,7 +3,7 @@ import postcss from "postcss";
3
3
import { parse as SCSSparse } from "postcss-scss" ;
4
4
5
5
import type { Context } from "../context" ;
6
- import type { SvelteStyleElement } from "../ast" ;
6
+ import type { SourceLocation , SvelteStyleElement } from "../ast" ;
7
7
8
8
export type StyleContext =
9
9
| StyleContextNoStyleElement
@@ -80,6 +80,46 @@ export function parseStyleContext(
80
80
return { status : "success" , sourceLang, sourceAst } ;
81
81
}
82
82
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
+
83
123
/**
84
124
* Fixes PostCSS AST locations to be relative to the whole file instead of relative to the <style> element.
85
125
*/
0 commit comments