From 9374dd09d77799bce1b1869d8e318d09b2e10662 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-converter-utils.ts | 28 +++++++++++ .../style-selector-location-converter.ts | 49 +++++++++++++++++++ tools/update-fixtures.ts | 28 +++++++++++ 6 files changed, 176 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-converter-utils.ts create mode 100644 tests/src/parser/style-selector-location-converter.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-converter-utils.ts b/tests/src/parser/style-selector-location-converter-utils.ts new file mode 100644 index 00000000..73d1ef7b --- /dev/null +++ b/tests/src/parser/style-selector-location-converter-utils.ts @@ -0,0 +1,28 @@ +import assert from "assert"; +import fs from "fs"; +import path from "path"; +import type { AnyNode, Root } from "postcss"; +import type { Node as SelectorNode } from "postcss-selector-parser"; + +import type { SourceLocation } from "../../../src/ast/common.js"; + +export function extractSelectorLocations( + services: Record, + styleAST: Root, +): [string, Partial][][] { + const locations: [string, Partial][][] = []; + styleAST.walk((node: AnyNode) => { + if (node.type === "rule") { + const selectorAst = services.getStyleSelectorAST(node); + const selectorLocations: [string, Partial][] = []; + selectorAst.walk((selectorNode: SelectorNode) => { + selectorLocations.push([ + selectorNode.type, + services.styleSelectorNodeLoc(selectorNode, node), + ]); + }); + locations.push(selectorLocations); + } + }); + return locations; +} diff --git a/tests/src/parser/style-selector-location-converter.ts b/tests/src/parser/style-selector-location-converter.ts new file mode 100644 index 00000000..69946a39 --- /dev/null +++ b/tests/src/parser/style-selector-location-converter.ts @@ -0,0 +1,49 @@ +import assert from "assert"; +import fs from "fs"; +import path from "path"; + +import { parseForESLint } from "../../../src/index.js"; +import { extractSelectorLocations } from "./style-selector-location-converter-utils.js"; +import { generateParserOptions, listupFixtures } from "./test-utils.js"; + +const dirname = path.dirname(new URL(import.meta.url).pathname); +const SELECTOR_CONVERTER_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(SELECTOR_CONVERTER_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 = extractSelectorLocations( + services, + styleContext.sourceAst, + ); + const output = fs.readFileSync(outputFileName, "utf8"); + assert.strictEqual( + `${JSON.stringify(locations, undefined, 2)}\n`, + output, + ); + }); + }); + } +}); diff --git a/tools/update-fixtures.ts b/tools/update-fixtures.ts index 41b30bfa..64a13fb3 100644 --- a/tools/update-fixtures.ts +++ b/tools/update-fixtures.ts @@ -15,6 +15,7 @@ import { selectorAstToJson, styleContextToJson, } from "../tests/src/parser/test-utils.js"; +import { extractSelectorLocations } from "../tests/src/parser/style-selector-location-converter-utils.js"; import type ts from "typescript"; import type ESTree from "estree"; import globals from "globals"; @@ -38,6 +39,10 @@ const SELECTOR_PARSING_FIXTURE_ROOT = path.resolve( dirname, "../tests/fixtures/parser/selector-parsing", ); +const SELECTOR_CONVERTER_FIXTURE_ROOT = path.resolve( + dirname, + "../tests/fixtures/parser/style-selector-location-converter", +); const RULES = [ "no-unused-labels", @@ -232,6 +237,29 @@ for (const { ); } +for (const { + input, + inputFileName, + outputFileName, + config, + meetRequirements, +} of listupFixtures(SELECTOR_CONVERTER_FIXTURE_ROOT)) { + if (!meetRequirements("parse")) { + continue; + } + const services = parse(input, inputFileName, config).services; + const styleContext = services.getStyleContext(); + if (styleContext.status !== "success") { + continue; + } + const locations = extractSelectorLocations(services, styleContext.sourceAst); + fs.writeFileSync( + outputFileName, + `${JSON.stringify(locations, undefined, 2)}\n`, + "utf8", + ); +} + function buildTypes( input: string, result: {