From 385e9ec3454b44a36ab74bdfebda2659e28d3db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Fri, 13 Dec 2024 18:07:24 +0100 Subject: [PATCH] feat(selector-parsing): added helper function to convert style selector locations to eslint locations --- src/parser/index.ts | 2 ++ src/parser/style-context.ts | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/parser/index.ts b/src/parser/index.ts index 01f33526..084b9002 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -39,6 +39,7 @@ import { type StyleContextUnknownLang, styleNodeLoc, styleNodeRange, + styleSelectorNodeLoc, } from "./style-context.js"; import { getGlobalsForSvelte, getGlobalsForSvelteScript } from "./globals.js"; import type { NormalizedParserOptions } from "./parser-options.js"; @@ -251,6 +252,7 @@ function parseAsSvelte( }, styleNodeLoc, styleNodeRange, + styleSelectorNodeLoc, svelteParseContext, }); resultScript.visitorKeys = Object.assign({}, KEYS, resultScript.visitorKeys); diff --git a/src/parser/style-context.ts b/src/parser/style-context.ts index a4f0ca90..fa161c03 100644 --- a/src/parser/style-context.ts +++ b/src/parser/style-context.ts @@ -141,6 +141,24 @@ export function styleNodeRange( ]; } +/** + * Extracts a node location (like that of any ESLint node) from a parsed svelte selector node. + */ +export function styleSelectorNodeLoc( + node: SelectorNode, +): Partial { + return { + start: + node.source?.start !== undefined + ? { + line: node.source.start.line, + column: node.source.start.column - 1, + } + : undefined, + end: node.source?.end, + }; +} + /** * Fixes PostCSS AST locations to be relative to the whole file instead of relative to the