Track source locations in the AST

These are stored as character offsets for the original input (`src`) as well as the printed output (`dst`)
This commit is contained in:
Jordan Pittman 2025-04-16 17:47:20 -04:00
parent 8319bd83b3
commit f08f89d7ba
2 changed files with 46 additions and 0 deletions

View file

@ -1,6 +1,7 @@
import { Polyfills } from '.'
import { parseAtRule } from './css-parser'
import type { DesignSystem } from './design-system'
import type { Source, SourceLocation } from './source-maps/source'
import { Theme, ThemeOptions } from './theme'
import { DefaultMap } from './utils/default-map'
import { extractUsedVariables } from './utils/variables'
@ -12,6 +13,9 @@ export type StyleRule = {
kind: 'rule'
selector: string
nodes: AstNode[]
src?: SourceLocation
dst?: SourceLocation
}
export type AtRule = {
@ -19,6 +23,9 @@ export type AtRule = {
name: string
params: string
nodes: AstNode[]
src?: SourceLocation
dst?: SourceLocation
}
export type Declaration = {
@ -26,22 +33,34 @@ export type Declaration = {
property: string
value: string | undefined
important: boolean
src?: SourceLocation
dst?: SourceLocation
}
export type Comment = {
kind: 'comment'
value: string
src?: SourceLocation
dst?: SourceLocation
}
export type Context = {
kind: 'context'
context: Record<string, string | boolean>
nodes: AstNode[]
src?: undefined
dst?: undefined
}
export type AtRoot = {
kind: 'at-root'
nodes: AstNode[]
src?: undefined
dst?: undefined
}
export type Rule = StyleRule | AtRule

View file

@ -0,0 +1,27 @@
/**
* The source code for one or more nodes in the AST
*
* This generally corresponds to a stylesheet
*/
export interface Source {
/**
* The path to the file that contains the referenced source code
*
* If this references the *output* source code, this is `null`.
*/
file: string | null
/**
* The referenced source code
*/
code: string
}
/**
* The file and offsets within it that this node covers
*
* This can represent either:
* - A location in the original CSS which caused this node to be created
* - A location in the output CSS where this node resides
*/
export type SourceLocation = [source: Source, start: number, end: number]