Skip to content

Commit e964f89

Browse files
committed
feat: add no-not-data-props-in-kit-pages
1 parent f321a12 commit e964f89

13 files changed

+197
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
289289
| [svelte/no-dupe-style-properties](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-dupe-style-properties/) | disallow duplicate style properties | :star: |
290290
| [svelte/no-dynamic-slot-name](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-dynamic-slot-name/) | disallow dynamic slot name | :star::wrench: |
291291
| [svelte/no-export-load-in-svelte-module-in-kit-pages](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-export-load-in-svelte-module-in-kit-pages/) | Disallow exporting load functions in `*.svelte` module in Svelte Kit page components. | :star: |
292+
| [svelte/no-not-data-props-in-kit-pages](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-not-data-props-in-kit-pages/) | Disallow props other than data or errors in Svelte Kit page components. | |
292293
| [svelte/no-not-function-handler](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-not-function-handler/) | disallow use of not function in event handler | :star: |
293294
| [svelte/no-object-in-text-mustaches](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-object-in-text-mustaches/) | disallow objects in text mustache interpolation | :star: |
294295
| [svelte/no-shorthand-style-property-overrides](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-shorthand-style-property-overrides/) | disallow shorthand style properties that override related longhand properties | :star: |

docs/rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
2020
| [svelte/no-dupe-style-properties](./rules/no-dupe-style-properties.md) | disallow duplicate style properties | :star: |
2121
| [svelte/no-dynamic-slot-name](./rules/no-dynamic-slot-name.md) | disallow dynamic slot name | :star::wrench: |
2222
| [svelte/no-export-load-in-svelte-module-in-kit-pages](./rules/no-export-load-in-svelte-module-in-kit-pages.md) | Disallow exporting load functions in `*.svelte` module in Svelte Kit page components. | :star: |
23+
| [svelte/no-not-data-props-in-kit-pages](./rules/no-not-data-props-in-kit-pages.md) | Disallow props other than data or errors in Svelte Kit page components. | |
2324
| [svelte/no-not-function-handler](./rules/no-not-function-handler.md) | disallow use of not function in event handler | :star: |
2425
| [svelte/no-object-in-text-mustaches](./rules/no-object-in-text-mustaches.md) | disallow objects in text mustache interpolation | :star: |
2526
| [svelte/no-shorthand-style-property-overrides](./rules/no-shorthand-style-property-overrides.md) | disallow shorthand style properties that override related longhand properties | :star: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "svelte/no-not-data-props-in-kit-pages"
5+
description: "Disallow props other than data or errors in Svelte Kit page components."
6+
---
7+
8+
# svelte/no-not-data-props-in-kit-pages
9+
10+
> Disallow props other than data or errors in Svelte Kit page components.
11+
12+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
13+
14+
## :book: Rule Details
15+
16+
This rule reports ???.
17+
18+
<script>
19+
const config = {settings: {
20+
kit: {
21+
files: {
22+
routes: "",
23+
},
24+
},
25+
},
26+
}
27+
</script>
28+
29+
<ESLintCodeBlock config="{config}">
30+
31+
<!--eslint-skip-->
32+
33+
```svelte
34+
<script>
35+
/* eslint svelte/no-not-data-props-in-kit-pages: "error" */
36+
/** ✓ GOOD */
37+
export let data
38+
export let errors
39+
/** ✗ BAD */
40+
export let foo
41+
export let bar
42+
</script>
43+
44+
{foo}, {bar}
45+
```
46+
47+
</ESLintCodeBlock>
48+
49+
## :wrench: Options
50+
51+
```json
52+
{
53+
"svelte/no-not-data-props-in-kit-pages": ["error", {}]
54+
}
55+
```
56+
57+
-
58+
59+
## :books: Further Reading
60+
61+
-
62+
63+
## :mag: Implementation
64+
65+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/no-not-data-props-in-kit-pages.ts)
66+
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/no-not-data-props-in-kit-pages.ts)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type * as ESTree from "estree"
2+
import { createRule } from "../utils"
3+
import { isKitPageComponent, hasSvelteKit } from "./kit-helpers/kit-helpers"
4+
5+
const EXPECTED_PROP_NAMES = ["data", "errors"]
6+
7+
export default createRule("no-not-data-props-in-kit-pages", {
8+
meta: {
9+
docs: {
10+
description:
11+
"Disallow props other than data or errors in Svelte Kit page components.",
12+
category: "Possible Errors",
13+
recommended: false,
14+
},
15+
schema: [],
16+
messages: {
17+
unexpected:
18+
"Disallow props other than data or errors in Svelte Kit page components.",
19+
},
20+
type: "problem",
21+
},
22+
create(context) {
23+
if (!hasSvelteKit || !isKitPageComponent(context)) return {}
24+
let isModule = false
25+
return {
26+
// <script context="module">
27+
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute[key.name="context"] > SvelteLiteral[value="module"]`]:
28+
() => {
29+
isModule = true
30+
},
31+
32+
// <script>
33+
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute[key.name="context"] > SvelteLiteral[value!="module"]`]:
34+
() => {
35+
isModule = false
36+
},
37+
38+
// </script>
39+
["SvelteEndTag"]: () => {
40+
isModule = false
41+
},
42+
43+
// export let xxx
44+
[`ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > Identifier`]:
45+
(node: ESTree.Identifier) => {
46+
if (isModule) return {}
47+
const { name } = node
48+
if (EXPECTED_PROP_NAMES.includes(name)) return {}
49+
return context.report({
50+
node,
51+
loc: node.loc!,
52+
messageId: "unexpected",
53+
})
54+
},
55+
}
56+
},
57+
})

