mirror of
https://github.com/Ku6epXBOCTuK/easy-png-tools.git
synced 2026-11-13 17:08:20 +00:00
chore: update lint rules for comments
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
// Tests for the conventions/comment-format rule
|
||||
// (web/eslint-plugins/conventions/comment-format.js).
|
||||
//
|
||||
// Pure AST check (comment nodes + line counts), standard RuleTester.
|
||||
// Pure AST check (comment nodes + line counts), standard RuleTester. The
|
||||
// svelte pass pins the two comment kinds the token stream never sees: template
|
||||
// `<!-- -->` comments (AST nodes) and CSS in `<style>` (a postcss AST); both
|
||||
// need a svelte parser to exist at all.
|
||||
import { RuleTester } from "eslint";
|
||||
import svelteParser from "svelte-eslint-parser";
|
||||
import tseslint from "typescript-eslint";
|
||||
import { describe, it } from "vitest";
|
||||
import commentFormat from "../conventions/comment-format.js";
|
||||
@@ -17,6 +21,13 @@ const tester = new RuleTester({
|
||||
},
|
||||
});
|
||||
|
||||
const svelteTester = new RuleTester({
|
||||
languageOptions: {
|
||||
parser: svelteParser,
|
||||
parserOptions: { parser: tseslint.parser },
|
||||
},
|
||||
});
|
||||
|
||||
const longBlock = [
|
||||
"// one",
|
||||
"// two",
|
||||
@@ -96,3 +107,101 @@ tester.run("comment-format", asRuleModule(commentFormat), {
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
svelteTester.run("comment-format (svelte)", asRuleModule(commentFormat), {
|
||||
valid: [
|
||||
{
|
||||
code: "<!-- why -->\n<main>Hello</main>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
},
|
||||
{
|
||||
// A template comment at the cap is fine, same as in a .ts file.
|
||||
code: "<!--\na\nb\nc\n-->\n<main>Hello</main>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
},
|
||||
{
|
||||
// Script and template comments are separate blocks, not one run.
|
||||
code: '<!-- why -->\n<main>Hello</main>\n<script lang="ts">\n// why\n</script>',
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
},
|
||||
{
|
||||
// A CSS comment at the cap is fine, same as a script comment.
|
||||
code: "<style>\n/*\na\nb\nc\n*/\n.x {\n\tcolor: red;\n}\n</style>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
},
|
||||
{
|
||||
// Script and <style> comments live in different ASTs: no run across,
|
||||
// so 2 + 5 lines pass where a merged block of 7 would not.
|
||||
code: '<script lang="ts">\n// one\n// two\n</script>\n<style>\n/*\na\nb\nc\n*/\n.x {\n\tcolor: red;\n}\n</style>',
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
},
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
// The template comment the ts pass could never see, over the cap.
|
||||
code: "<!--\na\nb\nc\nd\n-->\n<main>Hello</main>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
errors: [{ messageId: "blockTooLong" }],
|
||||
},
|
||||
{
|
||||
// <style> is a postcss AST, so its comments are invisible to
|
||||
// getAllComments(); the loc must still point at the real line.
|
||||
code: "<style>\n/*\na\nb\nc\nd\n*/\n.x {\n\tcolor: red;\n}\n</style>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
errors: [{ messageId: "blockTooLong", line: 2 }],
|
||||
},
|
||||
{
|
||||
// A comment nested in markup is collected too (not Program.body).
|
||||
code: "<div>\n\t<!--\n\ta\n\tb\n\tc\n\td\n\t-->\n</div>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
errors: [{ messageId: "blockTooLong" }],
|
||||
},
|
||||
{
|
||||
// Template comment lines count toward density: 2 of 4 non-blank.
|
||||
code: "<!-- a -->\n<!-- b -->\n<p>x</p>\n<p>y</p>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxBlockLines: 5, maxRatio: 0.4 }],
|
||||
errors: [{ messageId: "tooDense" }],
|
||||
},
|
||||
{
|
||||
// Script and template comments share one density budget.
|
||||
code: '<!-- a -->\n<script lang="ts">\n// b\n</script>\n<p>c</p>',
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxBlockLines: 5, maxRatio: 0.3 }],
|
||||
errors: [{ messageId: "tooDense" }],
|
||||
},
|
||||
{
|
||||
// CSS lines count toward density too: 2 of 8 non-blank. Consecutive
|
||||
// CSS comments stay separate blocks (each one line, under the cap).
|
||||
code: "<p>x</p>\n<style>\n/* a */\n/* b */\n.x {\n\tcolor: red;\n}\n</style>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxBlockLines: 5, maxRatio: 0.2 }],
|
||||
errors: [{ messageId: "tooDense" }],
|
||||
},
|
||||
{
|
||||
// All three kinds over the cap: one report per block, by position.
|
||||
code: "<!--\na\nb\nc\nd\ne\n-->\n<style>\n/*\nx\ny\nz\nw\nv\n*/\n.x {\n\tcolor: red;\n}\n</style>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxRatio: 1 }],
|
||||
errors: [
|
||||
{ messageId: "blockTooLong", line: 1 },
|
||||
{ messageId: "blockTooLong", line: 9 },
|
||||
],
|
||||
},
|
||||
{
|
||||
// Both violations in one file produce two reports.
|
||||
code: "<!--\na\nb\nc\nd\n-->\n<p>x</p>",
|
||||
filename: "Component.svelte",
|
||||
options: [{ maxBlockLines: 5, maxRatio: 0.5 }],
|
||||
errors: [{ messageId: "blockTooLong" }, { messageId: "tooDense" }],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user