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 12, 2024
1 parent 43093b4 commit 897a67e
Show file tree
Hide file tree
Showing 4 changed files with 132 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>
61 changes: 61 additions & 0 deletions tests/src/parser/style-selector-location-coverter.ts
Original file line number Diff line number Diff line change
@@ -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<SourceLocation>,
[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,
);
});
});
}
});

0 comments on commit 897a67e

Please sign in to comment.