Skip to content

Commit

Permalink
test(selector-node-loc-fixing): added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Dec 13, 2024
1 parent 577eec0 commit 9374dd0
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script>
let a = 10
</script>

<span class="myClass">Hello!</span>

<b>{a}</b>

<style>
.myClass {
color: red;
}
b {
font-size: xx-large;
}
a:active,
a::before,
b + a,
b + .myClass,
a[data-key="value"] {
color: blue;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="container">
<div class="div-class">Hello</div>

<span class="span-class">World!</span>
</div>

<style lang="postcss">
body {
colour: white;
background-colour: grey;
}
a:active,
a::before,
b + a,
b + .myClass,
a[data-key="value"] {
color: blue;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="container">
<div class="div-class">Hello</div>

<span class="span-class">World!</span>
</div>

<style lang="scss">
.container {
.div-class {
// This is an inline comment
color: red;
}
.span-class {
font-weight: bold;
}
a:active,
a::before,
b + a,
b + .myClass,
a[data-key="value"] {
color: blue;
}
}
</style>
28 changes: 28 additions & 0 deletions tests/src/parser/style-selector-location-converter-utils.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>,
styleAST: Root,
): [string, Partial<SourceLocation>][][] {
const locations: [string, Partial<SourceLocation>][][] = [];
styleAST.walk((node: AnyNode) => {
if (node.type === "rule") {
const selectorAst = services.getStyleSelectorAST(node);
const selectorLocations: [string, Partial<SourceLocation>][] = [];
selectorAst.walk((selectorNode: SelectorNode) => {
selectorLocations.push([
selectorNode.type,
services.styleSelectorNodeLoc(selectorNode, node),
]);
});
locations.push(selectorLocations);
}
});
return locations;
}
49 changes: 49 additions & 0 deletions tests/src/parser/style-selector-location-converter.ts
Original file line number Diff line number Diff line change
@@ -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,
);
});
});
}
});
28 changes: 28 additions & 0 deletions tools/update-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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",
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 9374dd0

Please sign in to comment.