|
| 1 | +import type { Node, Parser, Root } from "postcss"; |
| 2 | +import postcss from "postcss"; |
| 3 | +import { parse as SCSSparse } from "postcss-scss"; |
| 4 | + |
| 5 | +import type { Context } from "../context"; |
| 6 | +import type { SourceLocation, SvelteStyleElement } from "../ast"; |
| 7 | + |
| 8 | +export type StyleContext = |
| 9 | + | StyleContextNoStyleElement |
| 10 | + | StyleContextParseError |
| 11 | + | StyleContextSuccess |
| 12 | + | StyleContextUnknownLang; |
| 13 | + |
| 14 | +export interface StyleContextNoStyleElement { |
| 15 | + status: "no-style-element"; |
| 16 | +} |
| 17 | + |
| 18 | +export interface StyleContextParseError { |
| 19 | + status: "parse-error"; |
| 20 | + sourceLang: string; |
| 21 | + error: any; |
| 22 | +} |
| 23 | + |
| 24 | +export interface StyleContextSuccess { |
| 25 | + status: "success"; |
| 26 | + sourceLang: string; |
| 27 | + sourceAst: Root; |
| 28 | +} |
| 29 | + |
| 30 | +export interface StyleContextUnknownLang { |
| 31 | + status: "unknown-lang"; |
| 32 | + sourceLang: string; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Extracts style source from a SvelteStyleElement and parses it into a PostCSS AST. |
| 37 | + */ |
| 38 | +export function parseStyleContext( |
| 39 | + styleElement: SvelteStyleElement | undefined, |
| 40 | + ctx: Context |
| 41 | +): StyleContext { |
| 42 | + if (!styleElement || !styleElement.endTag) { |
| 43 | + return { status: "no-style-element" }; |
| 44 | + } |
| 45 | + let sourceLang = "css"; |
| 46 | + for (const attribute of styleElement.startTag.attributes) { |
| 47 | + if ( |
| 48 | + attribute.type === "SvelteAttribute" && |
| 49 | + attribute.key.name === "lang" && |
| 50 | + attribute.value.length > 0 && |
| 51 | + attribute.value[0].type === "SvelteLiteral" |
| 52 | + ) { |
| 53 | + sourceLang = attribute.value[0].value; |
| 54 | + } |
| 55 | + } |
| 56 | + let parseFn: Parser<Root>, sourceAst: Root; |
| 57 | + switch (sourceLang) { |
| 58 | + case "css": |
| 59 | + parseFn = postcss.parse; |
| 60 | + break; |
| 61 | + case "scss": |
| 62 | + parseFn = SCSSparse; |
| 63 | + break; |
| 64 | + default: |
| 65 | + return { status: "unknown-lang", sourceLang }; |
| 66 | + } |
| 67 | + const styleCode = ctx.code.slice( |
| 68 | + styleElement.startTag.range[1], |
| 69 | + styleElement.endTag.range[0] |
| 70 | + ); |
| 71 | + try { |
| 72 | + sourceAst = parseFn(styleCode, { |
| 73 | + from: ctx.parserOptions.filePath, |
| 74 | + }); |
| 75 | + } catch (error) { |
| 76 | + return { status: "parse-error", sourceLang, error }; |
| 77 | + } |
| 78 | + fixPostCSSNodeLocation(sourceAst, styleElement); |
| 79 | + sourceAst.walk((node) => fixPostCSSNodeLocation(node, styleElement)); |
| 80 | + return { status: "success", sourceLang, sourceAst }; |
| 81 | +} |
| 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 : undefined, |
| 119 | + node.source.end !== undefined ? node.source.end.offset + 1 : undefined, |
| 120 | + ]; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Fixes PostCSS AST locations to be relative to the whole file instead of relative to the <style> element. |
| 125 | + */ |
| 126 | +function fixPostCSSNodeLocation(node: Node, styleElement: SvelteStyleElement) { |
| 127 | + if (node.source?.start?.offset !== undefined) { |
| 128 | + node.source.start.offset += styleElement.startTag.range[1]; |
| 129 | + } |
| 130 | + if (node.source?.start?.line !== undefined) { |
| 131 | + node.source.start.line += styleElement.loc.start.line - 1; |
| 132 | + } |
| 133 | + if (node.source?.end?.offset !== undefined) { |
| 134 | + node.source.end.offset += styleElement.startTag.range[1]; |
| 135 | + } |
| 136 | + if (node.source?.end?.line !== undefined) { |
| 137 | + node.source.end.line += styleElement.loc.start.line - 1; |
| 138 | + } |
| 139 | + if (node.source?.start?.line === styleElement.loc.start.line) { |
| 140 | + node.source.start.column += styleElement.startTag.loc.end.column; |
| 141 | + } |
| 142 | + if (node.source?.end?.line === styleElement.loc.start.line) { |
| 143 | + node.source.end.column += styleElement.startTag.loc.end.column; |
| 144 | + } |
| 145 | +} |
0 commit comments