-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(selector-node-loc-fixing): added tests
- Loading branch information
1 parent
577eec0
commit 9374dd0
Showing
6 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
tests/fixtures/parser/style-selector-location-converter/simple-css-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
20 changes: 20 additions & 0 deletions
20
tests/fixtures/parser/style-selector-location-converter/simple-postcss-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
26 changes: 26 additions & 0 deletions
26
tests/fixtures/parser/style-selector-location-converter/simple-scss-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
tests/src/parser/style-selector-location-converter-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters