-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
missing-directive-value
rule (#322)
* wip: add new rule * feat: add changeset file * fix: add new check for the rule and more tests * fix: rename the rule from missing-client-only-directive-value --------- Co-authored-by: OM <[email protected]>
- Loading branch information
Showing
13 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"eslint-plugin-astro": minor | ||
--- | ||
|
||
feat add `astro/missing-client-only-directive-value` rule |
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
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
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 @@ | ||
--- | ||
title: "astro/missing-client-only-directive-value" | ||
description: "the client:only directive is missing the correct component's framework value" | ||
--- | ||
|
||
# astro/missing-client-only-directive-value | ||
|
||
> the client:only directive is missing the correct component's framework value | ||
- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
- ⚙ This rule is included in `"plugin:astro/recommended"`. | ||
|
||
## 📖 Rule Details | ||
|
||
This rule reports not setting a value for the `client:only` directive. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```astro | ||
--- | ||
/* eslint astro/missing-client-only-directive-value: "error" */ | ||
--- | ||
{/* ✓ GOOD */} | ||
<SomeSvelteComponent client:only="svelte" /> | ||
{/* ✗ BAD */} | ||
<SomeSvelteComponent client:only /> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## 🔧 Options | ||
|
||
Nothing. | ||
|
||
|
||
## 📚 Further Reading | ||
|
||
- [Astro Documentation | Template Directives Reference > client:only](https://docs.astro.build/en/reference/directives-reference/#clientonly) | ||
|
||
## 🔍 Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/src/rules/missing-client-only-directive-value.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/tests/src/rules/missing-client-only-directive-value.ts) | ||
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-astro/tree/main/tests/fixtures/rules/missing-client-only-directive-value) |
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
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
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,50 @@ | ||
import type { AST } from "astro-eslint-parser" | ||
import { createRule } from "../utils" | ||
import { getAttributeName, getStaticAttributeValue } from "../utils/ast-utils" | ||
import { getSourceCode } from "../utils/compat" | ||
|
||
export default createRule("missing-client-only-directive-value", { | ||
meta: { | ||
docs: { | ||
description: | ||
"the client:only directive is missing the correct component's framework value", | ||
category: "Possible Errors", | ||
recommended: true, | ||
}, | ||
schema: [], | ||
messages: { | ||
missingValue: "`client:only` directive is missing a value", | ||
}, | ||
type: "problem", | ||
}, | ||
create(context) { | ||
const sourceCode = getSourceCode(context) | ||
if (!sourceCode.parserServices.isAstro) { | ||
return {} | ||
} | ||
|
||
/** VerifyDirectiveValue */ | ||
function verifyDirectiveValue( | ||
attr: AST.JSXAttribute | AST.AstroTemplateLiteralAttribute, | ||
) { | ||
const directiveName = getAttributeName(attr) | ||
const directiveValue = getStaticAttributeValue(attr) | ||
|
||
if (directiveName !== "client:only") return | ||
|
||
if (directiveValue !== null) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node: attr.name, | ||
messageId: "missingValue", | ||
}) | ||
} | ||
|
||
return { | ||
JSXAttribute: verifyDirectiveValue, | ||
AstroTemplateLiteralAttribute: verifyDirectiveValue, | ||
} | ||
}, | ||
}) |
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
6 changes: 6 additions & 0 deletions
6
tests/fixtures/rules/missing-client-only-directive-value/SomeSvelteComponent.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,6 @@ | ||
<script> | ||
</script> | ||
|
||
<main> | ||
<h1>Hello Svelte!</h1> | ||
</main> |
7 changes: 7 additions & 0 deletions
7
...ssing-client-only-directive-value/invalid/missing-client-only-directive-value-errors.json
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,7 @@ | ||
[ | ||
{ | ||
"message": "`client:only` directive is missing a value", | ||
"line": 5, | ||
"column": 22 | ||
} | ||
] |
5 changes: 5 additions & 0 deletions
5
...ssing-client-only-directive-value/invalid/missing-client-only-directive-value-input.astro
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,5 @@ | ||
--- | ||
import SomeSvelteComponent from "../SomeSvelteComponent.svelte" | ||
--- | ||
|
||
<SomeSvelteComponent client:only /> |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/rules/missing-client-only-directive-value/valid/client-only-value.astro
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,9 @@ | ||
--- | ||
import SomeSvelteComponent from "../SomeSvelteComponent.svelte" | ||
let string = "svelte" | ||
--- | ||
|
||
<!-- prettier-ignore-start --> | ||
<SomeSvelteComponent client:only={string} /> | ||
<SomeSvelteComponent client:idle /> | ||
<!-- prettier-ignore-end --> |
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,16 @@ | ||
import { RuleTester } from "../../utils/eslint-compat" | ||
import rule from "../../../src/rules/missing-client-only-directive-value" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"missing-client-only-directive-value", | ||
rule as any, | ||
loadTestCases("missing-client-only-directive-value"), | ||
) |