-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: no-not-data-props-in-kit-pages #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
177b0af
14283bd
7a74d85
7a41b55
bf2297f
34d0a5d
10dccec
0bfd972
66d28d0
090a025
fc75a8e
2e6c25c
cde30a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"eslint-plugin-svelte": minor | ||
--- | ||
|
||
feat: add `svelte/no-not-data-props-in-kit-pages` rule |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/no-not-data-props-in-kit-pages" | ||
description: "disallow props other than data or errors in Svelte Kit page components." | ||
--- | ||
|
||
# svelte/no-not-data-props-in-kit-pages | ||
|
||
> disallow props other than data or errors in Svelte Kit page components. | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports unexpected exported variables at `<script>`.<br> | ||
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. | ||
|
||
<script> | ||
const config = {settings: { | ||
kit: { | ||
files: { | ||
routes: "", | ||
}, | ||
}, | ||
}, | ||
} | ||
</script> | ||
|
||
<ESLintCodeBlock config="{config}"> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-not-data-props-in-kit-pages: "error" */ | ||
/** ✓ GOOD */ | ||
export let data | ||
export let errors | ||
/** ✗ BAD */ | ||
export let foo | ||
export let bar | ||
</script> | ||
|
||
{foo}, {bar} | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. But if use are using not default routes folder, please set configuration according to the [user guide](../user-guide.md#settings-kit). | ||
|
||
## :books: Further Reading | ||
|
||
- [SvelteKit Migration Guide (v1.0.0-next.405)](https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292707) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/no-not-data-props-in-kit-pages.ts) | ||
- [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 number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { AST } from "svelte-eslint-parser" | ||
import type * as ESTree from "estree" | ||
import { createRule } from "../utils" | ||
import { isKitPageComponent } from "../utils/svelte-kit" | ||
|
||
const EXPECTED_PROP_NAMES = ["data", "errors"] | ||
|
||
export default createRule("no-not-data-props-in-kit-pages", { | ||
meta: { | ||
docs: { | ||
description: | ||
"disallow props other than data or errors in Svelte Kit page components.", | ||
category: "Possible Errors", | ||
// TODO Switch to recommended in the major version. | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
unexpected: | ||
"disallow props other than data or errors in Svelte Kit page components.", | ||
}, | ||
type: "problem", | ||
}, | ||
create(context) { | ||
if (!isKitPageComponent(context)) return {} | ||
let isScript = false | ||
return { | ||
// <script> | ||
[`Program > SvelteScriptElement > SvelteStartTag`]: ( | ||
node: AST.SvelteStartTag, | ||
) => { | ||
// except for <script context="module"> | ||
isScript = !node.attributes.some( | ||
(a) => | ||
a.type === "SvelteAttribute" && | ||
a.key.name === "context" && | ||
a.value.some( | ||
(v) => v.type === "SvelteLiteral" && v.value === "module", | ||
), | ||
) | ||
}, | ||
|
||
// </script> | ||
"Program > SvelteScriptElement:exit": () => { | ||
isScript = false | ||
}, | ||
|
||
// export let xxx | ||
[`ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > Identifier`]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not enough to just check the export let {a, b} = obj I think it's better to select a
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh https://svelte.dev/repl/61922177a5944a5294e9551787479d6a?version=3.52.0 I updated the logic, so could you please review it again? |
||
(node: ESTree.Identifier) => { | ||
if (!isScript) return {} | ||
const { name } = node | ||
if (EXPECTED_PROP_NAMES.includes(name)) return {} | ||
return context.report({ | ||
node, | ||
loc: node.loc!, | ||
messageId: "unexpected", | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"settings": { | ||
"kit": { | ||
"files": { | ||
"routes": "tests/fixtures" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
- message: disallow props other than data or errors in Svelte Kit page components. | ||
line: 2 | ||
column: 14 | ||
suggestions: null | ||
- message: disallow props other than data or errors in Svelte Kit page components. | ||
line: 3 | ||
column: 14 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script> | ||
export let foo | ||
export let bar | ||
</script> | ||
|
||
{foo}, {bar} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"settings": { | ||
"kit": { | ||
"files": { | ||
"routes": "tests/fixtures" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script> | ||
export let data | ||
export let errors | ||
export let foo | ||
export let bar | ||
</script> | ||
|
||
{data}, {errors}, {foo}, {bar} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script> | ||
export let data | ||
export let errors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw this and noticed that Do you have any good ideas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the rule name according to your suggestion! |
||
</script> | ||
|
||
{data}, {errors} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script context="module"> | ||
export let data | ||
export let errors | ||
export let foo | ||
export let bar | ||
</script> | ||
|
||
{data}, {errors}, {foo}, {bar} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { RuleTester } from "eslint" | ||
import rule from "../../../src/rules/no-not-data-props-in-kit-pages" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"no-not-data-props-in-kit-pages", | ||
rule as any, | ||
loadTestCases("no-not-data-props-in-kit-pages"), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
isKitPageComponent
needs to be changed to be more strict.I think we need to check if the file name starts with
+
.For filenames that don't start with
+
, it may just be a component.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added check logic and fixed tests.