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

Update no-exports-from-components rule to allow exceptions #395

Merged
merged 3 commits into from
Jun 11, 2024
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/many-carrots-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": patch
---

Update `astro/no-exports-from-components` rule to allow exceptions
8 changes: 8 additions & 0 deletions docs/rules/no-exports-from-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ since: "v1.1.0"
This rule reports value exports from Astro components.
The use of typed exports are still allowed.

However, there are exceptions for specific named exports that are allowed:
- `getStaticPath`: This function can be exported for dynamic routing purposes.
- `prerender`: This constant can be exported to opt-in to pre-rendering in server mode.

<ESLintCodeBlock>

<!--eslint-skip-->
Expand All @@ -22,6 +26,10 @@ The use of typed exports are still allowed.
/* eslint astro/no-exports-from-components: "error" */
/* ✓ GOOD */
export type A = number | boolean
export const getStaticPath = () => {
// logic here
}
export const prerender = true;
/* ✗ BAD */
export const x = 42
---
Expand Down
22 changes: 21 additions & 1 deletion src/rules/no-exports-from-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import { createRule } from "../utils"
import { getSourceCode } from "../utils/compat"

const ALLOWED_EXPORTS = new Set(["getStaticPath", "prerender"])

export default createRule("no-exports-from-components", {
meta: {
docs: {
description: "disallow value export",
category: "Possible Errors",
// TODO: Switch to recommended: true, in next major version

Check warning on line 12 in src/rules/no-exports-from-components.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: Switch to recommended: true, in...'
recommended: false,
},
schema: [],
Expand All @@ -34,6 +36,20 @@
if (node.type.startsWith("TS") && !node.type.endsWith("Expression")) {
return
}
if (
(node.type === "FunctionDeclaration" &&
node.id &&
ALLOWED_EXPORTS.has(node.id.name)) ||
(node.type === "VariableDeclaration" &&
node.declarations.every(
(decl) =>
decl.id.type === "Identifier" &&
ALLOWED_EXPORTS.has(decl.id.name),
))
) {
// Allow specific named exports
return
}
context.report({
node,
messageId: "disallowExport",
Expand All @@ -56,7 +72,11 @@
if (node.exportKind === "type") return
verifyDeclaration(node.declaration)
for (const spec of node.specifiers) {
if (spec.exportKind === "type") return
if (spec.exportKind === "type") continue
if (ALLOWED_EXPORTS.has(spec.exported.name)) {
// Allow specific named exports
continue
}
context.report({
node: spec,
messageId: "disallowExport",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
// This file is used to test the exceptions for the `no-exports-from-components` rule.
// The following exports are allowed as exceptions.
export const getStaticPath = (): { param: unknown }[] => {
// logic here
return [{ param: "value" }]
}
export const prerender = true
---

<div>Hello World</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
// This file is used to test the exception for the `getStaticPath` function in the `no-exports-from-components` rule.
export async function getStaticPath(): Promise<{ param: unknown }[]> {
// logic here
return []
}
---

<div>Hello World</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
// This file is used to test the exceptions for the `no-exports-from-components` rule.
// The following exports are allowed as exceptions.
const getStaticPath = (): { param: unknown }[] => {
// logic here
return []
}
const prerender = true

export { getStaticPath, prerender }
---

<div>Hello World</div>
Loading