Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jun 11, 2024
1 parent c43ffa4 commit eb15b84
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
22 changes: 19 additions & 3 deletions 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,10 +72,10 @@ 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 (["getStaticPath", "prerender"].includes(spec.exported.name)) {
if (spec.exportKind === "type") continue
if (ALLOWED_EXPORTS.has(spec.exported.name)) {
// Allow specific named exports
return
continue
}
context.report({
node: spec,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +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 = () => {
export const getStaticPath = (): { param: unknown }[] => {
// logic here
return [{ param: "value" }]
}
export const prerender = true;
export const prerender = true
---

<div>Hello World</div>
Original file line number Diff line number Diff line change
@@ -1,7 +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() {
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 eb15b84

Please sign in to comment.