Skip to content

Commit

Permalink
Update no-exports-from-components rule to allow exceptions (#395)
Browse files Browse the repository at this point in the history
* Update no-exports-from-components rule to allow exceptions

* Create many-carrots-doubt.md

* fix
  • Loading branch information
ota-meshi committed Jun 11, 2024
1 parent 43aa17b commit 5174590
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
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,6 +2,8 @@ import type { TSESTree } from "@typescript-eslint/types"
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: {
Expand Down Expand Up @@ -34,6 +36,20 @@ export default createRule("no-exports-from-components", {
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 @@ export default createRule("no-exports-from-components", {
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>

0 comments on commit 5174590

Please sign in to comment.