-
-
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 4 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
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
--- | ||
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 | ||
|
||
```json | ||
{ | ||
"svelte/no-export-load-in-svelte-module-in-kit-pages": ["error", {}] | ||
} | ||
``` | ||
|
||
- | ||
|
||
## :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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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")) | ||
return Boolean( | ||
packageJson.dependencies["@sveltejs/kit"] ?? | ||
packageJson.devDependencies["@sveltejs/kit"], | ||
) | ||
} catch (_e) { | ||
return false | ||
} | ||
})() | ||
|
||
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 { | ||
// <script context="module"> | ||
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute > SvelteLiteral[value="module"]`]: | ||
() => { | ||
isModule = true | ||
}, | ||
|
||
// 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
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 |
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> |
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> |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/valid/test03-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"> | ||
function load() {} | ||
</script> |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/valid/test04-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"> | ||
const load = () => {} | ||
</script> |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-export-load-in-svelte-module-in-kit-pages/valid/test05-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 foo() {} | ||
</script> |
16 changes: 16 additions & 0 deletions
16
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
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"), | ||
) |
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.
Due to
package.json
check, I couldn't find a way to cover the conditions.So helplessly, I changed here.
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 don't think it's a problem because I think it's a sufficient coverage rate.