Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add missing-directive-value rule #322

Merged
merged 4 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slimy-pillows-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": minor
---

feat add `missing-directive-value` rule
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ These rules relate to possible syntax or logic errors in Astro component code:

| Rule ID | Description | |
|:--------|:------------|:---|
| [astro/missing-client-directive-value](https://ota-meshi.github.io/eslint-plugin-astro/rules/missing-client-directive-value/) | the client directive is missing the correct component's framework value | ⭐ |
| [astro/no-conflict-set-directives](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-conflict-set-directives/) | disallow conflicting set directives and child contents | ⭐ |
| [astro/no-deprecated-astro-canonicalurl](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-deprecated-astro-canonicalurl/) | disallow using deprecated `Astro.canonicalURL` | ⭐ |
| [astro/no-deprecated-astro-fetchcontent](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-deprecated-astro-fetchcontent/) | disallow using deprecated `Astro.fetchContent()` | ⭐🔧 |
Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ These rules relate to possible syntax or logic errors in Astro component code:

| Rule ID | Description | |
|:--------|:------------|:---|
| [astro/missing-client-directive-value](./rules/missing-client-directive-value.md) | the client directive is missing the correct component's framework value | ⭐ |
| [astro/no-conflict-set-directives](./rules/no-conflict-set-directives.md) | disallow conflicting set directives and child contents | ⭐ |
| [astro/no-deprecated-astro-canonicalurl](./rules/no-deprecated-astro-canonicalurl.md) | disallow using deprecated `Astro.canonicalURL` | ⭐ |
| [astro/no-deprecated-astro-fetchcontent](./rules/no-deprecated-astro-fetchcontent.md) | disallow using deprecated `Astro.fetchContent()` | ⭐🔧 |
Expand Down
60 changes: 60 additions & 0 deletions docs/rules/missing-client-directive-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: "astro/missing-client-directive-value"
description: "the client directive is missing the correct component's framework value"
since: "v0.31.4"
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
---

# astro/missing-client-directive-value

> the client directive is missing the correct component's framework value

- ⚙ 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-directive-value: "error" */

---

{/* ✓ GOOD */}
<SomeSvelteComponent client:only="svelte" />

{/* ✗ BAD */}
<SomeSvelteComponent client:only />
```

</ESLintCodeBlock>

## 🔧 Options

```
<SomeReactComponent client:only="react" />
<SomePreactComponent client:only="preact" />
<SomeSvelteComponent client:only="svelte" />
<SomeVueComponent client:only="vue" />
<SomeSolidComponent client:only="solid-js" />
<SomeLitComponent client:only="lit" />
```

ota-meshi marked this conversation as resolved.
Show resolved Hide resolved

## 📚 Further Reading

- [Astro Documentation | Template Directives Reference > client:only](https://docs.astro.build/en/reference/directives-reference/#clientonly)

## 🚀 Version

This rule was introduced in eslint-plugin-astro v0.31.4

## 🔍 Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/src/rules/missing-client-directive-value.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/tests/src/rules/missing-client-directive-value.ts)
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-astro/tree/main/tests/fixtures/rules/missing-client-directive-value)
1 change: 1 addition & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export = {
extends: [baseExtend],
rules: {
// eslint-plugin-astro rules
"astro/missing-client-directive-value": "error",
"astro/no-conflict-set-directives": "error",
"astro/no-deprecated-astro-canonicalurl": "error",
"astro/no-deprecated-astro-fetchcontent": "error",
Expand Down
45 changes: 45 additions & 0 deletions src/rules/missing-client-directive-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { AST } from "astro-eslint-parser"
import { createRule } from "../utils"
import { getStaticAttributeValue } from "../utils/ast-utils"
import { getSourceCode } from "../utils/compat"

export default createRule("missing-client-directive-value", {
meta: {
docs: {
description:
"the client 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,
) {
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
if (getStaticAttributeValue(attr) !== null) {
return
}

context.report({
node: attr.name,
messageId: "missingValue",
})
}

return {
JSXAttribute: verifyDirectiveValue,
AstroTemplateLiteralAttribute: verifyDirectiveValue,
}
},
})
2 changes: 2 additions & 0 deletions src/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import type { RuleModule } from "../types"
import missingClientDirectiveValue from "../rules/missing-client-directive-value"
import noConflictSetDirectives from "../rules/no-conflict-set-directives"
import noDeprecatedAstroCanonicalurl from "../rules/no-deprecated-astro-canonicalurl"
import noDeprecatedAstroFetchcontent from "../rules/no-deprecated-astro-fetchcontent"
Expand All @@ -19,6 +20,7 @@ import validCompile from "../rules/valid-compile"
import { buildA11yRules } from "../a11y"

export const rules = [
missingClientDirectiveValue,
noConflictSetDirectives,
noDeprecatedAstroCanonicalurl,
noDeprecatedAstroFetchcontent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>

Check failure on line 2 in tests/fixtures/rules/missing-client-directive-value/SomeSvelteComponent.svelte

View workflow job for this annotation

GitHub Actions / lint

Delete `····⏎`
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
</script>

<main>
<h1>Hello Svelte!</h1>

Check failure on line 6 in tests/fixtures/rules/missing-client-directive-value/SomeSvelteComponent.svelte

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
</main>

Check failure on line 7 in tests/fixtures/rules/missing-client-directive-value/SomeSvelteComponent.svelte

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎`
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
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
import SomeSvelteComponent from "../SomeSvelteComponent.svelte"
---

<SomeSvelteComponent client:only />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
import SomeSvelteComponent from "../SomeSvelteComponent.svelte"
let string = "svelte"
---

<!-- prettier-ignore-start -->
<SomeSvelteComponent client:only={string} />
<!-- prettier-ignore-end -->
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions tests/src/rules/missing-client-directive-value.ts
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-directive-value"
import { loadTestCases } from "../../utils/utils"

const tester = new RuleTester({
languageOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
})

tester.run(
"missing-client-directive-value",
rule as any,
loadTestCases("missing-client-directive-value"),
)
Loading