-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: add no-export-load-in-svelte-module-in-kit-pages #281
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 11 commits
bdd3580
ab5e4d2
c92a052
07dbc08
08fc369
4cf5a14
f8de8bc
203bf8e
a9fb99d
59d3097
4fc4c22
332c4bc
d5bdacd
f321a12
dc451d3
d0d21e7
38c2690
2eb81df
3137c67
5353b32
d68a1cf
20ad311
7584b10
cfaf58c
a903027
7d870c0
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 `no-export-load-in-svelte-module-in-kit-pages` rule |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/no-export-load-in-svelte-module-in-kit-pages" | ||
description: "Disallow exporting load functions in `*.svelte` module in Svelte Kit page components." | ||
--- | ||
|
||
# svelte/no-export-load-in-svelte-module-in-kit-pages | ||
|
||
> Disallow exporting load functions in `*.svelte` module in Svelte Kit page components. | ||
|
||
- :gear: This rule is included in `"plugin:svelte/recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports unexpected exported `load` function at `<script context="module">`. | ||
At SvelteKit v1.0.0-next.405, `load` function has been moved into a separate file — `+page.js` for pages, `+layout.js` for layouts. | ||
And the API has changed. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script context="module"> | ||
/* eslint svelte/no-export-load-in-svelte-module-in-kit-pages: "error" */ | ||
/* ✓ GOOD */ | ||
export function foo() {} | ||
export function bar() {} | ||
/* ✗ BAD */ | ||
export function load() {} | ||
// export const load = () => {} | ||
</script> | ||
``` | ||
|
||
</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-3292693) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/no-export-load-in-svelte-module-in-kit-pages.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/no-export-load-in-svelte-module-in-kit-pages.ts) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type * as ESTree from "estree" | ||
import { createRule } from "../utils" | ||
import fs from "fs" | ||
|
||
const hasSvelteKit = (() => { | ||
try { | ||
const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf8")) | ||
// Hack: CI removes `@sveltejs/kit` and it returns false and test failed. | ||
// So always it returns true if it runs on the package. | ||
if (packageJson.name === "eslint-plugin-svelte") return true | ||
return Boolean( | ||
packageJson.dependencies["@sveltejs/kit"] ?? | ||
packageJson.devDependencies["@sveltejs/kit"], | ||
) | ||
} catch (_e) { | ||
return false | ||
} | ||
})() | ||
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. If a user doesn't use SvelteKit, I don't want to show an error, so I added to check package.json. If correct, I will move it to utils for other SvelteKit-related rules. |
||
|
||
export default createRule("no-export-load-in-svelte-module-in-kit-pages", { | ||
meta: { | ||
docs: { | ||
description: | ||
"Disallow exporting load functions in `*.svelte` module in Svelte Kit page components.", | ||
category: "Possible Errors", | ||
recommended: true, | ||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
schema: [], | ||
messages: { | ||
unexpected: | ||
"Disallow exporting load functions in `*.svelte` module in Svelte Kit page components.", | ||
}, | ||
type: "problem", | ||
}, | ||
create(context) { | ||
if (!hasSvelteKit) return {} | ||
let isModule = false | ||
|
||
// return false if it's not a Svelte Kit page component. | ||
const routes = | ||
context.settings?.kit?.files?.routes?.replace(/^\//, "") || "src/routes" | ||
|
||
const filePath = context | ||
.getFilename() | ||
.replace(context.getCwd?.() ?? "", "") | ||
.replace(/^\//, "") | ||
if (!filePath.startsWith(routes)) return {} | ||
|
||
return { | ||
// <script context="module"> | ||
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute > SvelteLiteral[value="module"]`]: | ||
() => { | ||
isModule = true | ||
}, | ||
|
||
// <script> | ||
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute > SvelteLiteral[value!="module"]`]: | ||
() => { | ||
isModule = false | ||
}, | ||
|
||
// </script> | ||
["SvelteEndTag"]: () => { | ||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
isModule = false | ||
}, | ||
|
||
// export function load() {} | ||
// export const load = () => {} | ||
[`ExportNamedDeclaration :matches(FunctionDeclaration, VariableDeclaration > VariableDeclarator) > Identifier[name="load"]`]: | ||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(node: ESTree.Identifier) => { | ||
if (!isModule) return {} | ||
return context.report({ | ||
node, | ||
loc: node.loc!, | ||
messageId: "unexpected", | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- message: Disallow self-closing on HTML elements. | ||
line: 3 | ||
column: 3 | ||
suggestions: null | ||
- message: Require self-closing on HTML void elements. | ||
line: 4 | ||
column: 3 | ||
suggestions: null | ||
- message: Disallow self-closing on Svelte custom components. | ||
line: 5 | ||
column: 3 | ||
suggestions: null | ||
- message: Require self-closing on Svelte special elements. | ||
line: 8 | ||
column: 1 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- message: Disallow self-closing on HTML elements. | ||
line: 3 | ||
column: 3 | ||
suggestions: null | ||
- message: Disallow self-closing on Svelte custom components. | ||
line: 4 | ||
column: 3 | ||
suggestions: null | ||
- message: Disallow self-closing on HTML void elements. | ||
line: 5 | ||
column: 3 | ||
suggestions: null | ||
- message: Disallow self-closing on Svelte special elements. | ||
line: 8 | ||
column: 1 | ||
suggestions: null |
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,4 @@ | ||
- message: "Disallow exporting load functions in `*.svelte` module in Svelte Kit page components." | ||
line: 2 | ||
column: 19 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export function load() {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- message: "Disallow exporting load functions in `*.svelte` module in Svelte Kit page components." | ||
line: 2 | ||
column: 16 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export const load = () => {} | ||
</script> |
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,9 @@ | ||
{ | ||
"settings": { | ||
"kit": { | ||
"files": { | ||
"routes": "some-path" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export function load() {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export const load = () => {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script> | ||
export function load() {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script> | ||
export const load = () => {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
function load() {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
const load = () => {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export function foo() {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script context="module"> | ||
export function foo() {} | ||
</script> | ||
|
||
<script> | ||
export function load() {} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { RuleTester } from "eslint" | ||
import rule from "../../../src/rules/no-export-load-in-svelte-module-in-kit-pages" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"no-export-load-in-svelte-module-in-kit-pages", | ||
rule as any, | ||
loadTestCases("no-export-load-in-svelte-module-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 choose that I added shared configuration for kit routes path.
I think over 95% of users use default settings
src/routes
.So only a few users need to config it.
And for 5% users, we discussed that we will use
espree
but it may fail to get the path if they rely to env of some external information. So for now I didn't implement this logic.#241 (comment)