-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
svelte/no-svelte-internal
rule (#749)
`svelte/internal` is deprecated in Svelte 5 and will be removed in Svelte 6 according to our current plan. The Svelte compiler only compiles `.svelte` files and `.svelte.[jt]s` files, so the compiler can not check normal `.[jt]s` files. Therefore, the ESLint plugin checks this.
- Loading branch information
1 parent
bb245f1
commit da4d535
Showing
23 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 `svelte/no-svelte-internal` rule |
This file contains 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 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 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-svelte-internal' | ||
description: 'svelte/internal will be removed in Svelte 6.' | ||
--- | ||
|
||
# svelte/no-svelte-internal | ||
|
||
> svelte/internal will be removed in Svelte 6. | ||
- :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 the use of the deprecated API `svelte/internal` and `svelte/internal/xxx`. `svelte/internal` is deprecated in Svelte 5. And it will be deleted in Svelte 6. These APIs can change in breaking ways at any time without notice. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-svelte-internal: "error" */ | ||
// ✓ GOOD | ||
import { mount } from 'svelte'; | ||
// ✗ BAD | ||
import { get_current_component } from 'svelte/internal'; | ||
import { inspect } from 'svelte/internal/client'; | ||
import('svelte/internal'); | ||
import('svelte/internal/disclose-version'); | ||
export * from 'svelte/internal'; | ||
export { listen } from 'svelte/internal'; | ||
export * from 'svelte/internal/server'; | ||
</script> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further Reading | ||
|
||
<!--TODO: update here when relevant statements are added in Svelte 5 documentation --> | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/no-svelte-internal.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/no-svelte-internal.ts) |
This file contains 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 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,59 @@ | ||
import { createRule } from '../utils'; | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
|
||
export default createRule('no-svelte-internal', { | ||
meta: { | ||
docs: { | ||
description: 'svelte/internal will be removed in Svelte 6.', | ||
category: 'Best Practices', | ||
// TODO Switch to recommended in the major version. | ||
// recommended: true, | ||
recommended: false | ||
}, | ||
schema: [], | ||
messages: { | ||
unexpected: 'Using svelte/internal is prohibited. This will be removed in Svelte 6.' | ||
}, | ||
type: 'problem' | ||
}, | ||
create(context) { | ||
function report(node: TSESTree.Node) { | ||
context.report({ | ||
node, | ||
messageId: 'unexpected' | ||
}); | ||
} | ||
|
||
function isSvelteInternal(value: string) { | ||
return value === 'svelte/internal' || value.startsWith('svelte/internal/'); | ||
} | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
if (node.source && isSvelteInternal(node.source.value)) { | ||
report(node); | ||
} | ||
}, | ||
ImportExpression(node) { | ||
if ( | ||
node.source && | ||
node.source.type === 'Literal' && | ||
typeof node.source.value === 'string' && | ||
isSvelteInternal(node.source.value) | ||
) { | ||
report(node); | ||
} | ||
}, | ||
ExportNamedDeclaration(node) { | ||
if (node.source && isSvelteInternal(node.source.value)) { | ||
report(node); | ||
} | ||
}, | ||
ExportAllDeclaration(node) { | ||
if (node.source && isSvelteInternal(node.source.value)) { | ||
report(node); | ||
} | ||
} | ||
}; | ||
} | ||
}); |
This file contains 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
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal01-errors.yaml
This file contains 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: Using svelte/internal is prohibited. This will be removed in Svelte 6. | ||
line: 3 | ||
column: 2 | ||
suggestions: null |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal01-input.svelte
This file contains 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 @@ | ||
<script> | ||
// eslint-disable-next-line camelcase -- this is a test | ||
import { get_current_component } from 'svelte/internal'; | ||
</script> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal02-errors.yaml
This file contains 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: Using svelte/internal is prohibited. This will be removed in Svelte 6. | ||
line: 2 | ||
column: 2 | ||
suggestions: null |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal02-input.svelte
This file contains 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> | ||
import { inspect } from 'svelte/internal/client'; | ||
</script> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal03-errors.yaml
This file contains 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: Using svelte/internal is prohibited. This will be removed in Svelte 6. | ||
line: 2 | ||
column: 2 | ||
suggestions: null |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal03-input.svelte
This file contains 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> | ||
import * as svelteInternal from 'svelte/internal'; | ||
</script> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal04-errors.yaml
This file contains 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: Using svelte/internal is prohibited. This will be removed in Svelte 6. | ||
line: 2 | ||
column: 2 | ||
suggestions: null |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal04-input.svelte
This file contains 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 * from 'svelte/internal'; | ||
</script> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal05-errors.yaml
This file contains 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: Using svelte/internal is prohibited. This will be removed in Svelte 6. | ||
line: 2 | ||
column: 2 | ||
suggestions: null |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal05-input.svelte
This file contains 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 * from 'svelte/internal/client'; | ||
</script> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal06-errors.yaml
This file contains 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: Using svelte/internal is prohibited. This will be removed in Svelte 6. | ||
line: 2 | ||
column: 2 | ||
suggestions: null |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal06-input.svelte
This file contains 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 { inspect } from 'svelte/internal/client'; | ||
</script> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal07-errors.yaml
This file contains 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: Using svelte/internal is prohibited. This will be removed in Svelte 6. | ||
line: 2 | ||
column: 2 | ||
suggestions: null |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-svelte-internal/invalid/no-svelte-internal07-input.svelte
This file contains 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> | ||
import('svelte/internal'); | ||
</script> |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-svelte-internal/valid/no-svelte-internal01-input.svelte
This file contains 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> | ||
import { mount } from 'svelte'; | ||
</script> |
This file contains 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,12 @@ | ||
import { RuleTester } from '../../utils/eslint-compat'; | ||
import rule from '../../../src/rules/no-svelte-internal'; | ||
import { loadTestCases } from '../../utils/utils'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('no-svelte-internal', rule as any, loadTestCases('no-svelte-internal')); |