src/utils/rules.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import noDynamicSlotName from "../rules/no-dynamic-slot-name"
1818
import noExportLoadInSvelteModuleInKitPages from "../rules/no-export-load-in-svelte-module-in-kit-pages"
1919
import noExtraReactiveCurlies from "../rules/no-extra-reactive-curlies"
2020
import noInnerDeclarations from "../rules/no-inner-declarations"
21+
import noNotDataPropsInKitPages from "../rules/no-not-data-props-in-kit-pages"
2122
import noNotFunctionHandler from "../rules/no-not-function-handler"
2223
import noObjectInTextMustaches from "../rules/no-object-in-text-mustaches"
2324
import noReactiveFunctions from "../rules/no-reactive-functions"
@@ -62,6 +63,7 @@ export const rules = [
6263
noExportLoadInSvelteModuleInKitPages,
6364
noExtraReactiveCurlies,
6465
noInnerDeclarations,
66+
noNotDataPropsInKitPages,
6567
noNotFunctionHandler,
6668
noObjectInTextMustaches,
6769
noReactiveFunctions,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"settings": {
3+
"kit": {
4+
"files": {
5+
"routes": "tests/fixtures"
6+
}
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- message: Disallow props other than data or errors in Svelte Kit page components.
2+
line: 2
3+
column: 14
4+
suggestions: null
5+
- message: Disallow props other than data or errors in Svelte Kit page components.
6+
line: 3
7+
column: 14
8+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
export let foo
3+
export let bar
4+
</script>
5+
6+
{foo}, {bar}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"settings": {
3+
"kit": {
4+
"files": {
5+
"routes": "tests/fixtures"
6+
}
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
export let data
3+
export let errors
4+
export let foo
5+
export let bar
6+
</script>
7+
8+
{data}, {errors}, {foo}, {bar}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
export let data
3+
export let errors
4+
</script>
5+
6+
{data}, {errors}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script context="module">
2+
export let data
3+
export let errors
4+
export let foo
5+
export let bar
6+
</script>
7+
8+
{data}, {errors}, {foo}, {bar}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../src/rules/no-not-data-props-in-kit-pages"
3+
import { loadTestCases } from "../../utils/utils"
4+
5+
const tester = new RuleTester({
6+
parserOptions: {
7+
ecmaVersion: 2020,
8+
sourceType: "module",
9+
},
10+
})
11+
12+
tester.run(
13+
"no-not-data-props-in-kit-pages",
14+
rule as any,
15+
loadTestCases("no-not-data-props-in-kit-pages"),
16+
)

0 commit comments

Comments
 (0)