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

Add astro/no-deprecated-getentrybyslug rule #234

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/heavy-carpets-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": minor
---

feat: add `astro/no-deprecated-getentrybyslug` rule
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ These rules relate to possible syntax or logic errors in Astro component code:
| [astro/no-deprecated-astro-canonicalurl](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-deprecated-astro-canonicalurl/) | disallow using deprecated `Astro.canonicalURL` | :star: |
| [astro/no-deprecated-astro-fetchcontent](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-deprecated-astro-fetchcontent/) | disallow using deprecated `Astro.fetchContent()` | :star::wrench: |
| [astro/no-deprecated-astro-resolve](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-deprecated-astro-resolve/) | disallow using deprecated `Astro.resolve()` | :star: |
| [astro/no-deprecated-getentrybyslug](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-deprecated-getentrybyslug/) | disallow using deprecated `getEntryBySlug()` | :star: |
| [astro/no-unused-define-vars-in-style](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-unused-define-vars-in-style/) | disallow unused `define:vars={...}` in `style` tag | :star: |
| [astro/valid-compile](https://ota-meshi.github.io/eslint-plugin-astro/rules/valid-compile/) | disallow warnings when compiling. | :star: |

Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ These rules relate to possible syntax or logic errors in Astro component code:
| [astro/no-deprecated-astro-canonicalurl](./rules/no-deprecated-astro-canonicalurl.md) | disallow using deprecated `Astro.canonicalURL` | :star: |
| [astro/no-deprecated-astro-fetchcontent](./rules/no-deprecated-astro-fetchcontent.md) | disallow using deprecated `Astro.fetchContent()` | :star::wrench: |
| [astro/no-deprecated-astro-resolve](./rules/no-deprecated-astro-resolve.md) | disallow using deprecated `Astro.resolve()` | :star: |
| [astro/no-deprecated-getentrybyslug](./rules/no-deprecated-getentrybyslug.md) | disallow using deprecated `getEntryBySlug()` | :star: |
| [astro/no-unused-define-vars-in-style](./rules/no-unused-define-vars-in-style.md) | disallow unused `define:vars={...}` in `style` tag | :star: |
| [astro/valid-compile](./rules/valid-compile.md) | disallow warnings when compiling. | :star: |

Expand Down
47 changes: 47 additions & 0 deletions docs/rules/no-deprecated-getentrybyslug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: "astro/no-deprecated-getentrybyslug"
description: "disallow using deprecated `getEntryBySlug()`"
---

# astro/no-deprecated-getentrybyslug

> disallow using deprecated `getEntryBySlug()`

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
- :gear: This rule is included in `"plugin:astro/recommended"`.

## :book: Rule Details

This rule reports use of deprecated `getEntryBySlug()`.

<ESLintCodeBlock>

<!--eslint-skip-->

```astro
---
/* eslint astro/no-deprecated-getentrybyslug: "error" */

/* ✓ GOOD */
import { getEntry } from "astro:content";

/* ✗ BAD */
import { getEntryBySlug } from "astro:content";
---
```

</ESLintCodeBlock>

## :wrench: Options

Nothing.

## :books: Further Reading

- [Astro Documentation | API Reference - getEntryBySlug()](https://docs.astro.build/en/reference/api-reference/#getentrybyslug)

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/src/rules/no-deprecated-getentrybyslug.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/tests/src/rules/no-deprecated-getentrybyslug.ts)
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-astro/tree/main/tests/fixtures/rules/no-deprecated-getentrybyslug)
1 change: 1 addition & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export = {
"astro/no-deprecated-astro-canonicalurl": "error",
"astro/no-deprecated-astro-fetchcontent": "error",
"astro/no-deprecated-astro-resolve": "error",
"astro/no-deprecated-getentrybyslug": "error",
"astro/no-unused-define-vars-in-style": "error",
"astro/valid-compile": "error",
},
Expand Down
37 changes: 37 additions & 0 deletions src/rules/no-deprecated-getentrybyslug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createRule } from "../utils"
import type { TSESTree } from "@typescript-eslint/types"

export default createRule("no-deprecated-getentrybyslug", {
meta: {
docs: {
description: "disallow using deprecated `getEntryBySlug()`",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {
deprecated: "'getEntryBySlug()' is deprecated. Use 'getEntry()' instead.",
},
type: "problem",
},
create(context) {
if (!context.parserServices.isAstro) {
return {}
}

return {
ImportSpecifier(node: TSESTree.ImportSpecifier) {
if (
node.imported.name === "getEntryBySlug" &&
node.parent?.type === "ImportDeclaration" &&
node.parent.source.value === "astro:content"
) {
context.report({
node,
messageId: "deprecated",
})
}
},
}
},
})
2 changes: 2 additions & 0 deletions src/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import noConflictSetDirectives from "../rules/no-conflict-set-directives"
import noDeprecatedAstroCanonicalurl from "../rules/no-deprecated-astro-canonicalurl"
import noDeprecatedAstroFetchcontent from "../rules/no-deprecated-astro-fetchcontent"
import noDeprecatedAstroResolve from "../rules/no-deprecated-astro-resolve"
import noDeprecatedGetentrybyslug from "../rules/no-deprecated-getentrybyslug"
import noSetHtmlDirective from "../rules/no-set-html-directive"
import noSetTextDirective from "../rules/no-set-text-directive"
import noUnusedCssSelector from "../rules/no-unused-css-selector"
Expand All @@ -22,6 +23,7 @@ export const rules = [
noDeprecatedAstroCanonicalurl,
noDeprecatedAstroFetchcontent,
noDeprecatedAstroResolve,
noDeprecatedGetentrybyslug,
noSetHtmlDirective,
noSetTextDirective,
noUnusedCssSelector,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "'getEntryBySlug()' is deprecated. Use 'getEntry()' instead.",
"line": 3,
"column": 10
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
/* ✗ BAD */
import { getEntryBySlug } from "astro:content"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
/* ✓ GOOD */
import { getEntry } from "astro:content"
---
16 changes: 16 additions & 0 deletions tests/src/rules/no-deprecated-getentrybyslug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { RuleTester } from "eslint"
import rule from "../../../src/rules/no-deprecated-getentrybyslug"
import { loadTestCases } from "../../utils/utils"

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

tester.run(
"no-deprecated-getentrybyslug",
rule as any,
loadTestCases("no-deprecated-getentrybyslug"),
)
Loading