-
-
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
43093b4
commit 897a67e
Showing
4 changed files
with
132 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> |
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,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, | ||
); | ||
}); | ||
}); | ||
} | ||
}); |