-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
astro/no-exports-from-components
rule
- Loading branch information
Showing
26 changed files
with
322 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
title: "astro/no-exports-from-components" | ||
description: "disallow value export" | ||
--- | ||
|
||
# astro/no-exports-from-components | ||
|
||
> disallow value export | ||
- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## 📖 Rule Details | ||
|
||
This rule reports value exports from Astro components. | ||
The use of typed exports are still allowed. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```astro | ||
--- | ||
/* eslint astro/no-exports-from-components: "error" */ | ||
/* ✓ GOOD */ | ||
export type A = number | boolean | ||
/* ✗ BAD */ | ||
export const x = 42 | ||
--- | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## 🔧 Options | ||
|
||
Nothing. | ||
|
||
## 🔍 Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/src/rules/no-exports-from-components.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/tests/src/rules/no-exports-from-components.ts) | ||
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-astro/tree/main/tests/fixtures/rules/no-exports-from-components) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { TSESTree } from "@typescript-eslint/types" | ||
import { createRule } from "../utils" | ||
import { getSourceCode } from "../utils/compat" | ||
|
||
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 10 in src/rules/no-exports-from-components.ts GitHub Actions / lint
|
||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
disallowExport: "Exporting values from components is not allowed.", | ||
}, | ||
type: "problem", | ||
}, | ||
create(context) { | ||
const sourceCode = getSourceCode(context) | ||
if (!sourceCode.parserServices.isAstro) { | ||
return {} | ||
} | ||
|
||
/** | ||
* Verify for export declarations | ||
*/ | ||
function verifyDeclaration( | ||
node: | ||
| TSESTree.ExportDefaultDeclaration["declaration"] | ||
| TSESTree.ExportNamedDeclaration["declaration"], | ||
) { | ||
if (!node) return | ||
if (node.type.startsWith("TS") && !node.type.endsWith("Expression")) { | ||
return | ||
} | ||
context.report({ | ||
node, | ||
messageId: "disallowExport", | ||
}) | ||
} | ||
|
||
return { | ||
ExportAllDeclaration(node) { | ||
if (node.exportKind === "type") return | ||
context.report({ | ||
node, | ||
messageId: "disallowExport", | ||
}) | ||
}, | ||
ExportDefaultDeclaration(node) { | ||
if (node.exportKind === "type") return | ||
verifyDeclaration(node.declaration) | ||
}, | ||
ExportNamedDeclaration(node) { | ||
if (node.exportKind === "type") return | ||
verifyDeclaration(node.declaration) | ||
for (const spec of node.specifiers) { | ||
if (spec.exportKind === "type") return | ||
context.report({ | ||
node: spec, | ||
messageId: "disallowExport", | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/no-exports-from-components/invalid/_config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"languageOptions": { | ||
"parserOptions": { | ||
"parser": "@typescript-eslint/parser" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/no-exports-from-components/invalid/export-all-01-errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 2, | ||
"column": 1 | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-exports-from-components/invalid/export-all-01-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
export * from "./foo.js" | ||
--- |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/no-exports-from-components/invalid/export-default-01-errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 2, | ||
"column": 16 | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-exports-from-components/invalid/export-default-01-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
export default {} | ||
--- |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/no-exports-from-components/invalid/export-default-02-errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 4, | ||
"column": 16 | ||
} | ||
] |
5 changes: 5 additions & 0 deletions
5
tests/fixtures/rules/no-exports-from-components/invalid/export-default-02-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
export default function fn(): boolean | ||
export default function fn(a: string): string | ||
export default function fn(a?: string): string | boolean {} | ||
--- |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/no-exports-from-components/invalid/export-default-03-errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 2, | ||
"column": 16 | ||
} | ||
] |
5 changes: 5 additions & 0 deletions
5
tests/fixtures/rules/no-exports-from-components/invalid/export-default-03-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
export default class A { | ||
private x(): void {} | ||
} | ||
--- |
32 changes: 32 additions & 0 deletions
32
tests/fixtures/rules/no-exports-from-components/invalid/named-export01-errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[ | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 2, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 3, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 3, | ||
"column": 13 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 4, | ||
"column": 8 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 7, | ||
"column": 8 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 10, | ||
"column": 8 | ||
} | ||
] |
11 changes: 11 additions & 0 deletions
11
tests/fixtures/rules/no-exports-from-components/invalid/named-export01-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
export { A } from "./foo.js" | ||
export { B, B2 } from "./foo.js" | ||
export const C = { | ||
// ... | ||
} | ||
export class D { | ||
private x(): void {} | ||
} | ||
export function E(): void {} | ||
--- |
32 changes: 32 additions & 0 deletions
32
tests/fixtures/rules/no-exports-from-components/invalid/named-export02-errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[ | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 13, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 13, | ||
"column": 13 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 13, | ||
"column": 16 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 14, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 14, | ||
"column": 13 | ||
}, | ||
{ | ||
"message": "Exporting values from components is not allowed.", | ||
"line": 14, | ||
"column": 16 | ||
} | ||
] |
15 changes: 15 additions & 0 deletions
15
tests/fixtures/rules/no-exports-from-components/invalid/named-export02-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
import { A } from "./foo.js" | ||
import { B, B2 } from "./foo.js" | ||
const C = { | ||
// ... | ||
} | ||
class D { | ||
private x(): void {} | ||
} | ||
function E(): void {} | ||
export { A, B, B2 } | ||
export { C, D, E } | ||
--- |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/no-exports-from-components/valid/_config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"languageOptions": { | ||
"parserOptions": { | ||
"parser": "@typescript-eslint/parser" | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-exports-from-components/valid/export-all-01-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
export type * from "./foo.js" | ||
--- |
5 changes: 5 additions & 0 deletions
5
tests/fixtures/rules/no-exports-from-components/valid/export-default-01-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
export default interface A { | ||
a: string | ||
} | ||
--- |
14 changes: 14 additions & 0 deletions
14
tests/fixtures/rules/no-exports-from-components/valid/named-export01-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
export { type A } from "./foo.js" | ||
export type { B, B2 } from "./foo.js" | ||
export type C = { | ||
// ... | ||
} | ||
export interface D { | ||
a(): void | ||
} | ||
export const enum E { | ||
x = 1, | ||
y = 2, | ||
} | ||
--- |
15 changes: 15 additions & 0 deletions
15
tests/fixtures/rules/no-exports-from-components/valid/named-export02-input.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
import type { A } from "./foo.js" | ||
import type { B, B2 } from "./foo.js" | ||
type C = { | ||
// ... | ||
} | ||
interface D { | ||
a(): void | ||
} | ||
enum E {} | ||
export type { A, B, B2 } | ||
export { type C, type D, type E } | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/no-exports-from-components" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"no-exports-from-components", | ||
rule as any, | ||
loadTestCases("no-exports-from-components"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters