From 897a67e57b043d524dc15aeeb498955546050640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Wed, 11 Dec 2024 21:45:02 +0100 Subject: [PATCH] test(selector-node-loc-fixing): added tests --- .../simple-css-input.svelte | 25 ++++++++ .../simple-postcss-input.svelte | 20 ++++++ .../simple-scss-input.svelte | 26 ++++++++ .../style-selector-location-coverter.ts | 61 +++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 tests/fixtures/parser/style-selector-location-converter/simple-css-input.svelte create mode 100644 tests/fixtures/parser/style-selector-location-converter/simple-postcss-input.svelte create mode 100644 tests/fixtures/parser/style-selector-location-converter/simple-scss-input.svelte create mode 100644 tests/src/parser/style-selector-location-coverter.ts diff --git a/tests/fixtures/parser/style-selector-location-converter/simple-css-input.svelte b/tests/fixtures/parser/style-selector-location-converter/simple-css-input.svelte new file mode 100644 index 00000000..15be4986 --- /dev/null +++ b/tests/fixtures/parser/style-selector-location-converter/simple-css-input.svelte @@ -0,0 +1,25 @@ + + +Hello! + +{a} + + diff --git a/tests/fixtures/parser/style-selector-location-converter/simple-postcss-input.svelte b/tests/fixtures/parser/style-selector-location-converter/simple-postcss-input.svelte new file mode 100644 index 00000000..68cfd1a3 --- /dev/null +++ b/tests/fixtures/parser/style-selector-location-converter/simple-postcss-input.svelte @@ -0,0 +1,20 @@ +
+
Hello
+ + World! +
+ + diff --git a/tests/fixtures/parser/style-selector-location-converter/simple-scss-input.svelte b/tests/fixtures/parser/style-selector-location-converter/simple-scss-input.svelte new file mode 100644 index 00000000..4f5df63b --- /dev/null +++ b/tests/fixtures/parser/style-selector-location-converter/simple-scss-input.svelte @@ -0,0 +1,26 @@ +
+
Hello
+ + World! +
+ + diff --git a/tests/src/parser/style-selector-location-coverter.ts b/tests/src/parser/style-selector-location-coverter.ts new file mode 100644 index 00000000..9d711c32 --- /dev/null +++ b/tests/src/parser/style-selector-location-coverter.ts @@ -0,0 +1,61 @@ +import assert from "assert"; +import fs from "fs"; +import path from "path"; +import type { Node } from "postcss"; + +import { parseForESLint } from "../../../src/index.js"; +import { generateParserOptions, listupFixtures } from "./test-utils.js"; +import type { SourceLocation } from "../../../src/ast/common.js"; + +const dirname = path.dirname(new URL(import.meta.url).pathname); +const STYLE_SELECTOR_CONTEXT_FIXTURE_ROOT = path.resolve( + dirname, + "../../fixtures/parser/style-selector-location-converter", +); + +function parse(code: string, filePath: string, config: any) { + return parseForESLint(code, generateParserOptions({ filePath }, config)); +} + +describe("Check for AST.", () => { + for (const { + input, + inputFileName, + outputFileName, + config, + meetRequirements, + } of listupFixtures(STYLE_SELECTOR_CONTEXT_FIXTURE_ROOT)) { + describe(inputFileName, () => { + let services: any; + + it("most to generate the expected style context.", () => { + services = parse(input, inputFileName, config).services; + if (!meetRequirements("test")) { + return; + } + const styleContext = services.getStyleContext(); + assert.strictEqual(styleContext.status, "success"); + const locations: [ + Partial, + [number | undefined, number | undefined], + ][] = [ + [ + services.styleSelectorNodeLoc(styleContext.sourceAst), + services.styleSelectorNodeRange(styleContext.sourceAst), + ], + ]; + styleContext.sourceAst.walk((node: Node) => { + locations.push([ + services.styleSelectorNodeLoc(node), + services.styleSelectorNodeRange(node), + ]); + }); + const output = fs.readFileSync(outputFileName, "utf8"); + assert.strictEqual( + `${JSON.stringify(locations, undefined, 2)}\n`, + output, + ); + }); + }); + } +});