-
-
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
Merged
ota-meshi
merged 26 commits into
sveltejs:main
from
baseballyama:feature/no-export-load-in-svelte-module-in-kit-pages
Nov 1, 2022
Merged
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
bdd3580
feat: add no-export-load-in-svelte-module-in-kit-pages
baseballyama ab5e4d2
chore: add changeset
baseballyama c92a052
chore: add test
baseballyama 07dbc08
fix: check devDependencies also
baseballyama 08fc369
fix: adjust for test
baseballyama 4cf5a14
feat: add config
baseballyama f8de8bc
chore: fix typo
baseballyama 203bf8e
fix: bug
baseballyama a9fb99d
fix: bug
baseballyama 59d3097
chore: uupdate docus
baseballyama 4fc4c22
chore: update docs
baseballyama 332c4bc
chore: add kit helper
baseballyama d5bdacd
fix: fix for demo
baseballyama f321a12
chore: adjust doc style
baseballyama dc451d3
Merge branch 'main' into feature/no-export-load-in-svelte-module-in-k…
baseballyama d0d21e7
chore: revert .eslintrc.js
baseballyama 38c2690
chore: update uppercase -> lowercase
baseballyama 2eb81df
fix: update recommended
baseballyama 3137c67
fix: i did not know :exit
baseballyama 5353b32
fix: project root path finding logic
baseballyama d68a1cf
chore: utilize
baseballyama 20ad311
chore: yarn update
baseballyama 7584b10
fix: remove needless logic
baseballyama cfaf58c
chore: simplify
baseballyama a903027
fix: false positive
baseballyama 7d870c0
Merge branch 'main' into feature/no-export-load-in-svelte-module-in-k…
ota-meshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
docs/rules/no-export-load-in-svelte-module-in-kit-pages.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
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. | ||
|
||
<script> | ||
const config = { | ||
settings: { | ||
kit: { | ||
files: { | ||
routes: "", | ||
}, | ||
}, | ||
}, | ||
} | ||
</script> | ||
|
||
<ESLintCodeBlock config="{config}"> | ||
|
||
<!--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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { RuleContext } from "../../types" | ||
import fs from "fs" | ||
|
||
/** | ||
* return true if it's a Svelte Kit page component. | ||
* @param context | ||
* @returns | ||
*/ | ||
export function isKitPageComponent(context: RuleContext): boolean { | ||
// 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(/^\//, "") | ||
return filePath.startsWith(routes) | ||
} | ||
|
||
export const hasSvelteKit = (() => { | ||
// Hack: if it runs on browser, it regards as Svelte Kit project. | ||
if (!fs.readFileSync) return true | ||
try { | ||
const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf8")) | ||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 | ||
} | ||
})() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type * as ESTree from "estree" | ||
import { createRule } from "../utils" | ||
import { isKitPageComponent, hasSvelteKit } from "./kit-helpers/kit-helpers" | ||
|
||
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 || !isKitPageComponent(context)) return {} | ||
let isModule = false | ||
return { | ||
// <script context="module"> | ||
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute[key.name="context"] > SvelteLiteral[value="module"]`]: | ||
() => { | ||
isModule = true | ||
}, | ||
|
||
// <script> | ||
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute[key.name="context"] > SvelteLiteral[value!="module"]`]: | ||
() => { | ||
isModule = false | ||
}, | ||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// </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", | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
tests/fixtures/rules/html-self-closing/invalid/presets/html/preset-html-errors.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
16 changes: 16 additions & 0 deletions
16
tests/fixtures/rules/html-self-closing/invalid/presets/none/preset-none-errors.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/invalid/_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"settings": { | ||
"kit": { | ||
"files": { | ||
"routes": "tests/fixtures" | ||
} | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/invalid/test01-errors.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
...s/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/invalid/test01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export function load() {} | ||
</script> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/invalid/test02-errors.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
...s/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/invalid/test02-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export const load = () => {} | ||
</script> |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/valid/_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"settings": { | ||
"kit": { | ||
"files": { | ||
"routes": "tests/fixtures" | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...s/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/valid/not-page/_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"settings": { | ||
"kit": { | ||
"files": { | ||
"routes": "some-path" | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...res/rules/no-export-load-in-svelte-module-in-kit-pages/valid/not-page/test01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export function load() {} | ||
</script> |
3 changes: 3 additions & 0 deletions
3
...res/rules/no-export-load-in-svelte-module-in-kit-pages/valid/not-page/test02-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script context="module"> | ||
export const load = () => {} | ||
</script> |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/valid/test01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script> | ||
export function load() {} | ||
</script> |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/valid/test02-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script> | ||
export const load = () => {} | ||
</script> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)