Skip to content

Commit 18d1448

Browse files
committed
Merge commit 'ed6e3470eb9c47faa21d51c037c632774a1f6998' into require-store-reactive-access
2 parents a2b951b + ed6e347 commit 18d1448

36 files changed

+277
-4
lines changed

.changeset/chilly-lemons-love.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-svelte": minor
3+
---
4+
5+
feat: update dependency svelte-eslint-parser to ^0.20.0

.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
@@ -310,6 +310,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
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/require-store-reactive-access](https://ota-meshi.github.io/eslint-plugin-svelte/rules/require-store-reactive-access/) | disallow to use of the store itself as an operand. Need to use $ prefix or get function. | :wrench: |
312312
| [svelte/valid-compile](https://ota-meshi.github.io/eslint-plugin-svelte/rules/valid-compile/) | disallow warnings when compiling. | :star: |
313+
| [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. | |
313314

314315
## Security Vulnerability
315316

docs-svelte-kit/tools/markdown-it-replace-link.mts

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ export default (md: Md, options: { baseUrl: string; root: string }): void => {
3939
if (/^https?:/.test(href)) {
4040
const proxyToken = {
4141
...token,
42-
attrs: [...token.attrs!, ["target", "_blank"]],
42+
attrs: [
43+
...token.attrs!,
44+
["target", "_blank"],
45+
["rel", "noopener noreferrer"],
46+
],
4347
} as Token
4448
return self.renderToken([proxyToken], 0, options)
4549
} else if (/\.md(?:#.*)?$/.test(href) || /^#.*$/.test(href)) {

docs/rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
2828
| [svelte/require-store-callbacks-use-set-param](./rules/require-store-callbacks-use-set-param.md) | store callbacks must use `set` param | |
2929
| [svelte/require-store-reactive-access](./rules/require-store-reactive-access.md) | disallow to use of the store itself as an operand. Need to use $ prefix or get function. | :wrench: |
3030
| [svelte/valid-compile](./rules/valid-compile.md) | disallow warnings when compiling. | :star: |
31+
| [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. | |
3132

3233
## Security Vulnerability
3334

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-svelte-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)

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"postcss-load-config": "^3.1.4",
7474
"postcss-safe-parser": "^6.0.0",
7575
"sourcemap-codec": "^1.4.8",
76-
"svelte-eslint-parser": "^0.19.0"
76+
"svelte-eslint-parser": "^0.20.0"
7777
},
7878
"devDependencies": {
7979
"@1stg/browserslist-config": "^1.2.3",
@@ -175,7 +175,7 @@
175175
"access": "public"
176176
},
177177
"typeCoverage": {
178-
"atLeast": 98.71,
178+
"atLeast": 98.72,
179179
"cache": true,
180180
"detail": true,
181181
"ignoreAsAssertion": true,
+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
@@ -43,6 +43,7 @@ import sortAttributes from "../rules/sort-attributes"
4343
import spacedHtmlComment from "../rules/spaced-html-comment"
4444
import system from "../rules/system"
4545
import validCompile from "../rules/valid-compile"
46+
import validPropNamesInKitPages from "../rules/valid-prop-names-in-kit-pages"
4647

4748
export const rules = [
4849
typescriptEslintNoUnnecessaryCondition,
@@ -89,4 +90,5 @@ export const rules = [
8990
spacedHtmlComment,
9091
system,
9192
validCompile,
93+
validPropNamesInKitPages,
9294
] 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)