Skip to content

Commit 909979e

Browse files
authored
feat: no-not-data-props-in-kit-pages (#283)
* feat: add no-not-data-props-in-kit-pages * chore: run yarn upadte * fix: bug * chore: add todo * chore: add changeset * chore: update document * fix: strict kit page component check * chore: change rule name * fix: test * fix: change file name * fix: logic * fix: regexp
1 parent 049ac2d commit 909979e

33 files changed

+265
-1
lines changed

.changeset/purple-coats-vanish.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-svelte": minor
3+
---
4+
5+
feat: add `svelte/valid-prop-names-in-kit-pages` rule

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
309309
| [svelte/no-unknown-style-directive-property](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-unknown-style-directive-property/) | disallow unknown `style:property` | :star: |
310310
| [svelte/require-store-callbacks-use-set-param](https://ota-meshi.github.io/eslint-plugin-svelte/rules/require-store-callbacks-use-set-param/) | store callbacks must use `set` param | |
311311
| [svelte/valid-compile](https://ota-meshi.github.io/eslint-plugin-svelte/rules/valid-compile/) | disallow warnings when compiling. | :star: |
312+
| [svelte/valid-prop-names-in-kit-pages](https://ota-meshi.github.io/eslint-plugin-svelte/rules/valid-prop-names-in-kit-pages/) | disallow props other than data or errors in Svelte Kit page components. | |
312313

313314
## Security Vulnerability
314315

docs/rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
2727
| [svelte/no-unknown-style-directive-property](./rules/no-unknown-style-directive-property.md) | disallow unknown `style:property` | :star: |
2828
| [svelte/require-store-callbacks-use-set-param](./rules/require-store-callbacks-use-set-param.md) | store callbacks must use `set` param | |
2929
| [svelte/valid-compile](./rules/valid-compile.md) | disallow warnings when compiling. | :star: |
30+
| [svelte/valid-prop-names-in-kit-pages](./rules/valid-prop-names-in-kit-pages.md) | disallow props other than data or errors in Svelte Kit page components. | |
3031

3132
## Security Vulnerability
3233

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "svelte/valid-prop-names-in-kit-pages"
5+
description: "disallow props other than data or errors in Svelte Kit page components."
6+
---
7+
8+
# svelte/valid-prop-names-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 unexpected exported variables at `<script>`.<br>
17+
At SvelteKit v1.0.0-next.405, instead of having multiple props corresponding to the props returned from a load function, page components now have a single data prop.
18+
19+
<script>
20+
const config = {settings: {
21+
kit: {
22+
files: {
23+
routes: "",
24+
},
25+
},
26+
},
27+
}
28+
</script>
29+
30+
<ESLintCodeBlock config="{config}">
31+
32+
<!--eslint-skip-->
33+
34+
```svelte
35+
<script>
36+
/* eslint svelte/valid-prop-names-in-kit-pages: "error" */
37+
/** ✓ GOOD */
38+
export let data
39+
export let errors
40+
// export let { data, errors } = { data: {}, errors: {} }
41+
42+
/** ✗ BAD */
43+
export let foo
44+
export let bar
45+
export let { baz, qux } = data
46+
export let { data: data2, errors: errors2 } = { data: {}, errors: {} }
47+
</script>
48+
49+
{foo}, {bar}
50+
```
51+
52+
</ESLintCodeBlock>
53+
54+
## :wrench: Options
55+
56+
Nothing. But if use are using not default routes folder, please set configuration according to the [user guide](../user-guide.md#settings-kit).
57+
58+
## :books: Further Reading
59+
60+
- [SvelteKit Migration Guide (v1.0.0-next.405)](https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292707)
61+
62+
## :mag: Implementation
63+
64+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/valid-prop-names-in-kit-pages.ts)
65+
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/valid-prop-names-in-kit-pages.ts)
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import type { AST } from "svelte-eslint-parser"
2+
import type * as ESTree from "estree"
3+
import { createRule } from "../utils"
4+
import { isKitPageComponent } from "../utils/svelte-kit"
5+
6+
const EXPECTED_PROP_NAMES = ["data", "errors"]
7+
8+
export default createRule("valid-prop-names-in-kit-pages", {
9+
meta: {
10+
docs: {
11+
description:
12+
"disallow props other than data or errors in Svelte Kit page components.",
13+
category: "Possible Errors",
14+
// TODO Switch to recommended in the major version.
15+
recommended: false,
16+
},
17+
schema: [],
18+
messages: {
19+
unexpected:
20+
"disallow props other than data or errors in Svelte Kit page components.",
21+
},
22+
type: "problem",
23+
},
24+
create(context) {
25+
if (!isKitPageComponent(context)) return {}
26+
let isScript = false
27+
return {
28+
// <script>
29+
"Program > SvelteScriptElement > SvelteStartTag": (
30+
node: AST.SvelteStartTag,
31+
) => {
32+
// except for <script context="module">
33+
isScript = !node.attributes.some(
34+
(a) =>
35+
a.type === "SvelteAttribute" &&
36+
a.key.name === "context" &&
37+
a.value.some(
38+
(v) => v.type === "SvelteLiteral" && v.value === "module",
39+
),
40+
)
41+
},
42+
43+
// </script>
44+
"Program > SvelteScriptElement:exit": () => {
45+
isScript = false
46+
},
47+
48+
"ExportNamedDeclaration > VariableDeclaration > VariableDeclarator": (
49+
node: ESTree.VariableDeclarator,
50+
) => {
51+
if (!isScript) return
52+
53+
// export let foo
54+
if (node.id.type === "Identifier") {
55+
if (!EXPECTED_PROP_NAMES.includes(node.id.name)) {
56+
context.report({
57+
node,
58+
loc: node.loc!,
59+
messageId: "unexpected",
60+
})
61+
}
62+
return
63+
}
64+
65+
// export let { xxx, yyy } = zzz
66+
if (node.id.type !== "ObjectPattern") return
67+
for (const p of node.id.properties) {
68+
if (
69+
p.type === "Property" &&
70+
p.value.type === "Identifier" &&
71+
!EXPECTED_PROP_NAMES.includes(p.value.name)
72+
) {
73+
context.report({
74+
node: p.value,
75+
loc: p.value.loc!,
76+
messageId: "unexpected",
77+
})
78+
}
79+
}
80+
},
81+
}
82+
},
83+
})

src/utils/rules.ts

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import sortAttributes from "../rules/sort-attributes"
4242
import spacedHtmlComment from "../rules/spaced-html-comment"
4343
import system from "../rules/system"
4444
import validCompile from "../rules/valid-compile"
45+
import validPropNamesInKitPages from "../rules/valid-prop-names-in-kit-pages"
4546

4647
export const rules = [
4748
typescriptEslintNoUnnecessaryCondition,
@@ -87,4 +88,5 @@ export const rules = [
8788
spacedHtmlComment,
8889
system,
8990
validCompile,
91+
validPropNamesInKitPages,
9092
] as RuleModule[]

src/utils/svelte-kit.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ export function isKitPageComponent(context: RuleContext): boolean {
2323
"src/routes"
2424
const filePath = context.getFilename()
2525
const projectRootDir = getProjectRootDir(context.getFilename()) ?? ""
26-
return filePath.startsWith(path.join(projectRootDir, routes))
26+
const fileName = filePath.split("/").pop() || filePath
27+
return (
28+
filePath.startsWith(path.join(projectRootDir, routes)) &&
29+
// MEMO: check only `+` and file extension for maintainability
30+
Boolean(/^\+.+\.svelte$/.test(fileName))
31+
)
2732
}
2833

2934
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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
9+
- message: disallow props other than data or errors in Svelte Kit page components.
10+
line: 4
11+
column: 16
12+
suggestions: null
13+
- message: disallow props other than data or errors in Svelte Kit page components.
14+
line: 4
15+
column: 21
16+
suggestions: null
17+
- message: disallow props other than data or errors in Svelte Kit page components.
18+
line: 5
19+
column: 22
20+
suggestions: null
21+
- message: disallow props other than data or errors in Svelte Kit page components.
22+
line: 5
23+
column: 37
24+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
export let foo
3+
export let bar
4+
export let { baz, qux } = data
5+
export let { data: data2, errors: errors2 } = { data: {}, errors: {} }
6+
</script>
7+
8+
{foo}, {bar}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"settings": {
3+
"svelte": {
4+
"kit": {
5+
"files": {
6+
"routes": "tests/fixtures"
7+
}
8+
}
9+
}
10+
}
11+
}
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,5 @@
1+
<script context="module">
2+
export let { data, errors } = { data: {}, errors: {} }
3+
</script>
4+
5+
{data}, {errors}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script context="module">
2+
export let { data2: data, errors2: errors } = { data2: {}, errors2: {} }
3+
</script>
4+
5+
{data}, {errors}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"settings": {
3+
"svelte": {
4+
"kit": {
5+
"files": {
6+
"routes": "tests/fixtures"
7+
}
8+
}
9+
}
10+
}
11+
}
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,16 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../src/rules/valid-prop-names-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+
"valid-prop-names-in-kit-pages",
14+
rule as any,
15+
loadTestCases("valid-prop-names-in-kit-pages"),
16+
)

0 commit comments

Comments
 (0)